ng2-file-upload 2.0.0-2 → 3.0.0

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.
Files changed (52) hide show
  1. package/README.md +1 -1
  2. package/esm2020/file-upload/file-drop.directive.mjs +92 -0
  3. package/esm2020/file-upload/file-item.class.mjs +123 -0
  4. package/esm2020/file-upload/file-like-object.class.mjs +21 -0
  5. package/esm2020/file-upload/file-select.directive.mjs +43 -0
  6. package/esm2020/file-upload/file-type.class.mjs +171 -0
  7. package/esm2020/file-upload/file-upload.module.mjs +21 -0
  8. package/esm2020/file-upload/file-uploader.class.mjs +404 -0
  9. package/esm2020/index.mjs +8 -0
  10. package/esm2020/ng2-file-upload.mjs +5 -0
  11. package/fesm2015/{ng2-file-upload.js → ng2-file-upload.mjs} +118 -104
  12. package/fesm2015/ng2-file-upload.mjs.map +1 -0
  13. package/fesm2020/ng2-file-upload.mjs +867 -0
  14. package/fesm2020/ng2-file-upload.mjs.map +1 -0
  15. package/file-upload/file-drop.directive.d.ts +3 -0
  16. package/file-upload/file-select.directive.d.ts +3 -0
  17. package/file-upload/file-upload.module.d.ts +9 -0
  18. package/package.json +23 -11
  19. package/bundles/ng2-file-upload.umd.js +0 -1233
  20. package/bundles/ng2-file-upload.umd.js.map +0 -1
  21. package/bundles/ng2-file-upload.umd.min.js +0 -2
  22. package/bundles/ng2-file-upload.umd.min.js.map +0 -1
  23. package/esm2015/file-upload/file-drop.directive.js +0 -86
  24. package/esm2015/file-upload/file-drop.directive.js.map +0 -1
  25. package/esm2015/file-upload/file-drop.directive.metadata.json +0 -1
  26. package/esm2015/file-upload/file-item.class.js +0 -123
  27. package/esm2015/file-upload/file-item.class.js.map +0 -1
  28. package/esm2015/file-upload/file-item.class.metadata.json +0 -1
  29. package/esm2015/file-upload/file-like-object.class.js +0 -21
  30. package/esm2015/file-upload/file-like-object.class.js.map +0 -1
  31. package/esm2015/file-upload/file-like-object.class.metadata.json +0 -1
  32. package/esm2015/file-upload/file-select.directive.js +0 -42
  33. package/esm2015/file-upload/file-select.directive.js.map +0 -1
  34. package/esm2015/file-upload/file-select.directive.metadata.json +0 -1
  35. package/esm2015/file-upload/file-type.class.js +0 -172
  36. package/esm2015/file-upload/file-type.class.js.map +0 -1
  37. package/esm2015/file-upload/file-type.class.metadata.json +0 -1
  38. package/esm2015/file-upload/file-upload.module.js +0 -14
  39. package/esm2015/file-upload/file-upload.module.js.map +0 -1
  40. package/esm2015/file-upload/file-upload.module.metadata.json +0 -1
  41. package/esm2015/file-upload/file-uploader.class.js +0 -408
  42. package/esm2015/file-upload/file-uploader.class.js.map +0 -1
  43. package/esm2015/file-upload/file-uploader.class.metadata.json +0 -1
  44. package/esm2015/index.js +0 -8
  45. package/esm2015/index.js.map +0 -1
  46. package/esm2015/index.metadata.json +0 -1
  47. package/esm2015/ng2-file-upload.js +0 -5
  48. package/esm2015/ng2-file-upload.js.map +0 -1
  49. package/esm2015/ng2-file-upload.metadata.json +0 -1
  50. package/fesm2015/ng2-file-upload.js.map +0 -1
  51. package/ng2-file-upload.d.ts +0 -4
  52. package/ng2-file-upload.metadata.json +0 -1
@@ -0,0 +1,867 @@
1
+ import * as i0 from '@angular/core';
2
+ import { EventEmitter, Directive, Input, Output, HostListener, NgModule } from '@angular/core';
3
+ import { CommonModule } from '@angular/common';
4
+
5
+ class FileLikeObject {
6
+ constructor(fileOrInput) {
7
+ this.rawFile = fileOrInput;
8
+ const fakePathOrObject = fileOrInput instanceof HTMLInputElement ? fileOrInput.value : fileOrInput;
9
+ const postfix = typeof fakePathOrObject === 'string' ? 'FakePath' : 'Object';
10
+ const method = `_createFrom${postfix}`;
11
+ this[method](fakePathOrObject);
12
+ }
13
+ _createFromFakePath(path) {
14
+ this.lastModifiedDate = void 0;
15
+ this.size = void 0;
16
+ this.type = `like/${path.slice(path.lastIndexOf('.') + 1).toLowerCase()}`;
17
+ this.name = path.slice(path.lastIndexOf('/') + path.lastIndexOf('\\') + 2);
18
+ }
19
+ _createFromObject(object) {
20
+ this.size = object.size;
21
+ this.type = object.type;
22
+ this.name = object.name;
23
+ }
24
+ }
25
+
26
+ class FileItem {
27
+ constructor(uploader, some, options) {
28
+ this.url = '/';
29
+ this.headers = [];
30
+ this.withCredentials = true;
31
+ this.formData = [];
32
+ this.isReady = false;
33
+ this.isUploading = false;
34
+ this.isUploaded = false;
35
+ this.isSuccess = false;
36
+ this.isCancel = false;
37
+ this.isError = false;
38
+ this.progress = 0;
39
+ this.uploader = uploader;
40
+ this.some = some;
41
+ this.options = options;
42
+ this.file = new FileLikeObject(some);
43
+ this._file = some;
44
+ if (uploader.options) {
45
+ this.method = uploader.options.method || 'POST';
46
+ this.alias = uploader.options.itemAlias || 'file';
47
+ }
48
+ this.url = uploader.options.url;
49
+ }
50
+ upload() {
51
+ try {
52
+ this.uploader.uploadItem(this);
53
+ }
54
+ catch (e) {
55
+ this.uploader._onCompleteItem(this, '', 0, {});
56
+ this.uploader._onErrorItem(this, '', 0, {});
57
+ }
58
+ }
59
+ cancel() {
60
+ this.uploader.cancelItem(this);
61
+ }
62
+ remove() {
63
+ this.uploader.removeFromQueue(this);
64
+ }
65
+ onBeforeUpload() {
66
+ return void 0;
67
+ }
68
+ onBuildForm(form) {
69
+ return { form };
70
+ }
71
+ onProgress(progress) {
72
+ return { progress };
73
+ }
74
+ onSuccess(response, status, headers) {
75
+ return { response, status, headers };
76
+ }
77
+ onError(response, status, headers) {
78
+ return { response, status, headers };
79
+ }
80
+ onCancel(response, status, headers) {
81
+ return { response, status, headers };
82
+ }
83
+ onComplete(response, status, headers) {
84
+ return { response, status, headers };
85
+ }
86
+ _onBeforeUpload() {
87
+ this.isReady = true;
88
+ this.isUploading = true;
89
+ this.isUploaded = false;
90
+ this.isSuccess = false;
91
+ this.isCancel = false;
92
+ this.isError = false;
93
+ this.progress = 0;
94
+ this.onBeforeUpload();
95
+ }
96
+ _onBuildForm(form) {
97
+ this.onBuildForm(form);
98
+ }
99
+ _onProgress(progress) {
100
+ this.progress = progress;
101
+ this.onProgress(progress);
102
+ }
103
+ _onSuccess(response, status, headers) {
104
+ this.isReady = false;
105
+ this.isUploading = false;
106
+ this.isUploaded = true;
107
+ this.isSuccess = true;
108
+ this.isCancel = false;
109
+ this.isError = false;
110
+ this.progress = 100;
111
+ this.index = undefined;
112
+ this.onSuccess(response, status, headers);
113
+ }
114
+ _onError(response, status, headers) {
115
+ this.isReady = false;
116
+ this.isUploading = false;
117
+ this.isUploaded = true;
118
+ this.isSuccess = false;
119
+ this.isCancel = false;
120
+ this.isError = true;
121
+ this.progress = 0;
122
+ this.index = undefined;
123
+ this.onError(response, status, headers);
124
+ }
125
+ _onCancel(response, status, headers) {
126
+ this.isReady = false;
127
+ this.isUploading = false;
128
+ this.isUploaded = false;
129
+ this.isSuccess = false;
130
+ this.isCancel = true;
131
+ this.isError = false;
132
+ this.progress = 0;
133
+ this.index = undefined;
134
+ this.onCancel(response, status, headers);
135
+ }
136
+ _onComplete(response, status, headers) {
137
+ this.onComplete(response, status, headers);
138
+ if (this.uploader.options.removeAfterUpload) {
139
+ this.remove();
140
+ }
141
+ }
142
+ _prepareToUploading() {
143
+ this.index = this.index || ++this.uploader._nextIndex;
144
+ this.isReady = true;
145
+ }
146
+ }
147
+
148
+ class FileType {
149
+ static getMimeClass(file) {
150
+ let mimeClass = 'application';
151
+ if (file?.type && this.mime_psd.indexOf(file.type) !== -1) {
152
+ mimeClass = 'image';
153
+ }
154
+ else if (file?.type?.match('image.*')) {
155
+ mimeClass = 'image';
156
+ }
157
+ else if (file?.type?.match('video.*')) {
158
+ mimeClass = 'video';
159
+ }
160
+ else if (file?.type?.match('audio.*')) {
161
+ mimeClass = 'audio';
162
+ }
163
+ else if (file?.type === 'application/pdf') {
164
+ mimeClass = 'pdf';
165
+ }
166
+ else if (file?.type && this.mime_compress.indexOf(file.type) !== -1) {
167
+ mimeClass = 'compress';
168
+ }
169
+ else if (file?.type && this.mime_doc.indexOf(file.type) !== -1) {
170
+ mimeClass = 'doc';
171
+ }
172
+ else if (file?.type && this.mime_xsl.indexOf(file.type) !== -1) {
173
+ mimeClass = 'xls';
174
+ }
175
+ else if (file?.type && this.mime_ppt.indexOf(file.type) !== -1) {
176
+ mimeClass = 'ppt';
177
+ }
178
+ if (mimeClass === 'application' && file?.name) {
179
+ mimeClass = this.fileTypeDetection(file.name);
180
+ }
181
+ return mimeClass;
182
+ }
183
+ static fileTypeDetection(inputFilename) {
184
+ const types = {
185
+ jpg: 'image',
186
+ jpeg: 'image',
187
+ tif: 'image',
188
+ psd: 'image',
189
+ bmp: 'image',
190
+ png: 'image',
191
+ nef: 'image',
192
+ tiff: 'image',
193
+ cr2: 'image',
194
+ dwg: 'image',
195
+ cdr: 'image',
196
+ ai: 'image',
197
+ indd: 'image',
198
+ pin: 'image',
199
+ cdp: 'image',
200
+ skp: 'image',
201
+ stp: 'image',
202
+ '3dm': 'image',
203
+ mp3: 'audio',
204
+ wav: 'audio',
205
+ wma: 'audio',
206
+ mod: 'audio',
207
+ m4a: 'audio',
208
+ compress: 'compress',
209
+ zip: 'compress',
210
+ rar: 'compress',
211
+ '7z': 'compress',
212
+ lz: 'compress',
213
+ z01: 'compress',
214
+ bz2: 'compress',
215
+ gz: 'compress',
216
+ pdf: 'pdf',
217
+ xls: 'xls',
218
+ xlsx: 'xls',
219
+ ods: 'xls',
220
+ mp4: 'video',
221
+ avi: 'video',
222
+ wmv: 'video',
223
+ mpg: 'video',
224
+ mts: 'video',
225
+ flv: 'video',
226
+ '3gp': 'video',
227
+ vob: 'video',
228
+ m4v: 'video',
229
+ mpeg: 'video',
230
+ m2ts: 'video',
231
+ mov: 'video',
232
+ doc: 'doc',
233
+ docx: 'doc',
234
+ eps: 'doc',
235
+ txt: 'doc',
236
+ odt: 'doc',
237
+ rtf: 'doc',
238
+ ppt: 'ppt',
239
+ pptx: 'ppt',
240
+ pps: 'ppt',
241
+ ppsx: 'ppt',
242
+ odp: 'ppt'
243
+ };
244
+ const chunks = inputFilename.split('.');
245
+ if (chunks.length < 2) {
246
+ return 'application';
247
+ }
248
+ const extension = chunks[chunks.length - 1].toLowerCase();
249
+ if (types[extension] === undefined) {
250
+ return 'application';
251
+ }
252
+ else {
253
+ return types[extension];
254
+ }
255
+ }
256
+ }
257
+ /* MS office */
258
+ // tslint:disable-next-line:variable-name
259
+ FileType.mime_doc = [
260
+ 'application/msword',
261
+ 'application/msword',
262
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
263
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
264
+ 'application/vnd.ms-word.document.macroEnabled.12',
265
+ 'application/vnd.ms-word.template.macroEnabled.12'
266
+ ];
267
+ // tslint:disable-next-line:variable-name
268
+ FileType.mime_xsl = [
269
+ 'application/vnd.ms-excel',
270
+ 'application/vnd.ms-excel',
271
+ 'application/vnd.ms-excel',
272
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
273
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
274
+ 'application/vnd.ms-excel.sheet.macroEnabled.12',
275
+ 'application/vnd.ms-excel.template.macroEnabled.12',
276
+ 'application/vnd.ms-excel.addin.macroEnabled.12',
277
+ 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
278
+ ];
279
+ // tslint:disable-next-line:variable-name
280
+ FileType.mime_ppt = [
281
+ 'application/vnd.ms-powerpoint',
282
+ 'application/vnd.ms-powerpoint',
283
+ 'application/vnd.ms-powerpoint',
284
+ 'application/vnd.ms-powerpoint',
285
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
286
+ 'application/vnd.openxmlformats-officedocument.presentationml.template',
287
+ 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
288
+ 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
289
+ 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
290
+ 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
291
+ 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'
292
+ ];
293
+ /* PSD */
294
+ // tslint:disable-next-line:variable-name
295
+ FileType.mime_psd = [
296
+ 'image/photoshop',
297
+ 'image/x-photoshop',
298
+ 'image/psd',
299
+ 'application/photoshop',
300
+ 'application/psd',
301
+ 'zz-application/zz-winassoc-psd'
302
+ ];
303
+ /* Compressed files */
304
+ // tslint:disable-next-line:variable-name
305
+ FileType.mime_compress = [
306
+ 'application/x-gtar',
307
+ 'application/x-gcompress',
308
+ 'application/compress',
309
+ 'application/x-tar',
310
+ 'application/x-rar-compressed',
311
+ 'application/octet-stream',
312
+ 'application/x-zip-compressed',
313
+ 'application/zip-compressed',
314
+ 'application/x-7z-compressed',
315
+ 'application/gzip',
316
+ 'application/x-bzip2'
317
+ ];
318
+
319
+ function isFile(value) {
320
+ return (File && value instanceof File);
321
+ }
322
+ class FileUploader {
323
+ constructor(options) {
324
+ this.isUploading = false;
325
+ this.queue = [];
326
+ this.progress = 0;
327
+ this._nextIndex = 0;
328
+ this.options = {
329
+ autoUpload: false,
330
+ isHTML5: true,
331
+ filters: [],
332
+ removeAfterUpload: false,
333
+ disableMultipart: false,
334
+ formatDataFunction: (item) => item._file,
335
+ formatDataFunctionIsAsync: false,
336
+ url: ''
337
+ };
338
+ this.setOptions(options);
339
+ this.response = new EventEmitter();
340
+ }
341
+ setOptions(options) {
342
+ this.options = Object.assign(this.options, options);
343
+ this.authToken = this.options.authToken;
344
+ this.authTokenHeader = this.options.authTokenHeader || 'Authorization';
345
+ this.autoUpload = this.options.autoUpload;
346
+ this.options.filters?.unshift({ name: 'queueLimit', fn: this._queueLimitFilter });
347
+ if (this.options.maxFileSize) {
348
+ this.options.filters?.unshift({ name: 'fileSize', fn: this._fileSizeFilter });
349
+ }
350
+ if (this.options.allowedFileType) {
351
+ this.options.filters?.unshift({ name: 'fileType', fn: this._fileTypeFilter });
352
+ }
353
+ if (this.options.allowedMimeType) {
354
+ this.options.filters?.unshift({ name: 'mimeType', fn: this._mimeTypeFilter });
355
+ }
356
+ for (let i = 0; i < this.queue.length; i++) {
357
+ this.queue[i].url = this.options.url;
358
+ }
359
+ }
360
+ addToQueue(files, _options, filters) {
361
+ let options = _options;
362
+ const list = [];
363
+ for (const file of files) {
364
+ list.push(file);
365
+ }
366
+ const arrayOfFilters = this._getFilters(filters);
367
+ const count = this.queue.length;
368
+ const addedFileItems = [];
369
+ list.map((some) => {
370
+ if (!options) {
371
+ options = this.options;
372
+ }
373
+ const temp = new FileLikeObject(some);
374
+ if (this._isValidFile(temp, arrayOfFilters, options)) {
375
+ const fileItem = new FileItem(this, some, options);
376
+ addedFileItems.push(fileItem);
377
+ this.queue.push(fileItem);
378
+ this._onAfterAddingFile(fileItem);
379
+ }
380
+ else {
381
+ if (this._failFilterIndex) {
382
+ const filter = arrayOfFilters[this._failFilterIndex];
383
+ this._onWhenAddingFileFailed(temp, filter, options);
384
+ }
385
+ }
386
+ });
387
+ if (this.queue.length !== count) {
388
+ this._onAfterAddingAll(addedFileItems);
389
+ this.progress = this._getTotalProgress();
390
+ }
391
+ this._render();
392
+ if (this.options.autoUpload) {
393
+ this.uploadAll();
394
+ }
395
+ }
396
+ removeFromQueue(value) {
397
+ const index = this.getIndexOfItem(value);
398
+ const item = this.queue[index];
399
+ if (item.isUploading) {
400
+ item.cancel();
401
+ }
402
+ this.queue.splice(index, 1);
403
+ this.progress = this._getTotalProgress();
404
+ }
405
+ clearQueue() {
406
+ while (this.queue.length) {
407
+ this.queue[0].remove();
408
+ }
409
+ this.progress = 0;
410
+ }
411
+ uploadItem(value) {
412
+ const index = this.getIndexOfItem(value);
413
+ const item = this.queue[index];
414
+ const transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport';
415
+ item._prepareToUploading();
416
+ if (this.isUploading) {
417
+ return;
418
+ }
419
+ this.isUploading = true;
420
+ this[transport](item);
421
+ }
422
+ cancelItem(value) {
423
+ const index = this.getIndexOfItem(value);
424
+ const item = this.queue[index];
425
+ const prop = this.options.isHTML5 ? item._xhr : item._form;
426
+ if (item && item.isUploading) {
427
+ prop.abort();
428
+ }
429
+ }
430
+ uploadAll() {
431
+ const items = this.getNotUploadedItems().filter((item) => !item.isUploading);
432
+ if (!items.length) {
433
+ return;
434
+ }
435
+ items.map((item) => item._prepareToUploading());
436
+ items[0].upload();
437
+ }
438
+ cancelAll() {
439
+ const items = this.getNotUploadedItems();
440
+ items.map((item) => item.cancel());
441
+ }
442
+ isFile(value) {
443
+ return isFile(value);
444
+ }
445
+ isFileLikeObject(value) {
446
+ return value instanceof FileLikeObject;
447
+ }
448
+ getIndexOfItem(value) {
449
+ return typeof value === 'number' ? value : this.queue.indexOf(value);
450
+ }
451
+ getNotUploadedItems() {
452
+ return this.queue.filter((item) => !item.isUploaded);
453
+ }
454
+ getReadyItems() {
455
+ return this.queue
456
+ .filter((item) => (item.isReady && !item.isUploading))
457
+ .sort((item1, item2) => item1.index - item2.index);
458
+ }
459
+ onAfterAddingAll(fileItems) {
460
+ return { fileItems };
461
+ }
462
+ onBuildItemForm(fileItem, form) {
463
+ return { fileItem, form };
464
+ }
465
+ onAfterAddingFile(fileItem) {
466
+ return { fileItem };
467
+ }
468
+ onWhenAddingFileFailed(item, filter, options) {
469
+ return { item, filter, options };
470
+ }
471
+ onBeforeUploadItem(fileItem) {
472
+ return { fileItem };
473
+ }
474
+ onProgressItem(fileItem, progress) {
475
+ return { fileItem, progress };
476
+ }
477
+ onProgressAll(progress) {
478
+ return { progress };
479
+ }
480
+ onSuccessItem(item, response, status, headers) {
481
+ return { item, response, status, headers };
482
+ }
483
+ onErrorItem(item, response, status, headers) {
484
+ return { item, response, status, headers };
485
+ }
486
+ onCancelItem(item, response, status, headers) {
487
+ return { item, response, status, headers };
488
+ }
489
+ onCompleteItem(item, response, status, headers) {
490
+ return { item, response, status, headers };
491
+ }
492
+ onCompleteAll() {
493
+ return void 0;
494
+ }
495
+ _mimeTypeFilter(item) {
496
+ return !(item?.type && this.options.allowedMimeType && this.options.allowedMimeType?.indexOf(item.type) === -1);
497
+ }
498
+ _fileSizeFilter(item) {
499
+ return !(this.options.maxFileSize && item.size > this.options.maxFileSize);
500
+ }
501
+ _fileTypeFilter(item) {
502
+ return !(this.options.allowedFileType &&
503
+ this.options.allowedFileType.indexOf(FileType.getMimeClass(item)) === -1);
504
+ }
505
+ _onErrorItem(item, response, status, headers) {
506
+ item._onError(response, status, headers);
507
+ this.onErrorItem(item, response, status, headers);
508
+ }
509
+ _onCompleteItem(item, response, status, headers) {
510
+ item._onComplete(response, status, headers);
511
+ this.onCompleteItem(item, response, status, headers);
512
+ const nextItem = this.getReadyItems()[0];
513
+ this.isUploading = false;
514
+ if (nextItem) {
515
+ nextItem.upload();
516
+ return;
517
+ }
518
+ this.onCompleteAll();
519
+ this.progress = this._getTotalProgress();
520
+ this._render();
521
+ }
522
+ _headersGetter(parsedHeaders) {
523
+ return (name) => {
524
+ if (name) {
525
+ return parsedHeaders[name.toLowerCase()] || undefined;
526
+ }
527
+ return parsedHeaders;
528
+ };
529
+ }
530
+ _xhrTransport(item) {
531
+ // tslint:disable-next-line:no-this-assignment
532
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
533
+ const that = this;
534
+ const xhr = item._xhr = new XMLHttpRequest();
535
+ let sendable;
536
+ this._onBeforeUploadItem(item);
537
+ if (typeof item._file.size !== 'number') {
538
+ throw new TypeError('The file specified is no longer valid');
539
+ }
540
+ if (!this.options.disableMultipart) {
541
+ sendable = new FormData();
542
+ this._onBuildItemForm(item, sendable);
543
+ const appendFile = () => sendable.append(item.alias, item._file, item.file.name);
544
+ if (!this.options.parametersBeforeFiles) {
545
+ appendFile();
546
+ }
547
+ // For AWS, Additional Parameters must come BEFORE Files
548
+ if (this.options.additionalParameter !== undefined) {
549
+ Object.keys(this.options.additionalParameter).forEach((key) => {
550
+ let paramVal = this.options.additionalParameter?.[key];
551
+ // Allow an additional parameter to include the filename
552
+ if (typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0 && item.file?.name) {
553
+ paramVal = paramVal.replace('{{file_name}}', item.file.name);
554
+ }
555
+ sendable.append(key, paramVal);
556
+ });
557
+ }
558
+ if (appendFile && this.options.parametersBeforeFiles) {
559
+ appendFile();
560
+ }
561
+ }
562
+ else {
563
+ if (this.options.formatDataFunction) {
564
+ sendable = this.options.formatDataFunction(item);
565
+ }
566
+ }
567
+ xhr.upload.onprogress = (event) => {
568
+ const progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
569
+ this._onProgressItem(item, progress);
570
+ };
571
+ xhr.onload = () => {
572
+ const headers = this._parseHeaders(xhr.getAllResponseHeaders());
573
+ const response = this._transformResponse(xhr.response, headers);
574
+ const gist = this._isSuccessCode(xhr.status) ? 'Success' : 'Error';
575
+ const method = `_on${gist}Item`;
576
+ this[method](item, response, xhr.status, headers);
577
+ this._onCompleteItem(item, response, xhr.status, headers);
578
+ };
579
+ xhr.onerror = () => {
580
+ const headers = this._parseHeaders(xhr.getAllResponseHeaders());
581
+ const response = this._transformResponse(xhr.response, headers);
582
+ this._onErrorItem(item, response, xhr.status, headers);
583
+ this._onCompleteItem(item, response, xhr.status, headers);
584
+ };
585
+ xhr.onabort = () => {
586
+ const headers = this._parseHeaders(xhr.getAllResponseHeaders());
587
+ const response = this._transformResponse(xhr.response, headers);
588
+ this._onCancelItem(item, response, xhr.status, headers);
589
+ this._onCompleteItem(item, response, xhr.status, headers);
590
+ };
591
+ if (item.method && item.url) {
592
+ xhr.open(item.method, item.url, true);
593
+ }
594
+ xhr.withCredentials = item.withCredentials;
595
+ if (this.options.headers) {
596
+ for (const header of this.options.headers) {
597
+ xhr.setRequestHeader(header.name, header.value);
598
+ }
599
+ }
600
+ if (item.headers.length) {
601
+ for (const header of item.headers) {
602
+ xhr.setRequestHeader(header.name, header.value);
603
+ }
604
+ }
605
+ if (this.authToken && this.authTokenHeader) {
606
+ xhr.setRequestHeader(this.authTokenHeader, this.authToken);
607
+ }
608
+ xhr.onreadystatechange = function () {
609
+ if (xhr.readyState == XMLHttpRequest.DONE) {
610
+ that.response.emit(xhr.responseText);
611
+ }
612
+ };
613
+ if (this.options.formatDataFunctionIsAsync) {
614
+ sendable.then((result) => xhr.send(JSON.stringify(result)));
615
+ }
616
+ else {
617
+ xhr.send(sendable);
618
+ }
619
+ this._render();
620
+ }
621
+ _getTotalProgress(value = 0) {
622
+ if (this.options.removeAfterUpload) {
623
+ return value;
624
+ }
625
+ const notUploaded = this.getNotUploadedItems().length;
626
+ const uploaded = notUploaded ? this.queue.length - notUploaded : this.queue.length;
627
+ const ratio = 100 / this.queue.length;
628
+ const current = value * ratio / 100;
629
+ return Math.round(uploaded * ratio + current);
630
+ }
631
+ _getFilters(filters) {
632
+ if (!filters) {
633
+ return this.options?.filters || [];
634
+ }
635
+ if (Array.isArray(filters)) {
636
+ return filters;
637
+ }
638
+ if (typeof filters === 'string') {
639
+ const names = filters.match(/[^\s,]+/g);
640
+ return this.options?.filters || []
641
+ .filter((filter) => names?.indexOf(filter.name) !== -1);
642
+ }
643
+ return this.options?.filters || [];
644
+ }
645
+ _render() {
646
+ return void 0;
647
+ }
648
+ _queueLimitFilter() {
649
+ return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
650
+ }
651
+ _isValidFile(file, filters, options) {
652
+ this._failFilterIndex = -1;
653
+ return !filters.length ? true : filters.every((filter) => {
654
+ if (this._failFilterIndex) {
655
+ this._failFilterIndex++;
656
+ }
657
+ return filter.fn.call(this, file, options);
658
+ });
659
+ }
660
+ _isSuccessCode(status) {
661
+ return (status >= 200 && status < 300) || status === 304;
662
+ }
663
+ _transformResponse(response, headers) {
664
+ return response;
665
+ }
666
+ _parseHeaders(headers) {
667
+ const parsed = {};
668
+ let key;
669
+ let val;
670
+ let i;
671
+ if (!headers) {
672
+ return parsed;
673
+ }
674
+ headers.split('\n').map((line) => {
675
+ i = line.indexOf(':');
676
+ key = line.slice(0, i).trim().toLowerCase();
677
+ val = line.slice(i + 1).trim();
678
+ if (key) {
679
+ parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
680
+ }
681
+ });
682
+ return parsed;
683
+ }
684
+ _onWhenAddingFileFailed(item, filter, options) {
685
+ this.onWhenAddingFileFailed(item, filter, options);
686
+ }
687
+ _onAfterAddingFile(item) {
688
+ this.onAfterAddingFile(item);
689
+ }
690
+ _onAfterAddingAll(items) {
691
+ this.onAfterAddingAll(items);
692
+ }
693
+ _onBeforeUploadItem(item) {
694
+ item._onBeforeUpload();
695
+ this.onBeforeUploadItem(item);
696
+ }
697
+ _onBuildItemForm(item, form) {
698
+ item._onBuildForm(form);
699
+ this.onBuildItemForm(item, form);
700
+ }
701
+ _onProgressItem(item, progress) {
702
+ const total = this._getTotalProgress(progress);
703
+ this.progress = total;
704
+ item._onProgress(progress);
705
+ this.onProgressItem(item, progress);
706
+ this.onProgressAll(total);
707
+ this._render();
708
+ }
709
+ _onSuccessItem(item, response, status, headers) {
710
+ item._onSuccess(response, status, headers);
711
+ this.onSuccessItem(item, response, status, headers);
712
+ }
713
+ _onCancelItem(item, response, status, headers) {
714
+ item._onCancel(response, status, headers);
715
+ this.onCancelItem(item, response, status, headers);
716
+ }
717
+ }
718
+
719
+ class FileDropDirective {
720
+ constructor(element) {
721
+ this.fileOver = new EventEmitter();
722
+ // eslint-disable-next-line @angular-eslint/no-output-on-prefix
723
+ this.onFileDrop = new EventEmitter();
724
+ this.element = element;
725
+ }
726
+ getOptions() {
727
+ return this.uploader?.options;
728
+ }
729
+ getFilters() {
730
+ return '';
731
+ }
732
+ onDrop(event) {
733
+ const transfer = this._getTransfer(event);
734
+ if (!transfer) {
735
+ return;
736
+ }
737
+ const options = this.getOptions();
738
+ const filters = this.getFilters();
739
+ this._preventAndStop(event);
740
+ if (options) {
741
+ this.uploader?.addToQueue(transfer.files, options, filters);
742
+ }
743
+ this.fileOver.emit(false);
744
+ this.onFileDrop.emit(transfer.files);
745
+ }
746
+ onDragOver(event) {
747
+ const transfer = this._getTransfer(event);
748
+ if (!this._haveFiles(transfer.types)) {
749
+ return;
750
+ }
751
+ transfer.dropEffect = 'copy';
752
+ this._preventAndStop(event);
753
+ this.fileOver.emit(true);
754
+ }
755
+ onDragLeave(event) {
756
+ if (this.element) {
757
+ if (event.currentTarget === this.element[0]) {
758
+ return;
759
+ }
760
+ }
761
+ this._preventAndStop(event);
762
+ this.fileOver.emit(false);
763
+ }
764
+ _getTransfer(event) {
765
+ return event.dataTransfer ? event.dataTransfer : event.originalEvent.dataTransfer; // jQuery fix;
766
+ }
767
+ _preventAndStop(event) {
768
+ event.preventDefault();
769
+ event.stopPropagation();
770
+ }
771
+ _haveFiles(types) {
772
+ if (!types) {
773
+ return false;
774
+ }
775
+ if (types.indexOf) {
776
+ return types.indexOf('Files') !== -1;
777
+ }
778
+ else if (types.contains) {
779
+ return types.contains('Files');
780
+ }
781
+ else {
782
+ return false;
783
+ }
784
+ }
785
+ }
786
+ FileDropDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: FileDropDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
787
+ FileDropDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.2", type: FileDropDirective, selector: "[ng2FileDrop]", inputs: { uploader: "uploader" }, outputs: { fileOver: "fileOver", onFileDrop: "onFileDrop" }, host: { listeners: { "drop": "onDrop($event)", "dragover": "onDragOver($event)", "dragleave": "onDragLeave($event)" } }, ngImport: i0 });
788
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: FileDropDirective, decorators: [{
789
+ type: Directive,
790
+ args: [{ selector: '[ng2FileDrop]' }]
791
+ }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { uploader: [{
792
+ type: Input
793
+ }], fileOver: [{
794
+ type: Output
795
+ }], onFileDrop: [{
796
+ type: Output
797
+ }], onDrop: [{
798
+ type: HostListener,
799
+ args: ['drop', ['$event']]
800
+ }], onDragOver: [{
801
+ type: HostListener,
802
+ args: ['dragover', ['$event']]
803
+ }], onDragLeave: [{
804
+ type: HostListener,
805
+ args: ['dragleave', ['$event']]
806
+ }] } });
807
+
808
+ class FileSelectDirective {
809
+ constructor(element) {
810
+ // eslint-disable-next-line @angular-eslint/no-output-on-prefix
811
+ this.onFileSelected = new EventEmitter();
812
+ this.element = element;
813
+ }
814
+ getOptions() {
815
+ return this.uploader?.options;
816
+ }
817
+ getFilters() {
818
+ return '';
819
+ }
820
+ isEmptyAfterSelection() {
821
+ return !!this.element.nativeElement.attributes.multiple;
822
+ }
823
+ onChange() {
824
+ const files = this.element.nativeElement.files;
825
+ const options = this.getOptions();
826
+ const filters = this.getFilters();
827
+ this.uploader?.addToQueue(files, options, filters);
828
+ this.onFileSelected.emit(files);
829
+ if (this.isEmptyAfterSelection()) {
830
+ this.element.nativeElement.value = '';
831
+ }
832
+ }
833
+ }
834
+ FileSelectDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: FileSelectDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
835
+ FileSelectDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.2", type: FileSelectDirective, selector: "[ng2FileSelect]", inputs: { uploader: "uploader" }, outputs: { onFileSelected: "onFileSelected" }, host: { listeners: { "change": "onChange()" } }, ngImport: i0 });
836
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: FileSelectDirective, decorators: [{
837
+ type: Directive,
838
+ args: [{ selector: '[ng2FileSelect]' }]
839
+ }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { uploader: [{
840
+ type: Input
841
+ }], onFileSelected: [{
842
+ type: Output
843
+ }], onChange: [{
844
+ type: HostListener,
845
+ args: ['change']
846
+ }] } });
847
+
848
+ class FileUploadModule {
849
+ }
850
+ FileUploadModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: FileUploadModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
851
+ FileUploadModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.2", ngImport: i0, type: FileUploadModule, declarations: [FileDropDirective, FileSelectDirective], imports: [CommonModule], exports: [FileDropDirective, FileSelectDirective] });
852
+ FileUploadModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: FileUploadModule, imports: [CommonModule] });
853
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.2", ngImport: i0, type: FileUploadModule, decorators: [{
854
+ type: NgModule,
855
+ args: [{
856
+ imports: [CommonModule],
857
+ declarations: [FileDropDirective, FileSelectDirective],
858
+ exports: [FileDropDirective, FileSelectDirective]
859
+ }]
860
+ }] });
861
+
862
+ /**
863
+ * Generated bundle index. Do not edit.
864
+ */
865
+
866
+ export { FileDropDirective, FileItem, FileLikeObject, FileSelectDirective, FileUploadModule, FileUploader };
867
+ //# sourceMappingURL=ng2-file-upload.mjs.map