forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
453 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"trailingComma": true | ||
} | ||
"trailingComma": "none" | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions
15
decidim-elections/app/assets/javascripts/decidim/elections/voter/casting-vote.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// = require decidim/bulletin_board/decidim-bulletin_board | ||
|
||
$(async () => { | ||
const $castingVoteWrapper = $(".casting-vote-wrapper"); | ||
const { Client } = decidimBulletinBoard; | ||
|
||
const bulletinBoardClient = new Client({ | ||
apiEndpointUrl: $castingVoteWrapper.data("apiEndpointUrl") | ||
}); | ||
const messageId = $castingVoteWrapper.data("messageId"); | ||
|
||
await bulletinBoardClient.waitForPendingMessageToBeProcessed(messageId); | ||
|
||
$("form.update_vote_status").trigger("submit"); | ||
}); |
81 changes: 81 additions & 0 deletions
81
decidim-elections/app/assets/javascripts/decidim/elections/voter/new-vote.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-disable no-console */ | ||
// = require ./vote_questions.component | ||
|
||
$(async () => { | ||
const { VoteQuestionsComponent, setupVoteComponent } = window.Decidim; | ||
|
||
// UI Elements | ||
const $voteWrapper = $(".vote-wrapper"); | ||
const $ballotHash = $voteWrapper.find(".ballot-hash"); | ||
|
||
// Use the questions component | ||
const questionsComponent = new VoteQuestionsComponent($voteWrapper); | ||
questionsComponent.init(); | ||
$(document).on("on.zf.toggler", () => { | ||
// continue and back btn | ||
questionsComponent.init(); | ||
}); | ||
|
||
// Get the vote component and bind it to all UI events | ||
const voteComponent = setupVoteComponent($voteWrapper); | ||
await voteComponent.bindEvents({ | ||
onBindEncryptButton(onEventTriggered) { | ||
$(".button.confirm").on("click", onEventTriggered); | ||
}, | ||
onStart() {}, | ||
onVoteEncryption(validVoteFn) { | ||
const getFormData = (formData) => { | ||
return formData.serializeArray().reduce((acc, { name, value }) => { | ||
if (!acc[name]) { | ||
acc[name] = []; | ||
} | ||
acc[name] = [...acc[name], `${name}_${value}`]; | ||
return acc; | ||
}, {}); | ||
}; | ||
const formData = getFormData($voteWrapper.find(".answer_input")); | ||
validVoteFn(formData); | ||
}, | ||
castOrAuditBallot({ encryptedData, encryptedDataHash }) { | ||
$voteWrapper.find("#encrypting").addClass("hide"); | ||
$ballotHash.text(`Your ballot identifier is: ${encryptedDataHash}`); | ||
$voteWrapper.find("#ballot_decision").removeClass("hide"); | ||
|
||
const $form = $("form.new_vote"); | ||
$("#vote_encrypted_data", $form).val(encryptedData); | ||
$("#vote_encrypted_data_hash", $form).val(encryptedDataHash); | ||
}, | ||
onBindAuditBallotButton(onEventTriggered) { | ||
$(".audit_ballot").on("click", onEventTriggered); | ||
}, | ||
onBindCastBallotButton(onEventTriggered) { | ||
$(".cast_ballot").on("click", onEventTriggered); | ||
}, | ||
onAuditBallot(auditedData, auditedDataFileName) { | ||
const vote = JSON.stringify(auditedData); | ||
const link = document.createElement("a"); | ||
$voteWrapper.find(".button.cast_ballot").addClass("hide"); | ||
$voteWrapper.find(".button.back").removeClass("hide"); | ||
questionsComponent.voteCasted = true; | ||
|
||
link.setAttribute("href", `data:text/plain;charset=utf-8,${vote}`); | ||
link.setAttribute("download", auditedDataFileName); | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
}, | ||
onAuditComplete() { | ||
console.log("Audit completed"); | ||
}, | ||
onCastBallot() { | ||
questionsComponent.voteCasted = true; | ||
$(".cast_ballot").prop("disabled", true); | ||
}, | ||
onCastComplete() { | ||
console.log("Cast completed"); | ||
}, | ||
onInvalid() { | ||
console.log("Something went wrong."); | ||
} | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
decidim-elections/app/assets/javascripts/decidim/elections/voter/setup-preview.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// The wait time used to simulate the encryption of the vote during the preview | ||
const FAKE_ENCRYPTION_TIME = 1000; | ||
|
||
((exports) => { | ||
class PreviewVoteComponent { | ||
constructor({ electionUniqueId, voterUniqueId }) { | ||
this.electionUniqueId = electionUniqueId; | ||
this.voterUniqueId = voterUniqueId; | ||
} | ||
|
||
async bindEvents({ | ||
onBindEncryptButton, | ||
onStart, | ||
onVoteEncryption, | ||
castOrAuditBallot, | ||
onBindAuditBallotButton, | ||
onBindCastBallotButton, | ||
onAuditBallot, | ||
onCastBallot, | ||
onAuditComplete, | ||
onCastComplete, | ||
onInvalid | ||
}) { | ||
onBindEncryptButton(async () => { | ||
onStart(); | ||
onVoteEncryption( | ||
(plainVote) => { | ||
this.fakeEncrypt(plainVote).then((ballot) => { | ||
castOrAuditBallot(ballot); | ||
onBindAuditBallotButton(() => { | ||
onAuditBallot( | ||
ballot, | ||
`${this.voterUniqueId}-election-${this.electionUniqueId}.txt` | ||
); | ||
onAuditComplete(); | ||
}); | ||
|
||
onBindCastBallotButton(async () => { | ||
await onCastBallot(ballot); | ||
onCastComplete(); | ||
}); | ||
}); | ||
}, | ||
() => { | ||
onInvalid(); | ||
} | ||
); | ||
}); | ||
} | ||
async fakeEncrypt(plainVote) { | ||
await new Promise((resolve) => setTimeout(resolve, FAKE_ENCRYPTION_TIME)); | ||
|
||
return { | ||
encryptedData: plainVote, | ||
encryptedDataHash: this.generateHexString(64), | ||
auditableData: plainVote | ||
}; | ||
} | ||
generateHexString(length) { | ||
return Array(length). | ||
fill(""). | ||
map(() => Math.random().toString(16).charAt(2)). | ||
join(""); | ||
} | ||
} | ||
|
||
const setupVoteComponent = ($voteWrapper) => { | ||
const voterUniqueId = $voteWrapper.data("voterId"); | ||
const electionUniqueId = $voteWrapper.data("electionUniqueId"); | ||
|
||
return new PreviewVoteComponent({ | ||
electionUniqueId, | ||
voterUniqueId | ||
}); | ||
}; | ||
|
||
exports.Decidim = exports.Decidim || {}; | ||
exports.Decidim.setupVoteComponent = setupVoteComponent; | ||
})(window); |
58 changes: 58 additions & 0 deletions
58
decidim-elections/app/assets/javascripts/decidim/elections/voter/setup-vote.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// = require decidim/bulletin_board/decidim-bulletin_board | ||
// = require decidim/bulletin_board/dummy-voting-scheme | ||
|
||
// = require decidim/bulletin_board/election_guard-voting-scheme | ||
// Note: these gems will be moved to the application in the next release | ||
// = require voting_schemes/dummy/dummy | ||
// = require voting_schemes/electionguard/electionguard | ||
|
||
((exports) => { | ||
const setupVoteComponent = ($voteWrapper) => { | ||
const { VoteComponent } = window.decidimBulletinBoard; | ||
const { | ||
VoterWrapperAdapter: DummyVoterWrapperAdapter | ||
} = window.dummyVotingScheme; | ||
const { | ||
VoterWrapperAdapter: ElectionGuardVoterWrapperAdapter | ||
} = window.electionGuardVotingScheme; | ||
|
||
// Data | ||
const bulletinBoardClientParams = { | ||
apiEndpointUrl: $voteWrapper.data("apiEndpointUrl") | ||
}; | ||
const electionUniqueId = $voteWrapper.data("electionUniqueId"); | ||
const authorityPublicKeyJSON = JSON.stringify( | ||
$voteWrapper.data("authorityPublicKey") | ||
); | ||
const voterUniqueId = $voteWrapper.data("voterId"); | ||
const schemeName = $voteWrapper.data("schemeName"); | ||
|
||
// Use the correct voter wrapper adapter | ||
let voterWrapperAdapter = null; | ||
|
||
if (schemeName === "dummy") { | ||
voterWrapperAdapter = new DummyVoterWrapperAdapter({ | ||
voterId: voterUniqueId | ||
}); | ||
} else if (schemeName === "electionguard") { | ||
voterWrapperAdapter = new ElectionGuardVoterWrapperAdapter({ | ||
voterId: voterUniqueId, | ||
workerUrl: "/assets/electionguard/webworker.js" | ||
}); | ||
} else { | ||
throw new Error(`Voting scheme ${schemeName} not supported.`); | ||
} | ||
|
||
// Returns the vote component | ||
return new VoteComponent({ | ||
bulletinBoardClientParams, | ||
authorityPublicKeyJSON, | ||
electionUniqueId, | ||
voterUniqueId, | ||
voterWrapperAdapter | ||
}); | ||
}; | ||
|
||
exports.Decidim = exports.Decidim || {}; | ||
exports.Decidim.setupVoteComponent = setupVoteComponent; | ||
})(window); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.