"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .directive('editHeader', ['events', 'ticketModel', 'systemAlertService', '$filter', 'configurationModel', function (events, ticketModel, systemAlertService, $filter, configurationModel) { return { restrict: 'EA', scope: { ticket: '=', metadata: '=', update: '&' }, templateUrl: 'views/ticket/edit-header.html', link: function (scope) { scope.disableSave = true; scope.datePickerOptions = { startingDay: configurationModel.getWeekStartingDay(), 'show-weeks': false, minDate: moment().year(1970).month(0).date(2), maxDate: moment().year(2038).month(0).date(18) }; scope.showMeridian = window.showMeridian; scope.initDate = scope.ticket.targetDate || new Date(); if (!scope.ticket.targetDate) { scope.minDate = new Date(); } else if ((scope.ticket.targetDate < (new Date()).getTime())) { scope.minDate = scope.ticket.targetDate; } else { scope.minDate = new Date(); } scope.updatePriority = function () { if ([EntityVO.TYPE_INCIDENT, EntityVO.TYPE_PROBLEM, EntityVO.TYPE_KNOWNERROR].indexOf(scope.ticket.type) >= 0) { ticketModel.getPriority(scope.ticket.company.name, scope.updatedInfo.impact.name, scope.updatedInfo.urgency.name, scope.ticket.type).then(function (data) { scope.updatedInfo.priority = _.find(scope.metadata.priorities, function (obj) { return obj.name === data; }); if (!scope.updatedInfo.priority) { systemAlertService.error({ text: $filter('i18n')('ticket.​calculationError.calculatedPriority'), clear: false }); } }); } scope.disableSave = false; }; scope.isPriorityRequired = function () { return ([EntityVO.TYPE_INCIDENT, EntityVO.TYPE_PROBLEM, EntityVO.TYPE_KNOWNERROR].indexOf(scope.ticket.type) >= 0); }; scope.save = function () { scope.update({ data: scope.updatedInfo }); }; scope.cancel = function () { scope.update(null); getInitialUpdateData(); }; scope.showImpactField = function () { return (([EntityVO.TYPE_INCIDENT, EntityVO.TYPE_PROBLEM, EntityVO.TYPE_KNOWNERROR].indexOf(scope.ticket.type)) === -1) ? false : true; }; scope.showPriorityField = function () { return (([EntityVO.TYPE_WORKORDER, EntityVO.TYPE_TASK].indexOf(scope.ticket.type)) === -1) ? false : true; }; scope.onSummaryChange = function () { scope.disableSave = false; scope.$emit(events.FIELD_FORM_IS_DIRTY); }; var handleSaveChanges = function () { // TODO: use promise here scope.save(); scope.$emit(events.SAVE_CHANGES_COMPLETE); }; var handleDiscardChanges = function () { scope.cancel(); }; function getInitialUpdateData() { scope.updatedInfo = { summary: scope.ticket.summary, priority: _.find(scope.metadata.priorities, function (obj) { return obj.name === scope.ticket.priority; }) || { name: scope.ticket.priority } }; if ([EntityVO.TYPE_INCIDENT, EntityVO.TYPE_PROBLEM, EntityVO.TYPE_KNOWNERROR].indexOf(scope.ticket.type) >= 0) { scope.updatedInfo.impact = _.find(scope.metadata.impacts, function (obj) { return obj.name === scope.ticket.impact; }); scope.updatedInfo.urgency = _.find(scope.metadata.urgencies, function (obj) { return obj.name === scope.ticket.urgency; }); } if ([EntityVO.TYPE_PROBLEM, EntityVO.TYPE_KNOWNERROR].indexOf(scope.ticket.type) >= 0) { scope.updatedInfo.targetDate = scope.ticket.targetDate; } } getInitialUpdateData(); scope.$on(events.SAVE_CHANGES, handleSaveChanges); scope.$on(events.DISCARD_CHANGES, handleDiscardChanges); scope.$on(events.PROBLEM_UPDATE_TARGET_DATE_EVENT, function (event, targetDate) { scope.updatedInfo.targetDate = targetDate; }); } }; }]); }());