From 5475261733f834fcf068c09a9b00e0be5b58dd4c Mon Sep 17 00:00:00 2001 From: Findeton Date: Thu, 4 Jul 2024 16:13:43 -0500 Subject: [PATCH] wip --- .../voter-eligibility-screen-directive.html | 19 +- .../voter-eligibility-screen-directive.js | 180 ++++++++++++++++++ 2 files changed, 198 insertions(+), 1 deletion(-) diff --git a/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.html b/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.html index cf423093..df6afe71 100644 --- a/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.html +++ b/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.html @@ -1,4 +1,21 @@ + + + +
+
- Felix test +
\ No newline at end of file diff --git a/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.js b/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.js index 8929348a..de70903f 100644 --- a/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.js +++ b/avBooth/voter-eligibility-screen-directive/voter-eligibility-screen-directive.js @@ -19,6 +19,186 @@ angular.module('avBooth') .directive('avbVoterEligibilityScreen', function($window, $timeout, $q, $modal, ConfigService) { function link(scope, element, attrs) { + scope.showSkippedElections = false; + scope.organization = ConfigService.organization; + function findElectionCredentials(electionId, credentials) { + return _.find( + credentials, + function (credential) { + return credential.electionId === electionId; + } + ); + } + + function calculateCanVote(elCredentials) { + return ( + !!elCredentials && + !!elCredentials.token && + ( + elCredentials.numSuccessfulLogins < elCredentials.numSuccessfulLoginsAllowed || + elCredentials.numSuccessfulLoginsAllowed === 0 + ) + ); + } + + function calculateIsVoter(elCredentials) { + return ( + !!elCredentials && + elCredentials.numSuccessfulLoginsAllowed !== -1 + ); + } + + function getElectionCredentials() { + // need to reload in case this changed in success screen.. + var credentialsStr = $window.sessionStorage.getItem("vote_permission_tokens"); + return JSON.parse(credentialsStr); + } + + function isChooserDisabled() { + return ( + scope.parentElection && + scope.parentElection.presentation && + scope.parentElection.presentation.extra_options && + !!scope.parentElection.presentation.extra_options.disable__election_chooser_screen + ); + } + + function generateChildrenInfo() { + var childrenInfo = angular.copy( + scope.parentAuthEvent.children_election_info + ); + + // need to reload in case this changed in success screen.. + var credentials = getElectionCredentials(); + + // if it's a demo, yes, allow voting by default + scope.canVote = scope.isDemo || scope.isPreview; + scope.hasVoted = false; + scope.skippedElections = []; + childrenInfo.presentation.categories = _.map( + childrenInfo.presentation.categories, + function (category) { + category.events = _.map( + category.events, + function (election) { + var elCredentials = findElectionCredentials( + election.event_id, credentials + ); + var canVote = calculateCanVote(elCredentials); + var isVoter = calculateIsVoter(elCredentials); + if ( + elCredentials && + elCredentials.numSuccessfulLogins > 0 + ) { + scope.hasVoted = true; + } + var retValue = Object.assign( + {}, + election, + elCredentials || {}, + { + disabled: (!scope.isDemo && !scope.isPreview && !canVote), + hidden: (!scope.isDemo && !scope.isPreview && !isVoter) + } + ); + if (!!retValue.skipped) { + scope.skippedElections.push(retValue); + } + return retValue; + } + ); + return category; + }); + return childrenInfo; + } + + function getChildrenElectionsData() { + if (!scope.childrenElectionInfo || isChooserDisabled()) { + return; + } + + _.map( + scope.childrenElectionInfo.presentation.categories, + function (category) { + _.map( + category.events, + function (event) { + if (event.hidden) { + return {}; + } + return scope.simpleGetElection(event.event_id).then( + function (electionData) { + event.electionData = electionData; + $timeout(function () { + scope.$apply(); + }); + } + ); + } + ); + } + ); + } + + function chooseElection(electionId) { + scope.setState(scope.stateEnum.receivingElection, {}); + scope.retrieveElectionConfig(electionId + ""); + } + + scope.childrenElectionInfo = generateChildrenInfo(); + getChildrenElectionsData(); + + function checkDisabled() { + // if election chooser is disabled and can vote, then go to the first + // election in which it can vote + if (isChooserDisabled()) { + var orderedElectionIds = scope + .childrenElectionInfo + .natural_order; + // If it's a demo booth, do not rely on election credentials + if (scope.isDemo || scope.isPreview) { + scope.increaseDemoElectionIndex(); + if (scope.demoElectionIndex < orderedElectionIds.length) { + chooseElection( + orderedElectionIds[scope.demoElectionIndex] + ); + } else { + scope.hasVoted = true; + scope.canVote = false; + } + return; + } + + var credentials = getElectionCredentials(); + for (var i = 0; i < orderedElectionIds.length; i++) { + var electionId = orderedElectionIds[i]; + var elCredentials = findElectionCredentials( + electionId, + credentials + ); + if ( + !elCredentials.skipped && + !elCredentials.voted && + calculateCanVote(elCredentials) + ) { + chooseElection(electionId); + return; + } + } + // If redirected to no election but there are skipped elections, it + // means that the voter can re-login to vote again so we set the + // showSkippedElections flag + if (scope.skippedElections.length > 0) { + scope.showSkippedElections = true; + } + } + } + + checkDisabled(); + scope.chooseElection = chooseElection; + scope.goToVoterEligibility = function () { + scope.setState(scope.stateEnum.voterEligibilityScreen, {}); + }; } return { restrict: 'AE',