forked from nervgh/angular-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-file-upload.min.js
2 lines (2 loc) · 7.74 KB
/
angular-file-upload.min.js
1
2
!function(a,b){return"function"==typeof define&&define.amd?(define("angular-file-upload",["angular"],function(a){return b(a)}),void 0):b(a)}(angular||null,function(a){var b=a.module("angularFileUpload",[]);return b.directive("ngFileDrop",["$fileUploader",function(b){"use strict";return{link:b.hasHTML5?function(a,b,c){b.bind("drop",function(b){var d=b.dataTransfer?b.dataTransfer:b.originalEvent.dataTransfer;d&&(b.preventDefault(),b.stopPropagation(),a.$broadcast("file:removeoverclass"),a.$emit("file:add",d.files,a.$eval(c.ngFileDrop)))}).bind("dragover",function(b){var c=b.dataTransfer?b.dataTransfer:b.originalEvent.dataTransfer;b.preventDefault(),b.stopPropagation(),c.dropEffect="copy",a.$broadcast("file:addoverclass")}).bind("dragleave",function(){a.$broadcast("file:removeoverclass")})}:a.noop}}]),b.directive("ngFileOver",function(){"use strict";return{link:function(a,b,c){a.$on("file:addoverclass",function(){b.addClass(c.ngFileOver||"ng-file-over")}),a.$on("file:removeoverclass",function(){b.removeClass(c.ngFileOver||"ng-file-over")})}}}),b.directive("ngFileSelect",["$fileUploader",function(a){"use strict";return{link:function(b,c,d){a.hasHTML5||c.removeAttr("multiple"),c.bind("change",function(){b.$emit("file:add",this.files?this.files:this,b.$eval(d.ngFileSelect)),a.hasHTML5&&c.prop("value",null)})}}}]),b.factory("$fileUploader",["$compile","$rootScope","$http","$window",function(b,c,d,e){"use strict";function f(b){a.extend(this,{scope:c,url:"/",alias:"file",queue:[],headers:{},progress:null,autoUpload:!1,removeAfterUpload:!1,method:"POST",filters:[],formData:[],isUploading:!1,_nextIndex:0,_timestamp:Date.now()},b),this.filters.unshift(this._filter),this.scope.$on("file:add",function(a,b,c){a.stopPropagation(),this.addToQueue(b,c)}.bind(this)),this.bind("beforeupload",g.prototype._beforeupload),this.bind("in:progress",g.prototype._progress),this.bind("in:success",g.prototype._success),this.bind("in:error",g.prototype._error),this.bind("in:complete",g.prototype._complete),this.bind("changedqueue",this._changedQueue),this.bind("in:progress",this._progress),this.bind("in:complete",this._complete)}function g(c){if(a.isElement(c.file)){var d=a.element(c.file),e=b(d.clone())(c.uploader.scope),f=a.element('<form style="display: none;" />'),g=a.element('<iframe name="iframeTransport'+Date.now()+'">'),h=d.val();c.file={lastModifiedDate:null,size:null,type:"like/"+h.replace(/^.+\.(?!\.)|.*/,""),name:h.match(/[^\\]+$/)[0],_form:f},d.after(e).after(f),f.append(d).append(g)}a.extend(this,{isReady:!1,isUploading:!1,isUploaded:!1,isSuccess:!1,isError:!1,progress:null,index:null},c)}return f.prototype={_filter:function(b){return a.isElement(b)?!0:!!b.size},bind:function(a,b){return this.scope.$on(this._timestamp+":"+a,b.bind(this)),this},trigger:function(a){return arguments[0]=this._timestamp+":"+a,this.scope.$broadcast.apply(this.scope,arguments),this},hasHTML5:!(!e.File||!e.FormData),addToQueue:function(b,c){var d=this.queue.length;return a.forEach("length"in b?b:[b],function(b){var d=this.filters.length?this.filters.every(function(a){return a.call(this,b)},this):!0;d&&(b=new g(a.extend({url:this.url,alias:this.alias,headers:a.copy(this.headers),formData:a.copy(this.formData),removeAfterUpload:this.removeAfterUpload,method:this.method,uploader:this,file:b},c)),this.queue.push(b),this.trigger("afteraddingfile",b))},this),this.queue.length!==d&&(this.trigger("afteraddingall",this.queue),this.trigger("changedqueue",this.queue)),this.autoUpload&&this.uploadAll(),this},removeFromQueue:function(b){var c=a.isObject(b)?this.getIndexOfItem(b):b,d=this.queue.splice(c,1)[0];return d._destroyForm(),this.trigger("changedqueue",d),this},clearQueue:function(){return this.queue.forEach(function(a){a._destroyForm()},this),this.queue.length=0,this.trigger("changedqueue",this.queue),this},getIndexOfItem:function(a){return this.queue.indexOf(a)},getNotUploadedItems:function(){return this.queue.filter(function(a){return!a.isUploaded})},getReadyItems:function(){return this.queue.filter(function(a){return a.isReady&&!a.isUploading}).sort(function(a,b){return a.index-b.index})},uploadItem:function(b){var c=a.isObject(b)?this.getIndexOfItem(b):b,d=this.queue[c],e=d._hasForm()?"_iframeTransport":"_xhrTransport";return d.index=d.index||this._nextIndex++,d.isReady=!0,this.isUploading?this:(this.isUploading=!0,this[e](d),this)},uploadAll:function(){var a=this.getNotUploadedItems().filter(function(a){return!a.isUploading});return a.forEach(function(a){a.index=a.index||this._nextIndex++,a.isReady=!0},this),a.length&&this.uploadItem(a[0]),this},_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)},_progress:function(a,b,c){var d=this._getTotalProgress(c);this.progress=d,this.trigger("progressall",d),this.scope.$$phase||this.scope.$apply()},_complete:function(){var b=this.getReadyItems()[0];return this.isUploading=!1,a.isDefined(b)?(this.uploadItem(b),void 0):(this.progress=this._getTotalProgress(),this.trigger("completeall",this.queue),this.scope.$$phase||this.scope.$apply(),void 0)},_changedQueue:function(){this.progress=this._getTotalProgress(),this.scope.$$phase||this.scope.$apply()},_xhrTransport:function(b){var c=new XMLHttpRequest,d=new FormData,e=this;this.trigger("beforeupload",b),b.formData.forEach(function(b){a.forEach(b,function(a,b){d.append(b,a)})}),d.append(b.alias,b.file),c.upload.onprogress=function(a){var c=a.lengthComputable?100*a.loaded/a.total:0;e.trigger("in:progress",b,Math.round(c))},c.onload=function(){var a=e._transformResponse(c.response),d=e._isSuccessCode(c.status)?"success":"error";e.trigger("in:"+d,c,b,a),e.trigger("in:complete",c,b,a)},c.onerror=function(){e.trigger("in:error",c,b),e.trigger("in:complete",c,b)},c.onabort=function(){e.trigger("in:complete",c,b)},c.open(b.method,b.url,!0),a.forEach(b.headers,function(a,b){c.setRequestHeader(b,a)}),c.send(d)},_iframeTransport:function(b){var c=b.file._form,d=c.find("iframe"),e=c.find("input"),f=this;this.trigger("beforeupload",b),a.forEach(e,function(b){"file"!==b.type&&a.element(b).remove()}),e.prop("name",b.alias),b.formData.forEach(function(b){a.forEach(b,function(b,d){c.append(a.element('<input type="hidden" name="'+d+'" value="'+b+'" />'))})}),c.prop({action:b.url,method:b.method,target:d.prop("name"),enctype:"multipart/form-data",encoding:"multipart/form-data"}),d.unbind().bind("load",function(){var a={response:d.contents()[0].body.innerHTML,status:200,dummy:!0},c=f._transformResponse(a.response);f.trigger("in:complete",a,b,c)}),c[0].submit()},_isSuccessCode:function(a){return a>=200&&300>a||304===a},_transformResponse:function(a){return d.defaults.transformResponse.forEach(function(b){a=b(a)}),a}},g.prototype={remove:function(){this.uploader.removeFromQueue(this)},upload:function(){this.uploader.uploadItem(this)},_hasForm:function(){return!(!this.file||!this.file._form)},_destroyForm:function(){this._hasForm()&&this.file._form.remove()},_beforeupload:function(a,b){b.isReady=!0,b.isUploading=!0,b.isUploaded=!1,b.isSuccess=!1,b.isError=!1,b.progress=null},_progress:function(a,b,c){b.progress=c,b.uploader.trigger("progress",b,c)},_success:function(a,b,c,d){c.isReady=!1,c.isUploading=!1,c.isUploaded=!0,c.isSuccess=!0,c.isError=!1,c.progress=100,c.index=null,c.uploader.trigger("success",b,c,d)},_error:function(a,b,c,d){c.isReady=!1,c.isUploading=!1,c.isUploaded=!0,c.isSuccess=!1,c.isError=!0,c.index=null,c.uploader.trigger("error",b,c,d)},_complete:function(a,b,c,d){var e=c.uploader._isSuccessCode(b.status);c.isReady=!1,c.isUploading=!1,c.isUploaded=!0,c.isSuccess=e,c.isError=!e,c.index=null,c.uploader.trigger("complete",b,c,d),c.removeAfterUpload&&c.remove()}},{create:function(a){return new f(a)},hasHTML5:f.prototype.hasHTML5}}]),b});
//# sourceMappingURL=angular-file-upload.min.map