ngx-upload-steroids
Version:
Angular 2+ File Uploader
78 lines (62 loc) • 2.25 kB
text/typescript
import {Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy, HostListener} from '@angular/core';
import {isPlatformServer} from '@angular/common';
import {NgUploaderService, UploadOutput, UploadInput, UploadFile} from '../classes/ngx-uploader.class';
export class NgFileDropDirective implements OnInit, OnDestroy {
uploadInput: EventEmitter<UploadInput>;
uploadOutput: EventEmitter<UploadOutput>;
upload: NgUploaderService;
el: HTMLInputElement;
constructor(private elementRef: ElementRef) {
this.upload = new NgUploaderService();
this.uploadOutput = new EventEmitter<UploadOutput>();
}
ngOnInit() {
this.el = this.elementRef.nativeElement;
this.upload.serviceEvents.subscribe((event: UploadOutput) => {
this.uploadOutput.emit(event);
});
if (this.uploadInput instanceof EventEmitter) {
this.upload.initInputEvents(this.uploadInput);
}
this.el.addEventListener('drop', this.stopEvent, false);
this.el.addEventListener('dragenter', this.stopEvent, false);
this.el.addEventListener('dragover', this.stopEvent, false);
this.el.addEventListener('dragover', this.stopEvent, false);
}
ngOnDestroy() {
if (this.uploadInput) {
this.uploadInput.unsubscribe();
}
}
stopEvent = (e: Event) => {
e.stopPropagation();
e.preventDefault();
}
public onDrop(e: any) {
e.stopPropagation();
e.preventDefault();
const event: UploadOutput = {type: 'drop'};
this.uploadOutput.emit(event);
this.upload.handleFiles(e.dataTransfer.files);
}
public onDragOver(e: Event) {
if (!e) {
return;
}
const event: UploadOutput = {type: 'dragOver'};
this.uploadOutput.emit(event);
}
public onDragLeave(e: Event) {
if (!e) {
return;
}
const event: UploadOutput = {type: 'dragOut'};
this.uploadOutput.emit(event);
}
}