/
vue-upload.min.js
7 lines (6 loc) · 9.48 KB
/
vue-upload.min.js
1
2
3
4
5
6
7
/*!
* @websanova/vue-upload v1.4.0-beta
* https://websanova.com/docs/vue-upload
* Released under the MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.VueUpload=t()}(this,function(){"use strict";var o=null,n={url:null,name:"file",accept:null,body:{},dropzoneId:null,onSelect:null,onStart:null,onQueue:null,onProgress:null,onUpload:null,onError:null,onSuccess:null,onComplete:null,onEnd:null,startOnSelect:!0,extensions:["jpeg","jpg","png","gif"],multiple:!1,maxFilesSelect:4,maxFilesInProgress:2,maxSizePerFile:2097152,maxFilesSelectMsg:"Max of {max} files can be selected at a time.",maxFileSizeMsg:"Max of {max} mb per file.",invalidExtensionMsg:"File must be one of {extensions}.",parseErrors:function(e){if(e.data.errors)return e.data.errors[0]||{};if(e.data.msg)return{code:e.data.code,msg:e.data.msg};return{}},http:function(e){var t={};return o.Vue.http.post(e.url,e.body,{progress:e.progress,before:function(e){t=e}}).then(e.success,e.error),t}};function i(e){e.preventDefault(),e.stopPropagation()}function s(){return Math.random().toString(32).substring(2)}function r(e){o.instances[e]||(o.Vue.set(o.$vm.instances,e,{files:{all:[],queue:[],progress:[],upload:[],success:[],error:[]},errors:[],meta:{state:"ready",percentComplete:0},dropzone:{active:!1}}),o.instances[e]={key:e,$vm:o.$vm.instances[e]})}function l(e,t,n){var i=o.instances[e];i.options=Object.assign({},o.options,n),i.input=function(){var e,t=this;return(e=document.createElement("input")).type="file",e.multiple=!0===this.options.multiple,e.accept=this.options.accept?this.options.accept:"*/*",e.style.display="none",e.onchange=function(){h.call(t,e.files),e.value=null},document.body.appendChild(e),{$el:e}}.call(i),i.dropzone=c.call(i),a.call(i,t)}function a(e){this.onSelect=this.options.onSelect?this.options.onSelect.bind(e):function(){},this.onStart=this.options.onStart?this.options.onStart.bind(e):function(){},this.onQueue=this.options.onQueue?this.options.onQueue.bind(e):function(){},this.onProgress=this.options.onProgress?this.options.onProgress.bind(e):function(){},this.onUpload=this.options.onUpload?this.options.onUpload.bind(e):function(){},this.onError=this.options.onError?this.options.onError.bind(e):function(){},this.onSuccess=this.options.onSuccess?this.options.onSuccess.bind(e):function(){},this.onComplete=this.options.onComplete?this.options.onComplete.bind(e):function(){},this.onEnd=this.options.onEnd?this.options.onEnd.bind(e):function(){}}function c(){var t=this,e={$el:document.getElementById(this.options.dropzoneId),counter:0,dragenter:null,drageover:null,dragleave:null,drop:null};return e.$el&&(e.dragenter=function(e){i(e),t.dropzone.counter++,t.$vm.dropzone.active=!0},e.dragover=function(e){i(e),e.dataTransfer.dropEffect="copy",t.$vm.dropzone.active=!0},e.dragleave=function(e){i(e),t.dropzone.counter--,t.dropzone.counter<=0&&(t.dropzone.counter=0,t.$vm.dropzone.active=!1)},e.drop=function(e){i(e),t.dropzone.counter=0,t.$vm.dropzone.active=!1,h.call(t,e.dataTransfer.files)},e.$el.addEventListener("dragenter",e.dragenter,!1),e.$el.addEventListener("dragover",e.dragover,!1),e.$el.addEventListener("dragleave",e.dragleave,!1),e.$el.addEventListener("drop",e.drop,!1)),e}function p(){this.dropzone.$el&&(this.dropzone.$el.removeEventListener("dragenter",this.dropzone.dragenter,!1),this.dropzone.$el.removeEventListener("dragover",this.dropzone.dragover,!1),this.dropzone.$el.removeEventListener("dragleave",this.dropzone.dragleave,!1),this.dropzone.$el.removeEventListener("drop",this.dropzone.drop,!1),this.dropzone.dragenter=null,this.dropzone.dragover=null,this.dropzone.dragleave=null,this.dropzone.drop=null,this.dropzone.$el=null)}function u(e){(e={file:(e=e||{}).file||null,code:e.code||null,msg:e.msg||null}).$id=s(),e.file=e.file||null,e.clear=function(e){var t,n;if(!e)return;for(t=0,n=this.$vm.errors.length;t<n;t++)if(this.$vm.errors[t].$id===e.$id){this.$vm.errors.splice(t,1);break}}.bind(this,e),e.file&&(e.file.errors.push(e),e.file.error=e),this.$vm.errors.push(e)}function d(e){var t,n,i;if(e){for(e.$request&&e.$request.abort(),t=0,n=this.$vm.errors.length;t<n;t++)if(this.$vm.errors[t].file&&this.$vm.errors[t].file.$id===e.$id){this.$vm.errors.splice(t,1);break}i=m.call(this,e),this.$vm.files[e.state].splice(i,1),i=m.call(this,e,"all"),this.$vm.files.all.splice(i,1)}}function h(e){var t,n,i=this;if(e.length>this.options.maxFilesSelect)this.onSelect(e,{code:"file-max-select",msg:this.options.maxFilesSelectMsg.replace("{max}",this.options.maxFilesSelect)});else{for(t=0,n=e.length;t<n;t++)f.call(i,e[t]);this.onSelect(e),this.options.startOnSelect&&g.call(this)}}function f(o){var e=this,t=(o.name||"").split("."),n=(o.type||"").split("/"),i=1<t.length?t[t.length-1]:null;i=((i=i||(n[1]||null))||"").toLowerCase(),n=n[0]||null,o={$id:s(),$file:o,$request:null,$raw:null,$instance:this,name:o.name,size:o.size,type:n,extension:i,state:"queue",active:!0,sending:!1,errors:[],error:{},percentComplete:0,preview:function(e){var t,n,i;t=o,n=e,(i=new FileReader).addEventListener("load",function(){t.$raw=i.result,n&&n(t)},!1),t.$file&&i.readAsDataURL(t.$file)},clear:function(){d.call(e,o),"uploading"===e.$vm.meta.state&&g.call(e)}},this.$vm.files.all.push(o),this.$vm.files.queue.push(o),$.call(this,o),this.onQueue(o)}function m(e,t){for(var n=this.$vm.files[t||e.state],i=0,o=n.length;i<o;i++)if(e.$id===n[i].$id)return i;return-1}function v(e,t){var n=m.call(this,e);this.$vm.files[e.state].splice(n,1),this.$vm.files[t].push(e),e.state=t}function $(e){var t;return this.options.extensions&&this.options.extensions.indexOf(e.extension)<0?t={file:e,code:"file-extension",msg:this.options.invalidExtensionMsg.replace("{extensions}",this.options.extensions.join(", "))}:0<this.options.maxSizePerFile&&e.size>this.options.maxSizePerFile&&(t={file:e,code:"file-max-size",msg:this.options.maxFileSizeMsg.replace("{max}",Math.floor(this.options.maxSizePerFile/1024/1024))}),!t||(u.call(this,t),!1)}function g(){var e,t,n;if(!(this.$vm.files.progress.length>=this.options.maxFilesInProgress))if(this.$vm.files.queue.length){if(!this.$vm.files.queue.length||"ready"!==this.$vm.meta.state&&"complete"!==this.$vm.meta.state||this.onStart(),n=this.$vm.files.queue[0],!$.call(this,n))return v.call(this,n,"error"),this.onError(n),void g.call(this);this.$vm.meta.state="uploading",function(n){var e,t,i,o=this;for(e in n.sending=!0,v.call(this,n,"progress"),(t=new FormData).append(this.options.name,n.$file),n.$instance.options.body)t.append(e,n.$instance.options.body[e]);i=this.options.http({url:n.$instance.options.url,body:t,progress:function(e){n.percentComplete=e.lengthComputable?Math.ceil(e.loaded/e.total*100):0,o.onProgress(n,e),100<=n.percentComplete&&(v.call(o,n,"upload"),o.onUpload(n,e)),function(){var e,t,n,i;for(e=i=0,t=this.$vm.files.all.length;e<t;e++)this.$vm.files.all[e].active&&i++;for(n=100*(i-this.$vm.files.queue.length-this.$vm.files.progress.length),e=0,t=this.$vm.files.progress.length;e<t;e++)n+=this.$vm.files.progress[e].percentComplete;this.$vm.meta.percentComplete=Math.ceil(n/(100*i)*100)}.call(o)},success:function(e){n.sending=!1,v.call(o,n,"success"),o.onSuccess(n,e),o.onComplete(n,e),g.call(o)},error:function(e){var t;n.sending=!1,(t=o.options.parseErrors(e)).file=n,u.call(o,t),v.call(o,n,"error"),o.onError(n,e),o.onComplete(n,e),g.call(o)}}),n.$request=i}.call(this,n),g.call(this)}else if(!this.$vm.files.upload.length&&!this.$vm.files.progress.length&&"uploading"===this.$vm.meta.state){for(this.$vm.meta.state="complete",e=0,t=this.$vm.files.all.length;e<t;e++)this.$vm.files.all[e].active=!1;this.onEnd()}}function z(e,t){this.options=Object.assign({},n,t),this.Vue=e,this.$vm=new e({data:function(){return{instances:{}}}}),this.instances={},o=this}function e(e,t){var n=new z(e,t),i=n.on,o=n.bind,s=n.option;e.upload=n,Object.defineProperties(e.prototype,{$upload:{get:function(){return n.on=i.bind(this),n.bind=o.bind(this),n.option=s.bind(this),n}}})}return z.prototype.on=function(e,t){r(e),l(e,this,t)},z.prototype.off=function(e){var t,n;t=e,n=o.instances[t],function(){this.input&&this.input.$el&&(this.input.$el.parentNode.removeChild(this.input.$el),this.input.$el=null)}.call(n),p.call(n),delete n.$vm,delete o.instances[t]},z.prototype.bind=function(e){a.call(o.instances[e],this)},z.prototype.reset=function(e){r(e),function(){for(var e=this,t=0,n=this.$vm.files.all.length;t<n;t++)d.call(e,e.$vm.files.all[t]);this.$vm.files={all:[],queue:[],progress:[],upload:[],success:[],error:[]},this.$vm.errors=[],this.$vm.meta.state="ready",this.$vm.meta.percentComplete=0,this.$vm.dropzone.active=!1}.call(o.instances[e])},z.prototype.select=function(e){o.instances[e].input.$el.click()},z.prototype.start=function(e){r(e),g.call(o.instances[e])},z.prototype.files=function(e){return r(e),o.instances[e].$vm.files},z.prototype.file=function(e){var t;return r(e),(t=o.instances[e].$vm).files.all[t.files.all.length-1]||{error:{}}},z.prototype.exists=function(e){return!!o.instances[e]},z.prototype.meta=function(e){return r(e),o.instances[e].$vm.meta},z.prototype.percent=function(e){return r(e),o.instances[e].$vm.meta.percentComplete},z.prototype.state=function(e){return r(e),o.instances[e].$vm.meta.state},z.prototype.dropzone=function(e){return r(e),o.instances[e].$vm.dropzone},z.prototype.option=function(e,t,n){r(e),function(e,t){if(this.options[e]=t,"dropzone"===e)return p.call(this),void(this.dropzone=c.call(this))}.call(o.instances[e],t,n),a.call(o.instances[e],this)},z.prototype.errors=function(e){return r(e),o.instances[e].$vm.errors},"undefined"!=typeof window&&window.Vue&&window.Vue.use(e),e});