Skip to content

Commit

Permalink
don't apply filtering if no input changed
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliorivas committed Aug 13, 2024
1 parent 6953f08 commit 22dceaa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions js/filters/components/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const createCheckbox = () => {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.classList.add("filter-checkbox");
checkbox.classList.add("filter-input-checkbox");

return checkbox;
};

Expand Down
10 changes: 8 additions & 2 deletions js/filters/components/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ export function buildCollectionCheckboxes(collection) {
container.appendChild(checkboxesContainer);

selectAll.addEventListener("click", () => {
checkboxes.forEach((checkbox) => checkbox.checked(true));
checkboxes.forEach((checkbox) => {
checkbox.checked(true);
checkbox.checkbox.dispatchEvent(new Event("change"));
});
});

clearAll.addEventListener("click", () => {
checkboxes.forEach((checkbox) => checkbox.checked(false));
checkboxes.forEach((checkbox) => {
checkbox.checked(false);
checkbox.checkbox.dispatchEvent(new Event("change"));
});
});

return [container, checkboxes];
Expand Down
1 change: 1 addition & 0 deletions js/filters/components/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const createInput = (placeholder) => {
input.type = "number";
input.placeholder = placeholder;
input.classList.add("range-input");
input.classList.add("filter-input-range");

return input;
};
Expand Down
23 changes: 23 additions & 0 deletions js/filters/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function initFilters(
) {
const criteriaFunctions = {};

let someInputChanged = false;

const resetFiltersContent = () => {
const content = document.getElementById("filters-content");
content.replaceChildren();
Expand All @@ -69,11 +71,32 @@ export function initFilters(

const filterOutCheckbox = document.getElementById("invert-filter");
filterOutCheckbox.checked = false;

const allCheckboxes = document.getElementsByClassName(
"filter-input-checkbox"
);

for (const input of allCheckboxes) {
input.addEventListener("change", () => {
someInputChanged = true;
});
}

const allInputs = document.getElementsByClassName("filter-input-range");

for (const input of allInputs) {
input.addEventListener("input", () => {
someInputChanged = true;
});
}
};

resetFiltersContent();

filters.apply = async () => {
if (!someInputChanged) {
return;
}
const filterOutValue = document.getElementById("invert-filter").checked;
const ids = filterOut(
viewObjects,
Expand Down

0 comments on commit 22dceaa

Please sign in to comment.