(function () { "use strict"; angular.module("enrDetailsPaneModule") .directive("detailsCountyBreakdown", [function () { //function link(scope) { // if ((localStorage.getItem("CountyCode") != "null" && localStorage.getItem("CountyCode") != "undefined" && localStorage.getItem("CountyCode") != null)) { // if (localStorage.getItem("CountyCode").length > 0) { // var countyName= JSON.parse(localStorage.getItem("CountyCode")); // scope.updateSelectedCounty(countyName); // } // } // link:link, //} return { templateUrl: "Scripts/EnrAngular/EnrDetailsPane/detailsCountyBreakdown.html", scope: {}, controller: "detailsCountyBreakdownController", restrict: "E" }; }]) .controller("detailsCountyBreakdownController", ["$scope", "$rootScope", "$timeout", "detailsViewModeManager", "enrModeManager", "mapSelectedRegion", "detailsData", "regionsJsonParser", "$filter", "primaryService", "gradientService", "jsonDataFetcher", function ($scope, $rootScope, $timeout, detailsViewModeManager, enrModeManager, mapSelectedRegion, detailsData, regionsJsonParser, $filter, primaryService, gradientService, jsonDataFetcher) { // Visibility Handling var determineVisibility = function () { var isVisible = enrModeManager.isOfficeMode() && detailsViewModeManager.isCountysMode() && !mapSelectedRegion.isAnyRegionSelected(); return isVisible ? "" : "hidden"; }; $scope.componentVisible = determineVisibility(); $scope.$on(detailsViewModeManager.modeChangeEventName, function () { $scope.selectedCountyName = undefined; $scope.componentVisible = determineVisibility(); if ($rootScope.OFFICE_CATEGORY_TYPE == "Local") { $scope.componentVisible = ""; } if ((localStorage.getItem("CountyCode") != "null" && localStorage.getItem("CountyCode") != "undefined" && localStorage.getItem("CountyCode") != null)) { if (localStorage.getItem("CountyCode").length > 0) { var county = JSON.parse(localStorage.getItem("CountyCode")); $scope.updateSelectedCounty(county); } } }); $scope.$on(enrModeManager.modeChangeEventName, function () { $scope.componentVisible = determineVisibility(); }); $scope.$on(mapSelectedRegion.regionChangeEventName, function () { $scope.componentVisible = determineVisibility(); }); $scope.$on(detailsData.dataChangedEventName, function () { $scope.componentVisible = determineVisibility(); if (enrModeManager.isOfficeMode() && detailsData.isDataPresent() && detailsViewModeManager.isCountysMode()) { selectedCountyName = undefined; refreshDataBindings(); if ((localStorage.getItem("CountyCode") != "null" && localStorage.getItem("CountyCode") != "undefined" && localStorage.getItem("CountyCode") != null)) { if (localStorage.getItem("CountyCode").length > 0) { var county = JSON.parse(localStorage.getItem("CountyCode")); $timeout(function () { $scope.updateSelectedCounty(county); },1000); } } } }); // Data Population var selectedCountyName; var getCandidatesForOffice = function (countyRegionsJson) { var countyList = []; var isPrimary = primaryService.getIsPrimary(); var selectedPrimaryParty = primaryService.getSelectedParty(); for (var i = 0; i < countyRegionsJson.length; i++) { var curCountyJson = countyRegionsJson[i]; // Determine if there is sufficient valid data to include this county in the countyList results var isValidRacesJson = curCountyJson.Races !== undefined && curCountyJson.Races !== null && curCountyJson.Races.Race !== undefined && curCountyJson.Races.Race !== null; if (!isValidRacesJson) continue; var racesJson = curCountyJson.Races.Race; if (racesJson.length === undefined) racesJson = [racesJson]; var isAnyValidCandidateJsonInRegion = false; for (var j = 0; j < racesJson.length; j++) { var curRaceJson = racesJson[j]; var isValidCandidateJson = curRaceJson.Candidates !== undefined && curRaceJson.Candidates !== null && curRaceJson.Candidates.Candidate !== undefined && curRaceJson.Candidates.Candidate !== null; if (isValidCandidateJson) { isAnyValidCandidateJsonInRegion = true; break; } } if (!isAnyValidCandidateJsonInRegion) continue; var newCountyListItem = { countyName: curCountyJson.MAP_JURISDICTION_NAME, raceList: [] }; countyList.push(newCountyListItem); // We only populate race data for the selected County if (newCountyListItem.countyName !== selectedCountyName) continue; for (var j = 0; j < racesJson.length; j++) { var curRaceJson = racesJson[j]; // Determine if there is sufficient valid data to include this county-race in the results. var isValidCandidateJson = curRaceJson.Candidates !== undefined && curRaceJson.Candidates !== null && curRaceJson.Candidates.Candidate !== undefined && curRaceJson.Candidates.Candidate !== null; if (!isValidCandidateJson) continue; var candidateJson = curRaceJson.Candidates.Candidate; if (candidateJson.length === undefined) candidateJson = [candidateJson]; var newRaceListItem = { raceName: curRaceJson.OFFICE_TITLE, candidateJson: candidateJson, candidateList: [] }; var totalVotes = 0; for (var k = 0; k < candidateJson.length; k++) { // In primary mode we only include votes if the candidate party matches the selected party. var includeVotes = !isPrimary || candidateJson[k].PARTY_ABBREV === selectedPrimaryParty; if (includeVotes) totalVotes += candidateJson[k].TOTAL_VOTES; } var candidateInfoWithGradients = null; // Convert candidateJson into a format that can be consumed by detailsOfficeRaceVotes. for (var k = 0; k < candidateJson.length; k++) { var curCandidateJson = candidateJson[k]; var partyCode = curCandidateJson.PARTY_ABBREV; // In primary mode we only include votes if the candidate party matches the selected party. if (isPrimary && partyCode !== selectedPrimaryParty) continue; if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients() && candidateInfoWithGradients === null) candidateInfoWithGradients = gradientService.getGradientsByParty(curCandidateJson.DISPLAY_COLOR); var newCandidateListItem = { name: curCandidateJson.NAME_ON_BALLOT, partyLetter: partyCode, partyColor: curCandidateJson.DISPLAY_COLOR, votes: curCandidateJson.TOTAL_VOTES, percentage: totalVotes !== 0 ? curCandidateJson.TOTAL_VOTES / totalVotes : 0 }; if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients()) { var gradientColor = $filter("filter")(candidateInfoWithGradients, { NAME_ON_BALLOT: curCandidateJson.NAME_ON_BALLOT })[0].GRADIENT_COLOR; newCandidateListItem.gradientColor = gradientColor } newRaceListItem.candidateList.push(newCandidateListItem); } if (newRaceListItem.candidateList.length > 0) newCountyListItem.raceList.push(newRaceListItem); } } return countyList; } var getCountyListFromCountyJson = function (countyRegionsJson) { var countyList = []; var isPrimary = primaryService.getIsPrimary(); var selectedPrimaryParty = primaryService.getSelectedParty(); for (var i = 0; i < countyRegionsJson.length; i++) { var curCountyJson = countyRegionsJson[i]; // Determine if there is sufficient valid data to include this county in the countyList results var isValidRacesJson = curCountyJson.Races !== undefined && curCountyJson.Races !== null && curCountyJson.Races.Race !== undefined && curCountyJson.Races.Race !== null; if (!isValidRacesJson) continue; var racesJson = curCountyJson.Races.Race; if (racesJson.length === undefined) racesJson = [racesJson]; var isAnyValidCandidateJsonInRegion = false; for (var j = 0; j < racesJson.length; j++) { var curRaceJson = racesJson[j]; var isValidCandidateJson = curRaceJson.Candidates !== undefined && curRaceJson.Candidates !== null && curRaceJson.Candidates.Candidate !== undefined && curRaceJson.Candidates.Candidate !== null; if (isValidCandidateJson) { isAnyValidCandidateJsonInRegion = true; break; } } if (!isAnyValidCandidateJsonInRegion) continue; var newCountyListItem = { countyName: curCountyJson.MAP_JURISDICTION_NAME, raceList: [] }; countyList.push(newCountyListItem); // We only populate race data for the selected County if (newCountyListItem.countyName !== selectedCountyName) continue; for (var j = 0; j < racesJson.length; j++) { var curRaceJson = racesJson[j]; // Determine if there is sufficient valid data to include this county-race in the results. var isValidCandidateJson = curRaceJson.Candidates !== undefined && curRaceJson.Candidates !== null && curRaceJson.Candidates.Candidate !== undefined && curRaceJson.Candidates.Candidate !== null; if (!isValidCandidateJson) continue; var candidateJson = curRaceJson.Candidates.Candidate; if (candidateJson.length === undefined) candidateJson = [candidateJson]; var newRaceListItem = { raceName: curRaceJson.OFFICE_TITLE, candidateJson: candidateJson, candidateList: [] }; var totalVotes = 0; for (var k = 0; k < candidateJson.length; k++) { // In primary mode we only include votes if the candidate party matches the selected party. var includeVotes = !isPrimary || candidateJson[k].PARTY_ABBREV === selectedPrimaryParty; if (includeVotes) totalVotes += candidateJson[k].TOTAL_VOTES; } var candidateInfoWithGradients = null; // Convert candidateJson into a format that can be consumed by detailsOfficeRaceVotes. for (var k = 0; k < candidateJson.length; k++) { var curCandidateJson = candidateJson[k]; var partyCode = curCandidateJson.PARTY_ABBREV; // In primary mode we only include votes if the candidate party matches the selected party. if (isPrimary && partyCode !== selectedPrimaryParty) continue; if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients() && candidateInfoWithGradients === null) candidateInfoWithGradients = gradientService.getGradientsByParty(curCandidateJson.DISPLAY_COLOR); var newCandidateListItem = { name: curCandidateJson.NAME_ON_BALLOT, partyLetter: partyCode, partyColor: curCandidateJson.DISPLAY_COLOR, votes: curCandidateJson.TOTAL_VOTES, percentage: totalVotes !== 0 ? curCandidateJson.TOTAL_VOTES / totalVotes : 0 }; if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients()) { var gradientColor = $filter("filter")(candidateInfoWithGradients, { NAME_ON_BALLOT: curCandidateJson.NAME_ON_BALLOT })[0].GRADIENT_COLOR; newCandidateListItem.gradientColor = gradientColor } newRaceListItem.candidateList.push(newCandidateListItem); } if (newRaceListItem.candidateList.length > 0) newCountyListItem.raceList.push(newRaceListItem); } } return countyList; } var getdefaultSelectionCountyListFromCountyJson = function (countyRegionsJson) { var countyList = []; var isPrimary = primaryService.getIsPrimary(); var selectedPrimaryParty = primaryService.getSelectedParty(); for (var i = 0; i < countyRegionsJson.length; i++) { var curCountyJson = countyRegionsJson[i]; // Determine if there is sufficient valid data to include this county in the countyList results var isValidRacesJson = curCountyJson.Races !== undefined && curCountyJson.Races !== null && curCountyJson.Races.Race !== undefined && curCountyJson.Races.Race !== null; //if (!isValidRacesJson) // continue; var racesJson = ((curCountyJson && curCountyJson.Races && curCountyJson.Races.Race)||[]); if (racesJson.length === undefined) racesJson = [racesJson]; var isAnyValidCandidateJsonInRegion = false; for (var j = 0; j < racesJson.length; j++) { var curRaceJson = racesJson[j]; var isValidCandidateJson = curRaceJson.Candidates !== undefined && curRaceJson.Candidates !== null && curRaceJson.Candidates.Candidate !== undefined && curRaceJson.Candidates.Candidate !== null; if (isValidCandidateJson) { isAnyValidCandidateJsonInRegion = true; break; } } //if (!isAnyValidCandidateJsonInRegion) // continue; var newCountyListItem = { countyName: curCountyJson.MAP_JURISDICTION_NAME, raceList: [] }; countyList.push(newCountyListItem); // We only populate race data for the selected County //if (newCountyListItem.countyName !== selectedCountyName) // continue; //for (var j = 0; j < racesJson.length; j++) { // var curRaceJson = racesJson[j]; // // Determine if there is sufficient valid data to include this county-race in the results. // var isValidCandidateJson = // curRaceJson.Candidates !== undefined && curRaceJson.Candidates !== null && // curRaceJson.Candidates.Candidate !== undefined && curRaceJson.Candidates.Candidate !== null; // if (!isValidCandidateJson) // continue; // var candidateJson = curRaceJson.Candidates.Candidate; // if (candidateJson.length === undefined) // candidateJson = [candidateJson]; // var newRaceListItem = { raceName: curRaceJson.OFFICE_TITLE, candidateJson: candidateJson, candidateList: [] }; // var totalVotes = 0; // for (var k = 0; k < candidateJson.length; k++) { // // In primary mode we only include votes if the candidate party matches the selected party. // var includeVotes = !isPrimary || candidateJson[k].PARTY_ABBREV === selectedPrimaryParty; // if (includeVotes) // totalVotes += candidateJson[k].TOTAL_VOTES; // } // var candidateInfoWithGradients = null; // // Convert candidateJson into a format that can be consumed by detailsOfficeRaceVotes. // for (var k = 0; k < candidateJson.length; k++) { // var curCandidateJson = candidateJson[k]; // var partyCode = curCandidateJson.PARTY_ABBREV; // // In primary mode we only include votes if the candidate party matches the selected party. // if (isPrimary && partyCode !== selectedPrimaryParty) // continue; // if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients() && candidateInfoWithGradients === null) // candidateInfoWithGradients = gradientService.getGradientsByParty(curCandidateJson.DISPLAY_COLOR); // var newCandidateListItem = { // name: curCandidateJson.NAME_ON_BALLOT, // partyLetter: partyCode, // partyColor: curCandidateJson.DISPLAY_COLOR, // votes: curCandidateJson.TOTAL_VOTES, // percentage: totalVotes !== 0 ? curCandidateJson.TOTAL_VOTES / totalVotes : 0 // }; // if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients()) { // var gradientColor = $filter("filter")(candidateInfoWithGradients, { NAME_ON_BALLOT: curCandidateJson.NAME_ON_BALLOT })[0].GRADIENT_COLOR; // newCandidateListItem.gradientColor = gradientColor // } // newRaceListItem.candidateList.push(newCandidateListItem); // } // if (newRaceListItem.candidateList.length > 0) // newCountyListItem.raceList.push(newRaceListItem); //} } return countyList; } var getCountyListFromDistrictJson = function (districtRegionsJson) { var countyList = []; var isPrimary = primaryService.getIsPrimary(); var selectedPrimaryParty = primaryService.getSelectedParty(); for (var i = 0; i < districtRegionsJson.length; i++) { var curDistrictJson = districtRegionsJson[i]; // Validate Races Data var isValidRacesJson = curDistrictJson.Races !== undefined && curDistrictJson.Races !== null && curDistrictJson.Races.Race !== undefined && curDistrictJson.Races.Race !== null; if (!isValidRacesJson) continue; var racesJson = curDistrictJson.Races.Race; if (racesJson.length === undefined) racesJson = [racesJson]; for (var j = 0; j < racesJson.length; j++) { var curRaceJson = racesJson[j]; var raceId = curRaceJson.OFFICEID; var raceName = curRaceJson.OFFICE_TITLE; // Validate Jurisdiction and Candidate Data var isValidJurisdictionJson = curRaceJson.Jurisdiction !== undefined && curRaceJson.Jurisdiction !== null; var isValidCandidateJson = curRaceJson.Candidates !== undefined && curRaceJson.Candidates !== null && curRaceJson.Candidates.Candidate !== undefined && curRaceJson.Candidates.Candidate !== null; if (!isValidJurisdictionJson || !isValidCandidateJson) continue; var jurisdictionJson = curRaceJson.Jurisdiction; var candidateJson = curRaceJson.Candidates.Candidate; if (candidateJson.length === undefined) candidateJson = [candidateJson]; // Examine Jurisdiction. Pull existing countyListData for this county or insert into countyList if it's new. var countyName = jurisdictionJson.JURISDICTION_NAME; var countyListFilterResults = $filter("filter")(countyList, { countyName: countyName }, true); var isCountyInResults = countyListFilterResults.length > 0; var curCountyListItem; if (isCountyInResults) curCountyListItem = countyListFilterResults[0]; else { curCountyListItem = { countyName: countyName, raceList: [] }; countyList.push(curCountyListItem); } // We only populate race data for the selected County if (curCountyListItem.countyName !== selectedCountyName) continue; var curRaceListItem; var raceListFilterResults = $filter("filter")(curCountyListItem.raceList, { raceId: raceId }, true); var isRaceInResults = raceListFilterResults.length > 0; if (isRaceInResults) curRaceListItem = raceListFilterResults[0]; else curRaceListItem = { raceId: raceId, raceName: raceName, candidateList: [] }; var candidateInfoWithGradients = null; // Incorporate CandidateData into new/existing data. for (var k = 0; k < candidateJson.length; k++) { var curCandidateJson = candidateJson[k]; // In primary mode we only include data if the candidate party matches the selected party. if (isPrimary && curCandidateJson.PARTY_ABBREV !== selectedPrimaryParty) continue; if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients() && candidateInfoWithGradients === null) candidateInfoWithGradients = gradientService.getGradientsByParty(curCandidateJson.DISPLAY_COLOR); var candidateId = curCandidateJson.CANDIDATEID; var curCandidateListItem; var candidateListFilterResults = $filter("filter")(curRaceListItem.candidateList, { candidateId: candidateId }, true); var isCandidateInResults = candidateListFilterResults.length > 0; if (isCandidateInResults) { curCandidateListItem = candidateListFilterResults[0]; curCandidateListItem.votes += curCandidateJson.TOTAL_VOTES; } else { curCandidateListItem = { candidateId: candidateId, name: curCandidateJson.NAME_ON_BALLOT, partyLetter: curCandidateJson.PARTY_ABBREV, partyColor: curCandidateJson.DISPLAY_COLOR, votes: curCandidateJson.TOTAL_VOTES }; if (isPrimary && !gradientService.isCurOfficeExcludedFromGradients()) { var gradientColor = $filter("filter")(candidateInfoWithGradients, { NAME_ON_BALLOT: curCandidateJson.NAME_ON_BALLOT })[0].GRADIENT_COLOR; curCandidateListItem.gradientColor = gradientColor } curRaceListItem.candidateList.push(curCandidateListItem); } } if (!isRaceInResults && curRaceListItem.candidateList.length > 0) curCountyListItem.raceList.push(curRaceListItem); } } // Percentages for (var i = 0; i < countyList.length; i++) { var curCountyItem = countyList[i]; for (var j = 0; j < curCountyItem.raceList.length; j++) { var curRaceItem = curCountyItem.raceList[j]; var totalVotes = 0; for (var k = 0; k < curRaceItem.candidateList.length; k++) totalVotes += 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 countyList; } var refreshDataBindings = function () { var countyList = []; var offCatJson = detailsData.getData(); var isValidRegionsData = regionsJsonParser.validOfficesRegionsJsonExist(offCatJson); if (isValidRegionsData) { var regionsJson = regionsJsonParser.getOfficesRegionsJsonAsArray(offCatJson); var isCountyJson = regionsJson[0].MAP_JURISDICTION_TYPE === "Locality"; if (isCountyJson) { if ($rootScope.defaultOfficeSelection) { countyList = getdefaultSelectionCountyListFromCountyJson(regionsJson); } else { countyList = getCountyListFromCountyJson(regionsJson); } } else countyList = getCountyListFromDistrictJson(regionsJson); } $scope.countyList = countyList; $timeout(function () { angular.element("#countyDetailsScroll")[0].scrollTop = 0; }, 100); }; $scope.$on(detailsViewModeManager.modeChangeEventName, function () { if (enrModeManager.isOfficeMode() && detailsData.isDataPresent() && detailsViewModeManager.isCountysMode()) { //if ((localStorage.getItem("CountyCode") != "null" && localStorage.getItem("CountyCode") != "undefined" && localStorage.getItem("CountyCode") != null)) { // if (localStorage.getItem("CountyCode").length > 0) { // selectedCountyName = JSON.parse(localStorage.getItem("CountyCode")); // } //} else { // //} selectedCountyName = undefined; refreshDataBindings(); } }); $scope.$on("SELECTED_PARTY_CHANGED", function () { if (enrModeManager.isOfficeMode() && detailsData.isDataPresent() && detailsViewModeManager.isCountysMode()) { //if ((localStorage.getItem("CountyCode") != "null" && localStorage.getItem("CountyCode") != "undefined" && localStorage.getItem("CountyCode") != null)) { // if (localStorage.getItem("CountyCode").length > 0) { // selectedCountyName = JSON.parse(localStorage.getItem("CountyCode")); // } //} else { //} selectedCountyName = undefined; refreshDataBindings(); } }); $scope.selectedCountyName = ''; //$scope.$on("detailsViewModeChanged", function () { // $scope.updateSelectedCounty(selectedCountyName, 1); //}); $scope.updateSelectedCounty = function (countyName) { //var officesResult = primaryService.getOfficesList(); //$rootScope.$broadcast("PUSH_BUSY_WORKING"); $scope.currentOffices = primaryService.getOfficesList(); // Clicking an already expanded county collapses it if (selectedCountyName === countyName) selectedCountyName = undefined; else selectedCountyName = countyName; $scope.selectedCountyName = selectedCountyName; primaryService.setSelectedCounty(selectedCountyName); $rootScope.$broadcast("COUNTY_CHNAGED"); //refreshDataBindings(); } }]); }());