"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .directive('priority', ['metadataModel', 'ticketModel', 'events', 'systemAlertService', '$filter', 'objectValueMapperService', function (metadataModel, ticketModel, events, systemAlertService, $filter, objectValueMapperService) { return { restrict: 'E', replace: true, templateUrl: 'views/custom-widgets/priority.html', scope: { ticket: '=', data: '=', updateIsHandledByParent: '@', isEditable: '=' }, link: function (scope, element) { scope.setValuePriority = ''; scope.oldPriority = angular.copy(scope.data.value.priority); scope.state = { isCalculating: false }; if (scope.ticket.accessMappings && scope.ticket.accessMappings.fieldMappings && scope.ticket.accessMappings.fieldMappings.hasOwnProperty('priority')) { scope.isPriorityEditable = (scope.ticket.accessMappings.fieldMappings.priority === 'write'); } else if (scope.ticket.accessMappings && scope.ticket.accessMappings.hasOwnProperty('priorityEditAllowed')) { scope.isPriorityEditable = scope.ticket.accessMappings.priorityEditAllowed; } else if (scope.ticket.accessMappings && scope.ticket.accessMappings.hasOwnProperty('allEditAllowed')) { scope.isPriorityEditable = scope.ticket.accessMappings.allEditAllowed; } else { scope.isPriorityEditable = true; } if (scope.$parent.isNew) { scope.fieldLengthForSm6 = true; scope.isNew = true; } else { var closestParent = element.closest('.layout-renderer__child-column')[0] || element.closest('.layout-renderer__column')[0]; if (closestParent) { scope.fieldLengthForSm6 = _.includes(closestParent.className, 'col-sm-6'); scope.fieldLengthForSm4 = _.includes(closestParent.className, 'col-sm-4'); } } metadataModel.getMetadataByType(scope.ticket.type).then(function (metadata) { scope.metadata = metadata; setPriorityWidgetValues(); scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, source: 'fromTicketLoad' }); }); scope.$watch('ticket.priority', function (data) { if (scope.ticket.isDraft && scope.metadata) { scope.data.value.priority = _.find(scope.metadata.priorities, { name: data }); } scope.oldPriority = data; }); scope.$watch('data.setValueFlag', function (value) { if (value !== '#$#' && value !== undefined && value !== null) { var urgencyValueUpdate = _.has(value, 'urgency'), impactValueUpdate = _.has(value, 'impact'); if (urgencyValueUpdate || impactValueUpdate) { setPriorityWidgetValues(scope.data.value.priority.name, _.get(value, 'urgency.name', scope.data.value.urgency.name), _.get(value, 'impact.name', scope.data.value.impact.name)); scope.updatePriority(); } else { if (scope.metadata) { var filterObj = isNaN(value) ? { name: value } : { index: value }; scope.data.value.priority = _.find(scope.metadata.priorities, filterObj); } else { metadataModel.getMetadataByType(scope.ticket.type).then(function (metadata) { scope.metadata = metadata; scope.data.value.priority = _.find(scope.metadata.priorities, { name: value }); }); } scope.setValuePriority = value; if (scope.oldPriority !== value && scope.data && !scope.data.isFromTicketLoad) { scope.changePriority(); } scope.oldPriority = value; } scope.data.setValueFlag = '#$#'; } scope.data.isFromTicketLoad = false; }); scope.changePriority = function () { scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: scope.data.name }); }; scope.updatePriority = function (memberName) { scope.state.isCalculating = true; if (!scope.ticket.company) { var company = objectValueMapperService.getValueByFieldName("company"); scope.ticket.company = {}; scope.ticket.company.name = company; } getPriorityValue(memberName); }; function getPriorityValue(memberName) { if (scope.ticket.type === EntityVO.TYPE_CHANGE) { ticketModel.getChangePriority(scope.ticket.company.name, scope.data.value.impact.name, scope.data.value.urgency.name).then(function (data) { if (!_.isEmpty(data)) { scope.data.value.priority = _.find(scope.metadata.priorities, { name: data }); scope.state.isCalculating = false; scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: [memberName, scope.data.name] }); showPriorityAlert(scope.data.value.priority); } else { scope.state.isCalculating = false; scope.data.value.priority = null; showPriorityAlert(false); } }); } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT) { ticketModel.getPriority(scope.ticket.company.name, scope.data.value.impact.name, scope.data.value.urgency.name, scope.ticket.type).then(function (data) { if (!_.isEmpty(data)) { scope.data.value.priority = _.find(scope.metadata.priorities, { name: data }); scope.state.isCalculating = false; scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: [memberName, scope.data.name] }); showPriorityAlert(scope.data.value.priority); } }); } else { scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: [memberName, scope.data.name] }); } } function setPriorityWidgetValues(priorityValue, urgencyValue, impactValue) { var setValuePriority = priorityValue || scope.setValuePriority, setValueUrgency = urgencyValue || _.get(scope.ticket, 'urgency.name', scope.ticket.urgency), setValueImpact = impactValue || _.get(scope.ticket, 'impact.name', scope.ticket.impact); if (!setValuePriority) { scope.data.value.priority = _.find(scope.metadata.priorities, function (obj) { return scope.ticket.priority && (obj.name === scope.ticket.priority || obj.name === scope.ticket.priority.name); }) || { name: scope.ticket.priority }; } else { var filterObj = isNaN(setValuePriority) ? { name: setValuePriority } : { index: setValuePriority }; scope.data.value.priority = _.find(scope.metadata.priorities, filterObj); } scope.data.value.impact = _.find(scope.metadata.impacts, function (obj) { return obj.name === setValueImpact; }) || { name: setValueImpact }; scope.data.value.urgency = _.find(scope.metadata.urgencies, function (obj) { return obj.name === setValueUrgency; }) || { name: setValueUrgency }; if (scope.data.members) { _.forEach(scope.data.members, function (member) { member.value = { name: scope.ticket[member.name], label: scope.ticket[member.name] }; }); } if (!scope.data.value.priority.name && scope.ticket.impact.name && scope.ticket.urgency.name) { getPriorityValue(); } } function showPriorityAlert(priority) { if (!priority) { systemAlertService.error({ text: $filter('i18n')('ticket.priority.warning'), hide: 10000 }); } } function disabledImpactAndUrgency() { if (!objectValueMapperService.getValueByFieldName('customer') && !objectValueMapperService.getValueByFieldName('requestedFor')) { var priority = objectValueMapperService.getFieldsByType('priority'); if (priority && priority[0] && priority[0].members) { priority[0].isReadOnly = true; _.forEach(priority[0].members, function (member) { member.isReadOnly = true; }); } } } function toggleEditMode() { scope.editMode = true; setPriorityWidgetValues(); if (scope.ticket.type !== EntityVO.TYPE_TASK) { disabledImpactAndUrgency(); } } function close() { scope.data.value.priority = _.find(scope.metadata.priorities, { name: scope.oldPriority }); setPriorityWidgetValues(scope.data.value.priority.name); // should not set scope.setValuePriority, because on draft mode it will make widget always render setValuePriority scope.state.isCalculating = false; scope.editMode = false; } scope.$on(events.TOGGLE_EDIT_MODE, toggleEditMode); scope.$on(events.DISCARD_CHANGES, close); scope.$on(events.AFTER_SAVED_CHANGES, function () { // In draft mode, oldPriority should be updated manually after changes were applied to ticket var selectedPriorityValue = _.get(scope.data.value.priority, 'name') || scope.data.value.priority; scope.oldPriority = selectedPriorityValue; }); } }; } ]); })();