Search code examples
jqueryobjectinitialization

jquery.filedrop.js -- how do I change options after initialization?


I am using jquery.filedrop.js -- https://github.com/weixiyen/jquery-filedrop.

$(function () {
    $('#dropZone').filedrop({
        url: `http://example.com/Upload.asp?uploadLocation=${uploadLocation}&filename=${filename}`,
        paramname: 'file1',
        maxFiles: 5,
        dragOver: function () {
            $('#dropZone').css('background', 'blue');
        },
        dragLeave: function () {
            $('#dropZone').css('background', 'gray');
        },
        drop: function() {
            alert("dropping")
            $('#dropZone').css('background', 'gray');
        },
        afterAll: function () {
            // $('#dropZone').html('The file(s) have been uploaded successfully!');
        },
        uploadFinished: function (i, file, response, time) {
            $('#uploadResult').append('<li>' + file.name + '</li>');
        }
    });
});

However, after initialization, I need to be able to change some of those options on "Drop". maxFiles, for instance. Or, am I stuck with not initializing until I have the information I need?


Solution

  • Well, considering that it's a function, this is not doable. However, I added an option to the filedrop.js script -- myFiles=[] -- and modified it:

            function drop(e) {
            if (opts.drop.call(this, e) === false) return false;
            if ((e.originalEvent===undefined||!e.originalEvent.dataTransfer)&&opts.myFiles.length<0) //<--this line {
                return;
            }
    /* from here */
            if (opts.myFiles.length) {
                files = opts.myFiles;
            } else {
                files = e.originalEvent.dataTransfer.files;
            }
    /* to here */
            if (files === null || files === undefined || files.length === 0) {
                opts.error(errors[0]);
                return false;
            }
    
            files_count = files.length;
            upload();
            e.preventDefault();
            return false;
        }
    

    And then modified my jquery to this:

    var files;
    $(document).on('dragover drop dragenter dragleave', `.prodSelect`, function(event) {
        event.stopPropagation();
        event.preventDefault();
        if (event.type == 'drop') {
            files=event.originalEvent.dataTransfer.files;
            filename=$(this).prop(`id`)+`.jpg`;
            setURL($(this));
            $(`#dropZone`).trigger(`drop`);
        }
    });
    
    
    function setURL(curDiv) {
        $(function () {
            $(`#dropZone`).filedrop({
                url: `https://example.com/upload/Upload.asp?uploadLocation=${uploadLocation}&filename=${filename}`,
                paramname: 'file1',
                maxfiles: 1,
                myFiles: files,
                rename: function() {
                    return filename;
                },
                uploadFinished: function (i, file, response, time) {
                    curImg=curDiv.find(`.prdimg`).attr(`src`,``);  curImg.attr(`src`,`https://seasonal.kickeepants.net/shop/pc/images/catalogcreation/${filename}?${Math.floor(Math.random()*1000)}`)
                }
            });
        });
    }