Files
webgui/plugins/dynamix/javascript/jquery.filedrop.js
2022-03-03 21:07:35 +01:00

2 lines
6.3 KiB
JavaScript

/* jQuery filedrop v0.1.0, copyright Weixi Yen - modified by Bergware for jQuery v3.6 */
!function(R){var n={fallback_id:"",fallback_dropzoneClick:!1,url:"",refresh:1e3,paramname:"userfile",requestType:"POST",allowedfileextensions:[],allowedfiletypes:[],maxfiles:25,maxfilesize:1,queuefiles:0,queuewait:200,data:{},headers:{},drop:e,dragStart:e,dragEnter:e,dragOver:e,dragLeave:e,docEnter:e,docOver:e,docLeave:e,beforeEach:e,afterAll:e,rename:e,error:function(e,t,r,n){alert(e)},uploadStarted:e,uploadFinished:e,progressUpdated:e,globalProgressUpdated:e,speedUpdated:e},l=["BrowserNotSupported","TooManyFiles","FileTooLarge","FileTypeNotAllowed","NotFound","NotReadable","AbortError","ReadError","FileExtensionNotAllowed"];function e(){}R.fn.filedrop=function(e){var r,g,v=R.extend({},n,e),m=[],y=!1,w=0;if(!0===v.fallback_dropzoneClick&&R("#"+v.fallback_id).css({display:"none",width:0,height:0}),this.on("drop",function(e){return!1!==v.drop.call(this,e)&&(e.originalEvent.dataTransfer?(null!==(g=e.originalEvent.dataTransfer.files)&&void 0!==g&&0!==g.length?(w=g.length,t(),e.preventDefault()):v.error(l[0]),!1):void 0)}).on("dragstart",v.dragStart).on("dragenter",function(e){clearTimeout(r),e.preventDefault(),v.dragEnter.call(this,e)}).on("dragover",function(e){clearTimeout(r),e.preventDefault(),v.docOver.call(this,e),v.dragOver.call(this,e)}).on("dragleave",function(e){clearTimeout(r),v.dragLeave.call(this,e),e.stopPropagation()}),R(document).on("drop",function(e){return e.preventDefault(),v.docLeave.call(this,e),!1}).on("dragenter",function(e){return clearTimeout(r),e.preventDefault(),v.docEnter.call(this,e),!1}).on("dragover",function(e){return clearTimeout(r),e.preventDefault(),v.docOver.call(this,e),!1}).on("dragleave",function(t){r=setTimeout(function(e){return function(){v.docLeave.call(e,t)}}(this),200)}),!0===v.fallback_dropzoneClick){if(0<this.find("#"+v.fallback_id).length)throw"Fallback element ["+v.fallback_id+"] cannot be inside dropzone, unless option fallback_dropzoneClick is false";this.on("click",function(e){R("#"+v.fallback_id).trigger(e)}),R("#"+v.fallback_id).change(function(e){v.drop(e),g=e.target.files,w=g.length,t()})}function b(e,t,r,n){var a,o="--",i="\r\n",l="",s=v.paramname;return v.data&&(a=R.param(v.data).replace(/\+/g,"%20").split(/&/),R.each(a,function(){var e=this.split("=",2),t=decodeURIComponent(e[0]),r=decodeURIComponent(e[1]);2===e.length&&(l+=o,l+=n,l+=i,l+='Content-Disposition: form-data; name="'+t+'"',l+=i,l+=i,l+=r,l+=i)})),jQuery.isFunction(s)&&(s=s(e)),l+=o,l+=n,l+=i,l+='Content-Disposition: form-data; name="'+(s||"")+'"',l+='; filename="'+encodeURIComponent(e)+'"',l+=i,l+="Content-Type: "+r,l+=i,l+=i,l+=t,l+=i,l+=o,l+=n,l+=o,l+=i}function T(e){var t,r;e.lengthComputable&&(r=Math.round(100*e.loaded/e.total),this.currentProgress!==r&&(this.currentProgress=r,v.progressUpdated(this.index,this.file,this.currentProgress),m[this.global_progress_index]=this.currentProgress,x(),(r=(t=(new Date).getTime())-this.currentStart)>=v.refresh&&(r=(e.loaded-this.startData)/r,v.speedUpdated(this.index,this.file,r),this.startData=e.loaded,this.currentStart=t)))}function x(){if(0!==m.length){var e,t=0;for(e in m)m.hasOwnProperty(e)&&(t+=m[e]);v.globalProgressUpdated(Math.round(t/m.length))}}function t(){if(y=!1,g){if(v.allowedfiletypes.push&&v.allowedfiletypes.length)for(var e=g.length;e--;)if(!g[e].type||R.inArray(g[e].type,v.allowedfiletypes)<0)return void v.error(l[3],g[e]);if(v.allowedfileextensions.push&&v.allowedfileextensions.length)for(e=g.length;e--;){for(var t=!1,r=0;r<v.allowedfileextensions.length;r++)g[e].name.substr(g[e].name.length-v.allowedfileextensions[r].length).toLowerCase()==v.allowedfileextensions[r].toLowerCase()&&(t=!0);if(!t)return void v.error(l[8],g[e])}var f=0,c=0;if(w>v.maxfiles&&0===v.queuefiles)v.error(l[1]);else{for(var a=[],p=[],h=[],r=0;r<w;r++)a.push(r);var o=function(){var r,e;if(y)return!1;if(0<v.queuefiles&&p.length>=v.queuefiles)return e=v.queuewait,void setTimeout(o,e);r=a[0],a.splice(0,1),p.push(r);try{if(!1!==(n=g[r],v.beforeEach(n))){if(r===w)return;var t=new FileReader,n=1048576*v.maxfilesize;if(t.index=r,g[r].size>n)return v.error(l[2],g[r],r),p.forEach(function(e,t){e===r&&p.splice(t,1)}),c++,!0;t.onerror=function(e){switch(e.target.error.code){case e.target.error.NOT_FOUND_ERR:return v.error(l[4]),!1;case e.target.error.NOT_READABLE_ERR:return v.error(l[5]),!1;case e.target.error.ABORT_ERR:return v.error(l[6]),!1;default:return v.error(l[7]),!1}},t.onloadend=v.beforeSend?function(e){v.beforeSend(g[r],r,function(){i(e)})}:i,t.readAsDataURL(g[r])}else c++}catch(e){return p.forEach(function(e,t){e===r&&p.splice(t,1)}),v.error(l[0]),!1}0<a.length&&o()},i=function(e){var r=(e.srcElement||e.target).index;void 0===e.target.index&&(e.target.index=function(e){for(var t=0;t<w;t++)if(g[t].size===e)return t;return}(e.total));var n=new XMLHttpRequest,t=n.upload,a=g[e.target.index],o=e.target.index,i=(new Date).getTime(),l="------multipartformboundary"+(new Date).getTime(),s=m.length,d=(u=a.name,v.rename(u)),u=a.type;v.withCredentials&&(n.withCredentials=v.withCredentials);e=e.target.result.split(",")[1],e=void 0===e?"":atob(e),u=b("string"==typeof d?d:a.name,e,u,l);t.index=o,t.file=a,t.downloadStartTime=i,t.currentStart=i,t.currentProgress=0,t.global_progress_index=s,t.startData=0,t.addEventListener("progress",T,!1),jQuery.isFunction(v.url)?n.open(v.requestType,v.url(t),!0):n.open(v.requestType,v.url,!0),n.setRequestHeader("content-type","multipart/form-data; boundary="+l),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),R.each(v.headers,function(e,t){n.setRequestHeader(e,t)}),n.sendAsBinary||(n.sendAsBinary=function(e){e=Array.prototype.map.call(e,function(e){return 255&e.charCodeAt(0)}),e=new Uint8Array(e);this.send(e.buffer)}),n.sendAsBinary(u),m[s]=0,x(),v.uploadStarted(o,a,w),n.onload=function(){var t=null;if(n.responseText)try{t=jQuery.parseJSON(n.responseText)}catch(e){t=n.responseText}var e=(new Date).getTime()-i,e=v.uploadFinished(o,a,t,e,n);f++,p.forEach(function(e,t){e===r&&p.splice(t,1)}),h.push(r),m[s]=100,x(),f===w-c&&v.afterAll(),!1===e&&(y=!0),(n.status<200||299<n.status)&&v.error(n.statusText,a,r,n.status)}};o()}}else v.error(l[0])}return this};try{if(XMLHttpRequest.prototype.sendAsBinary)return;XMLHttpRequest.prototype.sendAsBinary=function(e){e=Array.prototype.map.call(e,function(e){return 255&e.charCodeAt(0)}),e=new Uint8Array(e);"ArrayBufferView"in window?this.send(e):this.send(e.buffer)}}catch(e){}}(jQuery);