@glr/ngx-file-uploader
Version:
An angular file uploader library
1,661 lines (1,660 loc) • 70.2 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* @license This package library v4.1.6-Beta
* Copyright (c) 2017 FUERSTVONMARTIN GmbH https://fuerstvonmartin.de/
* License: MIT
*/
import { Directive, ElementRef, EventEmitter, HostBinding, HostListener, Input, NgModule, Output, Pipe, Renderer } from '@angular/core';
import { BehaviorSubject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
import { DatePipe } from '@angular/common';
var Utils = /*@__PURE__*/(function () {
function Utils() {
}
/**
* Creates a unique id for submit form.
*
* \@static
* \@memberOf utils
* @return {?}
*
*/
Utils.uniqueID = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
// tslint:disable-next-line:no-bitwise
var /** @type {?} */ r = Math.random() * 16 | 0, /** @type {?} */ v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
/**
* Validate input and returns true if input is an element
*
* \@static
*
* \@memberOf FileManager
* @param {?} _input
* @return {?}
*/
Utils.isElement = function (_input) {
return !!(_input &&
(_input.nodeName ||
(_input.prop && _input.attr && _input.find))); // We have an on and find methode part of jQuery API
};
/**
* Validate input and returns true if input is a string
*
* \@static
*
* \@memberOf Utils
* @param {?} _input
* @return {?}
*/
Utils.isString = function (_input) {
return !!(typeof _input === 'string');
};
/**
* @param {...?} args
* @return {?}
*/
Utils.extendValue = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var /** @type {?} */ _value = args[0];
for (var /** @type {?} */ i = 1; i < args.length; i++) {
if (typeof args[i] !== 'undefined') {
_value = args[i];
}
}
return _value;
};
/**
* @param {?} subject
* @return {?}
*/
Utils.asObservable = function (subject) {
return new Observable(function (fn) { return subject.subscribe(fn); });
};
return Utils;
}());
Utils._uniqueNumber = 1;
Utils.nextUID = function () { return (Utils._uniqueNumber++).toString(); };
var hookType = {};
hookType.beforeAddingFile = 0;
hookType.afterAddingFile = 1;
hookType.errorAddingFile = 2;
hookType.prepareUploadFile = 3;
hookType.progressUploadFile = 4;
hookType.successUploadFile = 5;
hookType.completeUploadFile = 6;
hookType.failedUploadFile = 7;
hookType.cancelUploadFile = 8;
hookType.prepareUploadAll = 9;
hookType.progressUploadAll = 10;
hookType.completeUploadAll = 11;
hookType[hookType.beforeAddingFile] = "beforeAddingFile";
hookType[hookType.afterAddingFile] = "afterAddingFile";
hookType[hookType.errorAddingFile] = "errorAddingFile";
hookType[hookType.prepareUploadFile] = "prepareUploadFile";
hookType[hookType.progressUploadFile] = "progressUploadFile";
hookType[hookType.successUploadFile] = "successUploadFile";
hookType[hookType.completeUploadFile] = "completeUploadFile";
hookType[hookType.failedUploadFile] = "failedUploadFile";
hookType[hookType.cancelUploadFile] = "cancelUploadFile";
hookType[hookType.prepareUploadAll] = "prepareUploadAll";
hookType[hookType.progressUploadAll] = "progressUploadAll";
hookType[hookType.completeUploadAll] = "completeUploadAll";
var UploaderHook = /*@__PURE__*/(function () {
/**
* @param {?} _hookType
* @param {?} _callback
* @param {?=} _priority
*/
function UploaderHook(_hookType, _callback, _priority) {
if (_priority === void 0) { _priority = 0; }
this._type = null;
this._callback = null;
this._priority = null;
this._type = _hookType;
this._callback = _callback;
this._priority = +_priority;
}
Object.defineProperty(UploaderHook.prototype, "type", {
/**
* @return {?}
*/
get: function () {
return this._type;
},
enumerable: true,
configurable: true
});
Object.defineProperty(UploaderHook.prototype, "priority", {
/**
* @return {?}
*/
get: function () {
return this._priority;
},
enumerable: true,
configurable: true
});
Object.defineProperty(UploaderHook.prototype, "callback", {
/**
* @return {?}
*/
get: function () {
return this._callback;
},
enumerable: true,
configurable: true
});
return UploaderHook;
}());
// tslint:disable:no-unused-expression
var TransferOptionsDefault = {
url: '',
alias: 'file',
headers: {},
filters: [],
formData: [],
autoUpload: false,
method: 'POST',
removeBySuccess: false,
queueLimit: -1,
enableCors: false,
withCredentials: false,
uniqueFiles: false
};
/**
* An abstract class for the transport functionality
*
* @export
* @abstract
* \@class Transfer
* @abstract
*/
var Transfer = /*@__PURE__*/(function () {
/**
* Creates an instance of Transfer.
*
*
* \@memberOf Transfer
* @param {?} type
* @param {?=} _options
*/
function Transfer(type, _options) {
this.type = type;
this._queue$ = new BehaviorSubject([]);
var div = document.createElement('div');
this._isHTML5 = !!(File && FormData && FileReader);
this._isDragAndDrop = (('draggable' in div) || ('ondragstart' in div && 'ondrop' in div)) ? true : false;
this._id = Utils.uniqueID();
this._hooks = [];
if (!this._isHTML5) {
throw new Error("Your browser doesn't support HTML5. Our FileUploader can't work here.");
}
this.options = Object.assign({}, TransferOptionsDefault, _options);
}
Object.defineProperty(Transfer.prototype, "id", {
/**
* @return {?}
*/
get: function () {
return this._id;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Transfer.prototype, "queue$", {
/**
* @return {?}
*/
get: function () {
return Utils.asObservable(this._queue$);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Transfer.prototype, "queueObs", {
/**
* @return {?}
*/
get: function () {
return this._queue$.getValue();
},
enumerable: true,
configurable: true
});
/**
* Bind options to FileManager
*
*
* \@memberOf FileManager
* @param {?} _options
* @return {?}
*/
Transfer.prototype.bindOptions = function (_options) {
this.options = Object.assign({}, this.options, _options);
};
/**
* It gives HTML5 check back
*
*
* \@memberOf Transfer
* @return {?}
*/
Transfer.prototype.isHTML5 = function () {
return this._isHTML5;
};
/**
* It gives DragAndDrop check back
*
*
* \@memberOf Transfer
* @return {?}
*/
Transfer.prototype.isDragAndDrop = function () {
return this._isDragAndDrop;
};
/**
* To implement a hock
*
*
* \@memberOf Transfer
* @param {?} _hook
* @return {?}
*/
Transfer.prototype.hook = function (_hook) {
if (this.hookExists(_hook) === -1) {
this._hooks.push(_hook);
this._hooks.sort(function (a, b) {
if (!(a.type) || !(b.type)) {
return 0;
}
if (a.type !== b.type) {
if (a.type < b.type) {
return -1;
}
if (a.type > b.type) {
return 1;
}
return 0;
}
else {
if (!(a.priority) || !(b.priority)) {
return 0;
}
if (a.priority > b.priority) {
return -1;
}
if (a.priority < b.priority) {
return 1;
}
return 0;
}
});
}
};
/**
*
*
* \@memberOf Transfer
* @param {?} _hook
* @return {?} this
*
*/
Transfer.prototype.removeHook = function (_hook) {
var /** @type {?} */ key = this.hookExists(_hook);
if (key >= 0) {
this._hooks.splice(key, 1);
return true;
}
return false;
};
/**
* @param {?} _files
* @param {?=} options
* @return {?}
*/
Transfer.prototype.addFilesToQueue = function (_files, options) {
var /** @type {?} */ _retFiles = [];
var /** @type {?} */ _dummyFile;
var /** @type {?} */ _check = false;
if (Utils.isElement(_files)) {
// If _files was not converted
for (var _i = 0, _a = _files.files; _i < _a.length; _i++) {
var _fileElement = _a[_i];
try {
_dummyFile = new FileManager(_fileElement, options, this);
}
catch (e) {
throw Error("Couldn't create a new FileManager object.");
}
_check = this.addFile(_dummyFile);
(_check) && _retFiles.push(_dummyFile);
}
}
else if (_files instanceof Object) {
// If _files is an array of FileManger
if ((typeof _files[0] !== 'undefined') && (_files[0] instanceof FileManager)) {
for (var _b = 0, _files_1 = _files; _b < _files_1.length; _b++) {
var _file = _files_1[_b];
_check = this.addFile(_file);
(_check) && _retFiles.push(_file);
}
// If _files is only a FileManger
}
else if (_files instanceof FileManager) {
_check = this.addFile(_files);
(_check) && _retFiles.push(_files);
}
else {
throw new Error("Couldn't put file/files to the queue. [" + _files + "]");
}
}
else {
throw new Error("Couldn't initialise FileUploader file/files are not defined. [" + _files + "]");
}
this._onAddFileAll();
return _retFiles;
};
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype.addFile = function (_file) {
try {
this.validate(_file);
}
catch (e) {
console.log(e.message);
this._onAddFileError(_file);
return false;
}
var /** @type {?} */ queue = this._queue$.getValue();
if (this.options.uniqueFiles) {
if (this.notInQueue(_file) === -1) {
this._runHook(hookType.beforeAddingFile, _file);
queue.push(_file);
this._queue$.next(queue);
this._onAddFile(_file);
this._runHook(hookType.afterAddingFile, _file);
(this.options.autoUpload) && _file.upload();
return true;
}
}
else {
this._runHook(hookType.beforeAddingFile, _file);
queue.push(_file);
this._queue$.next(queue);
this._onAddFile(_file);
this._runHook(hookType.afterAddingFile, _file);
(this.options.autoUpload) && _file.upload();
return true;
}
this._onAddFileError(_file);
return false;
};
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype.removeFile = function (_file) {
var /** @type {?} */ key = this.notInQueue(_file);
var /** @type {?} */ queue = this._queue$.getValue();
if (key >= 0) {
queue.splice(key, 1);
this._queue$.next(queue);
return true;
}
return false;
};
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype.notInQueue = function (_file) {
var /** @type {?} */ queue = this._queue$.getValue();
for (var /** @type {?} */ key in queue) {
if (queue.hasOwnProperty(key)) {
var /** @type {?} */ obj = queue[key];
if ((obj.id === _file.id) ||
((obj.name + obj.type + obj.size) ===
(_file.name + _file.type + _file.size))) {
return +key;
}
}
}
return -1;
};
/**
* @param {?} _filter
* @return {?}
*/
Transfer.prototype.addFilter = function (_filter) {
if (this.filterExists(_filter) !== -1) {
((this.options.filters)).push(_filter);
}
};
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype.validate = function (_file) {
for (var _i = 0, _a = ((this.options.filters)); _i < _a.length; _i++) {
var _filter = _a[_i];
if (!_filter.validate(_file)) {
throw new Error("File [" + _file.name + "] doesn't fit with filter [" + _filter.name + "]");
}
}
return true;
};
/**
* @param {?} _protocol
* @return {?}
*/
Transfer.prototype._setProtocol = function (_protocol) {
this._protocol = _protocol;
};
/**
* @return {?}
*/
Transfer.prototype._getProtocol = function () {
return this._protocol;
};
/**
* Validate response status code.
*
*
* \@memberOf Protocol
* @param {?} status
* @return {?}
*/
Transfer.prototype._isSuccessCode = function (status) {
return (status >= 200 && status < 300) || status === 304;
};
/**
* Upload functions
* @return {?}
*/
Transfer.prototype.upload = function () {
this._onBeforeUploadAll();
for (var _i = 0, _a = this.queueObs; _i < _a.length; _i++) {
var _file = _a[_i];
this.uploadItem(_file);
}
};
/**
* @return {?}
*/
Transfer.prototype.cancel = function () {
for (var _i = 0, _a = this.queueObs; _i < _a.length; _i++) {
var _file = _a[_i];
this.cancelUploadItem(_file);
}
};
/**
* @return {?}
*/
Transfer.prototype.remove = function () {
for (var _i = 0, _a = this.queueObs; _i < _a.length; _i++) {
var _file = _a[_i];
this.removeFile(_file);
}
};
/**
* @param {?} item
* @return {?}
*/
Transfer.prototype.uploadItem = function (item) {
if (this.notInQueue(item) === -1) {
this.addFile(item);
}
this._onBeforeUpload(item);
this._getProtocol().run(item);
};
/**
* @param {?} item
* @return {?}
*/
Transfer.prototype.cancelUploadItem = function (item) {
if (this.notInQueue(item) === -1) {
return;
}
(item.isUploading) && this._getProtocol().cancel(item);
(item.isUploading) && item._cancel();
};
/**
* Overwrite functions
* @param {?} _uploader
* @return {?}
*/
Transfer.prototype.onAddFileAll = function (_uploader) { return; };
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype.onAddFile = function (_file) { return; };
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype.onAddFileError = function (_file) { return; };
/**
* @param {?} _uploader
* @return {?}
*/
Transfer.prototype.onBeforeUploadAll = function (_uploader) { return; };
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype.onBeforeUpload = function (_file) { return; };
/**
* @param {?} _uploader
* @param {?} _progress
* @return {?}
*/
Transfer.prototype.onProgress = function (_uploader, _progress) { return; };
/**
* @param {?} _file
* @param {?} _progress
* @return {?}
*/
Transfer.prototype.onProgressFile = function (_file, _progress) { return; };
/**
* @param {?} _file
* @param {?} _progress
* @return {?}
*/
Transfer.prototype.onProgressFileSpeed = function (_file, _progress) { return; };
/**
* @param {?} _file
* @param {?} _response
* @param {?} _status
* @param {?} _headers
* @return {?}
*/
Transfer.prototype.onSuccess = function (_file, _response, _status, _headers) { return; };
/**
* @param {?} _file
* @param {?} _response
* @param {?} _status
* @param {?} _headers
* @return {?}
*/
Transfer.prototype.onError = function (_file, _response, _status, _headers) { return; };
/**
* @param {?} _file
* @param {?} _response
* @param {?} _status
* @param {?} _headers
* @return {?}
*/
Transfer.prototype.onComplete = function (_file, _response, _status, _headers) { return; };
/**
* @param {?} _uploader
* @return {?}
*/
Transfer.prototype.onCompleteAll = function (_uploader) { return; };
;
/**
*
*
*
* \@memberOf FileManager
* @return {?}
*/
Transfer.prototype._onAddFileAll = function () {
this.onAddFileAll(this);
};
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype._onAddFile = function (_file) {
this.onAddFile(_file);
};
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype._onAddFileError = function (_file) {
this._runHook(hookType.errorAddingFile, _file);
this.onAddFileError(_file);
};
/**
* @return {?}
*/
Transfer.prototype._onBeforeUploadAll = function () {
this._runHook(hookType.prepareUploadAll, this);
this.onBeforeUploadAll(this);
};
/**
* @param {?} _file
* @return {?}
*/
Transfer.prototype._onBeforeUpload = function (_file) {
this._runHook(hookType.prepareUploadFile, _file);
_file._onBeforeUpload();
this.onBeforeUpload(_file);
};
/**
* @param {?} _file
* @param {?} _speed
* @return {?}
*/
Transfer.prototype._onProgressFileSpeed = function (_file, _speed) {
this._runHook(hookType.prepareUploadFile, _file, _speed);
_file._onProgressFileSpeed(_speed);
this.onProgressFileSpeed(_file, _speed);
};
/**
* @param {?} _file
* @param {?} _progress
* @return {?}
*/
Transfer.prototype._onProgressFile = function (_file, _progress) {
_file._onProgress(_progress);
this.onProgressFile(_file, _progress);
this._onProgress();
};
/**
* @return {?}
*/
Transfer.prototype._onProgress = function () {
var /** @type {?} */ queue = this._queue$.getValue();
if (queue.length > 0) {
var /** @type {?} */ percent = 0;
for (var _i = 0, queue_1 = queue; _i < queue_1.length; _i++) {
var file = queue_1[_i];
if (file.success || file.error) {
percent += 100;
}
else if (file.isUploading) {
percent += file.progress;
}
}
percent = Math.floor(percent / queue.length);
this._runHook(hookType.progressUploadAll, percent);
this.onProgress(this, percent);
(percent >= 100) && this._onCompleteAll();
return;
}
this.onProgress(this, 100);
this._onCompleteAll();
};
/**
* @param {?} _file
* @param {?} response
* @param {?} status
* @param {?} headers
* @return {?}
*/
Transfer.prototype._onSuccessFile = function (_file, response, status, headers) {
this._runHook(hookType.successUploadFile, _file, response, status, headers);
_file._onSuccess(response, status, headers);
this.onSuccess(_file, response, status, headers);
};
/**
* @param {?} _file
* @param {?} response
* @param {?} status
* @param {?} headers
* @return {?}
*/
Transfer.prototype._onErrorFile = function (_file, response, status, headers) {
this._runHook(hookType.failedUploadFile, _file, response, status, headers);
this.onError(_file, response, status, headers);
};
/**
* @param {?} _file
* @param {?} response
* @param {?} status
* @param {?} headers
* @return {?}
*/
Transfer.prototype._onCompleteFile = function (_file, response, status, headers) {
this._runHook(hookType.completeUploadFile, _file, response, status, headers);
this._onProgress();
this.onComplete(_file, response, status, headers);
};
/**
* @return {?}
*/
Transfer.prototype._onCompleteAll = function () {
this._runHook(hookType.completeUploadAll, this);
this.onCompleteAll(this);
};
/**
* @param {?} parsedHeaders
* @return {?}
*/
Transfer.prototype._headersGetter = function (parsedHeaders) {
return function (name) {
if (name) {
return parsedHeaders[name.toLowerCase()] || null;
}
return parsedHeaders;
};
};
/**
* @param {?} headers
* @return {?}
*/
Transfer.prototype._parseHeaders = function (headers) {
var /** @type {?} */ parsed = {};
var /** @type {?} */ key, /** @type {?} */ val, /** @type {?} */ i;
if (!headers) {
return parsed;
}
var /** @type {?} */ incomeHeaders = headers.split('\n');
for (var _i = 0, incomeHeaders_1 = incomeHeaders; _i < incomeHeaders_1.length; _i++) {
var line = incomeHeaders_1[_i];
i = line.indexOf(':');
key = line.slice(0, i).trim().toLowerCase();
val = line.slice(i + 1).trim();
if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
}
}
return parsed;
};
/**
* @param {?} response
* @param {?} headers
* @return {?}
*/
Transfer.prototype._transformResponse = function (response, headers) {
headers = {};
return response;
};
/**
* @param {?} type
* @param {...?} args
* @return {?}
*/
Transfer.prototype._runHook = function (type) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
for (var /** @type {?} */ key in this._hooks) {
if (this._hooks.hasOwnProperty(key)) {
var /** @type {?} */ obj = this._hooks[key];
if (obj.type === type) {
switch (type) {
case hookType.beforeAddingFile:
case hookType.prepareUploadAll:
case hookType.prepareUploadFile:
case hookType.afterAddingFile:
case hookType.errorAddingFile:
case hookType.completeUploadAll:
case hookType.progressUploadAll:
{
((obj.callback))(args[0]);
}
break;
case hookType.progressUploadFile:
{
((obj.callback))(args[0], args[1]);
}
break;
case hookType.cancelUploadFile:
case hookType.successUploadFile:
case hookType.failedUploadFile:
case hookType.completeUploadFile:
{
((obj.callback))(args[0], args[1], args[2], args[3]);
}
break;
default: {
throw new Error("hookType unknown or not defined");
}
}
}
}
}
};
/**
*
*
*
* \@memberOf Transfer
* @param {?} hook
* @return {?}
*/
Transfer.prototype.hookExists = function (hook) {
for (var /** @type {?} */ key in this._hooks) {
if (this._hooks.hasOwnProperty(key)) {
var /** @type {?} */ obj = this._hooks[key];
if ((obj.type === hook.type) && ('' + obj.callback === '' + hook.callback)) {
return +key;
}
}
}
return -1;
};
/**
* @param {?} _filter
* @return {?}
*/
Transfer.prototype.filterExists = function (_filter) {
var /** @type {?} */ filters = (this.options.filters);
for (var /** @type {?} */ key in filters) {
if (filters.hasOwnProperty(key)) {
var /** @type {?} */ obj = filters[key];
if (obj.name === _filter.name) {
return +key;
}
}
}
return -1;
};
return Transfer;
}());
// tslint:disable:no-unused-expression
var FileManagerOptionsDefault = {};
var speedObject = {
total: 0,
loaded: 0,
percent: 0,
speed: 0,
speedToText: '0 bytes'
};
var FileObject = /*@__PURE__*/(function () {
/**
* @param {?} fileOrInput
*/
function FileObject(fileOrInput) {
var _this = this;
var isInput = Utils.isElement(fileOrInput);
var fakePathOrObject = isInput ? fileOrInput.value : fileOrInput;
var isFakePath = Utils.isString(fakePathOrObject) ? true : false;
var method = function (v, x) {
if (v) {
_this._createFromFakePath(x);
}
else {
_this._createFromObject(x);
}
};
method(isFakePath, fakePathOrObject);
}
/**
* @param {?} path
* @return {?}
*/
FileObject.prototype._createFromFakePath = function (path) {
this.lastModifiedDate = null;
this.size = null;
this.type = 'like/' + path.slice(path.lastIndexOf('.') + 1).toLowerCase();
this.name = path.slice(path.lastIndexOf('/') + path.lastIndexOf('\\') + 2);
};
/**
* @param {?} object
* @return {?}
*/
FileObject.prototype._createFromObject = function (object) {
this.lastModifiedDate = new Date(object.lastModifiedDate.getTime());
this.size = object.size;
this.type = object.type;
this.name = object.name;
};
return FileObject;
}());
var FileManager = /*@__PURE__*/(function () {
/**
* Creates an instance of FileManager.
*
*
* \@memberOf FileManager
* @param {?} _file
* @param {?=} _options
* @param {?=} _uploader
*/
function FileManager(_file, _options, _uploader) {
this._progress$ = new BehaviorSubject(0);
this._speed$ = new BehaviorSubject(speedObject);
this.options = Object.assign({}, FileManagerOptionsDefault, _options);
this._speedDefault = {};
this._id = Utils.uniqueID();
this._isUploading = false;
this._isUploaded = false;
this._isSuccess = false;
this._isCancel = false;
this._isError = false;
var isInput = Utils.isElement(_file);
var file = isInput ? new FileObject(_file.files[0]) : new FileObject(_file);
this._file = file;
this._fileElement = isInput ? _file.files[0] : _file;
this._fileActive = false;
if (typeof _uploader !== 'undefined') {
this.bindUploader(_uploader);
}
}
Object.defineProperty(FileManager.prototype, "protocol", {
/**
* @param {?} _protocol
* @return {?}
*/
set: function (_protocol) {
this._protocol = _protocol;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "id", {
/**
* @return {?}
*/
get: function () {
return this._id;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "progressPercent$", {
/**
* @return {?}
*/
get: function () {
return Utils.asObservable(this._progress$);
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "progress$", {
/**
* @return {?}
*/
get: function () {
return Utils.asObservable(this._speed$);
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "progress", {
/**
* @return {?}
*/
get: function () {
return this._progress$.getValue();
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "element", {
/**
* @return {?}
*/
get: function () {
return this._fileElement;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "object", {
/**
* @return {?}
*/
get: function () {
return this._file;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "name", {
/**
* @return {?}
*/
get: function () {
return this._file.name;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "type", {
/**
* @return {?}
*/
get: function () {
return this._file.type;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "date", {
/**
* @return {?}
*/
get: function () {
return this._file.lastModifiedDate;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "size", {
/**
* @return {?}
*/
get: function () {
return this._file.size;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "inQueue", {
/**
* @return {?}
*/
get: function () {
return this._fileActive;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "success", {
/**
* @return {?}
*/
get: function () {
return this._isSuccess;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FileManager.prototype, "error", {
/**
* @return {?}
*/
get: function () {
return this._isError;
},
enumerable: true,
configurable: true
});
/**
* Bind uploader to FileManager
*
*
* \@memberOf FileManager
* @param {?} _uploader
* @return {?}
*/
FileManager.prototype.bindUploader = function (_uploader) {
if (this._uploader instanceof Transfer) {
this._uploader.removeFile(this);
}
this._uploader = _uploader;
var /** @type {?} */ check = this._uploader.addFile(this);
this._setFileActive(check);
};
/**
* Bind options to FileManager
*
*
* \@memberOf FileManager
* @param {?} _options
* @return {?}
*/
FileManager.prototype.bindOptions = function (_options) {
this.options = Object.assign({}, this.options, _options);
};
/**
* Return uploader if exists else throw error
*
*
* \@memberOf FileManager
* @return {?}
*/
FileManager.prototype.getUploader = function () {
if (this._uploader instanceof Transfer) {
return this._uploader;
}
throw new Error('Not uploader for this file defined.');
};
/**
* Start uploading this file
*
*
* \@memberOf FileManager
* @return {?}
*/
FileManager.prototype.upload = function () {
var /** @type {?} */ _uploader;
try {
_uploader = this.getUploader();
}
catch (e) {
throw new Error('Couldn`t upload because uploader was not defined. ERR_MSG: ' + ((e)).message);
}
this._isUploading = true;
try {
_uploader.uploadItem(this);
}
catch (e) {
// TODO write error handling
}
};
/**
* Cancel upload process from this file
*
*
* \@memberOf FileManager
* @return {?}
*/
FileManager.prototype.cancel = function () {
if (this._isUploading) {
var /** @type {?} */ uploader = this.getUploader();
uploader.cancelUploadItem(this);
}
};
/**
* @return {?}
*/
FileManager.prototype._cancel = function () {
if (this._isUploading) {
this._isCancel = true;
this._isUploaded = false;
this._isUploading = false;
}
};
/**
* Remove this FileManger from uploader queue
*
*
* \@memberOf FileManager
* @return {?}
*/
FileManager.prototype.remove = function () {
var /** @type {?} */ _uploader;
try {
_uploader = this.getUploader();
}
catch (e) {
throw new Error('Couldn`t remove because uploader was not defined. ERR_MSG: ' + ((e)).message);
}
(this._isUploading) && this.cancel();
_uploader.removeFile(this);
this._setFileActive(false);
};
/**
* @return {?}
*/
FileManager.prototype.isUploaded = function () {
return this._isUploaded;
};
/**
* @return {?}
*/
FileManager.prototype.isUploading = function () {
return this._isUploading;
};
/**
* @return {?}
*/
FileManager.prototype.canUpload = function () {
return (!this._isUploaded && !this._isUploading && !this._isSuccess && !this._isError);
};
/**
* @return {?}
*/
FileManager.prototype.handleImageLoad = function () {
this._imageLoad = true;
};
/**
* Callback
* @return {?}
*/
FileManager.prototype.onBeforeUpload = function () { return; };
/**
* @param {?} speed
* @return {?}
*/
FileManager.prototype.onProgressSpeed = function (speed) { speed = speed; };
/**
* Callback
* @param {?} progress
* @return {?}
*/
FileManager.prototype.onProgress = function (progress) { progress = progress; };
/**
* Callback
* @param {?} response
* @param {?} status
* @param {?} headers
* @return {?}
*/
FileManager.prototype.onSuccess = function (response, status, headers) { response = response; status = status; headers = headers; };
/**
* @param {?} response
* @param {?} status
* @param {?} headers
* @return {?}
*/
FileManager.prototype.onError = function (response, status, headers) { response = response; status = status; headers = headers; };
/**
* Internal functions
* @return {?}
*/
FileManager.prototype._onBeforeUpload = function () {
this._isUploading = true;
this._isUploaded = false;
this._isSuccess = false;
this._isCancel = false;
this._isError = false;
this._progress$.next(0);
this.onBeforeUpload();
};
/**
* @param {?} speed
* @return {?}
*/
FileManager.prototype._onProgressFileSpeed = function (speed) {
this._speed$.next(speed);
this.onProgressSpeed(speed);
};
/**
* @param {?} _progress
* @return {?}
*/
FileManager.prototype._onProgress = function (_progress) {
this._progress$.next(_progress);
this.onProgress(_progress);
};
/**
* @param {?} response
* @param {?} status
* @param {?} headers
* @return {?}
*/
FileManager.prototype._onSuccess = function (response, status, headers) {
if (this._uploader.options.removeBySuccess) {
this.remove();
}
this._isUploading = false;
this._isUploaded = true;
this._isSuccess = true;
this._isError = false;
this.onSuccess(response, status, headers);
};
/**
* @param {?} response
* @param {?} status
* @param {?} headers
* @return {?}
*/
FileManager.prototype._onError = function (response, status, headers) {
this._isUploading = false;
this._isUploaded = false;
this._isSuccess = false;
this._isError = true;
this.onError(response, status, headers);
};
/**
* @param {?} check
* @return {?}
*/
FileManager.prototype._setFileActive = function (check) {
this._fileActive = check;
};
return FileManager;
}());
// tslint:disable:max-line-length
// tslint:disable:no-unused-expression
var FileDropDirective = /*@__PURE__*/(function () {
/**
* @param {?} element
* @param {?} renderer
*/
function FileDropDirective(element, renderer) {
this.element = element;
this.renderer = renderer;
this.fileHoverStart = new EventEmitter();
this.fileHoverEnd = new EventEmitter();
this.fileAccepted = new EventEmitter();
this.fileRejected = new EventEmitter();
this._InputFile = null;
}
/**
* @return {?}
*/
FileDropDirective.prototype.ngOnInit = function () {
this._files = [];
this.renderer.setElementClass(this.element.nativeElement, 'drop-area', true);
};
/**
* @return {?}
*/
FileDropDirective.prototype.ngOnDestroy = function () {
if (this._files.length > 0) {
for (var _i = 0, _a = this._files; _i < _a.length; _i++) {
var _file = _a[_i];
if (_file instanceof FileManager) {
if (!_file.isUploaded()) {
_file.cancel();
this.uploader.removeFile(_file);
}
}
}
}
};
/**
* @param {?} event
* @return {?}
*/
FileDropDirective.prototype.onDragOver = function (event) {
// If we're already in the on-drag, don't bother with this
if (this._InputFile === null) {
// Get the object being dragged and reference it as a copy action
this._InputFile = this.getDataTransferObject(event);
if (this._InputFile === null) {
return;
}
this.renderer.setElementClass(this.element.nativeElement, 'drop-enter', true);
var /** @type {?} */ _dropValueText = 'Drop now';
this.renderer.setElementAttribute(this.element.nativeElement, 'dropValueText', _dropValueText);
// Let the client know
this.fileHoverStart.emit();
}
// Don't propagate
this.preventAndStopEventPropagation(event);
};
/**
* @param {?} event
* @return {?}
*/
FileDropDirective.prototype.onDragLeave = function (event) {
// Only bother if we have a file
if (this._InputFile !== null) {
this.renderer.setElementClass(this.element.nativeElement, 'drop-enter', false);
this.renderer.setElementAttribute(this.element.nativeElement, 'dropValueText', '');
// Finished with the file
this._InputFile = null;
if (event.currentTarget === ((this)).element[0]) {
return;
}
// Let the client know
this.fileHoverEnd.emit();
}
// Don't let it continue
this.preventAndStopEventPropagation(event);
};
/**
* @param {?} event
* @return {?}
*/
FileDropDirective.prototype.onDrop = function (event) {
// Only bother if we have a file
if (this._InputFile !== null) {
this.renderer.setElementClass(this.element.nativeElement, 'drop-enter', false);
this.renderer.setElementAttribute(this.element.nativeElement, 'dropValueText', '');
// Let the client know
this.fileHoverEnd.emit();
// Update our data
this._InputFile = this.getDataTransferObject(event);
if (this._InputFile.files.length === 0) {
this._InputFile = null;
return;
}
var /** @type {?} */ filesData = this._InputFile.files;
if (!this.hasFiles(this._InputFile.types)) {
return;
}
this.readFile(filesData);
this.fileAccepted.emit(this._files);
// Finished with the file
this._InputFile = null;
}
// Don't let it continue
this.preventAndStopEventPropagation(event);
};
/**
* @param {?} event
* @return {?}
*/
FileDropDirective.prototype.preventAndStopEventPropagation = function (event) {
event.preventDefault();
event.stopPropagation();
};
/**
* @param {?} _files
* @return {?}
*/
FileDropDirective.prototype.readFile = function (_files) {
var /** @type {?} */ fmObject;
for (var /** @type {?} */ i = 0; i < _files.length; i++) {
var /** @type {?} */ file = _files[i];
try {
fmObject = new FileManager(file, this.fileOptions, this.uploader);
}
catch (e) {
if (e.status === 100) {
this.fileRejected.emit(e);
}
else {
this.fileRejected.emit(e);
}
throw new Error('Something goes wrong e: ' + e.message);
}
(fmObject.inQueue) && this._files.push(fmObject);
}
};
/**
* @param {?} event
* @return {?}
*/
FileDropDirective.prototype.getDataTransferObject = function (event) {
return event.dataTransfer ? event.dataTransfer : event.originalEvent.dataTransfer;
};
/**
* @param {?} types
* @return {?}
*/
FileDropDirective.prototype.hasFiles = function (types) {
if (!types) {
return false;
}
if (types.indexOf) {
return types.indexOf('Files') !== -1;
}
if (types.contains) {
return types.contains('Files');
}
return false;
};
return FileDropDirective;
}());
FileDropDirective.decorators = [
{ type: Directive, args: [{
// Selector required in component HTML
// tslint:disable-next-line:directive-selector
selector: '[ng2File2Drop]'
},] },
];
/**
* @nocollapse
*/
FileDropDirective.ctorParameters = function () { return [
{ type: ElementRef, },
{ type: Renderer, },
]; };
FileDropDirective.propDecorators = {
'fileHoverStart': [{ type: Output },],
'fileHoverEnd': [{ type: Output },],
'fileAccepted': [{ type: Output },],
'fileRejected': [{ type: Output },],
'fileOptions': [{ type: Input },],
'uploader': [{ type: Input },],
'onDragOver': [{ type: HostListener, args: ['dragover', ['$event'],] },],
'onDragLeave': [{ type: HostListener, args: ['dragleave', ['$event'],] },],
'onDrop': [{ type: HostListener, args: ['drop', ['$event'],] },],
};
// tslint:disable:max-line-length
// tslint:disable:no-unused-expression
var FileSelectDirective = /*@__PURE__*/(function () {
function FileSelectDirective() {
this.role = 'input';
this.type = 'file';
this.fileHoverStart = new EventEmitter();
this.fileHoverEnd = new EventEmitter();
this.fileAccepted = new EventEmitter();
this.fileRejected = new EventEmitter();
this._InputFile = null;
}
/**
* @return {?}
*/
FileSelectDirective.prototype.ngOnInit = function () {
this._files = [];
};
/**
* @return {?}
*/
FileSelectDirective.prototype.ngOnDestroy = function () {
if (this._files.length > 0) {
for (var _i = 0, _a = this._files; _i < _a.length; _i++) {
var _file = _a[_i];
if (_file instanceof FileManager) {
if (!_file.isUploaded()) {
_file.cancel();
this.uploader.removeFile(_file);
}
}
}
}
};
/**
* @param {?} event
* @return {?}
*/
FileSelectDirective.prototype.onChange = function (event) {
// Update our data
this._InputFile = this.getEventTarget(event);
if (this._InputFile.files.length === 0) {
this._InputFile = null;
return;
}
var /** @type {?} */ filesData = this._InputFile.files;
console.log('onChange', filesData);
this.readFile(filesData);
this.fileAccepted.emit(this._files);
// Finished with the file
this._InputFile = null;
// Don't let it continue
this.preventAndStopEventPropagation(event);
};
/**
* @param {?} event
* @return {?}
*/
FileSelectDirective.prototype.preventAndStopEventPropagation = function (event) {
event.preventDefault();
event.stopPropagation();
};
/**
* @param {?} _files
* @return {?}
*/
FileSelectDirective.prototype.readFile = function (_files) {
var /** @type {?} */ fmObject;
for (var /** @type {?} */ i = 0; i < _files.length; i++) {
var /** @type {?} */ file = _files[i];
try {
fmObject = new FileManager(file, this.fileOptions, this.uploader);
}
catch (e) {
if (e.status === 100) {
this.fileRejected.emit(e);
}
else {
this.fileRejected.emit(e);
}
throw new Error('Something goes wrong e: ' + e.message);
}
(fmObject.inQueue) && this._files.push(fmObject);
}
};
/**
* @param {?} event
* @return {?}
*/
FileSelectDirective.prototype.getEventTarget = function (event) {
return event.target;
};
return FileSelectDirective;
}());
FileSelectDirective.decorators = [
{ type: Directive, args: [{
// Selector required in component HTML
// tslint:disable-next-line:directive-selector
selector: '[ng2File2Select]'
},] },
];
/**
* @nocollapse
*/
FileSelectDirective.ctorParameters = function () { return []; };
FileSelectDirective.propDecorators = {
'role': [{ type: HostBinding, args: ['attr.role',] },],
'type': [{ type: HostBinding, args: ['attr.type',] },],
'fileHoverStart': [{ type: Output },],
'fileHoverEnd': [{ type: Output },],
'fileAccepted': [{ type: Output },],
'fileRejected': [{ type: Output },],
'fileOptions': [{ type: Input },],
'uploader': [{ type: Input },],
'onChange': [{ type: HostListener, args: ['change', ['$event'],] },],
};
var FileReader$1 = ((window)).FileReader;
var ImagePreviewDirective = /*@__PURE__*/(function () {
/**
* @param {?} el
*/
function ImagePreviewDirective(el) {
this.el = el;
}
/**
* @return {?}
*/
ImagePreviewDirective.prototype.ngOnChanges = function () {
var /** @type {?} */ reader = new FileReader$1();
var /** @type {?} */ el = this.el;
// tslint:disable-next-line:max-line-length
el.nativeElement.src = 'data:image/svg+xml;charset=utf-8,%3Csvg xmlns%3D\'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg\' viewBox%3D\'0 0 200 150\'%2F%3E';
reader.onloadend = function () {
el.nativeElement.src = reader.result;
};
if (this.image) {
return reader.readAsDataURL(this.image.element);
}
};
return ImagePreviewDirective;
}());
ImagePreviewDirective.decorators = [
{ type: Directive, args: [{ selector: 'img[imgPreview]' },] },
];
/**
* @nocollapse
*/
ImagePreviewDirective.ctorParameters = function () { return [
{ type: ElementRef, },
]; };
ImagePreviewDirective.propDecorators = {
'image': [{ type: Input },],
};
var FileSizePipe = /*@__PURE__*/(function () {
function FileSizePipe() {
this.units = [