forked from safronizator/damnUploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.damnUploader.min.js
1 lines (1 loc) · 5.26 KB
/
jquery.damnUploader.min.js
1
!function(e,t){var n=function(e){return void 0!==e&&null!=e},a=!1;if(e.XMLHttpRequest){var u=new XMLHttpRequest;a=n(u.upload)}t.extend(t.support,{fileSelecting:n(e.File)&&n(e.FileList),fileReading:n(e.FileReader),fileSending:n(e.FormData),uploadControl:a});var i=function(e,t){if(e=parseInt(e),t=t||"",0==e||isNaN(e))return t;var n=String.fromCharCode(Math.floor(26*Math.random())+97);return t+n+i(--e)},l=function(e){return e instanceof File||e instanceof Blob},r=function(t,n){if(e.Blob)return new Blob([t],{type:n});var a=e.MSBlobBuilder||e.MozBlobBuilder||e.WebKitBlobBuilder||e.BlobBuilder;return a?(new a).append(ab).getBlob(n):null},o=function(e){return"data:"==e.substr(0,5)},d=function(e){e=n(e.toString)?e.toString():""+e;var t="text/plain";if(o(e)){var a=e.split(","),u=a[0].indexOf("base64")>=0?atob(a[1]):unescape(a[1]);t=a[0].split(":")[1].split(";")[0],e=new ArrayBuffer(u.length);for(var i=new Uint8Array(e),l=0;l<u.length;l++)i[l]=u.charCodeAt(l)}return r(e,t)},s=function(e,t){var n=e.split("/"),a=t.split("/");return n[0]!=a[0]?!1:"*"==a[1]||n[1]==a[1]};t.fn.damnUploader=function(e){if(0==this.length)return this;if(this.length>1)return this.each(function(){t(this).damnUploader(e)});var a=this;a._duSettings=t.extend({url:"/upload.php",multiple:!0,fieldName:"file",dropping:!0,dropBox:!1,limit:!1,dataType:"text",method:"POST",acceptType:null},e||{}),!a._duSettings.multiple&&(a._duSettings.limit=1);var u=function(e,t,n){this.file=e,this.fieldName=null,this.replaceName=null,this.progressCallback=n,this.completeCallback=t,this.xhr=null,this.cancelled=!1,this.started=!1,this.completed=!1,this._id=i(7),this._post=[]};t.extend(u.prototype,{id:function(){return this._id},addPostData:function(e,n){var a=this;return t.isArray(e)?void t.each(e,function(e,t){a.addPostData(t.name,t.value)}):void this._post.push({name:e,value:n})},upload:function(){a._duUploadItem(this)},cancel:function(){a.duCancel(this._id)},getReaderCallback:function(e){var n;return t.support.fileReading&&(n=new FileReader,n.onload=e),n},readAs:function(e,t){var n="readAs"+e,a=this.getReaderCallback(t);a&&a[n]&&a[n].call(a,this.file)}}),a._duQueue={},a._duItemsCount=0;var r=a._duQueue,o=a._duSettings;a._duAddItemsToQueue=function(e){var n,u=t.Event("du.add"),i=t.Event("du.limit");if(a._duItemsCount===o.limit)return a.trigger(i),!1;if(e instanceof FileList){var l=[];return t.each(e,function(e,t){l.push(a._duAddItemsToQueue(t))}),l}if((n=a.duNewUploadItem(e))===!1)return!1;if(u.uploadItem=n,a.trigger(u),u.isDefaultPrevented())return!1;var d=n.id();return a._duItemsCount++,r[d]=n,n},a._duUploadItem=function(e){if(!t.support.fileSending||!l(e.file))return!1;if(!e.started){e.started=!0;var n=new XMLHttpRequest,a=0,u=!1,i=t.isFunction(e.progressCallback);n.upload?(n.upload.addEventListener("progress",function(t){t.lengthComputable&&(a=100*t.loaded/t.total,i&&e.progressCallback.call(e,Math.round(a)))},!1),n.upload.addEventListener("load",function(){a=100,u=!0},!1)):u=!0,n.onreadystatechange=function(){var n=t.isFunction(e.completeCallback);if(4==this.readyState)if(e.completed=!0,this.status<400)if(u){t.isFunction(e.progressCallback)&&e.progressCallback.call(e,100);var a="json"==o.dataType?t.parseJSON(this.responseText):this.responseText;n&&e.completeCallback.call(e,!0,a)}else n&&e.completeCallback.call(e,!1,null,0);else n&&e.completeCallback.call(e,!1,null,this.status)};var r=e.replaceName||e.file.name,d="PUT"==o.method.toString().toUpperCase()?"PUT":"POST";n.open(d,o.url);var s=new FormData;s.append(e.fieldName||o.fieldName||"file",e.file,r),e._post.length>0&&t.each(e._post,function(e,t){s.append(t.name,t.value)}),n.send(s),e.xhr=n}};var c="INPUT"==a.get(0).tagName&&"file"==this.attr("type");return c&&(a.prop("multiple",o.multiple).on("change",function(){a._duAddItemsToQueue(this.files)}),null!==o.acceptType&&a.attr("accept",o.acceptType)),o.dropping&&(a.on("drop",function(e){return a._duAddItemsToQueue(e.originalEvent.dataTransfer.files),!1}),o.dropBox&&t(o.dropBox).on("drop",function(e){return a._duAddItemsToQueue(e.originalEvent.dataTransfer.files),!1})),a.on("du.add",function(e){null!==o.acceptType&&e.uploadItem.file&&(s(e.uploadItem.file.type,o.acceptType)||(e.preventDefault(),e.stopImmediatePropagation()))}),a.duStart=function(){return o.url?(t.each(r,function(e,n){var u=n.completeCallback;n.completeCallback=function(n,i,l){this.cancelled||(delete r[e],a._duItemsCount--),t.isFunction(u)&&u.call(this,n,i,l),0==a._duItemsCount&&a.trigger("du.completed")},a._duUploadItem(n)}),a):a},a.duCancel=function(e){return e&&a._duItemsCount>0&&n(r[e])&&(r[e].xhr&&(r[e].cancelled=!0,r[e].xhr.abort()),delete r[e],a._duItemsCount--),a},a.duCancelAll=function(){return c&&a.val(""),t.each(r,function(e){a.duCancel(e)}),a},a.duNewUploadItem=function(e){switch(!0){case e instanceof u:return e;case l(e):return new u(e);default:var t=d(e);return t!==!1?a.duNewUploadItem(t):!1}},a.duEnqueue=function(e){return a._duAddItemsToQueue(e)},a.duGetQueue=function(){return a._duQueue},a.duCount=function(){return a._duItemsCount},a.duOption=function(e,n){var u=["url","fieldName","limit","dataType","method"];if(t.isPlainObject(e))t.each(e,function(e,t){a.duOption(e,t)});else{if(void 0===n)return a._duSettings[e];t.inArray(e,u)>-1&&(a._duSettings[e]=n)}return a},a.duIsAcceptedType=function(e){return null===o.acceptType||s(e,o.acceptType)},a}}(window,window.jQuery);