"use strict"; (function () { 'use strict'; angular.module('changeModule') .directive('impactedAreasEditor', ['systemAlertService', '$filter', 'searchModel', function (systemAlertService, $filter, searchModel) { return { restrict: 'E', templateUrl: 'views/change/impacted-areas-editor.html', scope: { ticket: '=' }, controller: ['$scope', 'userModel', 'createTicketModel', 'events', '$q', 'ticketModel', function ($scope, userModel, createTicketModel, events, $q, ticketModel) { var impactedAreasCopy = _.clone($scope.ticket.impactedAreas); $scope.state = {}; $scope.selections = {}; searchModel.getOperatingCompanies().then(function (response) { $scope.selections.companies = _.cloneDeep(response.companies); $scope.state.tooManyCompanies = response.exceedsChunkSize; }); $scope.$watchCollection('ticket.impactedAreas', function () { $scope.state.pendingTicketDatesChange = !$scope.ticket.isDraft; }); $scope.$watch('ticket.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(); } }); $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 (_.where($scope.ticket.impactedAreas, impactedArea).length === 0) { $scope.ticket.impactedAreas = $scope.ticket.impactedAreas || []; $scope.ticket.impactedAreas.push(impactedArea); $scope.ticket.addedImpactedAreas = $scope.ticket.addedImpactedAreas || []; $scope.ticket.addedImpactedAreas.push(impactedArea); } else { systemAlertService.error({ text: $filter('i18n')('create.change.wizard.basicDetails.impactedAreas.duplicateError'), clear: false }); } $scope.currentImpactedArea = { company: $scope.ticket.company }; }; $scope.$on(events.TOGGLE_EDIT_MODE, toggleEditMode); $scope.$on(events.SAVE_CHANGES, save); $scope.$on(events.DISCARD_CHANGES, cancel); $scope.$on(events.SAVE_ALL_CHANGES_COMPLETE, function () { }); function toggleEditMode() { impactedAreasCopy = _.clone($scope.ticket.impactedAreas); clearPendingChangeFlag(); } function save() { $scope.$emit(events.SAVE_CHANGES_REQUEST, {}, true); if ($scope.state.pendingTicketDatesChange) { $q.all([ ticketModel.saveImpactedAreas($scope.ticket.id, $scope.ticket.type, $scope.ticket.addedImpactedAreas), ticketModel.deleteImpactedAreas($scope.ticket.id, $scope.ticket.type, $scope.ticket.removedImpactedAreas) ]).then(function () { $scope.$emit(events.SAVE_CHANGES_COMPLETE); $scope.ticket.addedImpactedAreas = []; $scope.ticket.removedImpactedAreas = []; closeEditor(); }).catch(function (error) { if (error.data && error.data.defaultMessage) { systemAlertService.error({ text: error.data.defaultMessage, clear: false }); } $scope.$emit(events.SAVE_CHANGES_COMPLETE); $scope.ticket.addedImpactedAreas = []; $scope.ticket.removedImpactedAreas = []; closeEditor(); }); } else { $scope.$emit(events.SAVE_CHANGES_COMPLETE); } } function cancel() { $scope.ticket.impactedAreas = impactedAreasCopy; $scope.ticket.addedImpactedAreas = []; clearPendingChangeFlag(); } function closeEditor() { //todo: asergiye - uncomment once unified loading indication mechanism is in place //todo: editable content section right now, closes the editor in response to every SAVE_CHANGES_COMPLETE //todo: incoming event. The new mechanism will keep track of editable blocks and the number of SAVE_CHANGES_COMPLETE //todo: raised event to decided when to close the editor. //$scope.$emit(events.SAVE_CHANGES_COMPLETE); clearPendingChangeFlag(); } function clearPendingChangeFlag() { $scope.state.pendingTicketDatesChange = false; } } ] }; }]); })();