(function () { "use strict"; var name = 'adTicketManageForm'; angular.module('app.directive') .directive('adTicketManageForm', ['$rootScope', '$q', '$state', 'tool', 'translate', 'cfg', 'notification', 'ticket', function ($rootScope, $q, $state, tool, translate, cfg, notification, ticket) { return { templateUrl: 'directive/' + name + '/' + name + '.html', scope: {}, controller: 'BaseFormController', link: function (scope, element, attributes, controller) { cfg.debug && console.log(name); controller.bridge = ticket; /** * @type {TicketFullInfo} */ scope.data = null; scope.tmp = {}; scope.form = { link: { geo: null, tag: {}, geoTag: {} } }; scope.patternFloat = /^-?\d+(?:\.\d+)?$/; // updated by "typeahead" directive (typeahead-loading="linkLoading.geo") scope.linkLoading = {geo: false, tag: false}; scope.linkBusy = {geo: false, tag: false}; scope.statusInfo = ticket.statusInfo; scope.linkSelect = function (type, $item) { if (null == scope.data) scope.data = {}; if (null == scope.form.link) scope.form.link = {}; scope.form.link[type] = $item; }; scope.linkRecommend = function (type, namePart) { var deferred = $q.defer(); ticket.recommend(type, scope.data.ticket.id, namePart).then(function (response) { deferred.resolve(response.data.list); }, function () { deferred.reject(); }); return deferred.promise; }; var onLinkActionDone = function (type, response) { if (null == scope.data) scope.data = {}; if (null == scope.form.link) scope.form.link = {}; if (null == scope.data.links) scope.data.links = {}; var data = response.data; scope.form.link[type] = null; if ('geo' === type) { scope.data.links.geo = data.links.geo; scope.data.links.geoAttr = data.links.geoAttr; scope.data.recommend = data.recommend; } if ('tag' === type) { scope.form.link[type] = {}; scope.data.links.tag = data.links.tag; scope.tmp.indexedTagLinks = response.links.tag ? _.indexBy(response.links.tag, 'typeCode') : {}; } }; var onLinkActionFail = function (response) { if (response.message) controller.notify(response.message, 'warning'); }; scope.onLinkChange = function (type, tagId) { if (scope.form.link[type][tagId]) { scope.linkLink(type, tagId); } else { scope.linkUnlink(type, tagId); } }; scope.linkLink = function (type, tagId) { scope.linkBusy[type] = true; ticket.link(type, scope.data.ticket.id, tagId).then(function (response) { scope.linkBusy[type] = false; onLinkActionDone(type, response); }, function (response) { scope.linkBusy[type] = false; onLinkActionFail(response); }); }; scope.linkUnlink = function (type, id) { scope.linkBusy[type] = true; ticket.unlink(type, scope.data.ticket.id, id).then(function (response) { scope.linkBusy[type] = false; onLinkActionDone(type, response); }, function (response) { scope.linkBusy[type] = false; onLinkActionFail(response); }); }; scope.saveTicket = function () { return ticket.save(scope.data.ticket).then(function (response) { notification.clear('ticket'); }, function (response) { if (response.message) controller.notify(response.message, 'warning'); if (response.errors) { angular.forEach(response.errors, function (msg, name) { scope.ticketManageForm.$setError(name, msg); }); } return $q.reject(response); }) }; scope.setStatus = function (status) { ticket.setStatus(scope.data.ticket.id, status).then(function (data) { scope.data.ticket = data; $state.go('menu.user.ticket'); }, function (data) { scope.data.ticket = data; if (data.message) controller.notify(data.message, 'warning'); }); }; scope.onAttributeChange = function (attrId, value) { return controller.action('saveAttribute', scope.data.ticket.id, attrId, value); }; scope.getDatepickerOptions = function (storageId) { var options = {}; switch (storageId) { case 'datetime': case 'date': options = {minMode: 'day', datepickerMode: 'day'}; break; case 'yearmonth': options = {minMode: 'month', datepickerMode: 'month'}; break; case 'year': options = {minMode: 'year', datepickerMode: 'year'}; break; default: throw new Error('Unknown datetimeStorageId: "' + storageId + '"'); } return options; }; scope.str2date = function (str) { if (null == str) return null; return new Date(str); }; scope.date2str = function (date) { if (null == date) return null; date = date instanceof Date ? date : new Date(date); return $filter('date')(date, 'yyyy-MM-dd HH:mm:ss'); }; if (attributes[name]) { controller.formAction('read', attributes[name]).then(function (response) { var data = response.data; if (data.ticket.statusId !== ticket.statusInfo.draft.id) { $state.go('menu.user.ticket'); } }); } else { controller.formAction('create', attributes[name]).then(function (response) { $state.go('menu.user.ticket.edit', {id: response.data.ticket.id}); }); } } }; }]); })();