forked from nervgh/angular-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-file-upload.min.js
7 lines (6 loc) · 15.2 KB
/
angular-file-upload.min.js
1
2
3
4
5
6
7
/*
angular-file-upload v1.1.5
https://github.com/nervgh/angular-file-upload
*/
!function(a,b){return"function"==typeof define&&define.amd?void define("angular-file-upload",["angular"],function(a){return b(a)}):b(a)}("undefined"==typeof angular?null:angular,function(a){var b,c=a.module("angularFileUpload",[]);return c.value("fileUploaderOptions",{url:"/",alias:"file",headers:{},queue:[],progress:0,autoUpload:!1,removeAfterUpload:!1,method:"POST",filters:[],formData:[],queueLimit:Number.MAX_VALUE,withCredentials:!1}).config(["$httpProvider",function(a){b=a}]).factory("FileUploader",["fileUploaderOptions","$rootScope","$http","$window","$compile","$injector",function(c,d,e,f,g,h){function i(b){var d=a.copy(c);a.extend(this,d,b,{isUploading:!1,_nextIndex:0,_failFilterIndex:-1,_directives:{select:[],drop:[],over:[]}}),this.filters.unshift({name:"queueLimit",fn:this._queueLimitFilter}),this.filters.unshift({name:"folder",fn:this._folderFilter})}function j(c,d){a.forEach(b.interceptors,function(a){var b=_.isString(a)?h.get(a):h.invoke(a);"Success"==c?b.response&&b.response(d):b.responseError&&b.responseError(d)})}function k(b){var c=a.isElement(b),d=c?b.value:b,e=a.isString(d)?"FakePath":"Object",f="_createFrom"+e;this[f](d)}function l(b,c,d){var e=a.isElement(c),f=e?a.element(c):null,g=e?null:c;a.extend(this,{url:b.url,alias:b.alias,headers:a.copy(b.headers),formData:a.copy(b.formData),removeAfterUpload:b.removeAfterUpload,withCredentials:b.withCredentials,method:b.method},d,{uploader:b,file:new i.FileLikeObject(c),isReady:!1,isUploading:!1,isUploaded:!1,isSuccess:!1,isCancel:!1,isError:!1,progress:0,index:null,_file:g,_input:f}),f&&this._replaceNode(f)}function m(b){a.extend(this,b),this.uploader._directives[this.prop].push(this),this._saveLinks(),this.bind()}function n(a){n.super_.apply(this,arguments),this.uploader.isHTML5||this.element.removeAttr("multiple"),this.element.prop("value",null)}function o(a){o.super_.apply(this,arguments)}function p(a){p.super_.apply(this,arguments)}return i.prototype.isHTML5=!(!f.File||!f.FormData),i.prototype.addToQueue=function(b,c,d){var e=this.isArrayLikeObject(b)?b:[b],f=this._getFilters(d),g=this.queue.length,h=[];a.forEach(e,function(a){var b=new i.FileLikeObject(a);if(this._isValidFile(b,f,c)){var d=new i.FileItem(this,a,c);h.push(d),this.queue.push(d),this._onAfterAddingFile(d)}else{var e=this.filters[this._failFilterIndex];this._onWhenAddingFileFailed(b,e,c)}},this),this.queue.length!==g&&(this._onAfterAddingAll(h),this.progress=this._getTotalProgress()),this._render(),this.autoUpload&&this.uploadAll()},i.prototype.removeFromQueue=function(a){var b=this.getIndexOfItem(a),c=this.queue[b];c.isUploading&&c.cancel(),this.queue.splice(b,1),c._destroy(),this.progress=this._getTotalProgress()},i.prototype.clearQueue=function(){for(;this.queue.length;)this.queue[0].remove();this.progress=0},i.prototype.uploadItem=function(a,b){var c=this.getIndexOfItem(a),d=this.queue[c],e=this.isHTML5?"_xhrTransport":"_iframeTransport";d._prepareToUploading(),this.isUploading||(this.isUploading=!0,this[e](d,b))},i.prototype.cancelItem=function(a){var b=this.getIndexOfItem(a),c=this.queue[b],d=this.isHTML5?"_xhr":"_form";c&&c.isUploading&&c[d].abort()},i.prototype.uploadAll=function(b){var c=this.getNotUploadedItems().filter(function(a){return!a.isUploading});c.length&&(a.forEach(c,function(a){a._prepareToUploading()}),c[0].upload(b))},i.prototype.cancelAll=function(){var b=this.getNotUploadedItems();a.forEach(b,function(a){a.cancel()})},i.prototype.isFile=function(a){var b=f.File;return b&&a instanceof b},i.prototype.isFileLikeObject=function(a){return a instanceof i.FileLikeObject},i.prototype.isArrayLikeObject=function(b){return a.isObject(b)&&"length"in b},i.prototype.getIndexOfItem=function(b){return a.isNumber(b)?b:this.queue.indexOf(b)},i.prototype.getNotUploadedItems=function(){return this.queue.filter(function(a){return!a.isUploaded})},i.prototype.getReadyItems=function(){return this.queue.filter(function(a){return a.isReady&&!a.isUploading}).sort(function(a,b){return a.index-b.index})},i.prototype.destroy=function(){a.forEach(this._directives,function(b){a.forEach(this._directives[b],function(a){a.destroy()},this)},this)},i.prototype.onAfterAddingAll=function(a){},i.prototype.onAfterAddingFile=function(a){},i.prototype.onWhenAddingFileFailed=function(a,b,c){},i.prototype.onBeforeUploadItem=function(a){},i.prototype.onProgressItem=function(a,b){},i.prototype.onProgressAll=function(a){},i.prototype.onSuccessItem=function(a,b,c,d){},i.prototype.onErrorItem=function(a,b,c,d){},i.prototype.onCancelItem=function(a,b,c,d){},i.prototype.onCompleteItem=function(a,b,c,d){},i.prototype.onCompleteAll=function(){},i.prototype._getTotalProgress=function(a){if(this.removeAfterUpload)return a||0;var b=this.getNotUploadedItems().length,c=b?this.queue.length-b:this.queue.length,d=100/this.queue.length,e=(a||0)*d/100;return Math.round(c*d+e)},i.prototype._getFilters=function(b){if(a.isUndefined(b))return this.filters;if(a.isArray(b))return b;var c=b.match(/[^\s,]+/g);return this.filters.filter(function(a){return-1!==c.indexOf(a.name)},this)},i.prototype._render=function(){d.$$phase||d.$apply()},i.prototype._folderFilter=function(a){return!(!a.size&&!a.type)},i.prototype._queueLimitFilter=function(){return this.queue.length<this.queueLimit},i.prototype._isValidFile=function(a,b,c){return this._failFilterIndex=-1,b.length?b.every(function(b){return this._failFilterIndex++,b.fn.call(this,a,c)},this):!0},i.prototype._isSuccessCode=function(a){return a>=200&&300>a||304===a},i.prototype._transformResponse=function(b,c){var d=this._headersGetter(c);return a.forEach(e.defaults.transformResponse,function(a){b=a(b,d)}),b},i.prototype._parseHeaders=function(b){var c,d,e,f={};return b?(a.forEach(b.split("\n"),function(a){e=a.indexOf(":"),c=a.slice(0,e).trim().toLowerCase(),d=a.slice(e+1).trim(),c&&(f[c]=f[c]?f[c]+", "+d:d)}),f):f},i.prototype._headersGetter=function(a){return function(b){return b?a[b.toLowerCase()]||null:a}},i.prototype._xhrTransport=function(c,d){var e=c._xhr=new XMLHttpRequest,f=new FormData,g=this;g._onBeforeUploadItem(c),f.append("file",c._file,c.file.name),e.upload.onprogress=function(a){var b=Math.round(a.lengthComputable?100*a.loaded/a.total:0);g._onProgressItem(c,b)},e.onload=function(){var a=g._parseHeaders(e.getAllResponseHeaders()),b=g._transformResponse(e.response,a),d=g._isSuccessCode(e.status)?"Success":"Error",h="_on"+d+"Item";g[h](c,b,e.status,a),g._onCompleteItem(c,b,e.status,a);var i={data:b,config:{headers:a,params:f,url:c.url,method:c.method},status:e.status};j(d,i)},e.onerror=function(){var a=g._parseHeaders(e.getAllResponseHeaders()),b=g._transformResponse(e.response,a);g._onErrorItem(c,b,e.status,a),g._onCompleteItem(c,b,e.status,a)},e.onabort=function(){var a=g._parseHeaders(e.getAllResponseHeaders()),b=g._transformResponse(e.response,a);g._onCancelItem(c,b,e.status,a),g._onCompleteItem(c,b,e.status,a)},e.open(c.method,c.url,!0),e.withCredentials=c.withCredentials;var i={headers:{},params:f,url:c.url,method:c.method};a.extend(i,d),a.forEach(c.headers,function(a,b){i.headers[b]=a}),a.forEach(b.interceptors,function(a){var b=_.isString(a)?h.get(a):h.invoke(a);b.request&&b.request(i)}),a.forEach(i.headers,function(a,b){e.setRequestHeader(b,a)}),e.send(f),this._render()},i.prototype._iframeTransport=function(b,c){var d=a.element('<form style="display: none;" />'),e=a.element('<iframe name="iframeTransport'+Date.now()+'">'),f=b._input,g=this;b._form&&b._form.replaceWith(f),b._form=d,g._onBeforeUploadItem(b),f.prop("name",b.alias),a.forEach(b.formData,function(b){a.forEach(b,function(b,c){var e=a.element('<input type="hidden" name="'+c+'" />');e.val(b),d.append(e)})}),d.prop({action:b.url,method:"POST",target:e.prop("name"),enctype:"multipart/form-data",encoding:"multipart/form-data"}),e.bind("load",function(){try{var a=e[0].contentDocument.body.innerHTML}catch(c){}var f={response:a,status:200,dummy:!0},h={},i=g._transformResponse(f.response,h),k=g._isSuccessCode(f.status)?"Success":"Error";g._onSuccessItem(b,i,f.status,h),g._onCompleteItem(b,i,f.status,h);var l={data:i,config:{headers:h,params:d,url:b.url,method:b.method},status:f.status};j(k,l)}),d.abort=function(){var a,c={status:0,dummy:!0},h={};e.unbind("load").prop("src","javascript:false;"),d.replaceWith(f),g._onCancelItem(b,a,c.status,h),g._onCompleteItem(b,a,c.status,h)},f.after(d),d.append(f).append(e),d[0].submit(),this._render()},i.prototype._onWhenAddingFileFailed=function(a,b,c){this.onWhenAddingFileFailed(a,b,c)},i.prototype._onAfterAddingFile=function(a){this.onAfterAddingFile(a)},i.prototype._onAfterAddingAll=function(a){this.onAfterAddingAll(a)},i.prototype._onBeforeUploadItem=function(a){a._onBeforeUpload(),this.onBeforeUploadItem(a)},i.prototype._onProgressItem=function(a,b){var c=this._getTotalProgress(b);this.progress=c,a._onProgress(b),this.onProgressItem(a,b),this.onProgressAll(c),this._render()},i.prototype._onSuccessItem=function(a,b,c,d){a._onSuccess(b,c,d),this.onSuccessItem(a,b,c,d)},i.prototype._onErrorItem=function(a,b,c,d){a._onError(b,c,d),this.onErrorItem(a,b,c,d)},i.prototype._onCancelItem=function(a,b,c,d){a._onCancel(b,c,d),this.onCancelItem(a,b,c,d)},i.prototype._onCompleteItem=function(b,c,d,e){b._onComplete(c,d,e),this.onCompleteItem(b,c,d,e);var f=this.getReadyItems()[0];return this.isUploading=!1,a.isDefined(f)?void f.upload():(this.onCompleteAll(b,c,d),this.progress=this._getTotalProgress(),void this._render())},i.isFile=i.prototype.isFile,i.isFileLikeObject=i.prototype.isFileLikeObject,i.isArrayLikeObject=i.prototype.isArrayLikeObject,i.isHTML5=i.prototype.isHTML5,i.inherit=function(a,b){a.prototype=Object.create(b.prototype),a.prototype.constructor=a,a.super_=b},i.FileLikeObject=k,i.FileItem=l,i.FileDirective=m,i.FileSelect=n,i.FileDrop=o,i.FileOver=p,k.prototype._createFromFakePath=function(a){this.lastModifiedDate=null,this.size=null,this.type="like/"+a.slice(a.lastIndexOf(".")+1).toLowerCase(),this.name=a.slice(a.lastIndexOf("/")+a.lastIndexOf("\\")+2)},k.prototype._createFromObject=function(b){this.lastModifiedDate=a.copy(b.lastModifiedDate),this.size=b.size,this.type=b.type,this.name=b.name},l.prototype.upload=function(a){this.uploader.uploadItem(this,a)},l.prototype.cancel=function(){this.uploader.cancelItem(this)},l.prototype.remove=function(){this.uploader.removeFromQueue(this)},l.prototype.onBeforeUpload=function(){},l.prototype.onProgress=function(a){},l.prototype.onSuccess=function(a,b,c){},l.prototype.onError=function(a,b,c){},l.prototype.onCancel=function(a,b,c){},l.prototype.onComplete=function(a,b,c){},l.prototype._onBeforeUpload=function(){this.isReady=!0,this.isUploading=!0,this.isUploaded=!1,this.isSuccess=!1,this.isCancel=!1,this.isError=!1,this.progress=0,this.onBeforeUpload()},l.prototype._onProgress=function(a){this.progress=a,this.onProgress(a)},l.prototype._onSuccess=function(a,b,c){this.isReady=!1,this.isUploading=!1,this.isUploaded=!0,this.isSuccess=!0,this.isCancel=!1,this.isError=!1,this.progress=100,this.index=null,this.onSuccess(a,b,c)},l.prototype._onError=function(a,b,c){this.isReady=!1,this.isUploading=!1,this.isUploaded=!0,this.isSuccess=!1,this.isCancel=!1,this.isError=!0,this.progress=0,this.index=null,this.onError(a,b,c)},l.prototype._onCancel=function(a,b,c){this.isReady=!1,this.isUploading=!1,this.isUploaded=!1,this.isSuccess=!1,this.isCancel=!0,this.isError=!1,this.progress=0,this.index=null,this.onCancel(a,b,c)},l.prototype._onComplete=function(a,b,c){this.onComplete(a,b,c),this.removeAfterUpload&&this.remove()},l.prototype._destroy=function(){this._input&&this._input.remove(),this._form&&this._form.remove(),delete this._form,delete this._input},l.prototype._prepareToUploading=function(){this.index=this.index||++this.uploader._nextIndex,this.isReady=!0},l.prototype._replaceNode=function(a){var b=g(a.clone())(a.scope());b.prop("value",null),a.css("display","none"),a.after(b)},m.prototype.events={},m.prototype.bind=function(){for(var a in this.events){var b=this.events[a];this.element.bind(a,this[b])}},m.prototype.unbind=function(){for(var a in this.events)this.element.unbind(a,this.events[a])},m.prototype.destroy=function(){var a=this.uploader._directives[this.prop].indexOf(this);this.uploader._directives[this.prop].splice(a,1),this.unbind()},m.prototype._saveLinks=function(){for(var a in this.events){var b=this.events[a];this[b]=this[b].bind(this)}},i.inherit(n,m),n.prototype.events={$destroy:"destroy",change:"onChange"},n.prototype.prop="select",n.prototype.getOptions=function(){},n.prototype.getFilters=function(){},n.prototype.isEmptyAfterSelection=function(){return!!this.element.attr("multiple")},n.prototype.onChange=function(){var a=this.uploader.isHTML5?this.element[0].files:this.element[0],b=this.getOptions(),c=this.getFilters();this.uploader.isHTML5||this.destroy(),this.uploader.addToQueue(a,b,c),this.isEmptyAfterSelection()&&this.element.prop("value",null)},i.inherit(o,m),o.prototype.events={$destroy:"destroy",drop:"onDrop",dragover:"onDragOver",dragleave:"onDragLeave"},o.prototype.prop="drop",o.prototype.getOptions=function(){},o.prototype.getFilters=function(){},o.prototype.onDrop=function(b){var c=this._getTransfer(b);if(c){var d=this.getOptions(),e=this.getFilters();this._preventAndStop(b),a.forEach(this.uploader._directives.over,this._removeOverClass,this),this.uploader.addToQueue(c.files,d,e)}},o.prototype.onDragOver=function(b){var c=this._getTransfer(b);this._haveFiles(c.types)&&(c.dropEffect="copy",this._preventAndStop(b),a.forEach(this.uploader._directives.over,this._addOverClass,this))},o.prototype.onDragLeave=function(b){b.currentTarget===this.element[0]&&(this._preventAndStop(b),a.forEach(this.uploader._directives.over,this._removeOverClass,this))},o.prototype._getTransfer=function(a){return a.dataTransfer?a.dataTransfer:a.originalEvent.dataTransfer},o.prototype._preventAndStop=function(a){a.preventDefault(),a.stopPropagation()},o.prototype._haveFiles=function(a){return a?a.indexOf?-1!==a.indexOf("Files"):a.contains?a.contains("Files"):!1:!1},o.prototype._addOverClass=function(a){a.addOverClass()},o.prototype._removeOverClass=function(a){a.removeOverClass()},i.inherit(p,m),p.prototype.events={$destroy:"destroy"},p.prototype.prop="over",p.prototype.overClass="nv-file-over",p.prototype.addOverClass=function(){this.element.addClass(this.getOverClass())},p.prototype.removeOverClass=function(){this.element.removeClass(this.getOverClass())},p.prototype.getOverClass=function(){return this.overClass},i}]).directive("nvFileSelect",["$parse","FileUploader",function(a,b){return{link:function(c,d,e){var f=c.$eval(e.uploader);if(!(f instanceof b))throw new TypeError('"Uploader" must be an instance of FileUploader');var g=new b.FileSelect({uploader:f,element:d});g.getOptions=a(e.options).bind(g,c),g.getFilters=function(){return e.filters}}}}]).directive("nvFileDrop",["$parse","FileUploader",function(a,b){return{link:function(c,d,e){var f=c.$eval(e.uploader);if(!(f instanceof b))throw new TypeError('"Uploader" must be an instance of FileUploader');if(f.isHTML5){var g=new b.FileDrop({uploader:f,element:d});g.getOptions=a(e.options).bind(g,c),g.getFilters=function(){return e.filters}}}}}]).directive("nvFileOver",["FileUploader",function(a){return{link:function(b,c,d){var e=b.$eval(d.uploader);if(!(e instanceof a))throw new TypeError('"Uploader" must be an instance of FileUploader');var f=new a.FileOver({uploader:e,element:c});f.getOverClass=function(){return d.overClass||this.overClass}}}}]),c});
//# sourceMappingURL=angular-file-upload.min.map