Skip to content

Commit

Permalink
Improve the JS to support un-checking all for element state #488
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Sep 7, 2023
1 parent 36436d6 commit f08e705
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions scancodeio/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,28 @@ function setupSelectCheckbox() {
// Check if selectAllCheckbox or actionDropdown does not exist before proceeding
if (!selectAllCheckbox || !actionDropdown) return;

// Check if at least one row is checked and update the elements state accordingly
function updateButtonAndDropdownState() {
const atLeastOneChecked = Array.from(rowCheckboxes).some((cb) => cb.checked);

// Toggle the 'is-disabled' class and 'disabled' attribute of the button
if (atLeastOneChecked) {
actionDropdown.classList.remove("is-disabled");
dropdownButton.removeAttribute("disabled");
} else {
actionDropdown.classList.add("is-disabled");
dropdownButton.setAttribute("disabled", "disabled");
}
}

// Add a click event listener to the "Select All" checkbox
selectAllCheckbox.addEventListener("click", function () {
// Toggle the selection of all row checkboxes
rowCheckboxes.forEach((checkbox) => {
checkbox.checked = selectAllCheckbox.checked;
});

updateButtonAndDropdownState();
});

// Add a click event listener to each row checkbox to handle individual selections
Expand All @@ -196,17 +212,7 @@ function setupSelectCheckbox() {
// Update the last checked checkbox
lastChecked = checkbox;

// Check if at least one row checkbox is checked
const atLeastOneChecked = Array.from(rowCheckboxes).some((cb) => cb.checked);

// Toggle the 'is-disabled' class and 'disabled' attribute of the button
if (atLeastOneChecked) {
actionDropdown.classList.remove("is-disabled");
dropdownButton.removeAttribute("disabled");
} else {
actionDropdown.classList.add("is-disabled");
dropdownButton.setAttribute("disabled", "disabled");
}
updateButtonAndDropdownState();

// Check if all row checkboxes are checked and update the "Select All" checkbox accordingly
selectAllCheckbox.checked = Array.from(rowCheckboxes).every((cb) => cb.checked);
Expand Down

0 comments on commit f08e705

Please sign in to comment.