-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfile-uploader.component.ts
120 lines (102 loc) · 3.91 KB
/
file-uploader.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
Component,
CUSTOM_ELEMENTS_SCHEMA,
ElementRef,
EventEmitter,
Input,
Output,
ViewChild
} from '@angular/core';
import * as UC from '@uploadcare/file-uploader';
import { OutputFileEntry } from '@uploadcare/file-uploader';
UC.defineComponents(UC);
@Component({
selector: 'file-uploader',
standalone: true,
imports: [],
templateUrl: './file-uploader.component.html',
styleUrl: './file-uploader.component.scss',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class FileUploaderComponent {
@Input({ required: true }) theme!: 'light' | 'dark';
@Input() uploaderClassName: string | undefined;
@Input() uploaderCtxName: string = 'my-uploader';
@Input() files: OutputFileEntry<'success'>[] = [];
@Output() filesChange = new EventEmitter<OutputFileEntry<'success'>[]>();
uploadedFiles: OutputFileEntry<'success'>[] = [];
@ViewChild('ctxProvider', { static: true }) ctxProviderRef!: ElementRef<
InstanceType<UC.UploadCtxProvider>
>;
@ViewChild('config', { static: true }) configRef!: ElementRef<
InstanceType<UC.Config>
>;
ngOnInit() {
/*
Note: Event binding is the main way to get data and other info from File Uploader.
There plenty of events you may use.
See more: https://uploadcare.com/docs/file-uploader/events/
*/
this.ctxProviderRef.nativeElement.addEventListener(
'change',
this.handleChangeEvent
);
this.ctxProviderRef.nativeElement.addEventListener(
'modal-close',
this.handleModalCloseEvent
);
/*
Note: Localization of File Uploader is done via DOM property on the config node.
You can change any piece of text of File Uploader this way.
See more: https://uploadcare.com/docs/file-uploader/localization/
*/
this.configRef.nativeElement.localeDefinitionOverride = {
en: {
'photo__one': 'photo',
'photo__many': 'photos',
'photo__other': 'photos',
'upload-file': 'Upload photo',
'upload-files': 'Upload photos',
'choose-file': 'Choose photo',
'choose-files': 'Choose photos',
'drop-files-here': 'Drop photos here',
'select-file-source': 'Select photo source',
'edit-image': 'Edit photo',
'no-files': 'No photos selected',
'caption-edit-file': 'Edit photo',
'files-count-allowed': 'Only {{count}} {{plural:photo(count)}} allowed',
'files-max-size-limit-error': 'Photo is too big. Max photo size is {{maxFileSize}}.',
'header-uploading': 'Uploading {{count}} {{plural:photo(count)}}',
'header-succeed': '{{count}} {{plural:photo(count)}} uploaded',
'header-total': '{{count}} {{plural:photo(count)}} selected',
}
}
}
ngOnDestroy() {
this.ctxProviderRef.nativeElement.removeEventListener('change', this.handleChangeEvent);
this.ctxProviderRef.nativeElement.removeEventListener('modal-close', this.handleModalCloseEvent);
this.configRef.nativeElement.localeDefinitionOverride = null;
}
/*
Note: Here we use provider's API to reset File Uploader state.
It's not necessary though. We use it here to show users
a fresh version of File Uploader every time they open it.
Another way is to sync File Uploader state with an external store.
You can manipulate File Uploader using API calls like `addFileFromObject`, etc.
See more: https://uploadcare.com/docs/file-uploader/api/
*/
resetUploaderState() {
this.ctxProviderRef.nativeElement.getAPI().removeAllFiles();
}
handleRemoveClick(uuid: OutputFileEntry['uuid']) {
this.filesChange.emit(this.files.filter((f) => f.uuid !== uuid));
}
handleChangeEvent = (e: UC.EventMap['change']) => {
this.uploadedFiles = e.detail.allEntries.filter(f => f.status === 'success') as OutputFileEntry<'success'>[];
};
handleModalCloseEvent = () => {
this.resetUploaderState();
this.filesChange.emit([...this.files, ...this.uploadedFiles]);
this.uploadedFiles = [];
};
}