UNPKG

innotec-materials

Version:

Innotec Materials is an UI framework who's desgined for all products of Innotec GmbH. Innotec Materials is based on Angular 2 an bootstrap.

365 lines 15.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var file_like_object_class_1 = require("./file-like-object.class"); var file_item_class_1 = require("./file-item.class"); var file_type_class_1 = require("./file-type.class"); function isFile(value) { return (File && value instanceof File); } var FileUploader = (function () { function FileUploader(options) { this.isUploading = false; this.queue = []; this.progress = 0; this._nextIndex = 0; this.options = { autoUpload: false, isHTML5: true, filters: [], topics: [], removeAfterUpload: false }; this.setOptions(options); } FileUploader.prototype.setOptions = function (options) { this.options = Object.assign(this.options, options); this.topics = options.topics; this.authToken = options.authToken; this.autoUpload = options.autoUpload; this.options.filters.unshift({ name: 'queueLimit', fn: this._queueLimitFilter }); if (this.options.maxFileSize) { this.options.filters.unshift({ name: 'fileSize', fn: this._fileSizeFilter }); } if (this.options.allowedFileType) { this.options.filters.unshift({ name: 'fileType', fn: this._fileTypeFilter }); } if (this.options.allowedMimeType) { this.options.filters.unshift({ name: 'mimeType', fn: this._mimeTypeFilter }); } }; FileUploader.prototype.addToQueue = function (files, options, filters) { var _this = this; var list = []; for (var _i = 0, files_1 = files; _i < files_1.length; _i++) { var file = files_1[_i]; list.push(file); } var arrayOfFilters = this._getFilters(filters); var count = this.queue.length; var addedFileItems = []; list.map(function (some) { if (!options) { options = _this.options; } var temp = new file_like_object_class_1.FileLikeObject(some); if (_this._isValidFile(temp, arrayOfFilters, options)) { var fileItem = new file_item_class_1.FileItem(_this, some, options); addedFileItems.push(fileItem); _this.queue.push(fileItem); _this._onAfterAddingFile(fileItem); } else { var filter = arrayOfFilters[_this._failFilterIndex]; _this._onWhenAddingFileFailed(temp, filter, options); } }); if (this.queue.length !== count) { this._onAfterAddingAll(addedFileItems); this.progress = this._getTotalProgress(); } this._render(); if (this.options.autoUpload) { this.uploadAll(); } }; FileUploader.prototype.removeFromQueue = function (value) { var index = this.getIndexOfItem(value); var item = this.queue[index]; if (item.isUploading) { item.cancel(); } this.queue.splice(index, 1); this.progress = this._getTotalProgress(); }; FileUploader.prototype.clearQueue = function () { while (this.queue.length) { this.queue[0].remove(); } this.progress = 0; }; FileUploader.prototype.uploadItem = function (value) { var index = this.getIndexOfItem(value); var item = this.queue[index]; var transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport'; item._prepareToUploading(); if (this.isUploading) { return; } this.isUploading = true; this[transport](item); }; FileUploader.prototype.cancelItem = function (value) { var index = this.getIndexOfItem(value); var item = this.queue[index]; var prop = this.options.isHTML5 ? '_xhr' : '_form'; if (item && item.isUploading) { item[prop].abort(); } }; FileUploader.prototype.uploadAll = function () { var items = this.getNotUploadedItems().filter(function (item) { return !item.isUploading; }); if (!items.length) { return; } items.map(function (item) { return item._prepareToUploading(); }); items[0].upload(); }; FileUploader.prototype.cancelAll = function () { var items = this.getNotUploadedItems(); items.map(function (item) { return item.cancel(); }); }; FileUploader.prototype.isFile = function (value) { return isFile(value); }; FileUploader.prototype.isFileLikeObject = function (value) { return value instanceof file_like_object_class_1.FileLikeObject; }; FileUploader.prototype.getIndexOfItem = function (value) { return typeof value === 'number' ? value : this.queue.indexOf(value); }; FileUploader.prototype.getNotUploadedItems = function () { return this.queue.filter(function (item) { return !item.isUploaded; }); }; FileUploader.prototype.getReadyItems = function () { return this.queue .filter(function (item) { return (item.isReady && !item.isUploading); }) .sort(function (item1, item2) { return item1.index - item2.index; }); }; FileUploader.prototype.destroy = function () { return void 0; }; FileUploader.prototype.onAfterAddingAll = function (fileItems) { return { fileItems: fileItems }; }; FileUploader.prototype.onBuildItemForm = function (fileItem, form) { return { fileItem: fileItem, form: form }; }; FileUploader.prototype.onAfterAddingFile = function (fileItem) { return { fileItem: fileItem }; }; FileUploader.prototype.onWhenAddingFileFailed = function (item, filter, options) { return { item: item, filter: filter, options: options }; }; FileUploader.prototype.onBeforeUploadItem = function (fileItem) { return { fileItem: fileItem }; }; FileUploader.prototype.onProgressItem = function (fileItem, progress) { return { fileItem: fileItem, progress: progress }; }; FileUploader.prototype.onProgressAll = function (progress) { return { progress: progress }; }; FileUploader.prototype.onSuccessItem = function (item, response, status, headers) { return { item: item, response: response, status: status, headers: headers }; }; FileUploader.prototype.onErrorItem = function (item, response, status, headers) { return { item: item, response: response, status: status, headers: headers }; }; FileUploader.prototype.onCancelItem = function (item, response, status, headers) { return { item: item, response: response, status: status, headers: headers }; }; FileUploader.prototype.onCompleteItem = function (item, response, status, headers) { return { item: item, response: response, status: status, headers: headers }; }; FileUploader.prototype.onCompleteAll = function () { return void 0; }; FileUploader.prototype._mimeTypeFilter = function (item) { return !(this.options.allowedMimeType && this.options.allowedMimeType.indexOf(item.type) === -1); }; FileUploader.prototype._fileSizeFilter = function (item) { return !(this.options.maxFileSize && item.size > this.options.maxFileSize); }; FileUploader.prototype._fileTypeFilter = function (item) { return !(this.options.allowedFileType && this.options.allowedFileType.indexOf(file_type_class_1.FileType.getMimeClass(item)) === -1); }; FileUploader.prototype._onSuccessItem = function (item, response, status, headers) { item._onSuccess(response, status, headers); this.onSuccessItem(item, response, status, headers); }; FileUploader.prototype._onErrorItem = function (item, response, status, headers) { item._onError(response, status, headers); this.onErrorItem(item, response, status, headers); }; FileUploader.prototype._onCompleteItem = function (item, response, status, headers) { item._onComplete(response, status, headers); this.onCompleteItem(item, response, status, headers); var nextItem = this.getReadyItems()[0]; this.isUploading = false; if (nextItem) { nextItem.upload(); return; } this.onCompleteAll(); this.progress = this._getTotalProgress(); this._render(); }; FileUploader.prototype._headersGetter = function (parsedHeaders) { return function (name) { if (name) { return parsedHeaders[name.toLowerCase()] || void 0; } return parsedHeaders; }; }; FileUploader.prototype._xhrTransport = function (item) { var _this = this; var xhr = item._xhr = new XMLHttpRequest(); var reader = new FileReader(); window.localStorage.setItem('temp', JSON.stringify(this.topics)); reader.readAsBinaryString(item._file); this._onBeforeUploadItem(item); if (typeof item._file.size !== 'number') { throw new TypeError('The file specified is no longer valid'); } xhr.upload.onprogress = function (event) { var progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0); _this._onProgressItem(item, progress); }; xhr.onload = function () { var headers = _this._parseHeaders(xhr.getAllResponseHeaders()); var response = xhr.response; var gist = _this._isSuccessCode(xhr.status) ? 'Success' : 'Error'; var method = '_on' + gist + 'Item'; _this[method](item, response, xhr.status, headers); _this._onCompleteItem(item, response, xhr.status, headers); }; xhr.onerror = function () { var headers = _this._parseHeaders(xhr.getAllResponseHeaders()); var response = xhr.response; _this._onErrorItem(item, response, xhr.status, headers); _this._onCompleteItem(item, response, xhr.status, headers); }; xhr.onabort = function () { var headers = _this._parseHeaders(xhr.getAllResponseHeaders()); var response = xhr.response; _this._onCancelItem(item, response, xhr.status, headers); _this._onCompleteItem(item, response, xhr.status, headers); }; xhr.open(item.method, item.url, true); item.withCredentials = false; if (this.options.headers) { for (var _i = 0, _a = this.options.headers; _i < _a.length; _i++) { var header = _a[_i]; xhr.setRequestHeader(header.name, header.value); } } if (this.authToken) { xhr.setRequestHeader('Authorization', this.authToken); } xhr.setRequestHeader('Content-Type', 'application/json'); reader.onload = function (readerEvt) { var binaryString = readerEvt.target.result; var content = btoa(binaryString); var topics = JSON.parse(window.localStorage.getItem('temp')); window.localStorage.removeItem('temp'); for (var key in topics) { if (topics[key].contextid === 100) { var res = topics[key].value; topics[key].value = res.split('*/*').join(' '); } } var request = { filename: item.file.name, content: content, topics: topics }; xhr.send(JSON.stringify(request)); }; }; FileUploader.prototype._getTotalProgress = function (value) { if (value === void 0) { value = 0; } if (this.options.removeAfterUpload) { return value; } var notUploaded = this.getNotUploadedItems().length; var uploaded = notUploaded ? this.queue.length - notUploaded : this.queue.length; var ratio = 100 / this.queue.length; var current = value * ratio / 100; return Math.round(uploaded * ratio + current); }; FileUploader.prototype._getFilters = function (filters) { if (!filters) { return this.options.filters; } if (Array.isArray(filters)) { return filters; } if (typeof filters === 'string') { var names_1 = filters.match(/[^\s,]+/g); return this.options.filters .filter(function (filter) { return names_1.indexOf(filter.name) !== -1; }); } return this.options.filters; }; FileUploader.prototype._render = function () { return void 0; }; FileUploader.prototype._queueLimitFilter = function () { return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit; }; FileUploader.prototype._isValidFile = function (file, filters, options) { var _this = this; this._failFilterIndex = -1; return !filters.length ? true : filters.every(function (filter) { _this._failFilterIndex++; return filter.fn.call(_this, file, options); }); }; FileUploader.prototype._isSuccessCode = function (status) { return (status >= 200 && status < 300) || status === 304; }; FileUploader.prototype._parseHeaders = function (headers) { var parsed = {}; var key; var val; var i; if (!headers) { return parsed; } headers.split('\n').map(function (line) { 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; }; FileUploader.prototype._onWhenAddingFileFailed = function (item, filter, options) { this.onWhenAddingFileFailed(item, filter, options); }; FileUploader.prototype._onAfterAddingFile = function (item) { this.onAfterAddingFile(item); }; FileUploader.prototype._onAfterAddingAll = function (items) { this.onAfterAddingAll(items); }; FileUploader.prototype._onBeforeUploadItem = function (item) { item._onBeforeUpload(); this.onBeforeUploadItem(item); }; FileUploader.prototype._onProgressItem = function (item, progress) { var total = this._getTotalProgress(progress); this.progress = total; item._onProgress(progress); this.onProgressItem(item, progress); this.onProgressAll(total); this._render(); }; FileUploader.prototype._onCancelItem = function (item, response, status, headers) { item._onCancel(response, status, headers); this.onCancelItem(item, response, status, headers); }; return FileUploader; }()); exports.FileUploader = FileUploader; //# sourceMappingURL=file-uploader.class.js.map