-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuploader.min.js
2 lines (2 loc) · 13.7 KB
/
uploader.min.js
1
2
/*! UploaderJS 2016-11-04, 7:31:35 PM */
!function(a,b){"use strict";function c(a){this.init(a)}a.extend(c,{defaults:{selectButton:null,dropZone:null,name:"file",multiple:!0,url:null,method:"POST",autoUpload:!0,removeOnFail:!0,headers:{},data:{},acceptType:null,acceptSize:[null,null],maxFiles:null,simultaneousUploads:3,degrade:!1,cssClasses:{dropZoneDragOver:"drop-zone-drag-over"},errors:{invalidType:"The file '{{fileName}}' is not valid. Please upload only files with the following extensions: {{allowedExtensions}}.",sizeTooSmall:"The file '{{fileName}}' is too small. Please upload only files bigger than {{allowedMinSize}}.",sizeTooLarge:"The file '{{fileName}}' is too large. Please upload only files smaller than {{allowedMaxSize}}.",tooManyFiles:"Can not upload the file '{{fileName}}'', because you can upload only {{maxFiles}} file(s).",networkError:"There was a problem uploading the file '{{fileName}}'. Please try uploading that file again."},uniqueIdPrefix:"upload_"},uniqueIncrement:1,support:function(a){var b={};b.xhrUpload=!!a.FormData&&!!a.XMLHttpRequest&&"upload"in new XMLHttpRequest,b.selectMultiple=!!a.FileList&&"multiple"in document.createElement("input");var c=document.createElement("div");return b.dropFiles="ondragstart"in c&&"ondrop"in c&&!!a.FileList,b}(b),STATUS_ADDED:"ADDED",STATUS_UPLOADING:"UPLOADING",STATUS_COMPLETED:"COMPLETED",STATUS_PENDING:"PENDING",STATUS_FAILED:"FAILED"}),b.Uploader=c}(jQuery,window),function(a,b){"use strict";b.extend(a.prototype,{init:function(a){this.options=b.extend(!0,{},this.constructor.defaults,a),this.isModernBrowser=this.constructor.support.xhrUpload&&!this.options.degrade,this.fileList={},this.pendingList=[],this.fileCount=0,this.eventHandlers={},this.initElements()},initElements:function(){this.$selectButton=b(this.options.selectButton),this.createFileInput(),this.options.dropZone&&this.constructor.support.dropFiles&&this.isModernBrowser&&this.initDropZone()},createFileInput:function(){this.$fileInput=b("<input/>",{name:this.options.name,accept:(this.options.acceptType||[]).join(),type:"file"}).appendTo(this.$selectButton),this.options.multiple&&this.constructor.support.selectMultiple&&this.isModernBrowser&&this.$fileInput.attr("multiple","multiple"),this.$fileInput.on("change",b.proxy(this.onFileSelect,this))},initDropZone:function(){this.$dropZone=b(this.options.dropZone),this.$dropZone.on("dragover.Uploader",b.proxy(function(a){return a.preventDefault(),this.$dropZone.addClass(this.options.cssClasses.dropZoneDragOver),!1},this)),this.$dropZone.on("dragend.Uploader",b.proxy(function(a){return a.preventDefault(),this.$dropZone.removeClass(this.options.cssClasses.dropZoneDragOver),!1},this)),this.$dropZone.on("dragleave.Uploader",b.proxy(function(a){return a.preventDefault(),this.$dropZone.removeClass(this.options.cssClasses.dropZoneDragOver),!1},this)),this.$dropZone.on("drop.Uploader",b.proxy(function(a){a.preventDefault(),this.$dropZone.removeClass(this.options.cssClasses.dropZoneDragOver),a.originalEvent.dataTransfer&&a.originalEvent.dataTransfer.files&&this.addToList(a.originalEvent.dataTransfer.files)},this))}})}(window.Uploader,jQuery),function(a,b){"use strict";b.extend(a.prototype,{onFileSelect:function(){var a=this.isModernBrowser?this.$fileInput[0].files:[this.$fileInput];this.addToList(a),this.isModernBrowser?this.$fileInput.remove():this.$fileInput.off().detach(),this.createFileInput()},onFileAdd:function(a,b,c){this.trigger("fileAdd",a,b,c)},onFileRemove:function(a,b){this.trigger("fileRemove",a,b)},onFileInvalid:function(a,b){this.trigger("fileInvalid",a,b)},onTooManyFiles:function(a,b){this.trigger("tooManyFiles",a,b)},onBeforeUpload:function(a,b,c,d){this.trigger("beforeUpload",a,b,c,d)},onUploadStart:function(a,b){var c=(new Date).getTime();this.fileList[a].progress={startTime:c,endTime:null,previous:{time:null,bytes:0},current:{time:c,bytes:0}},this.trigger("uploadStart",a,b)},onUploadProgress:function(a,b,c,d){var e=(new Date).getTime(),f=this.fileList[a].progress;f.previous={time:f.current.time,bytes:f.current.bytes},f.current={time:e,bytes:c},c==d&&(f.endTime=e),this.trigger("uploadProgress",a,b,c,d)},onUploadComplete:function(a,b,c,d,e){try{this.trigger("uploadComplete",a,b,c,d,e),this.fileList[a].status=this.constructor.STATUS_COMPLETED,this.isModernBrowser||this.iFrameCleanUp(a),this.uploadNext()}catch(f){if("UploadError"!=f.name)throw f;this.onUploadFail(a,b,this.errorMessage(f.message,this.fileList[a].file))}},onUploadFail:function(a,b,c){this.fileList[a].status=this.constructor.STATUS_FAILED,this.trigger("uploadFail",a,b,c),this.fileCount--,this.isModernBrowser||this.iFrameCleanUp(a),this.options.removeOnFail&&this.removeFromList(a),this.uploadNext()},onUploadAbort:function(a,b){this.trigger("uploadAbort",a,b),this.fileCount--,this.isModernBrowser||this.iFrameCleanUp(a),this.removeFromList(a),this.uploadNext()},on:function(a,b){return this.eventHandlers[a]=this.eventHandlers[a]||[],this.eventHandlers[a].push(b),this},off:function(a){a?delete this.eventHandlers[a]:this.eventHandlers={}},trigger:function(){var a,b=arguments[0];this.eventHandlers[b]=this.eventHandlers[b]||[];for(var c=0;c<this.eventHandlers[b].length;c++)try{this.eventHandlers[b][c].apply(this,Array.prototype.slice.call(arguments,1))}catch(d){if("UploadError"!=d.name)throw d;a=d}if(a)throw a}})}(window.Uploader,jQuery),function(a,b,c,d){"use strict";b.extend(a.prototype,{addToList:function(a){b.each(a,b.proxy(function(a,c){var d=this.isModernBrowser?c.name:this.getFileName(c.val());if(!b.isNumeric(this.options.maxFiles)||this.fileCount<this.options.maxFiles)try{this.validateFile(c);var e=this.uniqueId("file");this.$fileInput.attr("id",e),this.fileList[e]={name:d,file:c,status:this.constructor.STATUS_ADDED},this.fileCount++,this.onFileAdd(e,d,c),this.options.autoUpload&&this.upload(e)}catch(f){if("FileValidationError"!=f.name)throw f;this.onFileInvalid(d,f)}else this.onTooManyFiles(d,this.errorMessage(this.options.errors.tooManyFiles,c))},this))},validateFile:function(a){var c=[],d=this.isModernBrowser?a.name:this.getFileName(a.val()),e=this.isModernBrowser?a.size:null,f=this.getFileExtension(d),g=this.options.acceptType,h=b.isNumeric(this.options.acceptSize[0])?1024*this.options.acceptSize[0]:null,i=b.isNumeric(this.options.acceptSize[1])?1024*this.options.acceptSize[1]:null;if(g&&b.inArray("."+f,g)<0&&c.push({code:1,message:this.errorMessage(this.options.errors.invalidType,a)}),b.isNumeric(e)&&(b.isNumeric(h)&&h>e&&c.push({code:2,message:this.errorMessage(this.options.errors.sizeTooSmall,a)}),b.isNumeric(i)&&e>i&&c.push({code:3,message:this.errorMessage(this.options.errors.sizeTooLarge,a)})),c.length)throw{name:"FileValidationError",message:"Invalid file.",errors:c};return!0},upload:function(a){if(!this.isUploading(a)){var b=this.countFiles(this.constructor.STATUS_UPLOADING);b<this.options.simultaneousUploads?(this.isPending(a)&&this.removeFromPendingList(a),this[this.isModernBrowser?"ajaxUpload":"iFrameUpload"](a),this.fileList[a].status=this.constructor.STATUS_UPLOADING,this.onUploadStart(a,this.fileList[a].name)):this.isPending(a)||(this.addToPendingList(a),this.fileList[a].status=this.constructor.STATUS_PENDING)}},uploadAll:function(){for(var a in this.fileList)this.fileList[a].status==this.constructor.STATUS_ADDED&&this.upload(a)},abort:function(a){this.isUploading(a)&&this[this.isModernBrowser?"ajaxAbort":"iFrameAbort"](a)},abortAll:function(){for(var a in this.fileList)this.fileList[a].status==this.constructor.STATUS_UPLOADING&&this.abort(a)},inList:function(a){return"undefined"!=typeof this.fileList[a]},removeFromList:function(a){var b=this.fileList[a].name;this.inList(a)&&delete this.fileList[a],this.onFileRemove(a,b)},countFiles:function(a){var b=0;for(var c in this.fileList)a&&this.fileList[c].status!=a||b++;return b},isAdded:function(a){return this.fileList[a].status==this.constructor.STATUS_ADDED},isPending:function(a){return this.fileList[a].status==this.constructor.STATUS_PENDING},isUploading:function(a){if(a)return this.fileList[a].status==this.constructor.STATUS_UPLOADING;for(var b in this.fileList)if(this.fileList[b].status==this.constructor.STATUS_UPLOADING)return!0;return!1},isCompleted:function(a){return this.fileList[a].status==this.constructor.STATUS_COMPLETED},isFailed:function(a){return this.fileList[a].status==this.constructor.STATUS_FAILED},addToPendingList:function(a){this.pendingList.push(a)},removeFromPendingList:function(a){var c=b.inArray(a,this.pendingList);c>-1&&this.pendingList.splice(c,1)},uploadNext:function(){this.pendingList.length>0&&this.upload(this.pendingList[0])},getBytes:function(a){if(!this.isModernBrowser)return null;if(a)return this.fileList[a].file.size;var b=0;for(var c in this.fileList)b+=this.fileList[c].file.size;return b},getUploadedBytes:function(a){if(!this.isModernBrowser)return null;if(a)return this.isUploading(a)?this.fileList[a].progress.current.bytes:0;var b=0;for(var c in this.fileList)b+=this.getUploadedBytes(c);return b},getUploadSpeed:function(a,b){if(!this.isModernBrowser)return null;if(b===d&&(b=!0),a){var c=this.fileList[a].progress;return c.current.bytes&&c.current.bytes!=this.getBytes(a)?b?Math.round(c.current.bytes/((c.current.time-c.startTime)/1e3)):Math.round((c.current.bytes-c.previous.bytes)/((c.current.time-c.previous.time)/1e3)):0}var e=0;for(var f in this.fileList)e+=this.getUploadSpeed(f,b);return e},getTimeLeft:function(a,b){if(!this.isModernBrowser)return null;if(a)return Math.max(Math.ceil((this.getBytes(a)-this.getUploadedBytes(a))/this.getUploadSpeed(a,b)),1);var c=0;for(var d in this.fileList)c=Math.max(c,this.getTimeLeft(d,b));return c},destroy:function(){delete this.eventHandlers,this.$dropZone&&this.$dropZone.off(".Uploader");for(var a in this.fileList)this.fileList[a].status==this.constructor.STATUS_UPLOADING&&(this.isModernBrowser&&this.removeXHREventHandlers(a),this.abort(a));delete this.fileList,delete this.pendingList,this.$fileInput.remove()},ajaxUpload:function(a){var c,d=new XMLHttpRequest;this.attachXHREventHandlers(d,a),this.fileList[a].request=d;var e=b.extend({},this.options.data);this.onBeforeUpload(a,this.fileList[a].name,e,d);var f=new FormData;for(c in e)f.append(c,e[c]);f.append(this.options.name,this.fileList[a].file),d.open(this.options.method,this.options.url,!0),d.setRequestHeader("X-Requested-With","XMLHttpRequest");for(c in this.options.headers)d.setRequestHeader(c,this.options.headers[c]);d.send(f)},ajaxAbort:function(a){this.isUploading(a)&&this.fileList[a].request.abort()},attachXHREventHandlers:function(a,c){var d=this.fileList[c].name,e={load:b.proxy(function(a){this.onUploadComplete(c,d,a.target.responseText,a.target.status,a.target)},this),error:b.proxy(function(){var a=this.errorMessage(this.options.errors.networkError,this.fileList[c].file);this.onUploadFail(c,d,a)},this),abort:b.proxy(function(){this.onUploadAbort(c,d)},this),progress:b.proxy(function(a){this.onUploadProgress(c,d,a.loaded,a.total)},this)};a.addEventListener("load",e.load,!1),a.addEventListener("error",e.error,!1),a.addEventListener("abort",e.abort,!1),a.upload.addEventListener("progress",e.progress,!1),a.eventHandlers=e},removeXHREventHandlers:function(a){if(this.isUploading(a)){var b=this.fileList[a].request,c=b.eventHandlers;b.removeEventListener("load",c.load),b.removeEventListener("error",c.error),b.removeEventListener("abort",c.abort),b.upload.removeEventListener("progress",c.progress),delete b.eventHandlers}},iFrameUpload:function(a){var c=this.createForm(a),d=this.createIFrame(a);c.attr("target",d.attr("id"));var e=b.extend({},this.options.data);this.onBeforeUpload(a,this.fileList[a].name,e,null);for(var f in e)b("<input/>",{type:"hidden",name:f,value:e[f]}).appendTo(c);this.fileList[a].file.appendTo(c),c.submit(),this.fileList[a].request=d},iFrameAbort:function(a){this.isUploading(a)&&(c.navigator.userAgent.indexOf("MSIE")>-1?this.fileList[a].request[0].contentWindow.document.execCommand("Stop"):this.fileList[a].request[0].contentWindow.stop(),b("#"+a+"_iframe").off(),this.onUploadAbort(a,this.fileList[a].name))},createForm:function(a){return b("<form/>",{enctype:"multipart/form-data",method:this.options.method,action:this.options.url,id:a+"_form"}).css({display:"none"}).appendTo(document.body)},createIFrame:function(a){var c=b("<iframe src='javascript:false;' name='"+a+"_iframe' id='"+a+"_iframe'/>").css("display","none").appendTo(document.body);return c.off().on("load",b.proxy(function(){this.readIframe(a,c)},this)),c},readIframe:function(a,b){try{var c=b.contents().find("body").text();this.isUploading(a)&&this.onUploadComplete(a,this.fileList[a].name,c,null,null)}catch(d){this.onUploadFail(a,this.fileList[a].name,"Something went wrong.")}},iFrameCleanUp:function(a){this.fileList[a].file.detach(),setTimeout(function(){b("#"+a+"_form").remove(),b("#"+a+"_iframe").attr("src","javascript:false;").remove()},500)}})}(window.Uploader,jQuery,window),function(a,b){"use strict";b.extend(a.prototype,{uniqueId:function(a){return this.options.uniqueIdPrefix+a+"_"+this.constructor.uniqueIncrement++},errorMessage:function(a,c){var d=this.isModernBrowser?c.name:this.getFileName(c.val()),e=this.isModernBrowser?c.size:null,f=b.isNumeric(this.options.acceptSize[0])?1024*this.options.acceptSize[0]:null,g=b.isNumeric(this.options.acceptSize[1])?1024*this.options.acceptSize[1]:null,h={fileName:d,fileSize:this.formatBytes(e),fileExtension:this.getFileExtension(d),allowedExtensions:(this.options.acceptType||[]).join(),allowedMinSize:this.formatBytes(f),allowedMaxSize:this.formatBytes(g),maxFiles:this.options.maxFiles};return b.each(h,function(b,c){a=a.replace(new RegExp("{{"+b+"}}","gi"),c)}),a},getFileName:function(a){return a.replace(/\\/g,"/").split("/").pop()},getFileExtension:function(a){var b=a.split(".");return b.length>1?b.pop():""},formatBytes:function(a){for(var b=["KB","MB","GB","TB"],c=b.length;c>0;c--)if(a>=Math.pow(1024,c))return(a/Math.pow(1024,c)).toFixed(1)+" "+b[c-1];return a+" Bytes"}})}(window.Uploader,jQuery);