snappymail/vendors/jua/jua.min.js
2020-09-01 16:57:14 +02:00

2 lines
6.2 KiB
JavaScript

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