FabulaVotes/static_files/vote_handler.js

41 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

/**
* This script handles disabling/enabling the different choices
* accross the vote options.
* This prevents sending weird votes that choose the same person for multiple truths.
*/
const vote_select_tracking = new Map();
function log_select_change(event) {
const changed_select = event.target;
const original_index = vote_select_tracking.get(changed_select)
for (const select of vote_select_tracking) {
select.at(0)[original_index].disabled = false;
// Don't disable the default entry, or the one we just moved to.
if (changed_select.selectedIndex !== 0 && select.at(0) !== changed_select)
select.at(0)[changed_select.selectedIndex].disabled = true
}
// Make sure to change the selected *attribute*, because the .selected property doesn't impact
// the form output, it seems.
changed_select[original_index].removeAttribute("selected");
changed_select[changed_select.selectedIndex].setAttribute("selected", true);
// Update with the new tracked index.
vote_select_tracking.set(changed_select, changed_select.selectedIndex);
}
const selected_indices = [];
for (const select of document.getElementsByTagName("select")) {
select.addEventListener("change", log_select_change);
vote_select_tracking.set(select, select.selectedIndex);
if (select.selectedIndex !== 0) {
selected_indices.push(select.selectedIndex);
}
}
for (const select of document.getElementsByTagName("select")) {
for (const selected_index of selected_indices) {
select[selected_index].disabled = true;
}
}