ui: js: Make select all checkbox option available more broadly

- Make the code work outside backups module.

- Move code to main.js so that any app can use this functionality.

- Make the code work for multiple such form fields in the same page.

- Use only pure JS, don't use jQuery.

- Add event handlers only after DOM content is loaded to avoid race conditions.

Tests performed:

- Checking the select-all button checks all options.

- De-checking the select-all button de-checks all options.

- De-checking one option when everything is checked, de-checks the select-all
button.

- Checking the last option when everything else is checked, checks the
select-all button.

- When loading a schedule page with all options checked, select-all button is
checked.

- When loading a schedule page with some option unchecked, select-all button is
unchecked.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2021-01-21 20:33:28 -05:00
1 parent 77face68b0
commit 9b8388734d
6 files changed
+67 -77

No files matched your search

+63
View File
@@ -121,3 +121,66 @@ window.addEventListener('pageshow', function(event) {
element.remove();
}
});
/*
* Select all option for multiple checkboxes.
*/
document.addEventListener('DOMContentLoaded', function(event) {
let parents = document.querySelectorAll('ul.has-select-all');
for (const parent of parents) {
let li = document.createElement('li');
let label = document.createElement('label');
label.for = "select_all";
let checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.setAttribute('class', 'select-all');
label.appendChild(checkbox);
li.appendChild(label);
parent.insertBefore(li, parent.childNodes[0]);
setSelectAllValue(parent);
checkbox.addEventListener('change', onSelectAllChanged);
options = parent.querySelectorAll('input.has-select-all');
for (const option of options) {
option.addEventListener('change', onSelectAllOptionsChanged);
}
}
});
// When there is a change on the "select all" checkbox, set the checked property
// of all the checkboxes to the value of the "select all" checkbox
function onSelectAllChanged(event) {
const selectAllCheckbox = event.currentTarget;
const parent = selectAllCheckbox.parentElement.parentElement.parentElement;
const options = parent.querySelectorAll('input.has-select-all');
for (const option of options) {
option.checked = selectAllCheckbox.checked;
}
}
// When there is a change on a checkbox controlled by a select all checkbox,
// update the value of checkbox.
function onSelectAllOptionsChanged(event) {
const parent = event.currentTarget.parentElement.parentElement.parentElement;
setSelectAllValue(parent);
}
// Set/reset the checked property of "select all" checkbox based on whether all
// checkboxes it controls are checked.
function setSelectAllValue(parent) {
const options = parent.querySelectorAll('input.has-select-all');
let enableSelectAll = true;
for (const option of options) {
if (!option.checked) {
enableSelectAll = false;
break;
}
}
parent.querySelector('.select-all').checked = enableSelectAll;
}