snappymail/vendors/jua/jua.min.js

2 lines
9.4 KiB
JavaScript

/* RainLoop Webmail (c) RainLoop Team | MIT */
(()=>{"use strict";var e={},n={},t=jQuery;function o(e,n){this.oXhrs={},this.oUids={},this.oJua=e,this.oOptions=n}function i(t){t=n.isUndefined(t)?{}:t;var i=this,r=jQuery;i.bEnableDnD=!0,i.oEvents={onDialog:null,onSelect:null,onStart:null,onComplete:null,onCompleteAll:null,onProgress:null,onDragEnter:null,onDragLeave:null,onDrop:null,onBodyDragEnter:null,onBodyDragLeave:null,onLimitReached:null},i.oOptions=n.extend({action:"",name:"",hidden:{},queueSize:10,clickElement:!1,dragAndDropElement:!1,dragAndDropBodyElement:!1,disableDragAndDrop:!1,disableMultiple:!1,disableDocumentDropPrevent:!1,multipleSizeLimit:50},t),i.oQueue=function(e){function n(){if(t&&r<e){var i=t,l=i[0],d=Array.prototype.slice.call(i,1),c=i.index;t=t===o?o=null:t.next,++r,d.push(function(e,i){--r,u||(e?a&&p(u=e,a=s=t=o=null):(s[c]=i,--a?n():p(null,s)))}),l.apply(null,d)}}var t,o,i={},r=0,a=0,l=-1,u=null,s=[],p=()=>{};return arguments.length<1&&(e=1/0),i.defer=function(){if(!u){var e=arguments;e.index=++l,o?(o.next=e,o=o.next):t=o=e,++a,n()}return i},i.await=function(e){return p=e,a||p(u,s),i},i}(n.pInt(n.getValue(i.oOptions,"queueSize",10))),i.runEvent("onCompleteAll")&&i.oQueue.await(function(){i.runEvent("onCompleteAll")}),i.oDriver=new o(i,i.oOptions),i.oClickElement=n.getValue(i.oOptions,"clickElement",null),i.oClickElement&&(r(i.oClickElement).css({position:"relative",overflow:"hidden"}),"inline"===r(this.oClickElement).css("display")&&r(this.oClickElement).css("display","inline-block"),this.oDriver.generateNewInput(this.oClickElement)),this.oDriver.isDragAndDropSupported()&&n.getValue(this.oOptions,"dragAndDropElement",!1)?function(t){var o=r(window.document),i=r(n.getValue(t.oOptions,"dragAndDropBodyElement",!1)||o),a=n.getValue(t.oOptions,"dragAndDropElement",!1);a&&(n.getValue(t.oOptions,"disableDocumentDropPrevent",!1)||o.on("dragover",function(e){if(t.bEnableDnD&&e&&(e=n.getEvent(e))&&e.dataTransfer&&n.eventContainsFiles(e))try{e.dataTransfer.dropEffect="none",e.preventDefault()}catch(e){}}),i&&i[0]&&i.on("dragover",function(e){t.bEnableDnD&&e&&n.mainClearTimeout(t.iDocTimer)}).on("dragenter",function(e){t.bEnableDnD&&e&&(e=n.getEvent(e))&&n.eventContainsFiles(e)&&(n.mainClearTimeout(t.iDocTimer),e.preventDefault(),t.runEvent("onBodyDragEnter",[e]))}).on("dragleave",function(e){t.bEnableDnD&&e&&(e=n.getEvent(e))&&(n.mainClearTimeout(t.iDocTimer),t.iDocTimer=setTimeout(function(){t.runEvent("onBodyDragLeave",[e])},200))}).on("drop",function(e){if(t.bEnableDnD&&e&&(e=n.getEvent(e))){var o=n.eventContainsFiles(e);return o&&e.preventDefault(),t.runEvent("onBodyDragLeave",[e]),!o}return!1}),r(a).bind("dragenter",function(e){t.bEnableDnD&&e&&(e=n.getEvent(e))&&n.eventContainsFiles(e)&&(n.mainClearTimeout(t.iDocTimer),e.preventDefault(),t.runEvent("onDragEnter",[a,e]))}).bind("dragover",function(e){if(t.bEnableDnD&&e&&(e=n.getEvent(e))&&e.dataTransfer&&n.eventContainsFiles(e))try{var o=e.dataTransfer.effectAllowed;n.mainClearTimeout(t.iDocTimer),e.dataTransfer.dropEffect="move"===o||"linkMove"===o?"move":"copy",e.stopPropagation(),e.preventDefault(),i.trigger("dragover",e)}catch(e){}}).bind("dragleave",function(e){if(t.bEnableDnD&&e&&(e=n.getEvent(e))){var o=window.document.elementFromPoint?window.document.elementFromPoint(e.clientX,e.clientY):null;if(o&&n.contains(this,o))return;n.mainClearTimeout(t.iDocTimer),t.runEvent("onDragLeave",[a,e])}}).bind("drop",function(o){t.bEnableDnD&&o&&(o=n.getEvent(o))&&n.eventContainsFiles(o)&&(o.preventDefault(),n.getDataFromDragEvent(o,function(e){e&&(t.runEvent("onDrop",[e,o]),t.addNewFile(e),n.mainClearTimeout(t.iDocTimer))},n.getValue(t.oOptions,"multipleSizeLimit",e.iDefLimit),t.getEvent("onLimitReached"))),t.runEvent("onDragLeave",[o])}))}(i):i.bEnableDnD=!1,n.setValue(i,"on",i.on),n.setValue(i,"cancel",i.cancel),n.setValue(i,"isDragAndDropSupported",i.isDragAndDropSupported),n.setValue(i,"setDragAndDropEnabledStatus",i.setDragAndDropEnabledStatus)}e.iDefLimit=20,n.isUndefined=function(e){return void 0===e},n.extend=function(e,n){if(n)for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t]);return e},n.contains=function(e,n){var t=!1;return e&&n&&(t=e===n||(e.contains?e.contains(n):!!n.compareDocumentPosition&&!!(8&n.compareDocumentPosition(e)))),t},n.mainClearTimeout=function(e){0<e&&clearTimeout(e),e=0},n.getEvent=function(e){return(e=e&&(e.originalEvent?e.originalEvent:e)||window.event).dataTransfer?e:null},n.getValue=function(e,t,o){return e&&t&&!n.isUndefined(e[t])?e[t]:o},n.setValue=function(e,n,t){e[n]=t},n.isNonEmptyArray=function(e){return!!(e&&e.length&&0<e.length)},n.pInt=function(e){return parseInt(e||0,10)},n.scopeBind=function(e,t){return function(){return e.apply(n.isUndefined(t)?null:t,Array.prototype.slice.call(arguments))}},n.fakeMd5=function(e){var t="",o="0123456789abcdefghijklmnopqrstuvwxyz";for(e=n.isUndefined(e)?32:n.pInt(e);t.length<e;)t+=o.substr(window.Math.round(window.Math.random()*o.length),1);return t},n.getNewUid=function(){return"jua-uid-"+n.fakeMd5(16)+"-"+(new window.Date).getTime().toString()},n.getDataFromFile=function(e){var t=n.isUndefined(e.fileName)?n.isUndefined(e.name)?null:e.name:e.fileName,o=n.isUndefined(e.fileSize)?n.isUndefined(e.size)?null:e.size:e.fileSize,i=n.isUndefined(e.type)?null:e.type;return"/"===t.charAt(0)&&(t=t.substr(1)),""===i&&0===o?null:{FileName:t,Size:o,Type:i,Folder:"",File:e}},n.getDataFromFiles=function(t,o,i,r){var a,l,u=0,s=0,p=null,d=null,c=!1;if(a=i=n.isUndefined(i)?e.iDefLimit:n.pInt(i),l=0<i,t=t&&0<t.length?t:null)for(s=0,u=t.length;s<u;s++)(p=t[s])&&(!l||0<=--i?(d=n.getDataFromFile(p))&&o(d):l&&!c&&0>i&&r&&(c=!0,r(a)))},n.getDataFromInput=function(e,t,o,i){var r=e&&e.files&&0<e.files.length?e.files:null;r?n.getDataFromFiles(r,t,o,i):t({FileName:e.value.split("\\").pop().split("/").pop(),Size:null,Type:null,Folder:"",File:null})},n.eventContainsFiles=function(e){var n=!1;if(e&&e.dataTransfer&&e.dataTransfer.types&&e.dataTransfer.types.length)for(var t=0,o=e.dataTransfer.types.length;t<o;t++)if("files"===e.dataTransfer.types[t].toLowerCase()){n=!0;break}return n},n.getDataFromDragEvent=function(e,t,o,i){var r=null;(e=n.getEvent(e))&&n.eventContainsFiles(e)&&(r=n.getValue(e,"files",null)||(e.dataTransfer?n.getValue(e.dataTransfer,"files",null):null))&&0<r.length&&n.getDataFromFiles(r,t,o,i)},n.createNextLabel=function(){return t('<label style="position: absolute; background-color:#fff; right: 0px; top: 0px; left: 0px; bottom: 0px; margin: 0px; padding: 0px; cursor: pointer;"></label>').css({opacity:0})},n.createNextInput=function(){return t('<input type="file" tabindex="-1" hidefocus="hidefocus" style="position: absolute; left: -9999px;" />')},n.getNewInput=function(e,t){e=n.isUndefined(e)?"":e.toString();var o=n.createNextInput();return 0<e.length&&o.attr("name",e),(n.isUndefined(t)||t)&&o.prop("multiple",!0),o},n.getStringOrCallFunction=function(e,n){return t.isFunction(e)?e.apply(null,t.isArray(n)?n:[]).toString():e.toString()},o.prototype.oXhrs={},o.prototype.oUids={},o.prototype.oJua=null,o.prototype.oOptions={},o.prototype.isDragAndDropSupported=(()=>!0),o.prototype.regTaskUid=function(e){this.oUids[e]=!0},o.prototype.uploadTask=function(e,o,i){if(!1===this.oUids[e]||!o||!o.File)return i(null,e),!1;try{var r=this,a=new XMLHttpRequest,l=new FormData,u=n.getValue(this.oOptions,"action",""),s=n.getValue(this.oOptions,"hidden",{}),p=this.oJua.getEvent("onStart"),d=this.oJua.getEvent("onComplete"),c=this.oJua.getEvent("onProgress");return a.open("POST",u,!0),c&&a.upload&&(a.upload.onprogress=function(t){t&&t.lengthComputable&&!n.isUndefined(t.loaded)&&!n.isUndefined(t.total)&&c(e,t.loaded,t.total)}),a.onreadystatechange=function(){if(4===a.readyState&&200===a.status){if(d){var o=!1,l=null;try{l=t.parseJSON(a.responseText),o=!0}catch(e){l=null}d(e,o,l)}n.isUndefined(r.oXhrs[e])||(r.oXhrs[e]=null),i(null,e)}else 4===a.readyState&&(d(e,!1,null),i(null,e))},p&&p(e),l.append("jua-post-type","ajax"),l.append(n.getValue(this.oOptions,"name","juaFile"),o.File),t.each(s,function(e,t){l.append(e,n.getStringOrCallFunction(t,[o]))}),a.send(l),this.oXhrs[e]=a,!0}catch(e){}return i(null,e),!1},o.prototype.generateNewInput=function(o){var i=this,r=null,a=null;o&&(a=n.getNewInput("",!n.getValue(this.oOptions,"disableMultiple",!1)),(r=n.createNextLabel()).append(a),t(o).append(r),a.on("click",function(){var e=i.oJua.getEvent("onDialog");e&&e()}).on("change",function(){n.getDataFromInput(this,function(e){i.oJua.addNewFile(e),i.generateNewInput(o),setTimeout(function(){r.remove()},10)},n.getValue(i.oOptions,"multipleSizeLimit",e.iDefLimit),i.oJua.getEvent("onLimitReached"))}))},o.prototype.cancel=function(e){if(this.oUids[e]=!1,this.oXhrs[e]){try{this.oXhrs[e].abort&&this.oXhrs[e].abort()}catch(e){}this.oXhrs[e]=null}},i.prototype.bEnableDnD=!0,i.prototype.iDocTimer=0,i.prototype.oOptions={},i.prototype.oEvents={},i.prototype.oQueue=null,i.prototype.oDriver=null,i.prototype.on=function(e,n){return this.oEvents[e]=n,this},i.prototype.runEvent=function(e,n){this.oEvents[e]&&this.oEvents[e].apply(null,n||[])},i.prototype.getEvent=function(e){return this.oEvents[e]||null},i.prototype.cancel=function(e){this.oDriver.cancel(e)},i.prototype.setDragAndDropEnabledStatus=function(e){this.bEnableDnD=!!e},i.prototype.isDragAndDropSupported=function(){return this.oDriver.isDragAndDropSupported()},i.prototype.addNewFile=function(e){this.addFile(n.getNewUid(),e)},i.prototype.addFile=function(e,t){var o=this.getEvent("onSelect");!t||o&&!1===o(e,t)?this.oDriver.cancel(e):(this.oDriver.regTaskUid(e),this.oQueue.defer(n.scopeBind(this.oDriver.uploadTask,this.oDriver),e,t))},window.Jua=i})();