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