snappymail/vendors/jua/jua.min.js

3 lines
5.6 KiB
JavaScript
Raw Normal View History

2014-09-11 23:00:05 +08:00
/* RainLoop Webmail (c) RainLoop Team | MIT */
2020-09-19 17:21:11 +08:00
(()=>{"use strict";const e=document,t=20,n=e=>void 0!==e,r=(e,r,o,i)=>{if(e&&e.length){let a=o=n(o)?parseInt(o||0,10):t,s=null,u=0<o,d=!1;Array.from(e).forEach(e=>{e&&(!u||0<=--o?(s=l(e))&&r(s):u&&!d&&0>o&&i&&(d=!0,i(a)))})}},o=(e,t)=>{Object.entries(t).forEach(([t,n])=>e.addEventListener(t,n))},l=e=>{let t=n(e.fileName)?e.fileName:n(e.name)?e.name:null,r=n(e.fileSize)?e.fileSize:n(e.size)?e.size:null,o=n(e.type)?e.type:null;return"/"===t.charAt(0)&&(t=t.substr(1)),o||0!==r?{FileName:t,Size:r,Type:o,Folder:"",File:e}:null},i=e=>{if(e.dataTransfer&&e.dataTransfer.types&&e.dataTransfer.types.length){let t=e.dataTransfer.types.length;for(;t--;)if("files"===e.dataTransfer.types[t].toLowerCase())return!0}return!1};class a{constructor(e,t){this.oXhrs={},this.oUids={},this.oJua=e,this.oOptions=Object.assign({action:"",name:"juaFile",hidden:{},disableMultiple:!1},t)}regTaskUid(e){this.oUids[e]=!0}uploadTask(e,t,r){if(!1===this.oUids[e]||!t||!t.File)return r(null,e),!1;try{const o=this,l=new XMLHttpRequest,i=new FormData,a=this.oOptions.action,s=this.oOptions.hidden,u=this.oJua.getEvent("onStart"),d=this.oJua.getEvent("onComplete"),p=this.oJua.getEvent("onProgress");return l.open("POST",a,!0),p&&l.upload&&(l.upload.onprogress=function(t){t&&t.lengthComputable&&n(t.loaded)&&n(t.total)&&p(e,t.loaded,t.total)}),l.onreadystatechange=function(){if(4===l.readyState&&200===l.status){if(d){let t=!1,n=null;try{n=JSON.parse(l.responseText),t=!0}catch(e){n=null}d(e,t,n)}n(o.oXhrs[e])&&(o.oXhrs[e]=null),r(null,e)}else 4===l.readyState&&(d(e,!1,null),r(null,e))},u&&u(e),i.append("jua-post-type","ajax"),i.append(this.oOptions.name,t.File),Object.entries(s).forEach(([e,n])=>i.append(e,("function"==typeof n?n(t):n).toString())),l.send(i),this.oXhrs[e]=l,!0}catch(e){}return r(null,e),!1}generateNewInput(t){if(t){const n=this,o=e.createElement("label"),l=o.appendChild(e.createElement("input"));l.type="file",l.tabIndex=-1,l.style.cssText="position:absolute;left:-9999px;",l.multiple=!n.oOptions.disableMultiple,o.style.cssText="position:absolute;background-color:#fff;right:0;top:0;left:0;bottom:0;margin:0;padding:0;cursor:pointer;opacity:0",t.append(o),l.addEventListener("input",()=>{const e=e=>{n.oJua.addNewFile(e),n.generateNewInput(t),setTimeout(()=>o.remove(),10)};l.files&&l.files.length?r(l.files,e,n.oOptions.multipleSizeLimit,n.oJua.getEvent("onLimitReached")):e({FileName:l.value.split("\\").pop().split("/").pop(),Size:null,Type:null,Folder:"",File:null})})}}cancel(e){if(this.oUids[e]=!1,this.oXhrs[e]){try{this.oXhrs[e].abort&&this.oXhrs[e].abort()}catch(e){}this.oXhrs[e]=null}}}function s(e){let t,n,r={},o=0,l=0,i=-1,a=null,s=[],u=()=>{};return arguments.length<1&&(e=1/0),r.defer=function(){if(!a){let e=arguments;e.index=++i,n?(n.next=e,n=n.next):t=n=e,++l,function r(){if(t&&o<e){let e=t,i=e[0],d=Array.prototype.slice.call(e,1),p=e.index;t=t===n?n=null:t.next,++o,d.push(function(e,i){--o,a||(e?l&&u(a=e,l=s=t=n=null):(s[p]=i,--l?r():u(null,s)))}),i.apply(null,d)}}()}return r},r.await=function(e){return u=e,l||u(a,s),r},r}a.prototype.oXhrs={},a.prototype.oUids={},a.prototype.oJua=null,a.prototype.oOptions={};class u{constructor(n){const l=this;l.oEvents={onSelect:null,onStart:null,onComplete:null,onProgress:null,onDragEnter:null,onDragLeave:null,onBodyDragEnter:null,onBodyDragLeave:null,onLimitReached:null},n=Object.assign({queueSize:10,clickElement:null,dragAndDropElement:null,dragAndDropBodyElement:null,disableDocumentDropPrevent:!1,multipleSizeLimit:t},n||{}),l.oQueue=s(parseInt(n.queueSize||0,10)),l.oDriver=new a(l,n);let u=n.clickElement;if(u&&(u.style.position="relative",u.style.overflow="hidden","inline"===u.style.display&&(u.style.display="inline-block"),this.oDriver.generateNewInput(u)),u=n.dragAndDropElement){let t=n.dragAndDropBodyElement||e;n.disableDocumentDropPrevent||e.addEventListener("dragover",e=>{if(i(e))try{e.dataTransfer.dropEffect="none",e.preventDefault()}catch(e){}}),t&&o(t,{dragover:()=>l.docTimer.clear(),dragenter:e=>{i(e)&&(l.docTimer.clear(),e.preventDefault(),l.runEvent("onBodyDragEnter",[e]))},dragleave:e=>e.dataTr