/* global ajax,fetch*/ /* global display_questions,send_vote*/ /* global show,hide,display_if,translate,display_msg,dompurify_sanitize*/ const INDEX_URL = window.location.pathname.startsWith( '/dev/' ) ? '/dev-api/' : '/api/'; let g_poll; let g_votes = []; // Holds the selected options for each question let g_num_vote_attempts; // used to identify bad connection. It measures the number of failed vote attempts. let g_query_string; let g_lang; let g_pv_code; let g_contact_hash; let dictionary; document.addEventListener( 'DOMContentLoaded' , async () => { if ( !parse_url_input() ) return; await load_language_file( g_lang ); translate_page(); set_vote_scr_events(); display_voting_form(); }); const parse_url_input = () => { const raw_query_string = window.location.search ? window.location.search.substring( 1 ) : ''; const query_string = raw_query_string.replace( /[^a-zA-Z0-9]/g , '' ); if ( !query_string || query_string.length != 21 ) { document.getElementById( 'poll-title' ).textContent = translate( 'The voting link is invalid or incomplete' ); return false; } g_query_string = query_string; g_lang = query_string.substring( 0 , 2 ); g_pv_code = query_string.substring( 2 , 18 ); g_contact_hash = query_string.substring( 18 , 21 ); return true; }; const load_language_file = async ( lang_code ) => { try { const response = await fetch( `js/lang/${lang_code}.json?2` ); if ( !response.ok ) { throw new Error( `HTTP ${response.status}` ); } dictionary = await response.json(); } catch ( error ) { console.error( 'Failed to load language file' , error ); dictionary = {}; } }; const translate_page = () => { document.querySelectorAll( '.text' ).forEach( ( $el ) => { $el.textContent = translate( $el.textContent ); }); document.body.classList.add( translate( 'dir' ) ); }; const set_vote_scr_events = () => { document.getElementById( 'btn-vote' ).addEventListener( 'click' , send_vote ); }; const display_voting_form = () => { // // Set `unsubscribe` URL // document.getElementById( 'url-unsubscribe' ).setAttribute( 'href' , `https://app.voteclick.net/u${ g_query_string }` ); // // Get poll // const params = `action=get_poll_for_vote&pv_code=${ encodeURIComponent( g_pv_code ) }`; ajax( params, ( response ) => { if ( !response.success ) { const error = response.error; const is_invalid_code = ( error === 'The link you have clicked is invalid' ); document.getElementById( 'poll-title' ).textContent = translate( error ); display_if( 'box-unsubscribe' , !is_invalid_code ); hide( 'box-voting-ends-on' ); return; } console.log( 'poll-for-vote' , response ); g_poll = response.data.poll; const voter = response.data.voter; document.getElementById( 'poll-sender' ).textContent = g_poll.sender; document.getElementById( 'poll-title' ).textContent = g_poll.title; document.getElementById( 'date-end-str' ).textContent = g_poll.datetime_end_str; display_questions( g_poll.questions , 'poll-questions' ); // js-get-poll-for-vote if ( g_poll.logo_url !== '' ) { const $logo = document.getElementById( 'logo' ); $logo.src = `${ g_poll.logo_url }?${ Math.floor( Math.random() * 1000000 ) }`; $logo.alt = g_poll.sender; $logo.onload = () => { show( 'box-logo' ); }; $logo.onerror = () => { hide( 'box-logo' ); }; } if ( voter.who !== '' ) { document.getElementById( 'who' ).textContent = voter.who; show( 'box-who' ); } if ( voter.weight > 1 ) { document.getElementById( 'weight' ).textContent = voter.weight; show( 'box-weight' ); } if ( g_poll.type === 'SECRET' ) { display_msg( 'This is a secret vote' , 'voting-form-top-msg' ); } if ( g_poll.intro !== '' ) { const $intro = document.getElementById( 'intro' ); $intro.innerHTML = dompurify_sanitize(g_poll.intro); $intro.querySelectorAll( 'a' ).forEach( $link => $link.style.textDecoration = 'underline' ); show( 'box-intro' ); } show( 'p-btn-vote' ); show( 'box-unsubscribe' ); show( 'box-voting-ends-on' ); document.getElementById('link-website').href = `https://${translate('lang-domain')}`; document.getElementById('link-accessibility').href = `https://${translate('lang-domain')}/p/accessibility.html`; // // Reset vote related globals // g_num_vote_attempts = 0; g_votes = g_poll.questions.map( () => [] ); // this function may be called multiple times, so we need to reset here. }); };