Files
dc/client/src/app/directives/file-select.directive.ts
2023-08-03 17:56:34 +02:00

43 lines
905 B
TypeScript

import {
Directive,
EventEmitter,
ElementRef,
Input,
HostListener,
Output
} from '@angular/core'
import { FileUploader } from '../models/FileUploader.class'
@Directive({
selector: '[appFileSelect]'
})
export class FileSelectDirective {
@Input() uploader?: FileUploader
@Output() fileSelected: EventEmitter<File[]> = new EventEmitter<File[]>()
protected element: ElementRef
constructor(element: ElementRef) {
this.element = element
}
/**
* Checks if files exist in the input after input change
*/
isEmptyAfterSelection(): boolean {
return !!this.element.nativeElement.attributes.multiple
}
@HostListener('change')
onChange(): void {
const files = this.element.nativeElement.files
this.uploader?.addToQueue(files)
this.fileSelected.emit(files)
if (this.isEmptyAfterSelection()) {
this.element.nativeElement.value = ''
}
}
}