(function () { 'use strict'; var name = 'adAdminTicketFileForm'; angular.module('app.directive') .directive('adAdminTicketFileForm', ['$controller', 'cfg', 'adminTicketBridge', 'staticData', function ($controller, cfg, adminTicketBridge, staticData) { return { templateUrl: 'directive/' + name + '/' + name + '.html', scope: {}, controller: 'BaseFormController', link: function (scope, element, attr, controller) { cfg.debug && console.log(name); controller.bridge = adminTicketBridge; scope.uploadApiUrl = adminTicketBridge.controller + '/upload'; scope.uploadRequest = function () { if (scope.data && scope.data.id) { return { data: { id: scope.data.id } }; } return {}; }; scope.uploadOnDone = function (response) { scope.data.fileId = response.fileId; if (scope.data.id) { controller.action('imageList', scope.data.id).then(function (response) { scope.data.imageList = response.data.imageList; }); } }; scope.uploadOnFail = function () { scope.data.fileId = null; }; scope.onImageMetaSave = function (index, file) { var data = { imageId: file.imageId, name: file.name }; return controller.action('imageSave', scope.data.id, data); }; /** * Delete uploaded file * @param {Number} index * @param {{imageId: Number}} file */ scope.imageDelete = function (index, file) { controller.action('imageDelete', scope.data.id, file.imageId).then(function (response) { var idx; if (-1 !== (idx = _.indexOf(scope.data.imageList, file))) { scope.data.imageList.splice(idx, 1); } }); }; scope.onImageReorder = function (list) { var fileIds = list.map(function (file) { return file.imageId; }); controller.action('imageReorder', scope.data.id, fileIds); }; scope.onSubmit = function () { cfg.debug && console.log(name , '.onSubmit'); // do nothing }; scope.data = null; if (attr[name]) { controller.formAction('imageList', attr[name]); } } }; }]); })();