(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", "regionsJsonParser", "primaryService", "gradientService", "sortDataService", function ($scope, $rootScope, $filter, regionsJsonParser, 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 = []; } } officeCatJson = CheckOfficeCat; } } //Binding races $scope.raceList = getRegionalRaceListFromOffCatJson(officeCatJson); } // Data Population var getBindableRaceListFromRaceJson = function (raceListJson) { var raceList = []; var isPrimary = primaryService.getIsPrimary(); var selectedPartyCode = primaryService.getSelectedParty(); raceListJson = sortDataService.ensureArray(raceListJson); for (var i = 0; i < raceListJson.length; i++) { var curRaceJson = raceListJson[i]; var newRaceListItem = { raceName: curRaceJson.OFFICE_TITLE, candidateList: [] }; var candidateListJson = sortDataService.ensureArray(curRaceJson.Candidates.Candidate); 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) { 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 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++) { regionsJson[i].Races.Race = sortDataService.ensureArray(regionsJson[i].Races.Race); 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(); }]); }());