snappymail/vendors/jua/jua.min.js

3 lines
6.2 KiB
JavaScript
Raw Normal View History

2014-09-11 23:00:05 +08:00
/* 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,"dragAn