319 lines
20 KiB
JavaScript
319 lines
20 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Created by andey on 28-08-2017.
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
angular.module('customWidgetsModule')
|
|
.directive('impactedAreasWidget', ['systemAlertService', '$filter', 'searchModel', function (systemAlertService, $filter, searchModel) {
|
|
return {
|
|
restrict: 'E',
|
|
templateUrl: 'views/custom-widgets/impacted-areas-widget.html',
|
|
scope: {
|
|
data: '=',
|
|
ticket: '=',
|
|
isNew: '='
|
|
},
|
|
controller: ['$scope', 'userModel', 'createTicketModel', 'events', '$q', 'ticketModel', 'objectValueMapperService',
|
|
function ($scope, userModel, createTicketModel, events, $q, ticketModel, objectValueMapperService) {
|
|
$scope.state = {};
|
|
$scope.selections = {};
|
|
var originaldata = _.cloneDeep($scope.data.value);
|
|
$scope.isEditable = $scope.data.isEditable($scope.ticket.accessMappings);
|
|
searchModel.getOperatingCompanies(null, -1).then(function (response) {
|
|
$scope.selections.companies = _.cloneDeep(response.companies);
|
|
$scope.state.tooManyCompanies = response.exceedsChunkSize;
|
|
});
|
|
if ($scope.isNew) {
|
|
setCustomer();
|
|
}
|
|
if (!$scope.data.value) {
|
|
$scope.data.value = {
|
|
impactedAreas: [],
|
|
addedImpactedAreas: [],
|
|
removedImpactedAreas: []
|
|
};
|
|
}
|
|
$scope.impactedAreas = $scope.data.value.impactedAreas = $scope.data.value.impactedAreas || [];
|
|
$scope.addedImpactedAreas = $scope.data.value.addedImpactedAreas = $scope.data.value.addedImpactedAreas || [];
|
|
$scope.removedImpactedAreas = $scope.data.value.removedImpactedAreas = $scope.data.value.removedImpactedAreas || [];
|
|
$scope.$watch('customer.company', function (company) {
|
|
if (company) {
|
|
$scope.currentImpactedArea = {
|
|
company: company
|
|
};
|
|
}
|
|
});
|
|
$scope.$watch('currentImpactedArea.company', function (company) {
|
|
if (company) {
|
|
$scope.currentImpactedArea.region = null;
|
|
$scope.currentImpactedArea.siteGroup = null;
|
|
$scope.currentImpactedArea.site = null;
|
|
$scope.loadOrganizations();
|
|
$scope.loadRegions();
|
|
$scope.loadSiteGroups();
|
|
$scope.loadSites();
|
|
}
|
|
});
|
|
$scope.$watch('currentImpactedArea.organization', function (organization) {
|
|
if (organization) {
|
|
$scope.loadDepartments();
|
|
}
|
|
});
|
|
$scope.$watch('currentImpactedArea.region', function (region) {
|
|
if (region) {
|
|
if ($scope.currentImpactedArea.siteGroup
|
|
&& $scope.currentImpactedArea.siteGroup.attributeMap.regionName !== region.name) {
|
|
$scope.currentImpactedArea.siteGroup = null;
|
|
}
|
|
if ($scope.currentImpactedArea.site
|
|
&& $scope.currentImpactedArea.site.attributeMap.regionName !== region.name) {
|
|
$scope.currentImpactedArea.site = null;
|
|
}
|
|
$scope.loadSiteGroups();
|
|
$scope.loadSites();
|
|
}
|
|
else {
|
|
$scope.selections.siteGroups = null;
|
|
$scope.selections.sites = null;
|
|
}
|
|
});
|
|
$scope.$watch('currentImpactedArea.siteGroup', function (siteGroup) {
|
|
if (siteGroup) {
|
|
$scope.currentImpactedArea.region = { name: siteGroup.attributeMap.regionName };
|
|
if ($scope.currentImpactedArea.site
|
|
&& $scope.currentImpactedArea.site.attributeMap.siteGroupName !== siteGroup.name) {
|
|
$scope.currentImpactedArea.site = null;
|
|
}
|
|
}
|
|
});
|
|
$scope.$watch('currentImpactedArea.site', function (site) {
|
|
if (site) {
|
|
$scope.currentImpactedArea.siteGroup = {
|
|
name: site.attributeMap.siteGroupName,
|
|
attributeMap: { regionName: site.attributeMap.regionName }
|
|
};
|
|
}
|
|
});
|
|
$scope.getCompaniesByName = function (name) {
|
|
return searchModel.getCompaniesByText(name).then(function (response) {
|
|
return { list: response.companies, exceedsChunkSize: response.exceedsChunkSize };
|
|
});
|
|
};
|
|
$scope.loadOrganizations = function () {
|
|
$scope.currentImpactedArea.organization = null;
|
|
$scope.currentImpactedArea.department = null;
|
|
$scope.selections.organizations = null;
|
|
$scope.state.organizationsLoading = true;
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
createTicketModel.getList(EntityVO.TYPE_ORGANIZATION, queryParam).then(function (organizations) {
|
|
$scope.state.organizationsLoading = false;
|
|
$scope.selections.organizations = organizations.objects;
|
|
$scope.state.tooManyOrganizations = organizations.exceedsChunkSize;
|
|
});
|
|
};
|
|
$scope.loadDepartments = function () {
|
|
$scope.currentImpactedArea.department = null;
|
|
$scope.selections.departments = null;
|
|
$scope.state.departmentsLoading = true;
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name,
|
|
organizationName: $scope.currentImpactedArea.organization.name
|
|
};
|
|
createTicketModel.getList(EntityVO.TYPE_DEPARTMENT, queryParam).then(function (departments) {
|
|
$scope.state.departmentsLoading = false;
|
|
$scope.selections.departments = departments.objects;
|
|
$scope.state.tooManyDepartments = departments.exceedsChunkSize;
|
|
});
|
|
};
|
|
$scope.loadRegions = function () {
|
|
$scope.selections.regions = null;
|
|
$scope.state.regionsLoading = true;
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
createTicketModel.getList(EntityVO.TYPE_REGION, queryParam).then(function (regions) {
|
|
$scope.state.regionsLoading = false;
|
|
$scope.selections.regions = regions.objects;
|
|
$scope.state.tooManyRegions = regions.exceedsChunkSize;
|
|
});
|
|
};
|
|
$scope.loadSiteGroups = function () {
|
|
$scope.selections.siteGroups = null;
|
|
$scope.state.siteGroupsLoading = true;
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
if ($scope.currentImpactedArea.region) {
|
|
queryParam.regionName = $scope.currentImpactedArea.region.name;
|
|
}
|
|
createTicketModel.getList(EntityVO.TYPE_SITE_GROUP, queryParam).then(function (siteGroups) {
|
|
$scope.state.siteGroupsLoading = false;
|
|
$scope.selections.siteGroups = siteGroups.objects;
|
|
$scope.state.tooManySiteGroups = siteGroups.exceedsChunkSize;
|
|
});
|
|
};
|
|
$scope.loadSites = function () {
|
|
$scope.selections.sites = null;
|
|
$scope.state.sitesLoading = true;
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
if ($scope.currentImpactedArea.region) {
|
|
queryParam.regionName = $scope.currentImpactedArea.region.name;
|
|
}
|
|
if ($scope.currentImpactedArea.siteGroup) {
|
|
queryParam.siteGroupName = $scope.currentImpactedArea.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.loadRegionsByName = function (regionName) {
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
return createTicketModel.getListOfRegionsByName(queryParam, regionName).then(function (regions) {
|
|
$scope.state.regionsLoading = false;
|
|
return { list: regions.objects, exceedsChunkSize: regions.exceedsChunkSize };
|
|
});
|
|
};
|
|
$scope.loadSiteGroupsByName = function (siteGroupName) {
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
if ($scope.currentImpactedArea.region) {
|
|
queryParam.regionName = $scope.currentImpactedArea.region.name;
|
|
}
|
|
return createTicketModel.getListOfSiteGroupsByName(queryParam, siteGroupName).then(function (siteGroups) {
|
|
$scope.state.siteGroupsLoading = false;
|
|
return { list: siteGroups.objects, exceedsChunkSize: siteGroups.exceedsChunkSize };
|
|
});
|
|
};
|
|
$scope.loadSitesByName = function (siteName) {
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
if ($scope.currentImpactedArea.region) {
|
|
queryParam.regionName = $scope.currentImpactedArea.region.name;
|
|
}
|
|
if ($scope.currentImpactedArea.siteGroup) {
|
|
queryParam.siteGroupName = $scope.currentImpactedArea.siteGroup.name;
|
|
}
|
|
return createTicketModel.getListOfSitesByName(queryParam, siteName).then(function (sites) {
|
|
$scope.state.sitesLoading = false;
|
|
return { list: sites.objects, exceedsChunkSize: sites.exceedsChunkSize };
|
|
});
|
|
};
|
|
$scope.loadOrganizationsByName = function (organizationName) {
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name
|
|
};
|
|
return createTicketModel.getListOfOrganizationsByName(queryParam, organizationName).then(function (organizations) {
|
|
$scope.state.organizationsLoading = false;
|
|
return { list: organizations.objects, exceedsChunkSize: organizations.exceedsChunkSize };
|
|
});
|
|
};
|
|
$scope.loadDepartmentsByName = function (departmentName) {
|
|
var queryParam = {
|
|
companyName: $scope.currentImpactedArea.company.name,
|
|
organizationName: $scope.currentImpactedArea.organization.name
|
|
};
|
|
return createTicketModel.getListOfDepartmentsByName(queryParam, departmentName).then(function (departments) {
|
|
$scope.state.departmentsLoading = false;
|
|
return { list: departments.objects, exceedsChunkSize: departments.exceedsChunkSize };
|
|
});
|
|
};
|
|
$scope.addImpactedArea = function () {
|
|
var impactedArea = {
|
|
company: {
|
|
name: $scope.currentImpactedArea.company.name
|
|
}
|
|
};
|
|
if ($scope.currentImpactedArea.site) {
|
|
impactedArea.site = impactedArea.site || {};
|
|
impactedArea.site.name = $scope.currentImpactedArea.site.name;
|
|
}
|
|
if ($scope.currentImpactedArea.region) {
|
|
impactedArea.site = impactedArea.site || {};
|
|
impactedArea.site.region = $scope.currentImpactedArea.region.name;
|
|
}
|
|
if ($scope.currentImpactedArea.siteGroup) {
|
|
impactedArea.site = impactedArea.site || {};
|
|
impactedArea.site.siteGroup = $scope.currentImpactedArea.siteGroup.name;
|
|
}
|
|
if ($scope.currentImpactedArea.organization) {
|
|
impactedArea.organization = $scope.currentImpactedArea.organization.name;
|
|
}
|
|
if ($scope.currentImpactedArea.department) {
|
|
impactedArea.department = $scope.currentImpactedArea.department.name;
|
|
}
|
|
if (_.filter($scope.impactedAreas, impactedArea).length === 0) {
|
|
$scope.impactedAreas = $scope.impactedAreas || [];
|
|
$scope.impactedAreas.push(impactedArea);
|
|
$scope.addedImpactedAreas.push(impactedArea);
|
|
}
|
|
else {
|
|
systemAlertService.error({
|
|
text: $filter('i18n')('create.change.wizard.basicDetails.impactedAreas.duplicateError'),
|
|
clear: false
|
|
});
|
|
}
|
|
$scope.currentImpactedArea = {
|
|
company: $scope.customer.company
|
|
};
|
|
};
|
|
$scope.formatImpactedArea = function (impactedArea) {
|
|
var formattedImpactedArea = _.filter([
|
|
impactedArea.company.name,
|
|
impactedArea.site ? impactedArea.site.region : null,
|
|
impactedArea.site ? impactedArea.site.siteGroup : null,
|
|
impactedArea.site ? impactedArea.site.name : null,
|
|
impactedArea.organization,
|
|
impactedArea.department
|
|
], function (item) {
|
|
return item;
|
|
}).join(' > ');
|
|
return formattedImpactedArea;
|
|
};
|
|
$scope.removeImpactedArea = function (index) {
|
|
$scope.impactedAreas = $scope.impactedAreas || [];
|
|
var impactedArea = $scope.impactedAreas[index];
|
|
$scope.impactedAreas.splice(index, 1);
|
|
if (_.includes($scope.addedImpactedAreas, impactedArea)) {
|
|
_.remove($scope.addedImpactedAreas, impactedArea);
|
|
}
|
|
else {
|
|
$scope.removedImpactedAreas.push(impactedArea);
|
|
}
|
|
};
|
|
function afterSaveChangesHandler(event, response) {
|
|
$scope.impactedAreas.length = 0;
|
|
_.forEach(response.impactedAreas, function (actualImpactedAreas) {
|
|
$scope.impactedAreas.push(actualImpactedAreas);
|
|
});
|
|
originaldata.impactedAreas = _.cloneDeep($scope.impactedAreas);
|
|
}
|
|
function setCustomer() {
|
|
$scope.customer = $scope.ticket.type === EntityVO.TYPE_CHANGE
|
|
? objectValueMapperService.getValueByFieldName(FieldVO.prototype.WIDET_NAMES.requestedFor)
|
|
: objectValueMapperService.getValueByFieldName(FieldVO.prototype.WIDET_NAMES.customer);
|
|
}
|
|
$scope.$on(events.DISCARD_CHANGES, function () {
|
|
$scope.data.value = _.cloneDeep(originaldata);
|
|
$scope.impactedAreas = $scope.data.value.impactedAreas = $scope.data.value.impactedAreas || [];
|
|
$scope.addedImpactedAreas = $scope.data.value.addedImpactedAreas = $scope.data.value.addedImpactedAreas || [];
|
|
$scope.removedImpactedAreas = $scope.data.value.removedImpactedAreas = $scope.data.value.removedImpactedAreas || [];
|
|
});
|
|
$scope.$on(events.AFTER_SAVED_CHANGES, afterSaveChangesHandler);
|
|
$scope.$on(events.TOGGLE_EDIT_MODE, setCustomer);
|
|
}
|
|
]
|
|
};
|
|
}]);
|
|
})();
|