snappymail/vendors/jua/jua.min.js
2020-10-01 11:10:40 +02:00

2 lines
5 KiB
JavaScript

/* RainLoop Webmail (c) RainLoop Team | MIT */
(e=>{const t=20,n=e=>void 0!==e,r=(e,r,i,o)=>{if(e&&e.length){let a=i=n(i)?parseInt(i||0,10):t,s=null,u=0<i,d=!1;[...e].forEach(e=>{e&&(!u||0<=--i?(s=l(e))&&r(s):u&&!d&&0>i&&o&&(d=!0,o(a)))})}},i=(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,i=n(e.type)?e.type:null;return"/"===t.charAt(0)&&(t=t.substr(1)),i||0!==r?{FileName:t,Size:r,Type:i,Folder:"",File:e}:null},o=e=>{try{return e.dataTransfer.types.includes("Files")}catch(e){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){if(!1===this.oUids[e]||!t||!t.File)return!1;try{const r=this,i=new XMLHttpRequest,l=new FormData,o=this.oOptions.action,a=this.oOptions.hidden,s=this.oJua.getEvent("onStart"),u=this.oJua.getEvent("onComplete"),d=this.oJua.getEvent("onProgress");return i.open("POST",o,!0),d&&i.upload&&(i.upload.onprogress=function(t){t&&t.lengthComputable&&n(t.loaded)&&n(t.total)&&d(e,t.loaded,t.total)}),i.onreadystatechange=function(){if(4===i.readyState&&200===i.status){if(u){let t=!1,n=null;try{n=JSON.parse(i.responseText),t=!0}catch(e){n=null}u(e,t,n)}n(r.oXhrs[e])&&(r.oXhrs[e]=null)}else 4===i.readyState&&u(e,!1,null)},s&&s(e),l.append("jua-post-type","ajax"),l.append(this.oOptions.name,t.File),Object.entries(a).forEach(([e,n])=>l.append(e,("function"==typeof n?n(t):n).toString())),i.send(l),this.oXhrs[e]=i,!0}catch(e){console.error(e)}return!1}generateNewInput(t){if(t){const n=this,i=e.createElement("input"),l=()=>i.click();i.type="file",i.tabIndex=-1,i.style.display="none",i.multiple=!n.oOptions.disableMultiple,t.addEventListener("click",l),i.addEventListener("input",()=>{const e=e=>{n.oJua.addNewFile(e),setTimeout(()=>{i.remove(),t.removeEventListener("click",l),n.generateNewInput(t)},10)};i.files&&i.files.length?r(i.files,e,n.oOptions.multipleSizeLimit,n.oJua.getEvent("onLimitReached")):e({FileName:i.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){console.error(e)}this.oXhrs[e]=null}}}class s extends Array{constructor(e){super(),this.limit=parseInt(e||0,10)}push(e,...t){this.limit>this.length&&(super.push([e,t]),this.call())}call(){if(!this.running){let e;for(this.running=!0;e=this.shift();)e[0](...e[1]);this.running=!1}}}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=new s(n.queueSize),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"),l.oDriver.generateNewInput(u)),u=n.dragAndDropElement){let t=n.dragAndDropBodyElement||e;n.disableDocumentDropPrevent||e.addEventListener("dragover",e=>{if(o(e))try{e.dataTransfer.dropEffect="none",e.preventDefault()}catch(e){console.error(e)}}),t&&i(t,{dragover:()=>l.docTimer.clear(),dragenter:e=>{o(e)&&(l.docTimer.clear(),e.preventDefault(),l.runEvent("onBodyDragEnter",[e]))},dragleave:e=>e.dataTransfer&&l.docTimer.start(()=>l.runEvent("onBodyDragLeave",[e])),drop:e=>{if(e.dataTransfer){let t=o(e);return t&&e.preventDefault(),l.runEvent("onBodyDragLeave",[e]),!t}return!1}}),i(u,{dragenter:e=>{o(e)&&(l.docTimer.clear(),e.preventDefault(),l.runEvent("onDragEnter",[u,e]))},dragover:e=>{if(o(e))try{let t=e.dataTransfer.effectAllowed;l.docTimer.clear(),e.dataTransfer.dropEffect="move"===t||"linkMove"===t?"move":"copy",e.stopPropagation(),e.preventDefault()}catch(e){console.error(e)}},dragleave:t=>{if(t.dataTransfer){let n=e.elementFromPoint(t.clientX,t.clientY);n&&u.contains(n)||(l.docTimer.clear(),l.runEvent("onDragLeave",[u,t]))}},drop:e=>{o(e)&&(e.preventDefault(),r(e.files||e.dataTransfer.files,e=>{e&&(l.addNewFile(e),l.docTimer.clear())},n.multipleSizeLimit,l.getEvent("onLimitReached"))),l.runEvent("onDragLeave",[e])}})}}on(e,t){return this.oEvents[e]=t,this}runEvent(e,t){this.oEvents[e]&&this.oEvents[e].apply(null,t||[])}getEvent(e){return this.oEvents[e]||null}cancel(e){this.oDriver.cancel(e)}addNewFile(e){let t=16,n="";for(;t--;)n+="0123456789abcdefghijklmnopqrstuvwxyz".substr(Math.round(36*Math.random()),1);this.addFile("jua-uid-"+n+"-"+(new Date).getTime().toString(),e)}addFile(e,t){const n=this.getEvent("onSelect");!t||n&&!1===n(e,t)?this.oDriver.cancel(e):(this.oDriver.regTaskUid(e),this.oQueue.push((...e)=>this.oDriver.uploadTask(...e),e,t))}}u.prototype.docTimer={start:function(e){this.clear(),this.timer=setTimeout(e,200)},clear:function(){this.timer&&clearTimeout(this.timer),this.timer=0}},this.Jua=u})(document);