ngx-upload-steroids
Version:
Angular 2+ File Uploader
2 lines • 10.7 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@angular/core"),require("rxjs/Observable"),require("rxjs/Subscriber"),require("rxjs/add/observable/from"),require("rxjs/add/observable/merge"),require("rxjs/add/operator/combineLatest"),require("rxjs/add/operator/mergeAll")):"function"==typeof define&&define.amd?define("ngx-uploader",["@angular/core","rxjs/Observable","rxjs/Subscriber","rxjs/add/observable/from","rxjs/add/observable/merge","rxjs/add/operator/combineLatest","rxjs/add/operator/mergeAll"],t):"object"==typeof exports?exports["ngx-uploader"]=t(require("@angular/core"),require("rxjs/Observable"),require("rxjs/Subscriber"),require("rxjs/add/observable/from"),require("rxjs/add/observable/merge"),require("rxjs/add/operator/combineLatest"),require("rxjs/add/operator/mergeAll")):e["ngx-uploader"]=t(e["@angular/core"],e["rxjs/Observable"],e["rxjs/Subscriber"],e["rxjs/add/observable/from"],e["rxjs/add/observable/merge"],e["rxjs/add/operator/combineLatest"],e["rxjs/add/operator/mergeAll"])}(this,function(e,t,n,r,i,o,s){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="/",t(t.s=5)}([function(t,n){t.exports=e},function(e,t,n){"use strict";function r(e){if(0===e)return"0 Byte";var t=["Bytes","KB","MB","GB","TB","PB"],n=Math.floor(Math.log(e)/Math.log(1024));return parseFloat((e/Math.pow(1024,n)).toFixed(2))+" "+t[n]}n.d(t,"a",function(){return i}),t.b=r,n.d(t,"c",function(){return c});var i,o=n(0),s=(n.n(o),n(6)),a=(n.n(s),n(7)),u=(n.n(a),n(9)),l=(n.n(u),n(8)),p=(n.n(l),n(11)),d=(n.n(p),n(10));n.n(d);!function(e){e[e.Queue=0]="Queue",e[e.Uploading=1]="Uploading",e[e.Done=2]="Done",e[e.Canceled=3]="Canceled"}(i||(i={}));var c=function(){function e(){this.files=[],this.serviceEvents=new o.EventEmitter,this.uploads=[]}return e.prototype.handleFiles=function(e){var t=this;this.fileList=e,this.files=[].map.call(e,function(e,n){var r={fileIndex:n,id:t.generateId(),name:e.name,size:e.size,type:e.type,progress:{status:i.Queue,data:{percentage:0,speed:null,speedHuman:null,startTime:null,endTime:null}},lastModifiedDate:e.lastModifiedDate};return t.serviceEvents.emit({type:"addedToQueue",file:r}),r}),this.files.length>1&&(this.files=[this.files[this.files.length-1]]),this.serviceEvents.emit({type:"allAddedToQueue",file:this.files[0]})},e.prototype.initInputEvents=function(e){var t=this;e.subscribe(function(e){switch(e.type){case"uploadFile":t.serviceEvents.emit({type:"start",file:e.file});var n={instance:null};t.uploads.push({file:e.file,sub:n}),n.instance=t.uploadFile(e.file,e).subscribe(function(e){t.serviceEvents.emit(e)});break;case"uploadAll":var r=e.concurrency>0?e.concurrency:Number.POSITIVE_INFINITY,o=a.Subscriber.create(function(e){t.serviceEvents.emit(e)});t.uploads=t.uploads.concat(t.files.map(function(e){return{file:e,sub:null}}));s.Observable.from(t.files.map(function(n){return t.uploadFile(n,e)})).mergeAll(r).combineLatest(function(e){return e}).subscribe(o);break;case"cancel":var u=e.id||null;if(!u)return;var l=t.uploads.findIndex(function(e){return e.file.id===u});-1!==l&&(t.uploads[l].sub&&t.uploads[l].sub.instance&&t.uploads[l].sub.instance.unsubscribe(),t.serviceEvents.emit({type:"cancelled",file:t.uploads[l].file}),t.uploads[l].file.progress.status=i.Canceled);break;case"cancelAll":t.uploads.forEach(function(e){e.file.progress.status=i.Canceled,t.serviceEvents.emit({type:"cancelled",file:e.file})});break;case"clearAll":t.files=[],t.fileList=null,t.serviceEvents.emit({type:"clearedAll"})}})},e.prototype.uploadFile=function(e,t){var n=this;return new s.Observable(function(o){var s=t.url,a=t.method||"POST",u=t.data||{},l=t.headers||{},p=new FileReader,d=new XMLHttpRequest,c=(new Date).getTime(),f=0;d.upload.addEventListener("progress",function(t){if(t.lengthComputable){var n=Math.round(100*t.loaded/t.total),s=(new Date).getTime()-c;f=Math.round(t.loaded/s*1e3),e.progress={status:i.Uploading,data:{percentage:n,speed:f,speedHuman:r(f)+"/s",startTime:e.progress.data.startTime||(new Date).getTime(),endTime:null}},o.next({type:"uploading",file:e})}},!1),d.upload.addEventListener("error",function(e){o.error(e),o.complete()}),d.onreadystatechange=function(){if(d.readyState===XMLHttpRequest.DONE){var t=Math.round(e.size/((new Date).getTime()-e.progress.data.startTime)*1e3);e.progress={status:i.Done,data:{percentage:100,speed:t,speedHuman:r(t)+"/s",startTime:e.progress.data.startTime,endTime:(new Date).getTime()}};try{e.response=JSON.parse(d.response)}catch(t){e.response=d.response}o.next({type:"done",file:e}),o.complete()}},d.open(a,s,!0),d.withCredentials=!!t.withCredentials;var v=new FormData;try{var h=n.fileList.item(e.fileIndex),m=n.uploads.findIndex(function(e){return e.file.size===h.size});n.uploads[m].file.progress.status===i.Canceled&&o.complete(),v.append(t.fieldName||"file",h,h.name),Object.keys(u).forEach(function(e){return v.append(e,u[e])}),Object.keys(l).forEach(function(e){return d.setRequestHeader(e,l[e])}),n.serviceEvents.emit({type:"start",file:e}),d.send(v)}catch(e){o.complete()}return function(){d.abort(),p.abort()}})},e.prototype.generateId=function(){return Math.random().toString(36).substring(7)},e}()},function(e,t,n){"use strict";n.d(t,"a",function(){return a});var r=n(0),i=(n.n(r),n(1)),o=this&&this.__decorate||function(e,t,n,r){var i,o=arguments.length,s=o<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,r);else for(var a=e.length-1;a>=0;a--)(i=e[a])&&(s=(o<3?i(s):o>3?i(t,n,s):i(t,n))||s);return o>3&&s&&Object.defineProperty(t,n,s),s},s=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},a=function(){function e(e){this.elementRef=e,this.stopEvent=function(e){e.stopPropagation(),e.preventDefault()},this.upload=new i.c,this.uploadOutput=new r.EventEmitter}return e.prototype.ngOnInit=function(){var e=this;this.el=this.elementRef.nativeElement,this.upload.serviceEvents.subscribe(function(t){e.uploadOutput.emit(t)}),this.uploadInput instanceof r.EventEmitter&&this.upload.initInputEvents(this.uploadInput),this.el.addEventListener("drop",this.stopEvent,!1),this.el.addEventListener("dragenter",this.stopEvent,!1),this.el.addEventListener("dragover",this.stopEvent,!1),this.el.addEventListener("dragover",this.stopEvent,!1)},e.prototype.ngOnDestroy=function(){this.uploadInput&&this.uploadInput.unsubscribe()},e.prototype.onDrop=function(e){e.stopPropagation(),e.preventDefault();var t={type:"drop"};this.uploadOutput.emit(t),this.upload.handleFiles(e.dataTransfer.files)},e.prototype.onDragOver=function(e){if(e){var t={type:"dragOver"};this.uploadOutput.emit(t)}},e.prototype.onDragLeave=function(e){if(e){var t={type:"dragOut"};this.uploadOutput.emit(t)}},e}();o([n.i(r.Input)(),s("design:type",r.EventEmitter)],a.prototype,"uploadInput",void 0),o([n.i(r.Output)(),s("design:type",r.EventEmitter)],a.prototype,"uploadOutput",void 0),o([n.i(r.HostListener)("drop",["$event"]),s("design:type",Function),s("design:paramtypes",[Object]),s("design:returntype",void 0)],a.prototype,"onDrop",null),o([n.i(r.HostListener)("dragover",["$event"]),s("design:type",Function),s("design:paramtypes",[Event]),s("design:returntype",void 0)],a.prototype,"onDragOver",null),o([n.i(r.HostListener)("dragleave",["$event"]),s("design:type",Function),s("design:paramtypes",[Event]),s("design:returntype",void 0)],a.prototype,"onDragLeave",null),a=o([n.i(r.Directive)({selector:"[ngFileDrop]"}),s("design:paramtypes",[r.ElementRef])],a)},function(e,t,n){"use strict";n.d(t,"a",function(){return a});var r=n(0),i=(n.n(r),n(1)),o=this&&this.__decorate||function(e,t,n,r){var i,o=arguments.length,s=o<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,r);else for(var a=e.length-1;a>=0;a--)(i=e[a])&&(s=(o<3?i(s):o>3?i(t,n,s):i(t,n))||s);return o>3&&s&&Object.defineProperty(t,n,s),s},s=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},a=function(){function e(e){var t=this;this.elementRef=e,this.fileListener=function(){t.upload.handleFiles(t.el.files)},this.upload=new i.c,this.uploadOutput=new r.EventEmitter}return e.prototype.ngOnInit=function(){var e=this;this.el=this.elementRef.nativeElement,this.el.addEventListener("change",this.fileListener,!1),this.upload.serviceEvents.subscribe(function(t){e.uploadOutput.emit(t)}),this.uploadInput instanceof r.EventEmitter&&this.upload.initInputEvents(this.uploadInput)},e.prototype.ngOnDestroy=function(){this.el.removeEventListener("change",this.fileListener,!1),this.uploadInput&&this.uploadInput.unsubscribe()},e}();o([n.i(r.Input)(),s("design:type",r.EventEmitter)],a.prototype,"uploadInput",void 0),o([n.i(r.Output)(),s("design:type",r.EventEmitter)],a.prototype,"uploadOutput",void 0),a=o([n.i(r.Directive)({selector:"[ngFileSelect]"}),s("design:paramtypes",[r.ElementRef])],a)},function(e,t,n){"use strict";n.d(t,"a",function(){return a});var r=n(0),i=(n.n(r),n(2)),o=n(3),s=this&&this.__decorate||function(e,t,n,r){var i,o=arguments.length,s=o<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,r);else for(var a=e.length-1;a>=0;a--)(i=e[a])&&(s=(o<3?i(s):o>3?i(t,n,s):i(t,n))||s);return o>3&&s&&Object.defineProperty(t,n,s),s},a=function(){function e(){}return e}();a=s([n.i(r.NgModule)({declarations:[o.a,i.a],exports:[o.a,i.a]})],a)},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(4);n.d(t,"NgUploaderModule",function(){return r.a});var i=n(1);n.d(t,"UploadStatus",function(){return i.a}),n.d(t,"humanizeBytes",function(){return i.b}),n.d(t,"NgUploaderService",function(){return i.c});var o=n(3);n.d(t,"NgFileSelectDirective",function(){return o.a});var s=n(2);n.d(t,"NgFileDropDirective",function(){return s.a})},function(e,n){e.exports=t},function(e,t){e.exports=n},function(e,t){e.exports=r},function(e,t){e.exports=i},function(e,t){e.exports=o},function(e,t){e.exports=s}])});
//# sourceMappingURL=ngx-uploader.umd.js.map