Skip to content

Commit

Permalink
add files validation for ngFileDrop bleenco#224 (bleenco#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-galka authored and jkuri committed Mar 15, 2017
1 parent 4b4b4b5 commit e05b4de
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/directives/ng-file-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,14 @@ export class NgFileDropDirective implements OnChanges, OnInit {
return;
}

this.el.nativeElement.addEventListener('drop', (e: any) => {
e.stopPropagation();
e.preventDefault();
this.onFileOver.emit(false);
this.files = Array.from<File>(e.dataTransfer.files);
if (this.files && this.files.length) {
this.uploader.addFilesToQueue(this.files);
}
}, false);

this.el.nativeElement.addEventListener('dragenter', (e: DragEvent) => {
e.stopPropagation();
e.preventDefault();
}, false);

this.el.nativeElement.addEventListener('dragover', (e: DragEvent) => {
e.stopPropagation();
e.preventDefault();
}, false);
this.el.nativeElement.addEventListener('drop', this.stopEvent, false);
this.el.nativeElement.addEventListener('dragenter', this.stopEvent, false);
this.el.nativeElement.addEventListener('dragover', this.stopEvent, false);
}

@HostListener('change') onChange(): void {
this.files = this.el.nativeElement.files;
@HostListener('drop', ['$event']) onDrop(e: DragEvent): void {
this.onFileOver.emit(false);
this.files = Array.from<File>(e.dataTransfer.files);
if (!this.files || !this.files.length) {
return;
}
Expand Down Expand Up @@ -136,6 +121,11 @@ export class NgFileDropDirective implements OnChanges, OnInit {
});
}

if(this.options.maxUploads > 0 && this.files.length > this.options.maxUploads) {
this.onUploadRejected.emit({file: this.files.pop(), reason: UploadRejected.MAX_UPLOADS_EXCEEDED});
this.files = [];
}

if (this.files && this.files.length) {
this.uploader.addFilesToQueue(this.files);
}
Expand All @@ -153,4 +143,9 @@ export class NgFileDropDirective implements OnChanges, OnInit {
this.onFileOver.emit(false);
}

private stopEvent(e: DragEvent): void {
e.stopPropagation();
e.preventDefault();
}

}

0 comments on commit e05b4de

Please sign in to comment.