snappymail/vendors/jua/jua.min.js

3 lines
5.1 KiB
JavaScript

/* RainLoop Webmail (c) RainLoop Team | MIT */
(()=>{"use strict";const e=document,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,u=0<i,d=!1;Array.from(e).forEach(e=>{e&&(!u||0<=--i?(s=o(e))&&r(s):u&&!d&&0>i&&l&&(d=!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"),u=this.oJua.getEvent("onComplete"),d=this.oJua.getEvent("onProgress");return i.open("POST",l,!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),o.append("jua-post-type","ajax"),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("label"),o=i.appendChild(e.createElement("input"));o.type="file",o.tabIndex=-1,o.style.cssText="position:absolute;left:-9999px;",o.multiple=!n.oOptions.disableMultiple,i.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(i),o.addEventListener("input",()=>{const e=e=>{n.oJua.addNewFile(e),n.generateNewInput(t),setTimeout(()=>i.remove(),10)};o.files&&o.files.length?r(o.files,e,n.oOptions.multipleSizeLimit,n.oJua.getEvent("onLimitReached")):e({FileName:o.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 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 u=n.clickElement;if(u&&(u.style.position="relative",u.style.overflow="hidden","inline"===u.style.display&&(u.style.display="inline-block"),o.oDriver.generateNewInput(u)),u=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(u,{dragenter:e=>{l(e)&&(o.docTimer.clear(),e.preventDefault(),o.runEvent("onDragEnter",[u,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&&u.contains(n)||(o.docTimer.clear(),o.runEvent("onDragLeave",[u,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){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}},window.Jua=u})();