2026-04-16 22:19:46 +03:30
|
|
|
import { Maybe } from '@/core';
|
2026-04-28 20:06:21 +03:30
|
|
|
import {
|
|
|
|
|
Component,
|
|
|
|
|
EventEmitter,
|
|
|
|
|
Input,
|
|
|
|
|
OnChanges,
|
|
|
|
|
OnDestroy,
|
|
|
|
|
Output,
|
|
|
|
|
SimpleChanges,
|
|
|
|
|
ViewChild,
|
|
|
|
|
} from '@angular/core';
|
|
|
|
|
import { ButtonDirective } from 'primeng/button';
|
2026-04-16 22:19:46 +03:30
|
|
|
import { FileSelectEvent, FileUpload } from 'primeng/fileupload';
|
2026-04-18 12:14:39 +03:30
|
|
|
import { onSelectFileArgs } from './model';
|
2026-04-16 22:19:46 +03:30
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-shared-upload-file',
|
|
|
|
|
templateUrl: './upload-file.component.html',
|
2026-04-28 20:06:21 +03:30
|
|
|
imports: [FileUpload, ButtonDirective],
|
2026-04-16 22:19:46 +03:30
|
|
|
})
|
2026-04-28 20:06:21 +03:30
|
|
|
export class SharedUploadFileComponent implements OnChanges, OnDestroy {
|
2026-04-16 22:19:46 +03:30
|
|
|
@Input() loading: boolean = false;
|
|
|
|
|
@Input() name: string = 'file';
|
|
|
|
|
@Input() accept: string = 'image/*';
|
2026-04-18 12:14:39 +03:30
|
|
|
@Input() maxSize: Maybe<number> = 5 * 1024 * 1024;
|
2026-04-16 22:19:46 +03:30
|
|
|
@Input() error: Maybe<string> = null;
|
|
|
|
|
@Input() hint: Maybe<string> = null;
|
2026-04-28 20:06:21 +03:30
|
|
|
@Input() initial_file_url: Maybe<string> = null;
|
2026-04-16 22:19:46 +03:30
|
|
|
|
2026-04-18 12:14:39 +03:30
|
|
|
@Output() onSelect = new EventEmitter<onSelectFileArgs>();
|
2026-04-16 22:19:46 +03:30
|
|
|
|
2026-04-28 20:06:21 +03:30
|
|
|
@ViewChild(FileUpload) uploader?: FileUpload;
|
|
|
|
|
|
|
|
|
|
filePreview: Maybe<string> = null;
|
|
|
|
|
private objectUrl: Maybe<string> = null;
|
|
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
|
if ('initial_file_url' in changes && !this.objectUrl) {
|
|
|
|
|
this.filePreview = this.initial_file_url;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
|
this.revokeObjectUrl();
|
|
|
|
|
|
|
|
|
|
if (this.initial_file_url && !this.objectUrl) {
|
|
|
|
|
this.filePreview = this.initial_file_url;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this.revokeObjectUrl();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 22:19:46 +03:30
|
|
|
onSelectedFile($event: FileSelectEvent) {
|
|
|
|
|
const file = $event.files?.[0];
|
|
|
|
|
if (!file) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-28 20:06:21 +03:30
|
|
|
this.setPreviewFromFile(file);
|
2026-04-16 22:19:46 +03:30
|
|
|
const payload = new FormData();
|
|
|
|
|
payload.append(this.name, file, file.name);
|
|
|
|
|
this.onSelect.emit({ data: payload, file });
|
|
|
|
|
}
|
2026-04-28 20:06:21 +03:30
|
|
|
|
|
|
|
|
onEditFile() {
|
|
|
|
|
this.uploader?.choose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onDeleteFilePreview() {
|
|
|
|
|
this.revokeObjectUrl();
|
|
|
|
|
this.filePreview = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setPreviewFromFile(file: File) {
|
|
|
|
|
this.revokeObjectUrl();
|
|
|
|
|
this.objectUrl = URL.createObjectURL(file);
|
|
|
|
|
this.filePreview = this.objectUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private revokeObjectUrl() {
|
|
|
|
|
if (!this.objectUrl) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.filePreview = null;
|
|
|
|
|
URL.revokeObjectURL(this.objectUrl);
|
|
|
|
|
this.objectUrl = null;
|
|
|
|
|
}
|
2026-04-16 22:19:46 +03:30
|
|
|
}
|