(function () { "use strict"; angular.module("enrDetailsPaneModule") .directive("enrOffices", [function () { return { templateUrl: "Scripts/EnrAngular/EnrDetailsPane/enrOffices.html", controller: "enrOfficesController", scope: { officeName: "=", countyName: "=" }, restrict: "E" }; }]) .controller("enrOfficesController", ["$scope", "$rootScope", "$filter", "detailsData", "enrModeManager", "officeJsonRegionFilter", "regionsJsonParser", "participationCalculator", "primaryService", "gradientService", "sortDataService", function ($scope, $rootScope, $filter, detailsData, enrModeManager, officeJsonRegionFilter, regionsJsonParser, participationCalculator, primaryService, gradientService, sortDataService) { var selectedCountyName = $scope.countyName; var currentOfficeName = $scope.officeName; $scope.raceList = []; var refreshDataBindings = function () { var currentOffices = primaryService.getOfficesList().slice(); var officeCatJson = {}; for (var i = 0; i < currentOffices.length; i++) { if (currentOffices[i].OfficeCategory.OFFICE_CATEGORY_NAME == currentOfficeName) { officeCatJson = currentOffices[i]; break; } } if ($rootScope.defaultOfficeSelection) { var CheckOfficeCat = JSON.parse(JSON.stringify(officeCatJson)); if (typeof (CheckOfficeCat) != "undefined" && typeof (CheckOfficeCat.OfficeCategory) != "undefined" && typeof (CheckOfficeCat.OfficeCategory.Regions) != "undefined" && typeof (CheckOfficeCat.OfficeCategory.Regions.Region) != "undefined") { var officeCatRegions = CheckOfficeCat.OfficeCategory.Regions.Region; for (var j = 0; j < officeCatRegions.length; j++) { var officeCatRegion = officeCatRegions[j]; if (typeof (officeCatRegion.Races) == "undefined" || typeof (officeCatRegion.Races.Race) == "undefined") { officeCatRegion.Races = {}; officeCatRegion.Races.Race = []; //var gotData = false; //for (var i = 0; i < currentOffices.length; i++) { // if (typeof (currentOffices[i].OfficeCategory) != "undefined") { // var cmpOfficeCat = currentOffices[i].OfficeCategory; // if (typeof (cmpOfficeCat) != "undefined" && typeof (cmpOfficeCat.Regions) != "undefined" && typeof (cmpOfficeCat.Regions.Region) != "undefined") { // var cmpOfficeRegions = cmpOfficeCat.Regions.Region; // var matchcounty = false; // for (var k = 0; k < typeof (cmpOfficeRegions.length) != undefined ? cmpOfficeRegions.length : 0; k++) { // var cmpOfficeRegion = cmpOfficeRegions[k]; // if (matchcounty) { // break; // } // if (officeCatRegion.MAP_JURISDICTIONID == cmpOfficeRegion.MAP_JURISDICTIONID) { // matchcounty = true; // if (typeof (cmpOfficeRegion.Races) != "undefined" && typeof (cmpOfficeRegion.Races.Race) != "undefined") { // gotData = true; // if (cmpOfficeRegion.Races.Race.length == undefined) { // officeCatRegion.Races.Race.push(cmpOfficeRegion.Races.Race); // } // else { // cmpOfficeRegion.Races.Race.forEach((race) => { // officeCatRegion.Races.Race.push(race); // }); // } // } // } // } // } // } // if (gotData) { // break; // } //} } } officeCatJson = CheckOfficeCat; } } //Binding races $scope.raceList = getRegionalRaceListFromOffCatJson(officeCatJson); } // Data Population var getBindableRaceListFromRaceJson = function (raceListJson) { var raceList = []; var isPrimary = primaryService.getIsPrimary(); var selectedPartyCode = primaryService.getSelectedParty(); if (raceListJson.length === undefined) { raceListJson = [raceListJson]; } for (var i = 0; i < raceListJson.length; i++) { var curRaceJson = raceListJson[i]; var newRaceListItem = { raceName: curRaceJson.OFFICE_TITLE, candidateList: [] }; var candidateListJson = curRaceJson.Candidates.Candidate; if (candidateListJson.length === undefined) candidateListJson = [candidateListJson]; if (isPrimary && !gradientService.isOfficeExcludedFromGradients(currentOfficeName)) { gradientService.generateOfficeCandidateGradients(candidateListJson); } for (var j = 0; j < candidateListJson.length; j++) { var curCandidateJson = candidateListJson[j]; var partyCode = curCandidateJson.PARTY_ABBREV; // In primary mode, we only show members of the currently selected party. if (isPrimary && partyCode !== selectedPartyCode) continue; var newCandidateListItem = { name: curCandidateJson.NAME_ON_BALLOT, nameLast: curCandidateJson.CandidateName, partyLetter: partyCode, partyColor: curCandidateJson.DISPLAY_COLOR, votes: curCandidateJson.TOTAL_VOTES, order: sortDataService.getOrdrerNumber(partyCode) } if (isPrimary && !gradientService.isOfficeExcludedFromGradients(currentOfficeName)) { // Depending on the source of the JSON, we may have to manually grab gradient color if (!curCandidateJson.GRADIENT_COLOR) { var candidateInfoWithGradients = gradientService.getGradientsByParty(curCandidateJson.DISPLAY_COLOR); var gradient = $filter("filter")(candidateInfoWithGradients, { NAME_ON_BALLOT: curCandidateJson.NAME_ON_BALLOT })[0]; if (gradient && gradient.GRADIENT_COLOR) curCandidateJson.GRADIENT_COLOR = gradient.GRADIENT_COLOR; } newCandidateListItem.gradientColor = curCandidateJson.GRADIENT_COLOR; } newRaceListItem.candidateList.push(newCandidateListItem); } if (newRaceListItem.candidateList.length > 0) { //newRaceListItem.candidateList = sortCandidatesByVotes(newRaceListItem.candidateList,'votes'); //newRaceListItem.candidateList = sortDataService.sortCandiatesByParty(newRaceListItem.candidateList, 'order', 'votes', 'nameLast'); raceList.push(newRaceListItem); } } // Percentages for (var i = 0; i < raceList.length; i++) { var curRaceItem = raceList[i]; var totalVotes = 0; for (var k = 0; k < curRaceItem.candidateList.length; k++) totalVotes += Number(curRaceItem.candidateList[k].votes); for (var k = 0; k < curRaceItem.candidateList.length; k++) curRaceItem.candidateList[k].percentage = totalVotes !== 0 ? curRaceItem.candidateList[k].votes / totalVotes : 0; } return raceList; } var sortCandidatesByVotes = function (candidateList, field) { candidateList.sort(function (a, b) { return (parseInt(a[field]) > parseInt(b[field])) ? 1 : ((parseInt(a[field]) < parseInt(b[field])) ? -1 : 0); }); return candidateList.reverse(); } var getRegionalRaceListFromOffCatJson = function (offCatJson) { var raceList = []; var isIncluded = false; // US and state reps are also likely candidates for exclusion? var racesIncludedInCounty = [ "US Representative", "US Representative (Special)", "State Senator", "State Representative" ]; if (racesIncludedInCounty.indexOf(currentOfficeName) !== -1) { isIncluded = true; } var list; var isValidRegionsData = regionsJsonParser.validOfficesRegionsJsonExist(offCatJson); if (isValidRegionsData) { var regionsJson = regionsJsonParser.getOfficesRegionsJsonAsArray(offCatJson); if (isIncluded) { for (var i = 0; i < regionsJson.length; i++) { for (var j = 0; j < regionsJson[i].Races.Race.length; j++) { if (regionsJson[i].Races.Race[j].Jurisdiction.JURISDICTION_NAME.toUpperCase() === selectedCountyName.toUpperCase()) { if (regionsJson[i].Races != null) { list = getBindableRaceListFromRaceJson(regionsJson[i].Races.Race[j]); raceList = raceList.concat(list); } break; } } } } else { for (var i = 0; i < regionsJson.length; i++) { if (regionsJson[i].MAP_JURISDICTION_NAME.toUpperCase() === selectedCountyName.toUpperCase()) { if (regionsJson[i].Races != null) { list = getBindableRaceListFromRaceJson(regionsJson[i].Races.Race); raceList = raceList.concat(list); } break; } } } } // Log race name and candidates to console for debugging for (var i = 0; i < raceList.length; i++) { console.log(raceList[i].raceName, raceList[i].candidateList); } return raceList; } refreshDataBindings(); }]); }());