@@ -7,77 +7,80 @@ const { setupVoteComponent } = window.Decidim;
77$ ( async ( ) => {
88 // UI Elements
99 const $voteWrapper = $ ( ".vote-wrapper" ) ;
10- const $ballotHash = $voteWrapper . find ( ".ballot-hash" ) ;
11- const ballotStyleId = $voteWrapper . data ( "ballotStyleId" ) ;
1210
13- // Use the questions component
14- const questionsComponent = new VoteQuestionsComponent ( $voteWrapper ) ;
15- questionsComponent . init ( ) ;
16- $ ( document ) . on ( "on.zf.toggler" , ( ) => {
17- // continue and back btn
11+ if ( $voteWrapper . length ) {
12+ const $ballotHash = $voteWrapper . find ( ".ballot-hash" ) ;
13+ const ballotStyleId = $voteWrapper . data ( "ballotStyleId" ) ;
14+
15+ // Use the questions component
16+ const questionsComponent = new VoteQuestionsComponent ( $voteWrapper ) ;
1817 questionsComponent . init ( ) ;
19- } ) ;
18+ $ ( document ) . on ( "on.zf.toggler" , ( ) => {
19+ // continue and back btn
20+ questionsComponent . init ( ) ;
21+ } ) ;
2022
21- // Get the vote component and bind it to all UI events
22- const voteComponent = setupVoteComponent ( $voteWrapper ) ;
23- await voteComponent . bindEvents ( {
24- onBindEncryptButton ( onEventTriggered ) {
25- $ ( ".button.confirm" ) . on ( "click" , onEventTriggered ) ;
26- } ,
27- onStart ( ) { } ,
28- onVoteEncryption ( validVoteFn ) {
29- const getFormData = ( formData ) => {
30- return formData . serializeArray ( ) . reduce ( ( acc , { name, value } ) => {
31- if ( ! acc [ name ] ) {
32- acc [ name ] = [ ] ;
33- }
34- acc [ name ] = [ ...acc [ name ] , `${ name } _${ value } ` ] ;
35- return acc ;
36- } , { } ) ;
37- } ;
38- const formData = getFormData ( $voteWrapper . find ( ".answer_input" ) ) ;
39- validVoteFn ( formData , ballotStyleId ) ;
40- } ,
41- castOrAuditBallot ( { encryptedData, encryptedDataHash } ) {
42- $voteWrapper . find ( "#encrypting" ) . addClass ( "hide" ) ;
43- $ballotHash . text ( encryptedDataHash ) ;
44- $voteWrapper . find ( "#ballot_decision" ) . removeClass ( "hide" ) ;
23+ // Get the vote component and bind it to all UI events
24+ const voteComponent = setupVoteComponent ( $voteWrapper ) ;
25+ await voteComponent . bindEvents ( {
26+ onBindEncryptButton ( onEventTriggered ) {
27+ $ ( ".button.confirm" ) . on ( "click" , onEventTriggered ) ;
28+ } ,
29+ onStart ( ) { } ,
30+ onVoteEncryption ( validVoteFn ) {
31+ const getFormData = ( formData ) => {
32+ return formData . serializeArray ( ) . reduce ( ( acc , { name, value } ) => {
33+ if ( ! acc [ name ] ) {
34+ acc [ name ] = [ ] ;
35+ }
36+ acc [ name ] = [ ...acc [ name ] , `${ name } _${ value } ` ] ;
37+ return acc ;
38+ } , { } ) ;
39+ } ;
40+ const formData = getFormData ( $voteWrapper . find ( ".answer_input" ) ) ;
41+ validVoteFn ( formData , ballotStyleId ) ;
42+ } ,
43+ castOrAuditBallot ( { encryptedData, encryptedDataHash } ) {
44+ $voteWrapper . find ( "#encrypting" ) . addClass ( "hide" ) ;
45+ $ballotHash . text ( encryptedDataHash ) ;
46+ $voteWrapper . find ( "#ballot_decision" ) . removeClass ( "hide" ) ;
4547
46- const $form = $ ( "form.new_vote" ) ;
47- $ ( "#vote_encrypted_data" , $form ) . val ( encryptedData ) ;
48- $ ( "#vote_encrypted_data_hash" , $form ) . val ( encryptedDataHash ) ;
49- } ,
50- onBindAuditBallotButton ( onEventTriggered ) {
51- $ ( ".audit_ballot" ) . on ( "click" , onEventTriggered ) ;
52- } ,
53- onBindCastBallotButton ( onEventTriggered ) {
54- $ ( ".cast_ballot" ) . on ( "click" , onEventTriggered ) ;
55- } ,
56- onAuditBallot ( auditedData , auditedDataFileName ) {
57- const vote = JSON . stringify ( auditedData ) ;
58- const link = document . createElement ( "a" ) ;
59- $voteWrapper . find ( ".button.cast_ballot" ) . addClass ( "hide" ) ;
60- $voteWrapper . find ( ".button.back" ) . removeClass ( "hide" ) ;
61- questionsComponent . voteCasted = true ;
48+ const $form = $ ( "form.new_vote" ) ;
49+ $ ( "#vote_encrypted_data" , $form ) . val ( encryptedData ) ;
50+ $ ( "#vote_encrypted_data_hash" , $form ) . val ( encryptedDataHash ) ;
51+ } ,
52+ onBindAuditBallotButton ( onEventTriggered ) {
53+ $ ( ".audit_ballot" ) . on ( "click" , onEventTriggered ) ;
54+ } ,
55+ onBindCastBallotButton ( onEventTriggered ) {
56+ $ ( ".cast_ballot" ) . on ( "click" , onEventTriggered ) ;
57+ } ,
58+ onAuditBallot ( auditedData , auditedDataFileName ) {
59+ const vote = JSON . stringify ( auditedData ) ;
60+ const link = document . createElement ( "a" ) ;
61+ $voteWrapper . find ( ".button.cast_ballot" ) . addClass ( "hide" ) ;
62+ $voteWrapper . find ( ".button.back" ) . removeClass ( "hide" ) ;
63+ questionsComponent . voteCasted = true ;
6264
63- link . setAttribute ( "href" , `data:text/plain;charset=utf-8,${ vote } ` ) ;
64- link . setAttribute ( "download" , auditedDataFileName ) ;
65- document . body . appendChild ( link ) ;
66- link . click ( ) ;
67- document . body . removeChild ( link ) ;
68- } ,
69- onAuditComplete ( ) {
70- console . log ( "Audit completed" ) ;
71- } ,
72- onCastBallot ( ) {
73- questionsComponent . voteCasted = true ;
74- $ ( ".cast_ballot" ) . prop ( "disabled" , true ) ;
75- } ,
76- onCastComplete ( ) {
77- console . log ( "Cast completed" ) ;
78- } ,
79- onInvalid ( ) {
80- console . log ( "Something went wrong." ) ;
81- }
82- } ) ;
65+ link . setAttribute ( "href" , `data:text/plain;charset=utf-8,${ vote } ` ) ;
66+ link . setAttribute ( "download" , auditedDataFileName ) ;
67+ document . body . appendChild ( link ) ;
68+ link . click ( ) ;
69+ document . body . removeChild ( link ) ;
70+ } ,
71+ onAuditComplete ( ) {
72+ console . log ( "Audit completed" ) ;
73+ } ,
74+ onCastBallot ( ) {
75+ questionsComponent . voteCasted = true ;
76+ $ ( ".cast_ballot" ) . prop ( "disabled" , true ) ;
77+ } ,
78+ onCastComplete ( ) {
79+ console . log ( "Cast completed" ) ;
80+ } ,
81+ onInvalid ( ) {
82+ console . log ( "Something went wrong." ) ;
83+ }
84+ } ) ;
85+ }
8386} ) ;
0 commit comments