// This JavaScript just serves to improve the user experience // it is not at all required for submitting a vote. const form = document.getElementById('vote-form'); const commentInput = form.querySelector('textarea'); const radios = {}; // Make the comment input required if you select to oppose or abstain. for (const radio of form.querySelectorAll('input[type=radio]')) { radio.addEventListener('change', e => { commentInput.required = e.target.value != 'yes'; }); radios[radio.value] = radio; } // Disable the Enter key in the comment input (because // the PHP code replaces newlines with spaces anyway). commentInput.addEventListener('keypress', e => { if (e.key == 'Enter') { e.preventDefault(); } }); // Remind users what they voted for and prevent them from casting the same vote again. const script = form.querySelector('script[data-votes]'); const votes = JSON.parse(script.textContent); const previousVote = votes[mw.config.get('wgUserName')]; if (previousVote) { const note = document.createElement('span'); note.textContent = mw.message('vote-already-' + previousVote).text(); script.insertAdjacentElement('beforebegin', note); radios[previousVote].checked = true; const submitButton = form.querySelector('input[type=submit]'); submitButton.disabled = true; form.querySelector('h3').textContent = mw.message('vote-edit-your-vote').text(); for (const radio of Object.values(radios)) { radio.addEventListener('change', e => { if (e.target.value == previousVote) { submitButton.disabled = true; note.textContent = mw.message('vote-already-' + previousVote).text(); } else { submitButton.disabled = false; note.textContent = mw.message('vote-change', previousVote, e.target.value).text(); } }) } } // FUTURE: disable form if you are logged in but may not edit the page