(function () { "use strict"; angular.module("enrTickerModule", []) .directive("enrTicker", [function () { return { templateUrl: "Scripts/EnrAngular/Directives/enrTicker.html", scope: { tickerData: "=", tickerDataR: "=" }, restrict: "E", controller: enrTickerController }; }]); enrTickerController.$inject = ["$scope", "$rootScope", "$filter", "$timeout", "primaryService", "enrModeManager"]; function enrTickerController($scope, $rootScope, $filter, $timeout, primaryService, enrModeManager) { // Handle being assigned primary data if (enrModeManager.isOfficeMode()) { $scope.$watch("tickerData", function (newData) { if (newData) processRawTickerData(); }); } else if (enrModeManager.isOfficeMode()) { $scope.$watch("tickerDataR", function (newData) { if (newData) processRawTickerData(); }); } else { $scope.$watch("tickerData", function (newData) { if (newData) processRawTickerData(); }); } // Handle selected party changed $scope.$on("SELECTED_PARTY_CHANGED", function (event, partyCode) { if ($scope.tickerData || $scope.tickerDataR) processRawTickerData(); }); $scope.onTickerOfficeClick = function(office) { if (enrModeManager.getCurMode() === enrModeManager.modeEnum.offices) { $rootScope.$broadcast("SLICK_OFFICE_SELECTION", office); } else { enrModeManager.setCurMode(enrModeManager.modeEnum.offices); $timeout(function() { $rootScope.$broadcast("SLICK_OFFICE_SELECTION", office); }, 500); } }; $scope.onTickerReferendumClick = function(selectedItem) { if (enrModeManager.getCurMode() === enrModeManager.modeEnum.referendums) { $rootScope.$broadcast("SLICK_REFERENDUM_SELECTION", selectedItem); } else { enrModeManager.setCurMode(enrModeManager.modeEnum.referendums); $timeout(function () { $rootScope.$broadcast("SLICK_REFERENDUM_SELECTION", selectedItem); }, 500); } }; $rootScope.refreshTicker = function() { if ($scope.tickerData || $scope.tickerDataR) { processRawTickerData(); } }; // PRIVATE function processRawTickerData() { var processedTickerData = ""; var TickerR = false; if (enrModeManager.isReferendumsMode() || enrModeManager.isTurnoutMode()) TickerR = true; else TickerR = false; if (TickerR) { $scope.isReferendumTickerDisplay = true; processedTickerData = angular.copy($scope.tickerDataR); } else { $scope.isReferendumTickerDisplay = false; processedTickerData = angular.copy($scope.tickerData); if (primaryService.getIsPrimary()) { processedTickerData = filterTickerDataByParty(processedTickerData, primaryService.getSelectedParty()); // Prior to election results being entered, the top banner will display two candidates for the selected party determined alphabetically by last name. // TOTALS are strings so coercive comparision is intentional. processedTickerData.forEach(function (office) { var candidateList = office.Candidates.Candidate; var length = candidateList.length; if (length > 0) { var noResultsYet = candidateList[0].TOTAL == 0 && (length > 1 && candidateList[1].TOTAL == 0); if (noResultsYet) { var orderedNames = $filter('orderByLastName')(candidateList.map(function (candidate) { return candidate.NAME_ON_BALLOT; })); var orderedCandidates = []; orderedNames.forEach(function (name) { var candidate = $filter('filter')(candidateList, { NAME_ON_BALLOT: name })[0]; orderedCandidates.push(candidate); }); office.Candidates.Candidate = orderedCandidates; } } }); } else { processedTickerData.forEach(function (office) { var candidateList = office.Candidates.Candidate; if (candidateList && candidateList.length == undefined) { office.Candidates.Candidate = [candidateList]; } }); } } // Because we can't use init-onload (see enrTicker.html for details) if we simply reassign tickerDataFiletered it's going to mess up // the bounds of the ticker UI. So we assign it to null, which will tricker the ng-if in enrTicker.html and then assign the data, causing // the ticker to load from scratch and everything looks correct. We have to delay the assignment of the actual data a bit so that the null // is actually presente during the next digest cycle. $scope.tickerDataFiltered = null; if ($rootScope.isEnrNav) { $scope.tickerDataFiltered = processedTickerData; } else { window.setTimeout(function () { $scope.tickerDataFiltered = processedTickerData; }, 100); } } // TODO: Can this be refactored and simplified? function filterTickerDataByParty(arrayTicker, partyCode) { var returnValue = []; var candidates = []; var isCurrentParty = false; angular.forEach(arrayTicker, function (tickerOffice) { isCurrentParty = false; candidates = []; if (!angular.isArray(tickerOffice.Candidates.Candidate)) tickerOffice.Candidates.Candidate = [tickerOffice.Candidates.Candidate]; angular.forEach(tickerOffice.Candidates.Candidate, function (candiate) { if (candiate.PARTY == partyCode) { isCurrentParty = true; candidates.push(candiate); } }); if (isCurrentParty) { if (!angular.isArray(candidates)) tickerOffice.Candidates.Candidate = [candidates]; else tickerOffice.Candidates.Candidate = candidates; returnValue.push(tickerOffice); } }); return returnValue; }; } }());