From 9cb76653052fae9729870927aec3bc521d5299e3 Mon Sep 17 00:00:00 2001 From: kukhariev Date: Thu, 23 May 2019 12:10:06 +0300 Subject: [PATCH] feat: directive instance options resolve #41 --- src/uploadx/src/uploadx.directive.ts | 3 +-- src/uploadx/src/uploadx.service.ts | 16 +++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/uploadx/src/uploadx.directive.ts b/src/uploadx/src/uploadx.directive.ts index 8feb752c..4af29cc0 100644 --- a/src/uploadx/src/uploadx.directive.ts +++ b/src/uploadx/src/uploadx.directive.ts @@ -41,7 +41,6 @@ export class UploadxDirective implements OnInit, OnDestroy { this.uploadx.allowedTypes ); } - this.uploadService.init(this.uploadx); } this.uploadxState.emit(this.uploadService.events); this.listenerFn = this.renderer.listen( @@ -59,7 +58,7 @@ export class UploadxDirective implements OnInit, OnDestroy { fileListener = () => { if (this.elementRef.nativeElement.files) { - this.uploadService.handleFileList(this.elementRef.nativeElement.files); + this.uploadService.handleFileList(this.elementRef.nativeElement.files, this.uploadx); } }; } diff --git a/src/uploadx/src/uploadx.service.ts b/src/uploadx/src/uploadx.service.ts index 1c116f77..f6963b72 100644 --- a/src/uploadx/src/uploadx.service.ts +++ b/src/uploadx/src/uploadx.service.ts @@ -45,7 +45,6 @@ export class UploadxService { */ init(options: UploadxOptions): Observable { this.options = { ...this.options, ...options }; - this.uploaderClass = this.options.uploaderClass; return this.events; } /** @@ -71,21 +70,24 @@ export class UploadxService { /** * Create Uploader and add to the queue */ - handleFileList(fileList: FileList): void { - Array.from(fileList).forEach(file => this.addUploaderInstance(file)); + handleFileList(fileList: FileList, options = {}): void { + const instanceOptions = { ...this.options, ...options }; + Array.from(fileList).forEach(file => this.addUploaderInstance(file, instanceOptions)); this.autoUploadFiles(); } /** * Create Uploader for the file and add to the queue */ - handleFile(file: File): void { - this.addUploaderInstance(file); + handleFile(file: File, options = {}): void { + const instanceOptions = { ...this.options, ...options }; + this.addUploaderInstance(file, instanceOptions); this.autoUploadFiles(); } - private addUploaderInstance(file: File) { - const uploader = new this.uploaderClass(file, this.options as UploaderOptions); + private addUploaderInstance(file: File, options) { + this.uploaderClass = options.uploaderClass; + const uploader = new this.uploaderClass(file, options); this.queue.push(uploader); uploader.status = 'added'; }