SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/change/change-details-editor.js

162 lines
8.8 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('changeModule')
.directive('changeDetailsEditor', ['events', 'createTicketService', 'fieldValidationModel', 'systemAlertService', 'i18nService',
function (events, createTicketService, fieldValidationModel, systemAlertService, i18nService) {
return {
restrict: 'E',
replace: true,
templateUrl: 'views/change/change-details-editor.html',
scope: {
ticket: '=',
metadata: '=',
form: '='
},
link: function (scope) {
scope.ticketCopy = _.cloneDeep(scope.ticket);
scope.siteOptions = {
company: {
visible: false,
attribute: 'companyName'
},
region: {
attribute: 'region'
},
siteGroup: {
attribute: 'siteGroup'
},
site: {
attribute: 'name'
}
};
scope.clear = function (attribute) {
scope.updatedInfo[attribute] = null;
};
scope.getListPersons = function (term, company) {
scope.searchingPersons = true;
return createTicketService.getListOfPersonsByCompany(term, company).then(function (response) {
scope.searchingPersons = false;
return response[0].items;
});
};
scope.buildSiteTag = function (site) {
var siteGroup = site.siteGroup ? ' > ' + site.siteGroup : '', siteName = site.name ? ' > ' + site.name : '', siteRegion = site.region ? site.region : '';
return siteRegion + siteGroup + siteName;
};
scope.validateRequestedFor = function () {
if (!scope.updatedInfo.customer.id) {
scope.updatedInfo.customer = '';
}
scope.searchingPersons = false;
};
scope.setChangeLocation = function () {
return systemAlertService.modal({
title: i18nService.getLocalizedString('create.change.setRequestedForLocation.title'),
text: i18nService.getLocalizedString('create.change.setRequestedForLocation.text'),
buttons: [
{
text: i18nService.getLocalizedString('common.button.yes'),
data: true
},
{
text: i18nService.getLocalizedString('common.button.no'),
data: false
}
]
}).result.then(function (data) {
if (data) {
var location = scope.updatedInfo.customer.site;
location.company = scope.ticket.company.name;
scope.updatedInfo.location = location;
}
});
};
function toggleEditMode() {
scope.editMode = true;
scope.ticketCopy = _.cloneDeep(scope.ticket);
scope.updatedInfo = {
customer: scope.ticket.customer,
changeReason: (scope.ticket.changeReason ? _.find(scope.metadata.changeReasons, function (obj) {
return obj.name === scope.ticket.changeReason;
}) : ''),
location: scope.ticket.location,
timing: _.find(scope.metadata.timings, function (obj) {
return obj.name === scope.ticket.timing;
}),
timingReason: _.find(scope.metadata.timingReasons, function (obj) {
return obj.name === scope.ticket.timingReason;
})
};
scope.updatedInfo.location.companyName = scope.ticket.company.name; //necessary in order to limit the search for sites based on company
}
function handleSaveChanges() {
var changes = collectChanges();
scope.$emit(events.SAVE_CHANGES_REQUEST, changes);
}
function collectChanges() {
var changes = {};
if (scope.updatedInfo.changeReason.name !== scope.ticket.changeReason) {
changes.changeReason = scope.updatedInfo.changeReason.name;
}
if (scope.updatedInfo.customer.id !== scope.ticket.customer.id) {
changes.customer = scope.updatedInfo.customer;
}
if (scope.updatedInfo.location !== scope.ticket.location) {
changes.location = scope.updatedInfo.location;
}
if (scope.ticket.timing !== scope.updatedInfo.timing.name) {
changes.timing = scope.updatedInfo.timing.name;
}
if (scope.updatedInfo.timingReason && scope.updatedInfo.timingReason.name !== scope.ticket.timingReason) {
changes.timingReason = scope.updatedInfo.timingReason.name;
}
return changes;
}
function close() {
scope.editMode = false;
scope.updatedInfo = {};
}
function handleSaveComplete(event, eventData) {
if (!_.isEmpty(eventData)) {
scope.ticket.changeReason = eventData.changeReason;
scope.ticket.customer = eventData.customer;
scope.ticket.location = eventData.location;
scope.ticket.timing = eventData.timing;
scope.ticket.timingReason = eventData.timingReason;
scope.ticket.impactedAreas = eventData.impactedAreas;
}
if (scope.isDatesRequired()) {
scope.ticket.scheduledStartDate = scope.ticketCopy.scheduledStartDate;
scope.ticket.scheduledEndDate = scope.ticketCopy.scheduledEndDate;
scope.ticket.actualStartDate = scope.ticketCopy.actualStartDate;
scope.ticket.actualEndDate = scope.ticketCopy.actualEndDate;
scope.ticket.targetDate = scope.ticketCopy.targetDate;
}
close();
}
function handleSaveFault() {
scope.$broadcast(events.SAVE_CHANGES_COMPLETE); //needed to reset the child scopes of all contained directives
close();
}
scope.isDatesRequired = function () {
return (fieldValidationModel.isFieldRequired(scope.ticket.type, scope.ticket.status.value, scope.updatedInfo.timing.name, 'scheduledStartDate')
|| fieldValidationModel.isFieldRequired(scope.ticket.type, scope.ticket.status.value, scope.updatedInfo.timing.name, 'actualStartDate'))
&& (scope.ticket.accessMappings.actualdateEditAllowed || scope.ticket.accessMappings.scheduleddateEditAllowed);
};
scope.$watch('updatedInfo.timing', function (value) {
if (value) {
scope.ticketCopy.timing = value.name;
}
});
scope.$on(events.SAVE_CHANGES, handleSaveChanges);
scope.$on(events.TOGGLE_EDIT_MODE, toggleEditMode);
scope.$on(events.SAVE_ALL_CHANGES_COMPLETE, handleSaveComplete);
scope.$on(events.SAVE_ALL_CHANGES_FAULT, handleSaveFault);
scope.$on(events.DISCARD_CHANGES, close);
}
};
}
]);
})();