filesafe/public/js/pureupload.min.js
2017-01-14 03:01:23 -03:00

1 line
19 KiB
JavaScript
Executable File

var pu;!function(t){function e(t,e,i){if(t.addEventListener)t.addEventListener(e,i);else{var o=t;o.attachEvent?o.attachEvent("on"+e,i):o[e]=i}}function i(t,e){var i;return i="object"==typeof t?c(o(u(t),function(t){return"length"!==t}),function(e){return t[e]}):t,n(i,function(t){t.uploadStatus=e||t.uploadStatus,t.responseCode=t.responseCode||0,t.responseText=t.responseText||"",t.progress=t.progress||0,t.sentBytes=t.sentBytes||0,t.cancel=t.cancel||function(){}}),i}function o(t,e){var i=[];return t?(n(t,function(t){e(t)&&i.push(t)}),i):i}function n(t,e){if(t)for(var i=0;i<t.length;i++)e(t[i],i)}function s(t,e,i){return void 0===i&&(i=!1),t?i?function(){e(),t()}:function(){t(),e()}:e}function a(t,e){return new f(t,e)}function l(t,e){return new m(t,e)}function r(){var t=(new Date).getTime(),e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var i=(t+16*Math.random())%16|0;return t=Math.floor(t/16),("x"===e?i:3&i|8).toString(16)});return e}function p(t,e){if(!t)return-1;for(var i=0;i<t.length;i++)if(t[i]===e)return i;return-1}function u(t){if(Object&&Object.keys)return Object.keys(t);var e=[];for(var i in t)t.hasOwnProperty(i)&&e.push(i);return e}function c(t,e){var i=[];return t?(n(t,function(t){i.push(e(t))}),i):i}function h(t,e,i){if(t.removeEventListener)t.removeEventListener(e,i);else{var o=t;o.detachEvent?o.detachEvent("on"+e,i):o[e]=null}}t.addEventHandler=e,t.isFileApi=!(!window.File||!window.FormData),t.castFiles=i,t.filter=o,t.forEach=n,t.decorateSimpleFunction=s,t.getUploadCore=a,t.getUploader=l,t.newGuid=r,t.indexOf=p,t.keys=u,t.map=c,t.removeEventHandler=h;var d=function(){function o(e,i,o,n){n&&(this.formForNoFileApi="form"===n.tagName.toLowerCase()?n:n.getElementsByTagName("form")[0]),this.targetElement=e,this.options=i,this.uploader=o,this.uploadCore=a(this.options,this.uploader.queue.callbacks),this.setFullOptions(i),t.isFileApi?this.setupFileApiElements():this.setupOldSchoolElements()}return o.prototype.destroy=function(){t.isFileApi?(this.unregisterOnClick&&this.unregisterOnClick(),this.unregisterOnDrop&&this.unregisterOnDrop(),this.unregisterOnChange&&this.unregisterOnChange(),this.unregisterOnDragOver&&this.unregisterOnDragOver(),this.targetElement.removeEventListener("dragover",this.onDrag),this.targetElement.removeEventListener("drop",this.onDrop),document.body.removeChild(this.fileInput)):(this.unregisterFormOnChange&&this.unregisterFormOnChange(),this.lastIframe&&this.formForNoFileApi.parentNode&&this.formForNoFileApi.parentNode.removeChild(this.lastIframe),this.formForNoFileApiProvided||(this.formForNoFileApi.parentNode&&this.formForNoFileApi.parentNode.insertBefore(this.targetElement,this.formForNoFileApi.nextSibling||null),this.targetElement.parentNode&&this.targetElement.parentNode.removeChild(this.formForNoFileApi)))},o.prototype.setFullOptions=function(e){this.options.maxFileSize=e.maxFileSize||1024,this.options.allowDragDrop=t.isFileApi&&(void 0===e.allowDragDrop||null===e.allowDragDrop||e.allowDragDrop),this.options.clickable=void 0===e.clickable||null===e.clickable||e.clickable,this.options.accept=e.accept||"*.*",this.options.validateExtension=!!e.validateExtension,this.options.multiple=t.isFileApi&&(void 0===e.multiple||null===e.multiple||e.multiple)},o.prototype.putFilesToQueue=function(t){var e=this,o=i(t);n(o,function(t){t.guid=r(),t.url=e.uploadCore.getUrl(t),t.onError=e.options.onFileError||function(){},t.onCancel=e.options.onFileCanceled||function(){},e.validateFile(t)?t.start=function(){e.uploadCore.upload([t]),e.options.onFileAdded&&e.options.onFileAdded(t),t.start=function(){}}:t.onError(t)}),this.uploader.queue.addFiles(o)},o.prototype.validateFile=function(t){return this.isFileSizeValid(t)?!this.isFileTypeInvalid(t)||(t.uploadStatus=g.failed,t.responseText=this.options.localizer?this.options.localizer("File format is not allowed. Only { accept } files are allowed.",this.options):"File format is not allowed. Only "+(this.options.accept?this.options.accept.split(".").join(" "):"")+" files are allowed.",!1):(t.uploadStatus=g.failed,t.responseText=this.options.localizer?this.options.localizer("The selected file exceeds the allowed size of { maxFileSize } MB or its size is 0 MB. Please choose another file.",this.options):"The selected file exceeds the allowed size of "+this.options.maxFileSize+" or its size is 0 MB. Please choose another file.",!1)},o.prototype.setupFileApiElements=function(){var t=this;this.fileInput=document.createElement("input"),this.fileInput.setAttribute("type","file"),this.fileInput.setAttribute("accept",this.options.accept?this.options.accept:""),this.fileInput.style.display="none",this.formForNoFileApi&&(this.formForNoFileApi.style.display="none");var i=function(e){return t.onChange(e)};if(e(this.fileInput,"change",i),this.unregisterOnChange=function(){return h(t.fileInput,"change",onchange)},this.options.multiple&&this.fileInput.setAttribute("multiple",""),this.options.clickable){var o=function(){return t.onClick()};e(this.targetElement,"click",o),this.unregisterOnClick=function(){return h(t.targetElement,"click",o)}}if(this.options.allowDragDrop){var n=function(e){return t.onDrag(e)};e(this.targetElement,"dragover",n),this.unregisterOnDragOver=function(){return h(t.targetElement,"dragover",n)};var s=function(e){return t.onDrop(e)};e(this.targetElement,"drop",s),this.unregisterOnDrop=function(){return h(t.targetElement,"drop",s)}}document.body.appendChild(this.fileInput)},o.prototype.setupOldSchoolElements=function(){var t=this;if(this.options.clickable){this.formForNoFileApi?this.decorateInputForm():this.createFormWrapper();var i=this.findInnerSubmit(),o=function(e){return t.onFormChange(e,t.fileInput,i)};e(this.fileInput,"change",o),this.unregisterFormOnChange=function(){return h(t.fileInput,"change",o)}}},o.prototype.createFormWrapper=function(){this.fileInput=document.createElement("input"),this.fileInput.setAttribute("type","file"),this.fileInput.setAttribute("accept",this.options.accept?this.options.accept:""),this.fileInput.setAttribute("name","file"),this.fileInput.style.position="absolute",this.fileInput.style.left="0",this.fileInput.style.right="0",this.fileInput.style.top="0",this.fileInput.style.bottom="0",this.fileInput.style.width="100%",this.fileInput.style.height="100%",this.fileInput.style.fontSize="10000%",this.fileInput.style.opacity="0",this.fileInput.style.filter="alpha(opacity=0)",this.fileInput.style.cursor="pointer",this.formForNoFileApi=document.createElement("form"),this.formForNoFileApi.setAttribute("method",this.uploadCore.options.method),this.formForNoFileApi.setAttribute("enctype","multipart/form-data"),this.formForNoFileApi.setAttribute("encoding","multipart/form-data"),this.formForNoFileApi.style.position="relative",this.formForNoFileApi.style.display="block",this.formForNoFileApi.style.overflow="hidden",this.formForNoFileApi.style.width=this.targetElement.offsetWidth.toString()+"px",this.formForNoFileApi.style.height=this.targetElement.offsetHeight.toString()+"px",0!==this.targetElement.clientHeight&&0!==this.targetElement.clientWidth||console.warn("upload element height and width has to be set to be able catch upload"),this.targetElement.parentNode&&this.targetElement.parentNode.insertBefore(this.formForNoFileApi,this.targetElement.nextSibling||null),this.formForNoFileApi.appendChild(this.targetElement),this.formForNoFileApi.appendChild(this.fileInput)},o.prototype.decorateInputForm=function(){this.formForNoFileApiProvided=!0,this.targetElement.style.display="none",this.formForNoFileApi.setAttribute("method",this.uploadCore.options.method),this.formForNoFileApi.setAttribute("enctype","multipart/form-data"),this.formForNoFileApi.setAttribute("encoding","multipart/form-data");for(var t=this.formForNoFileApi.getElementsByTagName("input"),e=0;e<t.length;e++){var i=t[e];"file"===i.type&&(this.fileInput=i)}},o.prototype.findInnerSubmit=function(){for(var t=this.formForNoFileApi.getElementsByTagName("input"),e=0;e<t.length;e++){var i=t[e];if("submit"===i.type)return i}},o.prototype.fileListToList=function(t){if(!t)return[];for(var e=[],i=0;i<t.length;i++)e.push(t[i]);return e},o.prototype.onFormChange=function(t,e,i){var o=this,s=t.target?t.target.files?this.fileListToList(t.target.files):t.target.value?[{name:t.target.value.replace(/^.+\\/,"")}]:[]:e.value?[{name:e.value.replace(/^.+\\/,"")}]:[];n(s,function(t){t.guid=t.guid||r(),t.url=o.uploadCore.getUrl(t)}),0!==s.length&&(this.addTargetIframe(),this.formForNoFileApi.setAttribute("action",s[0].url),i||this.formForNoFileApi.submit())},o.prototype.addTargetIframe=function(){this.lastIframe&&this.formForNoFileApi.parentNode&&this.formForNoFileApi.parentNode.removeChild(this.lastIframe);var t="uploadIframe"+Date.now(),e=this.lastIframe=document.createElement("iframe");e.setAttribute("id",t),e.setAttribute("name",t),e.style.border="none",e.style.display="none",e.style.width="0",e.style.height="0",this.formForNoFileApi.setAttribute("target",t),this.formForNoFileApi.parentNode&&this.formForNoFileApi.parentNode.insertBefore(e,this.formForNoFileApi.nextSibling||null);var i=window.frames[t];i&&(i.name=t)},o.prototype.onChange=function(t){this.putFilesToQueue(t.target.files)},o.prototype.onDrag=function(t){var e=void 0;try{e=t.dataTransfer.effectAllowed}catch(i){}t.dataTransfer.dropEffect="move"===e||"linkMove"===e?"move":"copy",this.stopEventPropagation(t)},o.prototype.onDrop=function(t){if(this.stopEventPropagation(t),t.dataTransfer){var e=t.dataTransfer.files;if(e.length){this.options.multiple||(e=[e[0]]);var i=t.dataTransfer.items;if(i&&i.length&&null!==i[0].webkitGetAsEntry)if(this.options.multiple)this.addFilesFromItems(i);else{var o=[i[0]];this.addFilesFromItems(o)}else this.handleFiles(e)}}},o.prototype.isIeVersion=function(t){return RegExp("msie"+(isNaN(t)?"":"\\s"+t.toString()),"i").test(navigator.userAgent)},o.prototype.onClick=function(){var t=this;this.fileInput.value="",this.isIeVersion(10)?setTimeout(function(){t.fileInput.click()},200):this.fileInput.click()},o.prototype.addFilesFromItems=function(t){for(var e,i=0;i<t.length;i++){var o=t[i];o.webkitGetAsEntry&&(e=o.webkitGetAsEntry())?e.isFile?this.putFilesToQueue([o.getAsFile()]):e.isDirectory&&this.processDirectory(e,e.name):o.getAsFile&&(o.kind&&"file"!==o.kind||this.putFilesToQueue([o.getAsFile()]))}},o.prototype.processDirectory=function(t,e){var i=t.createReader(),o=this,n=function(t){for(var i=0;i<t.length;i++){var n=t[i];n.isFile?n.file(function(t){"."!==t.name.substring(0,1)&&(t.fullPath=""+e+"/"+t.name,o.putFilesToQueue([t]))}):n.isDirectory&&o.processDirectory(n,""+e+"/"+n.name)}};i.readEntries(n,function(t){return"undefined"!=typeof console&&null!==console&&"function"==typeof console.log?console.log(t):void 0})},o.prototype.handleFiles=function(t){for(var e=0;e<t.length;e++)this.putFilesToQueue([t[e]])},o.prototype.isFileSizeValid=function(t){var e=1024*this.options.maxFileSize*1024;return!(t.size>e||0===t.size)},o.prototype.isFileTypeInvalid=function(t){if(t.name&&this.options.accept&&("*"!==this.options.accept.trim()||"*.*"!==this.options.accept.trim())&&this.options.validateExtension&&this.options.accept.indexOf("/")===-1){var e=this.options.accept.split(","),i=t.name.substring(t.name.lastIndexOf("."),t.name.length);if(i.indexOf(".")===-1)return!0;for(var o=!0,n=0;n<e.length;n++)e[n].toUpperCase().trim()===i.toUpperCase()&&(o=!1);return o}return!1},o.prototype.stopEventPropagation=function(t){t.stopPropagation(),t.preventDefault?t.preventDefault():t.returnValue=!1},o}();t.UploadArea=d;var f=function(){function e(t,e){void 0===e&&(e={}),this.options=t,this.callbacks=e,this.setFullOptions(t),this.setFullCallbacks(e)}return e.prototype.upload=function(e){var o=this;if(t.isFileApi){var s=i(e,g.uploading);n(s,function(t){return o.processFile(t)})}},e.prototype.getUrl=function(t){return"function"==typeof this.options.url?this.options.url(t):this.options.url},e.prototype.processFile=function(t){var e=this.createRequest(t);this.setCallbacks(e,t),this.send(e,t)},e.prototype.createRequest=function(t){var e=new XMLHttpRequest,i=t.url||this.getUrl(t);return e.open(this.options.method,i,!0),e.withCredentials=!!this.options.withCredentials,this.setHeaders(e),e},e.prototype.setHeaders=function(t){var e=this;this.options.headers&&(this.options.headers.Accept||t.setRequestHeader("Accept","application/json"),this.options.headers["Cache-Control"]||t.setRequestHeader("Cache-Control","no-cache"),this.options.headers["X-Requested-With"]||t.setRequestHeader("X-Requested-With","XMLHttpRequest"),n(u(this.options.headers),function(i){if(e.options.headers){var o=e.options.headers[i];void 0!==o&&null!==o&&t.setRequestHeader(i,(o||"").toString())}}))},e.prototype.setCallbacks=function(t,e){var i=this;e.cancel=s(e.cancel,function(){t.abort(),e.uploadStatus=g.canceled,e.onCancel&&e.onCancel(e),i.callbacks.onCancelledCallback&&i.callbacks.onCancelledCallback(e),i.callbacks.onFileStateChangedCallback&&i.callbacks.onFileStateChangedCallback(e),i.callbacks.onFinishedCallback&&i.callbacks.onFinishedCallback(e)},!0),t.onload=function(){return i.onload(e,t)},t.onerror=function(){return i.handleError(e,t)},t.upload.onprogress=function(t){return i.updateProgress(e,t)}},e.prototype.send=function(t,e){var i=this.createFormData(e);this.callbacks.onUploadStartedCallback&&this.callbacks.onUploadStartedCallback(e),this.callbacks.onFileStateChangedCallback&&this.callbacks.onFileStateChangedCallback(e),t.send(i)},e.prototype.createFormData=function(t){var e=this,i=new FormData;return this.options.params&&n(u(this.options.params),function(t){if(e.options.params){var o=e.options.params[t];void 0!==o&&null!==o&&i.append(t,o)}}),i.append("file",t,t.name),i},e.prototype.handleError=function(t,e){t.uploadStatus=g.failed,this.setResponse(t,e),t.onError&&t.onError(t),this.callbacks.onErrorCallback&&this.callbacks.onErrorCallback(t),this.callbacks.onFileStateChangedCallback&&this.callbacks.onFileStateChangedCallback(t),this.callbacks.onFinishedCallback&&this.callbacks.onFinishedCallback(t)},e.prototype.updateProgress=function(t,e){e?e.lengthComputable?(t.progress=Math.round(100*(e.loaded/e.total)),t.sentBytes=e.loaded):(t.progress=0,t.sentBytes=0):(t.progress=100,t.sentBytes=t.size),this.callbacks.onProgressCallback&&this.callbacks.onProgressCallback(t)},e.prototype.onload=function(t,e){4===e.readyState&&(100!==t.progress&&this.updateProgress(t),200===e.status?this.finished(t,e):this.handleError(t,e))},e.prototype.finished=function(t,e){t.uploadStatus=g.uploaded,this.setResponse(t,e),this.callbacks.onUploadedCallback&&this.callbacks.onUploadedCallback(t),this.callbacks.onFileStateChangedCallback&&this.callbacks.onFileStateChangedCallback(t),this.callbacks.onFinishedCallback&&this.callbacks.onFinishedCallback(t)},e.prototype.setResponse=function(t,e){t.responseCode=e.status;var i=e.responseText||e.statusText||(e.status?e.status.toString():"Invalid response from server");t.responseText=this.options.localizer?this.options.localizer(i,{}):i},e.prototype.setFullOptions=function(t){this.options.url=t.url,this.options.method=t.method,this.options.headers=t.headers||{},this.options.params=t.params||{},this.options.withCredentials=t.withCredentials||!1,this.options.localizer=t.localizer},e.prototype.setFullCallbacks=function(t){this.callbacks.onProgressCallback=t.onProgressCallback||function(){},this.callbacks.onCancelledCallback=t.onCancelledCallback||function(){},this.callbacks.onFinishedCallback=t.onFinishedCallback||function(){},this.callbacks.onUploadedCallback=t.onUploadedCallback||function(){},this.callbacks.onErrorCallback=t.onErrorCallback||function(){},this.callbacks.onUploadStartedCallback=t.onUploadStartedCallback||function(){},this.callbacks.onFileStateChangedCallback=t.onFileStateChangedCallback||function(){}},e}();t.UploadCore=f;var m=function(){function t(t,e){void 0===t&&(t={}),void 0===e&&(e={}),this.setOptions(t),this.uploadAreas=[],this.queue=new F(t,e)}return t.prototype.setOptions=function(t){this.options=t},t.prototype.registerArea=function(t,e,i){var o=new d(t,e,this,i);return this.uploadAreas.push(o),o},t.prototype.unregisterArea=function(t){var e=p(this.uploadAreas,t);e>=0&&(this.uploadAreas[e].destroy(),this.uploadAreas.splice(e,1))},t}();t.Uploader=m;var F=function(){function e(t,e){this.queuedFiles=[],this.options=t,this.callbacks=e,this.setFullOptions(),this.setFullCallbacks()}return e.prototype.addFiles=function(t){var e=this;n(t,function(t){e.queuedFiles.push(t),t.remove=s(t.remove,function(){e.removeFile(t)}),e.callbacks.onFileAddedCallback&&e.callbacks.onFileAddedCallback(t),t.uploadStatus===g.failed?e.callbacks.onErrorCallback&&e.callbacks.onErrorCallback(t):t.uploadStatus=g.queued}),this.filesChanged()},e.prototype.removeFile=function(t,e){void 0===e&&(e=!1);var i=p(this.queuedFiles,t);i<0||(this.deactivateFile(t),this.queuedFiles.splice(i,1),this.callbacks.onFileRemovedCallback&&this.callbacks.onFileRemovedCallback(t),e||this.filesChanged())},e.prototype.clearFiles=function(t,e){var i=this;void 0===t&&(t=[]),void 0===e&&(e=!1),e||(t=t.concat([g.queued,g.uploading])),n(o(this.queuedFiles,function(e){return p(t,e.uploadStatus)<0}),function(t){return i.removeFile(t,!0)}),this.callbacks.onQueueChangedCallback&&this.callbacks.onQueueChangedCallback(this.queuedFiles)},e.prototype.filesChanged=function(){this.options.autoRemove&&this.removeFinishedFiles(),this.options.autoStart&&this.startWaitingFiles(),this.callbacks.onQueueChangedCallback&&this.callbacks.onQueueChangedCallback(this.queuedFiles),this.checkAllFinished()},e.prototype.checkAllFinished=function(){var t=o(this.queuedFiles,function(t){return p([g.queued,g.uploading],t.uploadStatus)>=0});0===t.length&&this.callbacks.onAllFinishedCallback&&this.callbacks.onAllFinishedCallback()},e.prototype.setFullOptions=function(){this.options.maxParallelUploads=this.options.maxParallelUploads||0,this.options.autoStart=t.isFileApi&&(this.options.autoStart||!1),this.options.autoRemove=this.options.autoRemove||!1},e.prototype.setFullCallbacks=function(){var t=this;this.callbacks.onFileAddedCallback=this.callbacks.onFileAddedCallback||function(){},this.callbacks.onFileRemovedCallback=this.callbacks.onFileRemovedCallback||function(){},this.callbacks.onAllFinishedCallback=this.callbacks.onAllFinishedCallback||function(){},this.callbacks.onQueueChangedCallback=this.callbacks.onQueueChangedCallback||function(){},this.callbacks.onFileStateChangedCallback=function(){return t.filesChanged()}},e.prototype.startWaitingFiles=function(){n(this.getWaitingFiles(),function(t){return t.start()})},e.prototype.removeFinishedFiles=function(){var t=this;n(o(this.queuedFiles,function(t){return p([g.uploaded,g.canceled],t.uploadStatus)>=0}),function(e){return t.removeFile(e,!0)})},e.prototype.deactivateFile=function(t){t.uploadStatus===g.uploading&&t.cancel(),t.uploadStatus=g.removed,t.cancel=function(){},t.remove=function(){},t.start=function(){}},e.prototype.getWaitingFiles=function(){if(!this.options.autoStart)return[];var t=o(this.queuedFiles,function(t){return t.uploadStatus===g.queued});if(this.options.maxParallelUploads>0){var e=o(this.queuedFiles,function(t){return t.uploadStatus===g.uploading}).length,i=this.options.maxParallelUploads-e;if(i<=0)return[];t=t.slice(0,i)}return t},e}();t.UploadQueue=F;var g;!function(t){t[t.queued=0]="queued",t[t.uploading=1]="uploading",t[t.uploaded=2]="uploaded",t[t.failed=3]="failed",t[t.canceled=4]="canceled",t[t.removed=5]="removed"}(g=t.UploadStatus||(t.UploadStatus={}))}(pu||(pu={}));