SmartIT_Extensions/BMC/smart-it-full/scripts/app/ticket/edit-customer-card-directiv...

237 lines
12 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.directive('editCustomerCard', ['createTicketModel', 'events', 'searchModel', function (createTicketModel, events, searchModel) {
return {
restrict: 'E',
scope: {
ticket: '=',
metadata: '=',
update: '&'
},
replace: true,
templateUrl: 'views/ticket/edit-customer-card.html',
link: function (scope) {
scope.showAddContact = false;
scope.state = {};
scope.selections = {};
scope.selected = {};
scope.removePerson = function (type) {
scope.personInfo = _.reject(scope.personInfo, { type: type });
if (type === 'contact') {
scope.showAddContact = true;
}
};
scope.addPerson = function () {
scope.showAddContact = false;
scope.personDropdown = true;
};
scope.getListPersonsByCompany = function (term) {
return createTicketModel.getPersonsByCompany(term, scope.company.name || scope.company).then(function (response) {
return response.items;
});
};
scope.getList = function (type, term) {
return createTicketModel.getList(type, term);
};
scope.clearPerson = function (person) {
person.data = '';
};
scope.updateCustomer = function ($item) {
scope.personInfo.unshift({ type: 'customer', data: _.cloneDeep($item) });
scope.company = $item.company;
scope.selected.site = {
name: $item.site.name,
id: $item.site.siteId,
attributeMap: {
companyName: $item.site.companyName,
siteAddress: $item.site.address,
regionName: $item.site.region,
siteGroupName: $item.site.siteGroup
}
};
};
scope.validateCustomer = function () {
if (!scope.personInfo[0] || scope.personInfo[0].type !== 'customer') {
scope.customer.data = '';
}
};
scope.updateContact = function ($item) {
scope.personDropdown = false;
scope.personInfo.push({ type: 'contact', data: _.cloneDeep($item) });
};
scope.save = function () {
scope.update({ data: scope.personInfo }).then(function () {
scope.$emit(events.SAVE_CHANGES_COMPLETE);
}, function () {
scope.$emit(events.SAVE_CHANGES_FAULT);
});
};
scope.isCustomerRequired = function () {
return scope.ticket.type !== 'incident' || scope.ticket.serviceType !== 'Infrastructure Event';
};
var handleToggleEditMode = function () {
scope.editMode = true;
scope.personDropdown = false;
scope.company = scope.ticket.company || scope.ticket.customer.company;
scope.personInfo = scope.ticket.customer.loginId ? [{ type: 'customer', data: _.cloneDeep(scope.ticket.customer) }] : [];
if (scope.ticket.customer.site) {
scope.selected.site = {
name: scope.ticket.customer.site.name,
id: scope.ticket.customer.site.siteId,
attributeMap: {
companyName: scope.ticket.customer.company.name,
siteAddress: scope.ticket.customer.site.address,
regionName: scope.ticket.customer.site.region,
siteGroupName: scope.ticket.customer.site.siteGroup
}
};
}
if (scope.ticket.contact.loginId) {
scope.personInfo.push({ type: 'contact', data: _.cloneDeep(scope.ticket.contact) });
}
else {
scope.showAddContact = true;
}
};
scope.$watchCollection('personInfo', function () {
if (scope.personInfo && scope.personInfo[0] && scope.personInfo[0].type === 'customer') {
scope.customer = scope.personInfo[0];
}
else {
scope.customer = {
data: ''
};
}
});
scope.$watch('company', function (company) {
if (company) {
scope.loadRegions();
}
});
scope.$watch('selected.region', function (region) {
if (region) {
if (scope.selected.siteGroup
&& scope.selected.siteGroup.attributeMap.regionName !== region.name) {
scope.selected.siteGroup = null;
}
if (scope.selected.site
&& scope.selected.site.attributeMap.regionName !== region.name) {
scope.selected.site = null;
}
}
else {
scope.selected.siteGroup = null;
scope.selected.site = null;
}
if (scope.editMode) {
scope.loadSiteGroups();
}
});
scope.$watch('selected.siteGroup', function (siteGroup) {
if (siteGroup) {
scope.selected.region = { name: siteGroup.attributeMap.regionName };
if (scope.selected.site
&& scope.selected.site.attributeMap.siteGroupName !== siteGroup.name) {
scope.selected.site = null;
}
}
else {
scope.selected.site = null;
}
if (scope.editMode) {
scope.loadSites();
}
});
scope.$watch('selected.site', function (site) {
if (site) {
scope.selected.siteGroup = {
name: site.attributeMap.siteGroupName,
attributeMap: { regionName: site.attributeMap.regionName }
};
scope.personInfo[0].data.site = {
name: site.name,
siteId: site.id,
address: site.attributeMap.siteAddress,
region: site.attributeMap.regionName,
siteGroup: site.attributeMap.siteGroupName,
companyName: site.attributeMap.companyName
};
}
});
scope.loadRegions = function () {
scope.selections.regions = null;
scope.state.regionsLoading = true;
createTicketModel.getList(EntityVO.TYPE_REGION, {
companyName: scope.company.name
}).then(function (regions) {
scope.state.regionsLoading = false;
scope.selections.regions = regions.objects;
});
};
scope.loadSiteGroups = function () {
scope.selections.siteGroups = null;
scope.state.siteGroupsLoading = true;
var queryParam = {
companyName: scope.company.name
};
if (scope.selected.region) {
queryParam.regionName = scope.selected.region.name;
}
createTicketModel.getList(EntityVO.TYPE_SITE_GROUP, queryParam).then(function (siteGroups) {
scope.state.siteGroupsLoading = false;
scope.selections.siteGroups = siteGroups.objects;
});
};
scope.loadSites = function () {
scope.selections.sites = null;
scope.state.sitesLoading = true;
var queryParam = {
companyName: scope.company.name,
chunkInfo: { startIndex: 0, chunkSize: searchModel.siteChunkSize }
};
if (scope.selected.region) {
queryParam.regionName = scope.selected.region.name;
}
if (scope.selected.siteGroup) {
queryParam.siteGroupName = scope.selected.siteGroup.name;
}
createTicketModel.getList(EntityVO.TYPE_SITE, queryParam).then(function (sites) {
scope.state.sitesLoading = false;
scope.selections.sites = sites.objects;
scope.state.tooManySites = sites.exceedsChunkSize;
});
};
scope.loadSitesByName = function (siteName) {
var queryParam = {
companyName: scope.company.name,
chunkInfo: { startIndex: 0, chunkSize: searchModel.siteChunkSize }
};
if (scope.selected.region) {
queryParam.regionName = scope.selected.region.name;
}
if (scope.selected.siteGroup) {
queryParam.siteGroupName = scope.selected.siteGroup.name;
}
return createTicketModel.getListOfSitesByName(queryParam, siteName).then(function (sites) {
return sites.objects;
});
};
var handleSaveChanges = function () {
scope.$emit(events.SAVE_CHANGES_REQUEST, null, true);
scope.save();
close();
};
function close() {
scope.personInfo = [];
scope.editMode = false;
}
scope.$on(events.TOGGLE_EDIT_MODE, handleToggleEditMode);
scope.$on(events.SAVE_CHANGES, handleSaveChanges);
scope.$on(events.DISCARD_CHANGES, close);
}
};
}]);
}());