Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do drag-and-drop re-ordering of the preview elements in a dropzone.js instance?

I have a dropzone.js instance on a web page with the following options:

autoProcessQueue:false
uploadMultiple:true
parallelUploads:20
maxFiles:20

It is programmatically instantiated, as it is part of a larger form. I have it rigged up to process the queue when the form is submitted.

The goal is for my users to be able to use the dropzone to manage images for an item, so I'd like them to be able to re-order the images by dragging and dropping the dropzone.js image previews. Is there a good way to do this? I've tried using jquery-ui's sortable but it doesn't seem to play nice with dropzone.js.

like image 241
ralbatross Avatar asked Mar 26 '14 16:03

ralbatross


People also ask

What is processQueue dropzone?

processQueue() which checks how many files are currently uploading, and if it's less than options. parallelUploads , . processFile(file) is called. If you set autoProcessQueue to false , then . processQueue() is never called implicitly.

How do I add an image to dropzone?

Show activity on this post. // Create the mock file: var mockFile = { name: "Filename", size: 12345 }; // Call the default addedfile event handler myDropzone. emit("addedfile", mockFile); // And optionally show the thumbnail of the file: myDropzone. emit("thumbnail", mockFile, "/image/url");


2 Answers

I've got it working now using jquery-ui's sortable. The trick was to make sure to use the 'items' option in sortable to pick only the dz-preview elements, because dropzone.js has the dz-message element along with the dz-preview elements in the main container. Here's how my code looks:

The HTML:

<div id="image-dropzone" class="dropzone square">

The script:

$(function() {
    $("#image-dropzone").sortable({
        items:'.dz-preview',
        cursor: 'move',
        opacity: 0.5,
        containment: '#image-dropzone',
        distance: 20,
        tolerance: 'pointer'
    });
})
like image 167
ralbatross Avatar answered Oct 08 '22 07:10

ralbatross


Besides the code from ralbatross you will need to set the order of the file queue of dropzone..

Something like:

$("#uploadzone").sortable({
    items: '.dz-preview',
    cursor: 'move',
    opacity: 0.5,
    containment: '#uploadzone',
    distance: 20,
    tolerance: 'pointer',
    stop: function () {

        var queue = uploadzone.files;
        $('#uploadzone .dz-preview .dz-filename [data-dz-name]').each(function (count, el) {           
            var name = el.getAttribute('data-name');
            queue.forEach(function(file) {
               if (file.name === name) {
                    newQueue.push(file);
               }
            });
        });

        uploadzone.files = newQueue;

    }
});

And remember that the file is processed async, i keep an hashtable for reference when the file is done and save the order at the end.

It doesn't work with duplicate filenames

like image 22
Chris Avatar answered Oct 08 '22 07:10

Chris