1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('rxjs/operators')) :
|
3 | typeof define === 'function' && define.amd ? define('ngx-uploader', ['exports', '@angular/core', 'rxjs', 'rxjs/operators'], factory) :
|
4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['ngx-uploader'] = {}, global.ng.core, global.rxjs, global.rxjs.operators));
|
5 | }(this, (function (exports, core, rxjs, operators) { 'use strict';
|
6 |
|
7 | (function (UploadStatus) {
|
8 | UploadStatus[UploadStatus["Queue"] = 0] = "Queue";
|
9 | UploadStatus[UploadStatus["Uploading"] = 1] = "Uploading";
|
10 | UploadStatus[UploadStatus["Done"] = 2] = "Done";
|
11 | UploadStatus[UploadStatus["Cancelled"] = 3] = "Cancelled";
|
12 | })(exports.UploadStatus || (exports.UploadStatus = {}));
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var extendStatics = function (d, b) {
|
30 | extendStatics = Object.setPrototypeOf ||
|
31 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
32 | function (d, b) { for (var p in b)
|
33 | if (Object.prototype.hasOwnProperty.call(b, p))
|
34 | d[p] = b[p]; };
|
35 | return extendStatics(d, b);
|
36 | };
|
37 | function __extends(d, b) {
|
38 | extendStatics(d, b);
|
39 | function __() { this.constructor = d; }
|
40 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
41 | }
|
42 | var __assign = function () {
|
43 | __assign = Object.assign || function __assign(t) {
|
44 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
45 | s = arguments[i];
|
46 | for (var p in s)
|
47 | if (Object.prototype.hasOwnProperty.call(s, p))
|
48 | t[p] = s[p];
|
49 | }
|
50 | return t;
|
51 | };
|
52 | return __assign.apply(this, arguments);
|
53 | };
|
54 | function __rest(s, e) {
|
55 | var t = {};
|
56 | for (var p in s)
|
57 | if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
58 | t[p] = s[p];
|
59 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
60 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
61 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
62 | t[p[i]] = s[p[i]];
|
63 | }
|
64 | return t;
|
65 | }
|
66 | function __decorate(decorators, target, key, desc) {
|
67 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
68 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
69 | r = Reflect.decorate(decorators, target, key, desc);
|
70 | else
|
71 | for (var i = decorators.length - 1; i >= 0; i--)
|
72 | if (d = decorators[i])
|
73 | r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
74 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
75 | }
|
76 | function __param(paramIndex, decorator) {
|
77 | return function (target, key) { decorator(target, key, paramIndex); };
|
78 | }
|
79 | function __metadata(metadataKey, metadataValue) {
|
80 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
81 | return Reflect.metadata(metadataKey, metadataValue);
|
82 | }
|
83 | function __awaiter(thisArg, _arguments, P, generator) {
|
84 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
85 | return new (P || (P = Promise))(function (resolve, reject) {
|
86 | function fulfilled(value) { try {
|
87 | step(generator.next(value));
|
88 | }
|
89 | catch (e) {
|
90 | reject(e);
|
91 | } }
|
92 | function rejected(value) { try {
|
93 | step(generator["throw"](value));
|
94 | }
|
95 | catch (e) {
|
96 | reject(e);
|
97 | } }
|
98 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
99 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
100 | });
|
101 | }
|
102 | function __generator(thisArg, body) {
|
103 | var _ = { label: 0, sent: function () { if (t[0] & 1)
|
104 | throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
105 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
|
106 | function verb(n) { return function (v) { return step([n, v]); }; }
|
107 | function step(op) {
|
108 | if (f)
|
109 | throw new TypeError("Generator is already executing.");
|
110 | while (_)
|
111 | try {
|
112 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)
|
113 | return t;
|
114 | if (y = 0, t)
|
115 | op = [op[0] & 2, t.value];
|
116 | switch (op[0]) {
|
117 | case 0:
|
118 | case 1:
|
119 | t = op;
|
120 | break;
|
121 | case 4:
|
122 | _.label++;
|
123 | return { value: op[1], done: false };
|
124 | case 5:
|
125 | _.label++;
|
126 | y = op[1];
|
127 | op = [0];
|
128 | continue;
|
129 | case 7:
|
130 | op = _.ops.pop();
|
131 | _.trys.pop();
|
132 | continue;
|
133 | default:
|
134 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
135 | _ = 0;
|
136 | continue;
|
137 | }
|
138 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
139 | _.label = op[1];
|
140 | break;
|
141 | }
|
142 | if (op[0] === 6 && _.label < t[1]) {
|
143 | _.label = t[1];
|
144 | t = op;
|
145 | break;
|
146 | }
|
147 | if (t && _.label < t[2]) {
|
148 | _.label = t[2];
|
149 | _.ops.push(op);
|
150 | break;
|
151 | }
|
152 | if (t[2])
|
153 | _.ops.pop();
|
154 | _.trys.pop();
|
155 | continue;
|
156 | }
|
157 | op = body.call(thisArg, _);
|
158 | }
|
159 | catch (e) {
|
160 | op = [6, e];
|
161 | y = 0;
|
162 | }
|
163 | finally {
|
164 | f = t = 0;
|
165 | }
|
166 | if (op[0] & 5)
|
167 | throw op[1];
|
168 | return { value: op[0] ? op[1] : void 0, done: true };
|
169 | }
|
170 | }
|
171 | var __createBinding = Object.create ? (function (o, m, k, k2) {
|
172 | if (k2 === undefined)
|
173 | k2 = k;
|
174 | Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
|
175 | }) : (function (o, m, k, k2) {
|
176 | if (k2 === undefined)
|
177 | k2 = k;
|
178 | o[k2] = m[k];
|
179 | });
|
180 | function __exportStar(m, o) {
|
181 | for (var p in m)
|
182 | if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p))
|
183 | __createBinding(o, m, p);
|
184 | }
|
185 | function __values(o) {
|
186 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
187 | if (m)
|
188 | return m.call(o);
|
189 | if (o && typeof o.length === "number")
|
190 | return {
|
191 | next: function () {
|
192 | if (o && i >= o.length)
|
193 | o = void 0;
|
194 | return { value: o && o[i++], done: !o };
|
195 | }
|
196 | };
|
197 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
198 | }
|
199 | function __read(o, n) {
|
200 | var m = typeof Symbol === "function" && o[Symbol.iterator];
|
201 | if (!m)
|
202 | return o;
|
203 | var i = m.call(o), r, ar = [], e;
|
204 | try {
|
205 | while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
|
206 | ar.push(r.value);
|
207 | }
|
208 | catch (error) {
|
209 | e = { error: error };
|
210 | }
|
211 | finally {
|
212 | try {
|
213 | if (r && !r.done && (m = i["return"]))
|
214 | m.call(i);
|
215 | }
|
216 | finally {
|
217 | if (e)
|
218 | throw e.error;
|
219 | }
|
220 | }
|
221 | return ar;
|
222 | }
|
223 | function __spread() {
|
224 | for (var ar = [], i = 0; i < arguments.length; i++)
|
225 | ar = ar.concat(__read(arguments[i]));
|
226 | return ar;
|
227 | }
|
228 | function __spreadArrays() {
|
229 | for (var s = 0, i = 0, il = arguments.length; i < il; i++)
|
230 | s += arguments[i].length;
|
231 | for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
232 | for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
233 | r[k] = a[j];
|
234 | return r;
|
235 | }
|
236 | ;
|
237 | function __await(v) {
|
238 | return this instanceof __await ? (this.v = v, this) : new __await(v);
|
239 | }
|
240 | function __asyncGenerator(thisArg, _arguments, generator) {
|
241 | if (!Symbol.asyncIterator)
|
242 | throw new TypeError("Symbol.asyncIterator is not defined.");
|
243 | var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
244 | return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
245 | function verb(n) { if (g[n])
|
246 | i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
247 | function resume(n, v) { try {
|
248 | step(g[n](v));
|
249 | }
|
250 | catch (e) {
|
251 | settle(q[0][3], e);
|
252 | } }
|
253 | function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
254 | function fulfill(value) { resume("next", value); }
|
255 | function reject(value) { resume("throw", value); }
|
256 | function settle(f, v) { if (f(v), q.shift(), q.length)
|
257 | resume(q[0][0], q[0][1]); }
|
258 | }
|
259 | function __asyncDelegator(o) {
|
260 | var i, p;
|
261 | return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
|
262 | function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
|
263 | }
|
264 | function __asyncValues(o) {
|
265 | if (!Symbol.asyncIterator)
|
266 | throw new TypeError("Symbol.asyncIterator is not defined.");
|
267 | var m = o[Symbol.asyncIterator], i;
|
268 | return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
269 | function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
270 | function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); }
|
271 | }
|
272 | function __makeTemplateObject(cooked, raw) {
|
273 | if (Object.defineProperty) {
|
274 | Object.defineProperty(cooked, "raw", { value: raw });
|
275 | }
|
276 | else {
|
277 | cooked.raw = raw;
|
278 | }
|
279 | return cooked;
|
280 | }
|
281 | ;
|
282 | var __setModuleDefault = Object.create ? (function (o, v) {
|
283 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
284 | }) : function (o, v) {
|
285 | o["default"] = v;
|
286 | };
|
287 | function __importStar(mod) {
|
288 | if (mod && mod.__esModule)
|
289 | return mod;
|
290 | var result = {};
|
291 | if (mod != null)
|
292 | for (var k in mod)
|
293 | if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
|
294 | __createBinding(result, mod, k);
|
295 | __setModuleDefault(result, mod);
|
296 | return result;
|
297 | }
|
298 | function __importDefault(mod) {
|
299 | return (mod && mod.__esModule) ? mod : { default: mod };
|
300 | }
|
301 | function __classPrivateFieldGet(receiver, privateMap) {
|
302 | if (!privateMap.has(receiver)) {
|
303 | throw new TypeError("attempted to get private field on non-instance");
|
304 | }
|
305 | return privateMap.get(receiver);
|
306 | }
|
307 | function __classPrivateFieldSet(receiver, privateMap, value) {
|
308 | if (!privateMap.has(receiver)) {
|
309 | throw new TypeError("attempted to set private field on non-instance");
|
310 | }
|
311 | privateMap.set(receiver, value);
|
312 | return value;
|
313 | }
|
314 |
|
315 | function humanizeBytes(bytes) {
|
316 | if (bytes === 0) {
|
317 | return '0 Byte';
|
318 | }
|
319 | var k = 1024;
|
320 | var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
321 | var i = Math.floor(Math.log(bytes) / Math.log(k));
|
322 | return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
323 | }
|
324 | var NgUploaderService = (function () {
|
325 | function NgUploaderService(concurrency, contentTypes, maxUploads, maxFileSize) {
|
326 | var _this = this;
|
327 | if (concurrency === void 0) { concurrency = Number.POSITIVE_INFINITY; }
|
328 | if (contentTypes === void 0) { contentTypes = ['*']; }
|
329 | if (maxUploads === void 0) { maxUploads = Number.POSITIVE_INFINITY; }
|
330 | if (maxFileSize === void 0) { maxFileSize = Number.POSITIVE_INFINITY; }
|
331 | this.queue = [];
|
332 | this.serviceEvents = new core.EventEmitter();
|
333 | this.uploadScheduler = new rxjs.Subject();
|
334 | this.subs = [];
|
335 | this.contentTypes = contentTypes;
|
336 | this.maxUploads = maxUploads;
|
337 | this.maxFileSize = maxFileSize;
|
338 | this.uploadScheduler
|
339 | .pipe(operators.mergeMap(function (upload) { return _this.startUpload(upload); }, concurrency))
|
340 | .subscribe(function (uploadOutput) { return _this.serviceEvents.emit(uploadOutput); });
|
341 | }
|
342 | NgUploaderService.prototype.handleFiles = function (incomingFiles) {
|
343 | var _a;
|
344 | var _this = this;
|
345 | var allowedIncomingFiles = [].reduce.call(incomingFiles, function (acc, checkFile, i) {
|
346 | var futureQueueLength = acc.length + _this.queue.length + 1;
|
347 | if (_this.isContentTypeAllowed(checkFile.type) &&
|
348 | futureQueueLength <= _this.maxUploads &&
|
349 | _this.isFileSizeAllowed(checkFile.size)) {
|
350 | acc = acc.concat(checkFile);
|
351 | }
|
352 | else {
|
353 | var rejectedFile = _this.makeUploadFile(checkFile, i);
|
354 | _this.serviceEvents.emit({ type: 'rejected', file: rejectedFile });
|
355 | }
|
356 | return acc;
|
357 | }, []);
|
358 | (_a = this.queue).push.apply(_a, __spread([].map.call(allowedIncomingFiles, function (file, i) {
|
359 | var uploadFile = _this.makeUploadFile(file, i);
|
360 | _this.serviceEvents.emit({ type: 'addedToQueue', file: uploadFile });
|
361 | return uploadFile;
|
362 | })));
|
363 | this.serviceEvents.emit({ type: 'allAddedToQueue' });
|
364 | };
|
365 | NgUploaderService.prototype.initInputEvents = function (input) {
|
366 | var _this = this;
|
367 | return input.subscribe(function (event) {
|
368 | switch (event.type) {
|
369 | case 'uploadFile':
|
370 | var uploadFileIndex = _this.queue.findIndex(function (file) { return file === event.file; });
|
371 | if (uploadFileIndex !== -1 && event.file) {
|
372 | _this.uploadScheduler.next({ file: _this.queue[uploadFileIndex], event: event });
|
373 | }
|
374 | break;
|
375 | case 'uploadAll':
|
376 | var files = _this.queue.filter(function (file) { return file.progress.status === exports.UploadStatus.Queue; });
|
377 | files.forEach(function (file) { return _this.uploadScheduler.next({ file: file, event: event }); });
|
378 | break;
|
379 | case 'cancel':
|
380 | var id_1 = event.id || null;
|
381 | if (!id_1) {
|
382 | return;
|
383 | }
|
384 | var subs = _this.subs.filter(function (sub) { return sub.id === id_1; });
|
385 | subs.forEach(function (sub) {
|
386 | if (sub.sub) {
|
387 | sub.sub.unsubscribe();
|
388 | var fileIndex = _this.queue.findIndex(function (file) { return file.id === id_1; });
|
389 | if (fileIndex !== -1) {
|
390 | _this.queue[fileIndex].progress.status = exports.UploadStatus.Cancelled;
|
391 | _this.serviceEvents.emit({ type: 'cancelled', file: _this.queue[fileIndex] });
|
392 | }
|
393 | }
|
394 | });
|
395 | break;
|
396 | case 'cancelAll':
|
397 | _this.subs.forEach(function (sub) {
|
398 | if (sub.sub) {
|
399 | sub.sub.unsubscribe();
|
400 | }
|
401 | var file = _this.queue.find(function (uploadFile) { return uploadFile.id === sub.id; });
|
402 | if (file) {
|
403 | file.progress.status = exports.UploadStatus.Cancelled;
|
404 | _this.serviceEvents.emit({ type: 'cancelled', file: file });
|
405 | }
|
406 | });
|
407 | break;
|
408 | case 'remove':
|
409 | if (!event.id) {
|
410 | return;
|
411 | }
|
412 | var i = _this.queue.findIndex(function (file) { return file.id === event.id; });
|
413 | if (i !== -1) {
|
414 | var file = _this.queue[i];
|
415 | _this.queue.splice(i, 1);
|
416 | _this.serviceEvents.emit({ type: 'removed', file: file });
|
417 | }
|
418 | break;
|
419 | case 'removeAll':
|
420 | if (_this.queue.length) {
|
421 | _this.queue = [];
|
422 | _this.serviceEvents.emit({ type: 'removedAll' });
|
423 | }
|
424 | break;
|
425 | }
|
426 | });
|
427 | };
|
428 | NgUploaderService.prototype.startUpload = function (upload) {
|
429 | var _this = this;
|
430 | return new rxjs.Observable(function (observer) {
|
431 | var sub = _this.uploadFile(upload.file, upload.event)
|
432 | .pipe(operators.finalize(function () {
|
433 | if (!observer.closed) {
|
434 | observer.complete();
|
435 | }
|
436 | }))
|
437 | .subscribe(function (output) {
|
438 | observer.next(output);
|
439 | }, function (err) {
|
440 | observer.error(err);
|
441 | observer.complete();
|
442 | }, function () {
|
443 | observer.complete();
|
444 | });
|
445 | _this.subs.push({ id: upload.file.id, sub: sub });
|
446 | });
|
447 | };
|
448 | NgUploaderService.prototype.uploadFile = function (file, event) {
|
449 | var _this = this;
|
450 | return new rxjs.Observable(function (observer) {
|
451 | var url = event.url || '';
|
452 | var method = event.method || 'POST';
|
453 | var data = event.data || {};
|
454 | var headers = event.headers || {};
|
455 | var xhr = new XMLHttpRequest();
|
456 | var time = new Date().getTime();
|
457 | var progressStartTime = (file.progress.data && file.progress.data.startTime) || time;
|
458 | var speed = 0;
|
459 | var eta = null;
|
460 | xhr.upload.addEventListener('progress', function (e) {
|
461 | if (e.lengthComputable) {
|
462 | var percentage = Math.round((e.loaded * 100) / e.total);
|
463 | var diff = new Date().getTime() - time;
|
464 | speed = Math.round((e.loaded / diff) * 1000);
|
465 | progressStartTime = (file.progress.data && file.progress.data.startTime) || new Date().getTime();
|
466 | eta = Math.ceil((e.total - e.loaded) / speed);
|
467 | file.progress = {
|
468 | status: exports.UploadStatus.Uploading,
|
469 | data: {
|
470 | percentage: percentage,
|
471 | speed: speed,
|
472 | speedHuman: humanizeBytes(speed) + "/s",
|
473 | startTime: progressStartTime,
|
474 | endTime: null,
|
475 | eta: eta,
|
476 | etaHuman: _this.secondsToHuman(eta)
|
477 | }
|
478 | };
|
479 | observer.next({ type: 'uploading', file: file });
|
480 | }
|
481 | }, false);
|
482 | xhr.upload.addEventListener('error', function (e) {
|
483 | observer.error(e);
|
484 | observer.complete();
|
485 | });
|
486 | xhr.onreadystatechange = function () {
|
487 | if (xhr.readyState === XMLHttpRequest.DONE) {
|
488 | var speedAverage = Math.round((file.size / (new Date().getTime() - progressStartTime)) * 1000);
|
489 | file.progress = {
|
490 | status: exports.UploadStatus.Done,
|
491 | data: {
|
492 | percentage: 100,
|
493 | speed: speedAverage,
|
494 | speedHuman: humanizeBytes(speedAverage) + "/s",
|
495 | startTime: progressStartTime,
|
496 | endTime: new Date().getTime(),
|
497 | eta: eta,
|
498 | etaHuman: _this.secondsToHuman(eta || 0)
|
499 | }
|
500 | };
|
501 | file.responseStatus = xhr.status;
|
502 | try {
|
503 | file.response = JSON.parse(xhr.response);
|
504 | }
|
505 | catch (e) {
|
506 | file.response = xhr.response;
|
507 | }
|
508 | file.responseHeaders = _this.parseResponseHeaders(xhr.getAllResponseHeaders());
|
509 | observer.next({ type: 'done', file: file });
|
510 | observer.complete();
|
511 | }
|
512 | };
|
513 | xhr.open(method, url, true);
|
514 | xhr.withCredentials = event.withCredentials ? true : false;
|
515 | try {
|
516 | var uploadFile_1 = file.nativeFile;
|
517 | var uploadIndex = _this.queue.findIndex(function (outFile) { return outFile.nativeFile === uploadFile_1; });
|
518 | if (_this.queue[uploadIndex].progress.status === exports.UploadStatus.Cancelled) {
|
519 | observer.complete();
|
520 | }
|
521 | Object.keys(headers).forEach(function (key) { return xhr.setRequestHeader(key, headers[key]); });
|
522 | var bodyToSend = void 0;
|
523 | if (event.includeWebKitFormBoundary !== false) {
|
524 | Object.keys(data).forEach(function (key) { return file.form.append(key, data[key]); });
|
525 | file.form.append(event.fieldName || 'file', uploadFile_1, uploadFile_1.name);
|
526 | bodyToSend = file.form;
|
527 | }
|
528 | else {
|
529 | bodyToSend = uploadFile_1;
|
530 | }
|
531 | _this.serviceEvents.emit({ type: 'start', file: file });
|
532 | xhr.send(bodyToSend);
|
533 | }
|
534 | catch (e) {
|
535 | observer.complete();
|
536 | }
|
537 | return function () {
|
538 | xhr.abort();
|
539 | };
|
540 | });
|
541 | };
|
542 | NgUploaderService.prototype.secondsToHuman = function (sec) {
|
543 | return new Date(sec * 1000).toISOString().substr(11, 8);
|
544 | };
|
545 | NgUploaderService.prototype.generateId = function () {
|
546 | return Math.random().toString(36).substring(7);
|
547 | };
|
548 | NgUploaderService.prototype.setContentTypes = function (contentTypes) {
|
549 | if (typeof contentTypes !== 'undefined' && contentTypes instanceof Array) {
|
550 | if (contentTypes.find(function (type) { return type === '*'; }) !== undefined) {
|
551 | this.contentTypes = ['*'];
|
552 | }
|
553 | else {
|
554 | this.contentTypes = contentTypes;
|
555 | }
|
556 | return;
|
557 | }
|
558 | this.contentTypes = ['*'];
|
559 | };
|
560 | NgUploaderService.prototype.allContentTypesAllowed = function () {
|
561 | return this.contentTypes.find(function (type) { return type === '*'; }) !== undefined;
|
562 | };
|
563 | NgUploaderService.prototype.isContentTypeAllowed = function (mimetype) {
|
564 | if (this.allContentTypesAllowed()) {
|
565 | return true;
|
566 | }
|
567 | return this.contentTypes.find(function (type) { return type === mimetype; }) !== undefined;
|
568 | };
|
569 | NgUploaderService.prototype.isFileSizeAllowed = function (fileSize) {
|
570 | if (!this.maxFileSize) {
|
571 | return true;
|
572 | }
|
573 | return fileSize <= this.maxFileSize;
|
574 | };
|
575 | NgUploaderService.prototype.makeUploadFile = function (file, index) {
|
576 | return {
|
577 | fileIndex: index,
|
578 | id: this.generateId(),
|
579 | name: file.name,
|
580 | size: file.size,
|
581 | type: file.type,
|
582 | form: new FormData(),
|
583 | progress: {
|
584 | status: exports.UploadStatus.Queue,
|
585 | data: {
|
586 | percentage: 0,
|
587 | speed: 0,
|
588 | speedHuman: humanizeBytes(0) + "/s",
|
589 | startTime: null,
|
590 | endTime: null,
|
591 | eta: null,
|
592 | etaHuman: null
|
593 | }
|
594 | },
|
595 | lastModifiedDate: new Date(file.lastModified),
|
596 | sub: undefined,
|
597 | nativeFile: file
|
598 | };
|
599 | };
|
600 | NgUploaderService.prototype.parseResponseHeaders = function (httpHeaders) {
|
601 | if (!httpHeaders) {
|
602 | return;
|
603 | }
|
604 | return httpHeaders
|
605 | .split('\n')
|
606 | .map(function (x) { return x.split(/: */, 2); })
|
607 | .filter(function (x) { return x[0]; })
|
608 | .reduce(function (acc, x) {
|
609 | acc[x[0]] = x[1];
|
610 | return acc;
|
611 | }, {});
|
612 | };
|
613 | return NgUploaderService;
|
614 | }());
|
615 |
|
616 | var NgFileDropDirective = (function () {
|
617 | function NgFileDropDirective(elementRef) {
|
618 | this.elementRef = elementRef;
|
619 | this.stopEvent = function (e) {
|
620 | e.stopPropagation();
|
621 | e.preventDefault();
|
622 | };
|
623 | this.uploadOutput = new core.EventEmitter();
|
624 | }
|
625 | NgFileDropDirective.prototype.ngOnInit = function () {
|
626 | var _this = this;
|
627 | this._sub = [];
|
628 | var concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
|
629 | var allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
|
630 | var maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
|
631 | var maxFileSize = this.options && this.options.maxFileSize || Number.POSITIVE_INFINITY;
|
632 | this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads, maxFileSize);
|
633 | this.el = this.elementRef.nativeElement;
|
634 | this._sub.push(this.upload.serviceEvents.subscribe(function (event) {
|
635 | _this.uploadOutput.emit(event);
|
636 | }));
|
637 | if (this.uploadInput instanceof core.EventEmitter) {
|
638 | this._sub.push(this.upload.initInputEvents(this.uploadInput));
|
639 | }
|
640 | this.el.addEventListener('drop', this.stopEvent, false);
|
641 | this.el.addEventListener('dragenter', this.stopEvent, false);
|
642 | this.el.addEventListener('dragover', this.stopEvent, false);
|
643 | };
|
644 | NgFileDropDirective.prototype.ngOnDestroy = function () {
|
645 | this._sub.forEach(function (sub) { return sub.unsubscribe(); });
|
646 | };
|
647 | NgFileDropDirective.prototype.onDrop = function (e) {
|
648 | e.stopPropagation();
|
649 | e.preventDefault();
|
650 | var event = { type: 'drop' };
|
651 | this.uploadOutput.emit(event);
|
652 | this.upload.handleFiles(e.dataTransfer.files);
|
653 | };
|
654 | NgFileDropDirective.prototype.onDragOver = function (e) {
|
655 | if (!e) {
|
656 | return;
|
657 | }
|
658 | var event = { type: 'dragOver' };
|
659 | this.uploadOutput.emit(event);
|
660 | };
|
661 | NgFileDropDirective.prototype.onDragLeave = function (e) {
|
662 | if (!e) {
|
663 | return;
|
664 | }
|
665 | var event = { type: 'dragOut' };
|
666 | this.uploadOutput.emit(event);
|
667 | };
|
668 | return NgFileDropDirective;
|
669 | }());
|
670 | NgFileDropDirective.decorators = [
|
671 | { type: core.Directive, args: [{
|
672 | selector: '[ngFileDrop]'
|
673 | },] }
|
674 | ];
|
675 | NgFileDropDirective.ctorParameters = function () { return [
|
676 | { type: core.ElementRef }
|
677 | ]; };
|
678 | NgFileDropDirective.propDecorators = {
|
679 | options: [{ type: core.Input }],
|
680 | uploadInput: [{ type: core.Input }],
|
681 | uploadOutput: [{ type: core.Output }],
|
682 | onDrop: [{ type: core.HostListener, args: ['drop', ['$event'],] }],
|
683 | onDragOver: [{ type: core.HostListener, args: ['dragover', ['$event'],] }],
|
684 | onDragLeave: [{ type: core.HostListener, args: ['dragleave', ['$event'],] }]
|
685 | };
|
686 |
|
687 | var NgFileSelectDirective = (function () {
|
688 | function NgFileSelectDirective(elementRef) {
|
689 | var _this = this;
|
690 | this.elementRef = elementRef;
|
691 | this.fileListener = function () {
|
692 | if (_this.el.files) {
|
693 | _this.upload.handleFiles(_this.el.files);
|
694 | }
|
695 | };
|
696 | this.uploadOutput = new core.EventEmitter();
|
697 | }
|
698 | NgFileSelectDirective.prototype.ngOnInit = function () {
|
699 | var _this = this;
|
700 | this._sub = [];
|
701 | var concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
|
702 | var allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
|
703 | var maxUploads = this.options && this.options.maxUploads || Number.POSITIVE_INFINITY;
|
704 | var maxFileSize = this.options && this.options.maxFileSize || Number.POSITIVE_INFINITY;
|
705 | this.upload = new NgUploaderService(concurrency, allowedContentTypes, maxUploads, maxFileSize);
|
706 | this.el = this.elementRef.nativeElement;
|
707 | this.el.addEventListener('change', this.fileListener, false);
|
708 | this._sub.push(this.upload.serviceEvents.subscribe(function (event) {
|
709 | _this.uploadOutput.emit(event);
|
710 | }));
|
711 | if (this.uploadInput instanceof core.EventEmitter) {
|
712 | this._sub.push(this.upload.initInputEvents(this.uploadInput));
|
713 | }
|
714 | };
|
715 | NgFileSelectDirective.prototype.ngOnDestroy = function () {
|
716 | if (this.el) {
|
717 | this.el.removeEventListener('change', this.fileListener, false);
|
718 | this._sub.forEach(function (sub) { return sub.unsubscribe(); });
|
719 | }
|
720 | };
|
721 | return NgFileSelectDirective;
|
722 | }());
|
723 | NgFileSelectDirective.decorators = [
|
724 | { type: core.Directive, args: [{
|
725 | selector: '[ngFileSelect]'
|
726 | },] }
|
727 | ];
|
728 | NgFileSelectDirective.ctorParameters = function () { return [
|
729 | { type: core.ElementRef }
|
730 | ]; };
|
731 | NgFileSelectDirective.propDecorators = {
|
732 | options: [{ type: core.Input }],
|
733 | uploadInput: [{ type: core.Input }],
|
734 | uploadOutput: [{ type: core.Output }]
|
735 | };
|
736 |
|
737 | var NgxUploaderModule = (function () {
|
738 | function NgxUploaderModule() {
|
739 | }
|
740 | return NgxUploaderModule;
|
741 | }());
|
742 | NgxUploaderModule.decorators = [
|
743 | { type: core.NgModule, args: [{
|
744 | declarations: [NgFileDropDirective, NgFileSelectDirective],
|
745 | exports: [NgFileDropDirective, NgFileSelectDirective]
|
746 | },] }
|
747 | ];
|
748 |
|
749 | |
750 |
|
751 |
|
752 |
|
753 | |
754 |
|
755 |
|
756 |
|
757 | exports.NgFileDropDirective = NgFileDropDirective;
|
758 | exports.NgFileSelectDirective = NgFileSelectDirective;
|
759 | exports.NgUploaderService = NgUploaderService;
|
760 | exports.NgxUploaderModule = NgxUploaderModule;
|
761 | exports.humanizeBytes = humanizeBytes;
|
762 |
|
763 | Object.defineProperty(exports, '__esModule', { value: true });
|
764 |
|
765 | })));
|
766 |
|