From 9066e158af51282c623bf71671e41b1893365b77 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 3 Oct 2022 20:58:18 +0200 Subject: initial commit --- resources/optional.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 resources/optional.js (limited to 'resources/optional.js') diff --git a/resources/optional.js b/resources/optional.js new file mode 100644 index 0000000..c5e85e9 --- /dev/null +++ b/resources/optional.js @@ -0,0 +1,51 @@ +// 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 -- cgit v1.2.3