snappymail/vendors/jua/jua.min.js
2020-12-30 15:50:47 +01:00

2 lines
4.9 KiB
JavaScript

/* RainLoop Webmail (c) RainLoop Team | MIT */
(e=>{const t=20,n=e=>void 0!==e,r=(e,r,i,l)=>{if(e&&e.length){let a=i=n(i)?parseInt(i||0,10):t,s=null,d=0<i,u=!1;[...e].forEach(e=>{e&&(!d||0<=--i?(s=o(e))&&r(s):d&&!u&&0>i&&l&&(u=!0,l(a)))})}},i=(e,t)=>Object.entries(t).forEach(([t,n])=>e.addEventListener(t,n)),o=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},l=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,o=new FormData,l=this.oOptions.action,a=this.oOptions.hidden,s=this.oJua.getEvent("onStart"),d=this.oJua.getEvent("onProgress");return i.open("POST",l,!0),d&&i.upload&&(i.upload.onprogress=(t=>{t&&t.lengthComputable&&n(t.loaded)&&n(t.total)&&d(e,t.loaded,t.total)})),i.onreadystatechange=(()=>{if(4===i.readyState){delete r.oXhrs[e];let t=!1,n=null;if(200===i.status)try{n=JSON.parse(i.responseText),t=!0}catch(e){console.error(e)}this.oJua.getEvent("onComplete")(e,t,t?n:null)}}),s&&s(e),o.append(this.oOptions.name,t.File),Object.entries(a).forEach(([e,n])=>o.append(e,("function"==typeof n?n(t):n).toString())),i.send(o),this.oXhrs[e]=i,!0}catch(e){console.error(e)}return!1}generateNewInput(t){if(t){const n=this,i=e.createElement("input"),o=()=>i.click();i.type="file",i.tabIndex=-1,i.style.display="none",i.multiple=!n.oOptions.disableMultiple,t.addEventListener("click",o),i.addEventListener("input",()=>{const e=e=>{n.oJua.addNewFile(e),setTimeout(()=>{i.remove(),t.removeEventListener("click",o),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)}delete this.oXhrs[e]}}}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 d{constructor(n){const o=this;o.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||{}),o.oQueue=new s(n.queueSize),o.oDriver=new a(o,n);let d=n.clickElement;if(d&&(d.style.position="relative",d.style.overflow="hidden","inline"===d.style.display&&(d.style.display="inline-block"),o.oDriver.generateNewInput(d)),d=n.dragAndDropElement){let t=n.dragAndDropBodyElement||e;n.disableDocumentDropPrevent||e.addEventListener("dragover",e=>{if(l(e))try{e.dataTransfer.dropEffect="none",e.preventDefault()}catch(e){console.error(e)}}),t&&i(t,{dragover:()=>o.docTimer.clear(),dragenter:e=>{l(e)&&(o.docTimer.clear(),e.preventDefault(),o.runEvent("onBodyDragEnter",[e]))},dragleave:e=>e.dataTransfer&&o.docTimer.start(()=>o.runEvent("onBodyDragLeave",[e])),drop:e=>{if(e.dataTransfer){let t=l(e);return t&&e.preventDefault(),o.runEvent("onBodyDragLeave",[e]),!t}return!1}}),i(d,{dragenter:e=>{l(e)&&(o.docTimer.clear(),e.preventDefault(),o.runEvent("onDragEnter",[d,e]))},dragover:e=>{if(l(e))try{let t=e.dataTransfer.effectAllowed;o.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&&d.contains(n)||(o.docTimer.clear(),o.runEvent("onDragLeave",[d,t]))}},drop:e=>{l(e)&&(e.preventDefault(),r(e.files||e.dataTransfer.files,e=>{e&&(o.addNewFile(e),o.docTimer.clear())},n.multipleSizeLimit,o.getEvent("onLimitReached"))),o.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){this.addFile("jua-uid-"+d.randomId(16)+"-"+Date.now().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))}}d.randomId=(e=>{let t=new Uint8Array((e||32)/2);return crypto.getRandomValues(t),t.map(e=>e.toString(16).padStart(2,"0")).join("")}),d.prototype.docTimer={start:function(e){this.clear(),this.timer=setTimeout(e,200)},clear:function(){this.timer&&clearTimeout(this.timer),this.timer=0}},this.Jua=d})(document);