-
Notifications
You must be signed in to change notification settings - Fork 38
/
jquery.fs.dropper.min.js
9 lines (8 loc) · 3.54 KB
/
jquery.fs.dropper.min.js
1
2
3
4
5
6
7
8
9
/*
* Dropper v1.0.1 - 2015-04-04
* A jQuery plugin for simple drag and drop uploads. Part of the Formstone Library.
* http://classic.formstone.it/dropper/
*
* Copyright 2015 Ben Plum; MIT Licensed
*/
!function(a,b){"use strict";function c(b){var c=a(this);if(n){b=a.extend({},o,b);for(var e=0,f=c.length;f>e;e++)d(c.eq(e),b)}return c}function d(b,c){c=a.extend({},c,b.data("dropper-options"));var d="";d+='<div class="dropper-dropzone">',d+=c.label,d+="</div>",d+='<input class="dropper-input" type="file"',c.maxQueue>1&&(d+=" multiple"),d+=">",b.addClass("dropper").append(d);var k=a.extend({$dropper:b,$input:b.find(".dropper-input"),queue:[],total:0,uploading:!1},c);b.on("click.dropper",".dropper-dropzone",k,e).on("dragenter.dropper",k,g).on("dragover.dropper",k,h).on("dragleave.dropper",k,i).on("drop.dropper",".dropper-dropzone",k,j).data("dropper",k),k.$input.on("change.dropper",k,f)}function e(a){a.stopPropagation(),a.preventDefault();var b=a.data;b.$input.trigger("click")}function f(a){a.stopPropagation(),a.preventDefault();var b=a.data,c=b.$input[0].files;c.length&&k(b,c)}function g(a){a.stopPropagation(),a.preventDefault();var b=a.data;b.$dropper.addClass("dropping")}function h(a){a.stopPropagation(),a.preventDefault();var b=a.data;b.$dropper.addClass("dropping")}function i(a){a.stopPropagation(),a.preventDefault();var b=a.data;b.$dropper.removeClass("dropping")}function j(a){a.preventDefault();var b=a.data,c=a.originalEvent.dataTransfer.files;b.$dropper.removeClass("dropping"),k(b,c)}function k(c,d){for(var e=[],f=0;f<d.length;f++){var g={index:c.total++,file:d[f],name:d[f].name,size:d[f].size,started:!1,complete:!1,error:!1,transfer:null};e.push(g),c.queue.push(g)}c.uploading||(a(b).on("beforeunload.dropper",function(){return"You have uploads pending, are you sure you want to leave this page?"}),c.uploading=!0),c.$dropper.trigger("start.dropper",[e]),l(c)}function l(c){var d=0,e=[];for(var f in c.queue)!c.queue.hasOwnProperty(f)||c.queue[f].complete||c.queue[f].error||e.push(c.queue[f]);c.queue=e;for(var g in c.queue)if(c.queue.hasOwnProperty(g)){if(!c.queue[g].started){var h=new FormData;h.append(c.postKey,c.queue[g].file);for(var i in c.postData)c.postData.hasOwnProperty(i)&&h.append(i,c.postData[i]);m(c,c.queue[g],h)}if(d++,d>=c.maxQueue)return;f++}0===d&&(a(b).off("beforeunload.dropper"),c.uploading=!1,c.$dropper.trigger("complete.dropper"))}function m(b,c,d){c.size>=b.maxSize?(c.error=!0,b.$dropper.trigger("fileError.dropper",[c,"Too large"]),l(b)):(c.started=!0,c.transfer=a.ajax({url:b.action,data:d,type:"POST",contentType:!1,processData:!1,cache:!1,xhr:function(){var d=a.ajaxSettings.xhr();return d.upload&&d.upload.addEventListener("progress",function(a){var d=0,e=a.loaded||a.position,f=a.total;a.lengthComputable&&(d=Math.ceil(e/f*100)),b.$dropper.trigger("fileProgress.dropper",[c,d])},!1),d},beforeSend:function(a){b.$dropper.trigger("fileStart.dropper",[c])},success:function(a,d,e){c.complete=!0,b.$dropper.trigger("fileComplete.dropper",[c,a]),l(b)},error:function(a,d,e){c.error=!0,b.$dropper.trigger("fileError.dropper",[c,e]),l(b)}}))}var n=b.File&&b.FileReader&&b.FileList,o={action:"",label:"Drag and drop files or click to select",maxQueue:2,maxSize:5242880,postData:{},postKey:"file"},p={defaults:function(b){return o=a.extend(o,b||{}),"object"==typeof this?a(this):!0}};a.fn.dropper=function(a){return p[a]?p[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?this:c.apply(this,arguments)},a.dropper=function(a){"defaults"===a&&p.defaults.apply(this,Array.prototype.slice.call(arguments,1))}}(jQuery,window);