(function () { 'use strict'; /** * @typedef {Array.} FileList */ /** * @typedef {Object} FileListItem * @property {String} name Display Name * @property {?Number} size File Size in bytes * @property {?String} fileUrl File url * @property {?String} thumbnailUrl Image thumbnail url */ var name = 'adElementFile'; angular.module('app.directive') .directive('adElementFile', ['$parse', '$controller', '$q', '$timeout', 'cfg', 'notification', 'translate', function ($parse, $controller, $q, $timeout, cfg, notification, translate) { return { templateUrl: 'directive/' + name + '/' + name + '.html', // ad-element-file-api="controller/action" // {String} // ad-element-file-request="getRequest" // {Function} // ad-element-file-on-done="onDone" // {?Function(object)} // ad-element-file-on-done="onFail" // {?Function(object)} // ad-element-file-on-save="onFileMataSave" // {?Function(number, object): promise} // ad-element-file-on-reorder="onReorder(index)" // {?Function(array.)} // ad-element-file-on-delete="onDelete(index)" // {?Function(number, object)} scope: { list: '=' + name, // ad-element-file="fileList" // ={FileList} name: '@' + name + 'Name', // ad-element-file-name="file" // @{String} multi: '@' + name + 'Multi' // ad-element-file-multi="false" // @{Boolean=} }, require: '^form', link: function (scope, element, attr, ctrl) { cfg.debug && console.log(name); var getAttrReflection = function (name, defValue) { var expr = (attr[name]) ? $parse(attr[name]) : null; if (expr || undefined !== defValue) { return function (locals) { return expr ? expr(scope.$parent, locals) : defValue; }; } return null; }; var attrApi = getAttrReflection(name + 'Api'); var attrRequest = getAttrReflection(name + 'Request', {}); var attrOnDone = getAttrReflection(name + 'OnDone', angular.noop); var attrOnFail = getAttrReflection(name + 'OnFail', angular.noop); var attrOnSave = getAttrReflection(name + 'OnSave'); var attrOnReorder = getAttrReflection(name + 'OnReorder'); var attrOnDelete = getAttrReflection(name + 'OnDelete'); var uploadController = $controller('BaseUploadController', {$scope: scope}); uploadController.getControllerAction = function () { return attrApi(); }; uploadController.getRequest = function () { return attrRequest()(); }; scope.ctrl = ctrl; scope.uploadStart = function (index) { cfg.debug && console.log(name, '.uploadStart'); uploadController.upload(index).then(function (data) { var response = data.data; response.message && notification.temporary(response.message); uploadController.remove(index); return response; }, function (data) { var response = data.data; cfg.debug && console.warn(name + ': Request Error: ', arguments); var msg = translate('Request failed'); if (response) { msg = response.message ? response.message : (angular.isString(response) && response.length < 250 ? response : msg); } response = {message: msg}; response.message && notification.temporary(response.message, 'warning'); uploadController.abort(index); return $q.reject(response); }).then(attrOnDone(), attrOnFail()); }; scope.fileDelete = attrOnDelete && function (index) { attrOnDelete({index: index, file: scope.list[index]}); }; scope.sortable = false; if (attrOnReorder) { scope.sortable = true; scope.sortableOptions = { stop: function (e, ui) { cfg.debug && console.log(name, '.onReorder'); attrOnReorder({ list: scope.list, index: ui.item.sortable.index, dropindex: ui.item.sortable.dropindex }); } }; } if (scope.multi) { $timeout(function () { var input; if ((input = element[0].querySelector('[type="file"]'))) { input.multiple = true; } }); } scope.editable = false; if (attrOnSave) { scope.editable = true; scope.validateName = function (name) { // return 'error'; }; scope.saveMeta = function (index, fileForm) { cfg.debug && console.log(name, '.saveMeta', index, scope.list[index]); // fixme: error messages does not shows at form return attrOnSave({index: index, file: scope.list[index]}).then(null, function (response) { if (response.errors && fileForm) { angular.forEach(response.errors, function (errors, name) { fileForm.$setError(name, String(errors)); }); } return $q.reject(response); }); }; } } }; }]); })();