@dodlhuat/basix 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/css/accordion.scss +31 -22
- package/css/alert.scss +79 -27
- package/css/button.scss +151 -102
- package/css/card.scss +11 -12
- package/css/carousel.scss +123 -87
- package/css/chart.scss +9 -11
- package/css/chat-bubbles.scss +2 -2
- package/css/checkbox.scss +72 -55
- package/css/chips.scss +52 -52
- package/css/code-viewer.scss +73 -98
- package/css/datepicker.scss +20 -0
- package/css/dropdown.scss +151 -137
- package/css/editor.scss +9 -6
- package/css/file-uploader.scss +187 -195
- package/css/flyout-menu.scss +20 -13
- package/css/form.scss +168 -115
- package/css/gallery.scss +62 -63
- package/css/grid.scss +0 -1
- package/css/modal.scss +117 -72
- package/css/placeholder.scss +17 -12
- package/css/properties.scss +6 -0
- package/css/push-menu.scss +70 -23
- package/css/radiobutton.scss +86 -64
- package/css/range-slider.scss +136 -0
- package/css/scrollbar.scss +69 -69
- package/css/spinner.scss +41 -66
- package/css/style.css +4351 -3735
- package/css/style.css.map +1 -1
- package/css/style.scss +2 -1
- package/css/switch.scss +43 -42
- package/css/table.scss +61 -40
- package/css/tabs.scss +12 -7
- package/css/timeline.scss +72 -69
- package/css/timepicker.scss +151 -72
- package/css/toast.scss +49 -48
- package/css/tooltip.scss +112 -122
- package/css/tree.scss +135 -192
- package/css/typography.scss +70 -9
- package/css/virtual-dropdown.scss +201 -142
- package/js/carousel.js +45 -18
- package/js/carousel.ts +217 -173
- package/js/datepicker.js +505 -497
- package/js/datepicker.ts +9 -0
- package/js/editor.js +398 -415
- package/js/file-uploader.js +142 -128
- package/js/file-uploader.ts +364 -350
- package/js/gallery.js +22 -15
- package/js/gallery.ts +17 -12
- package/js/index.js +718 -720
- package/js/index.ts +7 -8
- package/js/push-menu.js +113 -101
- package/js/push-menu.ts +17 -2
- package/js/range-slider.js +26 -0
- package/js/range-slider.ts +33 -0
- package/js/timepicker.js +144 -98
- package/js/timepicker.ts +194 -131
- package/js/tree.js +56 -28
- package/js/tree.ts +239 -218
- package/package.json +3 -2
- package/css/accordion.css +0 -109
- package/css/accordion.css.map +0 -1
- package/css/alert.css +0 -57
- package/css/alert.css.map +0 -1
- package/css/button.css +0 -69
- package/css/button.css.map +0 -1
- package/css/card.css +0 -144
- package/css/card.css.map +0 -1
- package/css/carousel.css +0 -118
- package/css/carousel.css.map +0 -1
- package/css/chart.css +0 -159
- package/css/chart.css.map +0 -1
- package/css/chat-bubbles.css +0 -97
- package/css/chat-bubbles.css.map +0 -1
- package/css/checkbox.css +0 -77
- package/css/checkbox.css.map +0 -1
- package/css/chips.css +0 -72
- package/css/chips.css.map +0 -1
- package/css/code-viewer.css +0 -97
- package/css/code-viewer.css.map +0 -1
- package/css/colors.css +0 -63
- package/css/colors.css.map +0 -1
- package/css/datepicker.css +0 -264
- package/css/datepicker.css.map +0 -1
- package/css/defaults.css +0 -118
- package/css/defaults.css.map +0 -1
- package/css/dropdown.css +0 -146
- package/css/dropdown.css.map +0 -1
- package/css/editor.css +0 -413
- package/css/file-uploader.css +0 -194
- package/css/file-uploader.css.map +0 -1
- package/css/flyout-menu.css +0 -345
- package/css/flyout-menu.css.map +0 -1
- package/css/form-builder.css +0 -9
- package/css/form-builder.css.map +0 -1
- package/css/form-builder.scss +0 -11
- package/css/form.css +0 -130
- package/css/form.css.map +0 -1
- package/css/gallery.css +0 -91
- package/css/gallery.css.map +0 -1
- package/css/grid.css +0 -44
- package/css/grid.css.map +0 -1
- package/css/icons.css +0 -327
- package/css/icons.css.map +0 -1
- package/css/modal.css +0 -97
- package/css/modal.css.map +0 -1
- package/css/parameters.css +0 -1
- package/css/parameters.css.map +0 -1
- package/css/placeholder.css +0 -50
- package/css/placeholder.css.map +0 -1
- package/css/progress.css +0 -51
- package/css/progress.css.map +0 -1
- package/css/properties.css +0 -31
- package/css/properties.css.map +0 -1
- package/css/push-menu.css +0 -145
- package/css/push-menu.css.map +0 -1
- package/css/radiobutton.css +0 -91
- package/css/radiobutton.css.map +0 -1
- package/css/reset.css +0 -46
- package/css/reset.css.map +0 -1
- package/css/scrollbar.css +0 -91
- package/css/scrollbar.css.map +0 -1
- package/css/spinner.css +0 -118
- package/css/spinner.css.map +0 -1
- package/css/switch.css +0 -66
- package/css/switch.css.map +0 -1
- package/css/table.css +0 -201
- package/css/table.css.map +0 -1
- package/css/tabs.css +0 -135
- package/css/tabs.css.map +0 -1
- package/css/timeline.css +0 -69
- package/css/timeline.css.map +0 -1
- package/css/toast.css +0 -98
- package/css/toast.css.map +0 -1
- package/css/tooltip.css +0 -151
- package/css/tooltip.css.map +0 -1
- package/css/tree.css +0 -199
- package/css/tree.css.map +0 -1
- package/css/typography.css +0 -137
- package/css/typography.css.map +0 -1
- package/css/virtual-dropdown.css +0 -149
- package/css/virtual-dropdown.css.map +0 -1
package/js/file-uploader.ts
CHANGED
|
@@ -1,350 +1,364 @@
|
|
|
1
|
-
interface FileData {
|
|
2
|
-
file: File;
|
|
3
|
-
element: HTMLDivElement;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
interface UploadCompletedDetail {
|
|
7
|
-
fileCount: number;
|
|
8
|
-
files: File[];
|
|
9
|
-
results: PromiseSettledResult<unknown>[];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
private
|
|
25
|
-
private
|
|
26
|
-
private
|
|
27
|
-
private
|
|
28
|
-
private
|
|
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
|
-
this.
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
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
|
-
private
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const removeBtn =
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
this.
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
this.
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
1
|
+
interface FileData {
|
|
2
|
+
file: File;
|
|
3
|
+
element: HTMLDivElement;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface UploadCompletedDetail {
|
|
7
|
+
fileCount: number;
|
|
8
|
+
files: File[];
|
|
9
|
+
results: PromiseSettledResult<unknown>[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface FileValidationErrorDetail {
|
|
13
|
+
file: File;
|
|
14
|
+
reason: 'size' | 'type';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface FileUploaderConfig {
|
|
18
|
+
uploadUrl?: string;
|
|
19
|
+
maxFileSize?: number;
|
|
20
|
+
allowedTypes?: string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class FileUploader {
|
|
24
|
+
private container: HTMLElement;
|
|
25
|
+
private dropZone: HTMLElement;
|
|
26
|
+
private fileInput: HTMLInputElement;
|
|
27
|
+
private fileList: HTMLElement;
|
|
28
|
+
private uploadBtn: HTMLButtonElement;
|
|
29
|
+
private files: Map<string, FileData> = new Map();
|
|
30
|
+
private uploadUrl: string;
|
|
31
|
+
private maxFileSize?: number;
|
|
32
|
+
private allowedTypes?: string[];
|
|
33
|
+
private abortControllers: Map<string, () => void> = new Map();
|
|
34
|
+
|
|
35
|
+
constructor(elementOrSelector: string | HTMLElement, config: FileUploaderConfig = {}) {
|
|
36
|
+
const container = typeof elementOrSelector === 'string'
|
|
37
|
+
? document.querySelector<HTMLElement>(elementOrSelector)
|
|
38
|
+
: elementOrSelector;
|
|
39
|
+
|
|
40
|
+
if (!container) {
|
|
41
|
+
throw new Error(`FileUploader: Element not found for selector "${elementOrSelector}"`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.container = container;
|
|
45
|
+
|
|
46
|
+
const dropZone = container.querySelector<HTMLElement>('#drop-zone');
|
|
47
|
+
const fileInput = container.querySelector<HTMLInputElement>('#file-input');
|
|
48
|
+
const fileList = container.querySelector<HTMLElement>('#file-list');
|
|
49
|
+
const uploadBtn = container.querySelector<HTMLButtonElement>('#upload-btn');
|
|
50
|
+
|
|
51
|
+
if (!dropZone || !fileInput || !fileList || !uploadBtn) {
|
|
52
|
+
throw new Error('Required elements not found in container');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.dropZone = dropZone;
|
|
56
|
+
this.fileInput = fileInput;
|
|
57
|
+
this.fileList = fileList;
|
|
58
|
+
this.uploadBtn = uploadBtn;
|
|
59
|
+
|
|
60
|
+
this.uploadUrl = config.uploadUrl ?? 'https://httpbin.org/post';
|
|
61
|
+
this.maxFileSize = config.maxFileSize;
|
|
62
|
+
this.allowedTypes = config.allowedTypes;
|
|
63
|
+
|
|
64
|
+
this.init();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private init(): void {
|
|
68
|
+
this.setupEventListeners();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private fileKey(file: File): string {
|
|
72
|
+
return `${file.name}-${file.size}-${file.lastModified}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private setupEventListeners(): void {
|
|
76
|
+
(['dragenter', 'dragover', 'dragleave', 'drop'] as const).forEach(event => {
|
|
77
|
+
this.dropZone.addEventListener(event, this.preventDefaults);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
(['dragenter', 'dragover'] as const).forEach(event => {
|
|
81
|
+
this.dropZone.addEventListener(event, this.handleDragEnter);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
(['dragleave', 'drop'] as const).forEach(event => {
|
|
85
|
+
this.dropZone.addEventListener(event, this.handleDragLeave);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.dropZone.addEventListener('drop', this.handleDrop);
|
|
89
|
+
this.dropZone.addEventListener('click', this.handleDropZoneClick);
|
|
90
|
+
this.fileInput.addEventListener('change', this.handleFileInputChange);
|
|
91
|
+
this.uploadBtn.addEventListener('click', this.handleUploadClick);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private preventDefaults = (e: Event): void => {
|
|
95
|
+
e.preventDefault();
|
|
96
|
+
e.stopPropagation();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
private handleDragEnter = (): void => {
|
|
100
|
+
this.dropZone.classList.add('drag-over');
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
private handleDragLeave = (): void => {
|
|
104
|
+
this.dropZone.classList.remove('drag-over');
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
private handleDrop = (e: DragEvent): void => {
|
|
108
|
+
const droppedFiles = e.dataTransfer?.files;
|
|
109
|
+
if (droppedFiles) {
|
|
110
|
+
this.handleFiles(droppedFiles);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
private handleDropZoneClick = (): void => {
|
|
115
|
+
this.fileInput.click();
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
private handleFileInputChange = (e: Event): void => {
|
|
119
|
+
const target = e.target as HTMLInputElement;
|
|
120
|
+
if (target.files) {
|
|
121
|
+
this.handleFiles(target.files);
|
|
122
|
+
target.value = '';
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
private handleUploadClick = async (): Promise<void> => {
|
|
127
|
+
if (this.files.size === 0) return;
|
|
128
|
+
|
|
129
|
+
this.uploadBtn.disabled = true;
|
|
130
|
+
this.uploadBtn.textContent = 'Uploading...';
|
|
131
|
+
|
|
132
|
+
const uploadPromises = Array.from(this.files.values()).map(({ file, element }) =>
|
|
133
|
+
this.uploadFile(file, element)
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const results = await Promise.allSettled(uploadPromises);
|
|
137
|
+
|
|
138
|
+
this.uploadBtn.textContent = 'Upload Complete';
|
|
139
|
+
|
|
140
|
+
setTimeout(() => {
|
|
141
|
+
this.dispatchUploadCompletedEvent(results);
|
|
142
|
+
this.fileList.innerHTML = '';
|
|
143
|
+
this.files.clear();
|
|
144
|
+
this.updateUploadButton();
|
|
145
|
+
}, 1500);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
private handleFiles(fileList: FileList): void {
|
|
149
|
+
Array.from(fileList).forEach(file => {
|
|
150
|
+
const key = this.fileKey(file);
|
|
151
|
+
if (this.validateFile(file) && !this.files.has(key)) {
|
|
152
|
+
const element = this.addFileToUI(file);
|
|
153
|
+
this.files.set(key, { file, element });
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
this.updateUploadButton();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private validateFile(file: File): boolean {
|
|
160
|
+
if (this.maxFileSize && file.size > this.maxFileSize) {
|
|
161
|
+
this.container.dispatchEvent(new CustomEvent<FileValidationErrorDetail>('file-validation-error', {
|
|
162
|
+
detail: { file, reason: 'size' },
|
|
163
|
+
bubbles: true,
|
|
164
|
+
}));
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (this.allowedTypes && !this.allowedTypes.includes(file.type)) {
|
|
169
|
+
this.container.dispatchEvent(new CustomEvent<FileValidationErrorDetail>('file-validation-error', {
|
|
170
|
+
detail: { file, reason: 'type' },
|
|
171
|
+
bubbles: true,
|
|
172
|
+
}));
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private addFileToUI(file: File): HTMLDivElement {
|
|
180
|
+
const key = this.fileKey(file);
|
|
181
|
+
const item = document.createElement('div');
|
|
182
|
+
item.className = 'file-item';
|
|
183
|
+
|
|
184
|
+
const escapedFileName = this.escapeHtml(file.name);
|
|
185
|
+
|
|
186
|
+
item.innerHTML = `
|
|
187
|
+
<div class="file-item-header">
|
|
188
|
+
<div class="file-info">
|
|
189
|
+
<div class="file-icon">
|
|
190
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
191
|
+
<path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path>
|
|
192
|
+
<polyline points="13 2 13 9 20 9"></polyline>
|
|
193
|
+
</svg>
|
|
194
|
+
</div>
|
|
195
|
+
<div class="file-details">
|
|
196
|
+
<span class="file-name" title="${escapedFileName}">${escapedFileName}</span>
|
|
197
|
+
<span class="file-size">${this.formatSize(file.size)}</span>
|
|
198
|
+
</div>
|
|
199
|
+
</div>
|
|
200
|
+
<button class="remove-btn" type="button" aria-label="Remove file">
|
|
201
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
202
|
+
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
203
|
+
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
204
|
+
</svg>
|
|
205
|
+
</button>
|
|
206
|
+
</div>
|
|
207
|
+
<div class="progress-container">
|
|
208
|
+
<div class="progress-bar"></div>
|
|
209
|
+
</div>
|
|
210
|
+
<div class="status-text">Waiting...</div>
|
|
211
|
+
`;
|
|
212
|
+
|
|
213
|
+
const removeBtn = item.querySelector<HTMLButtonElement>('.remove-btn');
|
|
214
|
+
if (removeBtn) {
|
|
215
|
+
removeBtn.addEventListener('click', (e) => {
|
|
216
|
+
e.stopPropagation();
|
|
217
|
+
this.removeFile(key);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
this.fileList.appendChild(item);
|
|
222
|
+
return item;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private uploadFile(file: File, element: HTMLDivElement): Promise<unknown> {
|
|
226
|
+
return new Promise((resolve, reject) => {
|
|
227
|
+
const progressContainer = element.querySelector<HTMLElement>('.progress-container');
|
|
228
|
+
const progressBar = element.querySelector<HTMLElement>('.progress-bar');
|
|
229
|
+
const statusText = element.querySelector<HTMLElement>('.status-text');
|
|
230
|
+
const removeBtn = element.querySelector<HTMLElement>('.remove-btn');
|
|
231
|
+
|
|
232
|
+
if (!progressContainer || !progressBar || !statusText || !removeBtn) {
|
|
233
|
+
reject(new Error('Required UI elements not found'));
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
progressContainer.style.display = 'block';
|
|
238
|
+
statusText.style.display = 'block';
|
|
239
|
+
statusText.textContent = '0%';
|
|
240
|
+
removeBtn.style.display = 'none';
|
|
241
|
+
|
|
242
|
+
const xhr = new XMLHttpRequest();
|
|
243
|
+
const key = this.fileKey(file);
|
|
244
|
+
this.abortControllers.set(key, () => xhr.abort());
|
|
245
|
+
|
|
246
|
+
xhr.upload.addEventListener('progress', (e) => {
|
|
247
|
+
if (e.lengthComputable) {
|
|
248
|
+
const pct = Math.round((e.loaded / e.total) * 100);
|
|
249
|
+
progressBar.style.width = pct + '%';
|
|
250
|
+
statusText.textContent = pct + '%';
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
xhr.addEventListener('load', () => {
|
|
255
|
+
this.abortControllers.delete(key);
|
|
256
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
257
|
+
progressBar.style.width = '100%';
|
|
258
|
+
progressBar.style.backgroundColor = 'var(--success)';
|
|
259
|
+
statusText.textContent = 'Completed';
|
|
260
|
+
statusText.classList.add('success');
|
|
261
|
+
try {
|
|
262
|
+
resolve(JSON.parse(xhr.responseText));
|
|
263
|
+
} catch {
|
|
264
|
+
resolve(xhr.responseText);
|
|
265
|
+
}
|
|
266
|
+
} else {
|
|
267
|
+
progressBar.style.backgroundColor = 'var(--error)';
|
|
268
|
+
statusText.textContent = 'Failed';
|
|
269
|
+
statusText.classList.add('error');
|
|
270
|
+
removeBtn.style.display = 'flex';
|
|
271
|
+
reject(new Error(`Upload failed: ${xhr.statusText}`));
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
xhr.addEventListener('error', () => {
|
|
276
|
+
this.abortControllers.delete(key);
|
|
277
|
+
progressBar.style.backgroundColor = 'var(--error)';
|
|
278
|
+
statusText.textContent = 'Network Error';
|
|
279
|
+
statusText.classList.add('error');
|
|
280
|
+
removeBtn.style.display = 'flex';
|
|
281
|
+
reject(new Error('Network error'));
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
xhr.addEventListener('abort', () => {
|
|
285
|
+
this.abortControllers.delete(key);
|
|
286
|
+
statusText.textContent = 'Cancelled';
|
|
287
|
+
statusText.classList.add('error');
|
|
288
|
+
removeBtn.style.display = 'flex';
|
|
289
|
+
reject(new Error('Upload aborted'));
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
const formData = new FormData();
|
|
293
|
+
formData.append('file', file);
|
|
294
|
+
xhr.open('POST', this.uploadUrl);
|
|
295
|
+
xhr.send(formData);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private removeFile(key: string): void {
|
|
300
|
+
const abort = this.abortControllers.get(key);
|
|
301
|
+
if (abort) abort();
|
|
302
|
+
|
|
303
|
+
const fileData = this.files.get(key);
|
|
304
|
+
if (fileData) {
|
|
305
|
+
fileData.element.remove();
|
|
306
|
+
this.files.delete(key);
|
|
307
|
+
this.updateUploadButton();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private updateUploadButton(): void {
|
|
312
|
+
this.uploadBtn.disabled = this.files.size === 0;
|
|
313
|
+
this.uploadBtn.textContent = this.files.size > 0
|
|
314
|
+
? `Upload ${this.files.size} File${this.files.size === 1 ? '' : 's'}`
|
|
315
|
+
: 'Upload Files';
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private dispatchUploadCompletedEvent(results: PromiseSettledResult<unknown>[]): void {
|
|
319
|
+
const files = Array.from(this.files.values()).map(({ file }) => file);
|
|
320
|
+
this.container.dispatchEvent(new CustomEvent<UploadCompletedDetail>('upload-completed', {
|
|
321
|
+
detail: { fileCount: this.files.size, files, results },
|
|
322
|
+
bubbles: true,
|
|
323
|
+
}));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private formatSize(bytes: number): string {
|
|
327
|
+
if (bytes === 0) return '0 Bytes';
|
|
328
|
+
const k = 1024;
|
|
329
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'] as const;
|
|
330
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
331
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private escapeHtml(text: string): string {
|
|
335
|
+
const div = document.createElement('div');
|
|
336
|
+
div.textContent = text;
|
|
337
|
+
return div.innerHTML;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
public destroy(): void {
|
|
341
|
+
this.abortControllers.forEach(abort => abort());
|
|
342
|
+
this.abortControllers.clear();
|
|
343
|
+
|
|
344
|
+
(['dragenter', 'dragover', 'dragleave', 'drop'] as const).forEach(event => {
|
|
345
|
+
this.dropZone.removeEventListener(event, this.preventDefaults);
|
|
346
|
+
});
|
|
347
|
+
(['dragenter', 'dragover'] as const).forEach(event => {
|
|
348
|
+
this.dropZone.removeEventListener(event, this.handleDragEnter);
|
|
349
|
+
});
|
|
350
|
+
(['dragleave', 'drop'] as const).forEach(event => {
|
|
351
|
+
this.dropZone.removeEventListener(event, this.handleDragLeave);
|
|
352
|
+
});
|
|
353
|
+
this.dropZone.removeEventListener('drop', this.handleDrop);
|
|
354
|
+
this.dropZone.removeEventListener('click', this.handleDropZoneClick);
|
|
355
|
+
this.fileInput.removeEventListener('change', this.handleFileInputChange);
|
|
356
|
+
this.uploadBtn.removeEventListener('click', this.handleUploadClick);
|
|
357
|
+
|
|
358
|
+
this.files.clear();
|
|
359
|
+
this.fileList.innerHTML = '';
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export { FileUploader };
|
|
364
|
+
export type { FileUploaderConfig, UploadCompletedDetail, FileValidationErrorDetail };
|