SmartIT_Extensions/BMC/smart-it-full/scripts/app/custom-widgets/person-site-directive.js

171 lines
8.8 KiB
JavaScript

"use strict";
/**
* Created by etang
*/
(function () {
'use strict';
angular.module('customWidgetsModule')
.directive('personSite', ['customFieldLinkFunction', 'createTicketModel', 'searchModel', 'events', function (customFieldLinkFunction, createTicketModel, searchModel, events) {
return {
restrict: 'E',
replace: true,
scope: {
data: '=',
isEditable: '='
},
templateUrl: 'views/custom-widgets/person-site.html',
link: function (scope) {
customFieldLinkFunction(scope);
scope.state = {};
scope.selections = {};
scope.company = scope.data.value.company;
scope.selected = scope.data.value;
loadRegions();
scope.$watch('selected.region', function (regionNew, regionOld) {
if (regionNew) {
if (scope.selected.siteGroup
&& scope.selected.siteGroup.attributeMap.regionName !== regionNew.name) {
scope.selected.siteGroup = null;
}
if (scope.selected.site
&& scope.selected.site.attributeMap.regionName !== regionNew.name) {
scope.selected.site = null;
}
}
else {
scope.selected.siteGroup = null;
scope.selected.site = null;
}
loadSiteGroups();
if (!(_.isEqual(regionNew, regionOld))) {
scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: 'region' });
}
});
scope.$watch('selected.siteGroup', function (siteGroupNew, siteGroupOld) {
if (siteGroupNew) {
scope.selected.region = { name: siteGroupNew.attributeMap.regionName };
if (scope.selected.site
&& scope.selected.site.attributeMap.siteGroupName !== siteGroupNew.name) {
scope.selected.site = null;
}
}
else {
scope.selected.site = null;
}
loadSites();
if (!(_.isEqual(siteGroupNew, siteGroupOld))) {
scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: 'siteGroup' });
}
});
scope.$watch('selected.site', function (siteNew, siteOld) {
if (siteNew) {
scope.selected.siteGroup = {
name: siteNew.attributeMap.siteGroupName,
attributeMap: { regionName: siteNew.attributeMap.regionName }
};
}
if (!(_.isEqual(siteNew, siteOld))) {
scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: 'site' });
}
});
scope.$watch('data.value', function (newVal, oldVal) {
if (newVal != oldVal) {
scope.selected = newVal;
}
});
scope.onValueChange = function () {
scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name });
};
scope.$watch('data.setValueFlag', function (value) {
if (value !== '#$#' && value) {
scope.data.value = value;
scope.onValueChange();
scope.data.setValueFlag = '#$#';
}
});
scope.loadRegionsByName = function (regionName) {
var queryParam = {
companyName: scope.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.company.name
};
if (scope.selected && scope.selected.region) {
queryParam.regionName = scope.selected.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.company.name
};
if (scope.selected && scope.selected.region) {
queryParam.regionName = scope.selected.region.name;
}
if (scope.selected && scope.selected.siteGroup) {
queryParam.siteGroupName = scope.selected.siteGroup.name;
}
return createTicketModel.getListOfSitesByName(queryParam, siteName).then(function (sites) {
scope.state.sitesLoading = false;
return { list: sites.objects, exceedsChunkSize: sites.exceedsChunkSize };
});
};
function loadRegions() {
scope.selections.regions = null;
scope.state.regionsLoading = true;
var queryParam = {
companyName: scope.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;
});
}
function loadSiteGroups() {
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.state.tooManySiteGroups = siteGroups.exceedsChunkSize;
});
}
function loadSites() {
scope.selections.sites = null;
scope.state.sitesLoading = true;
var queryParam = {
companyName: scope.company.name
};
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;
});
}
}
};
}]);
}());