Skip to content

Commit

Permalink
not alter view if no filter changed + new checboxes logic: filter out…
Browse files Browse the repository at this point in the history
… everythin if no checkbox checked
  • Loading branch information
brauliorivas committed Aug 14, 2024
1 parent 22dceaa commit e61673b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
16 changes: 15 additions & 1 deletion js/filters/collections/mcparticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ export function initMCParticleFilters(parentContainer, viewObjects) {

const { simStatus, generatorStatus, collectionNames } = checkboxes;

let areSimStatusChecked = false;

simStatus.forEach((checkbox) => {
const { checked } = checkbox.getValues();

if (checked) {
areSimStatusChecked = true;
}
});

const someSimStatusCheckbox = objectSatisfiesCheckbox(
object,
simStatus,
Expand All @@ -129,7 +139,11 @@ export function initMCParticleFilters(parentContainer, viewObjects) {
collectionNames,
]);

return someSimStatusCheckbox && normalCheckboxes;
if (areSimStatusChecked) {
return someSimStatusCheckbox && normalCheckboxes;
} else {
return normalCheckboxes;
}
};

return criteriaFunction;
Expand Down
14 changes: 11 additions & 3 deletions js/filters/collections/recoparticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
addCollectionTitle,
collectionFilterContainer,
} from "../components/lib.js";
import { RangeComponent } from "../components/range.js";
import { magnitudeRangeLogic, RangeComponent } from "../components/range.js";
import { rangeLogic } from "../components/range.js";

function renderRecoParticleFilters(viewObjects) {
Expand All @@ -19,12 +19,14 @@ function renderRecoParticleFilters(viewObjects) {
const charge = new RangeComponent("charge", "charge", "e");
const momentum = new RangeComponent("momentum", "momentum", "GeV");

const range = [energy, charge, momentum];
const range = [energy, charge];

range.forEach((rangeFilter) => {
container.appendChild(rangeFilter.render());
});

container.appendChild(momentum.render());

const [collectionNamesContainer, collectionCheckboxes] =
buildCollectionCheckboxes(
viewObjects.datatypes["edm4hep::ReconstructedParticle"].collection
Expand All @@ -37,13 +39,14 @@ function renderRecoParticleFilters(viewObjects) {
filters: {
range,
collectionCheckboxes,
momentum,
},
};
}

export function initRecoParticleFilters(parentContainer, viewObjects) {
const { container, filters } = renderRecoParticleFilters(viewObjects);
const { range, collectionCheckboxes } = filters;
const { range, collectionCheckboxes, momentum } = filters;

parentContainer.appendChild(container);

Expand All @@ -56,6 +59,11 @@ export function initRecoParticleFilters(parentContainer, viewObjects) {
}
}

const { min, max } = momentum.getValues();
if (!magnitudeRangeLogic(min, max, object, "momentum")) {
return false;
}

if (
!objectSatisfiesCheckbox(
object,
Expand Down
14 changes: 1 addition & 13 deletions js/filters/components/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,10 @@ export function objectSatisfiesCheckbox(
property,
logicFunction
) {
const checkedBoxes = [];

for (const checkbox of checkboxes) {
const { checked, value } = checkbox.getValues();

if (checked) {
checkedBoxes.push(value);
}
}

if (checkedBoxes.length === 0) {
return true;
}

for (const checked of checkedBoxes) {
if (logicFunction(checked, object, property)) {
if (checked && logicFunction(value, object, property)) {
return true;
}
}
Expand Down
2 changes: 0 additions & 2 deletions js/filters/components/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ export function buildCollectionCheckboxes(collection) {
selectAll.addEventListener("click", () => {
checkboxes.forEach((checkbox) => {
checkbox.checked(true);
checkbox.checkbox.dispatchEvent(new Event("change"));
});
});

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

Expand Down
62 changes: 54 additions & 8 deletions js/filters/filter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { setScroll, setScrollBarsPosition } from "../draw/scroll.js";
import { copyObject } from "../lib/copy.js";
import { checkEmptyObject } from "../lib/empty-object.js";
import { showMessage } from "../lib/messages.js";
import { initClusterFilters } from "./collections/cluster.js";
import { initMCParticleFilters } from "./collections/mcparticle.js";
import { initParticleIdFilters } from "./collections/particleid.js";
Expand Down Expand Up @@ -39,6 +41,34 @@ const filters = {
reset: null,
};

const filterOptionsChanged = (initialValues) => {
const allCheckboxes = document.getElementsByClassName(
"filter-input-checkbox"
);

for (let i = 0; i < allCheckboxes.length; i++) {
const checked = allCheckboxes[i].checked;
const initialValue = initialValues.checkboxes[i];

if (checked !== initialValue) {
return true;
}
}

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

for (let i = 0; i < allInputs.length; i++) {
const input = allInputs[i].value;
const initialInput = initialValues.range[i];

if (input !== initialInput) {
return true;
}
}

return false;
};

export function initFilters(
{ viewObjects, viewCurrentObjects },
collections,
Expand All @@ -47,7 +77,10 @@ export function initFilters(
) {
const criteriaFunctions = {};

let someInputChanged = false;
const initialValues = {
range: [],
checkboxes: [],
};

const resetFiltersContent = () => {
const content = document.getElementById("filters-content");
Expand Down Expand Up @@ -76,34 +109,47 @@ export function initFilters(
"filter-input-checkbox"
);

initialValues.checkboxes = [];

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

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

initialValues.range = [];

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

resetFiltersContent();

filters.apply = async () => {
if (!someInputChanged) {
const filtersChanged = filterOptionsChanged(initialValues);

if (!filtersChanged) {
return;
}

const filterOutValue = document.getElementById("invert-filter").checked;
const ids = filterOut(
viewObjects,
viewCurrentObjects,
criteriaFunctions,
filterOutValue
);

const isEmpty = checkEmptyObject(viewCurrentObjects);

if (isEmpty) {
showMessage("No objects satisfy the filter options");
return;
}

reconnectFunction(viewCurrentObjects, ids);
await render(viewCurrentObjects);
const { x, y } = filterScroll();
Expand Down

0 comments on commit e61673b

Please sign in to comment.