summaryrefslogtreecommitdiff
path: root/resources
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2022-10-03 20:58:18 +0200
committerMartin Fischer <martin@push-f.com>2022-10-04 20:35:21 +0200
commit9066e158af51282c623bf71671e41b1893365b77 (patch)
treeffa6b280d6c04579594eab74dc7f7336f239b782 /resources
initial commit
Diffstat (limited to 'resources')
-rw-r--r--resources/optional.js51
1 files changed, 51 insertions, 0 deletions
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