(function () { 'use strict'; angular.module('app.controller').controller('BaseUploadController', BaseUploadController); BaseUploadController.$inject = ['$scope', '$q', '$upload', 'cfg', 'apiUrl']; function BaseUploadController($scope, $q, $upload, cfg, apiUrl) { cfg.debug && console.log('BaseUploadController'); $scope.upload = this; /** * @abstract * @return {String} */ this.getControllerAction = function () { throw new Error('Method BaseUploadController.getControllerAction should be overwritten'); }; /** * @param {Number} index * @return {Object} */ this.getRequest = function (index) { return { // headers: {'key': 'value'}, // data: { // key: 'value' // } }; }; this.autoStart = false; /** * List of selected files * @type {Array.} */ this.listSelected = []; /** * Progress of each upload * @type {Array.} ex. -1..100 */ this.progress = []; /** * List of http promises for uploads with additional method .abort() * Filled after upload starts * @type {Array.} */ this.list = []; /** * List of responses for each upload * Filled after upload finished * @type {Array} */ this.listResult = []; /** * Indicates that all selected files proceeded * @readonly * @type {boolean} */ this.complete = true; this.refreshComplete = function () { this.complete = (this.listSelected.filter(function (v) { return !!v; }).length === this.listResult.length); }; /** * ex.: ng-file-select="upload.onFileSelect($files)" * @param {Array.} $files */ this.onFileSelect = function ($files) { var i; this.listSelected = []; this.progress = []; if (this.list && this.list.length > 0) { for (i = 0; i < this.list.length; i++) { if (this.list[i] != null) { this.list[i].abort(); } } } this.list = []; this.listResult = []; this.listSelected = $files; this.refreshComplete(); for (i = 0; i < $files.length; i++) { this.progress[i] = -1; if (this.autoStart) { this.start(i); } } }; this.start = function (index) { return this.upload(index); }; this.remove = function (index) { if (this.list[index]) { this.abort(index); } if (this.listSelected[index]) { this.listSelected[index] = null; } this.refreshComplete(); }; this.abort = function (index) { if (this.list[index]) { this.list[index].abort(); } this.list[index] = null; this.progress[index] = -1; this.listResult[index] = false; this.refreshComplete(); }; this.upload = function (index) { var deferred = $q.defer(); this.progress[index] = 0; this.list[index] = $upload.upload(angular.extend({ url: apiUrl + '/' + this.getControllerAction(), method: 'post', file: this.listSelected[index] }, this.getRequest(index)) ).then(this.onDone.bind(this, index, deferred), this.onFail.bind(this, index, deferred), this.onNotify.bind(this, index)); this.refreshComplete(); return deferred.promise; }; this.onDone = function (index, deferred, response) { this.list[index] = null; this.listResult[index] = response.data; this.refreshComplete(); deferred.resolve(response); }; this.onFail = function (index, deferred, response) { this.list[index] = null; this.listResult[index] = false; this.refreshComplete(); deferred.reject(response); }; this.onNotify = function (index, e) { this.progress[index] = parseInt(100.0 * e.loaded / e.total); }; } })();