Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Toggle only filtered layers in Groups #1599

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions apps/client/src/plugins/LayerSwitcher/LayerSwitcherProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,27 @@ const createDispatch = (map, staticLayerConfig, staticLayerTree) => {
.filter((l) => l.get("layerType") === "base");

return {
setLayerVisibility(layerId, visible) {
setLayerVisibility(layerId, visible, filteredSubLayers) {
// `filteredSubLayers` is an array if there is an active filter/search in
// the LayerSwitcher. In that case we only wants to toggle the layers
// that are shown in the UI, the filter hits.

const olLayer = map.getAllLayers().find((l) => l.get("name") === layerId);
olLayer.setVisible(visible);

// VectorLayers have no sublayers.
if (!(olLayer instanceof VectorLayer)) {
if (visible) {
// For GroupLayers:
const allSubLayers = staticLayerConfig[layerId]?.allSubLayers;
if (allSubLayers) {
olLayer.set("subLayers", allSubLayers);
setOLSubLayers(olLayer, allSubLayers);
if (filteredSubLayers instanceof Array) {
olLayer.set("subLayers", filteredSubLayers);
setOLSubLayers(olLayer, filteredSubLayers);
} else {
// For GroupLayers:
const allSubLayers = staticLayerConfig[layerId]?.allSubLayers;
if (allSubLayers) {
olLayer.set("subLayers", allSubLayers);
setOLSubLayers(olLayer, allSubLayers);
}
}
} else {
olLayer.set("subLayers", []);
Expand Down Expand Up @@ -230,15 +239,28 @@ const createDispatch = (map, staticLayerConfig, staticLayerTree) => {
olLayer.set("subLayers", sortedCurrentSubLayers);
setOLSubLayers(olLayer, sortedCurrentSubLayers);
},
setGroupVisibility(groupId, visible) {
// A Group is the grouping defined in the LayerSwitcherSettings
setGroupVisibility(groupId, visible, filterHits) {
// `filterHits` is a set if there is an active filter/search in the
// LayerSwitcher. In that case we only wants to toggle the layers that
// are shown in the UI, the filter hits.

const groupTree = getGroupConfigById(staticLayerTree, groupId);
const allLayerIdsInGroup = getAllLayerIdsInGroup(groupTree);

allLayerIdsInGroup.forEach((id) => {
const olLayer = map.getAllLayers().find((l) => l.get("name") === id);
olLayer.setVisible(visible);
});
if (filterHits instanceof Set) {
Array.from(filterHits).forEach((id) => {
const olLayer = map.getAllLayers().find((l) => l.get("name") === id);
olLayer.setVisible(visible);
});
} else {
allLayerIdsInGroup.forEach((id) => {
const olLayer = map.getAllLayers().find((l) => l.get("name") === id);
olLayer.setVisible(visible);
});
}
},
// A GroupLayer here is a layer with Sublayers.
setGroupLayerVisibility(layerId, visible) {
const olLayer = map.getAllLayers().find((l) => l.get("name") === layerId);
const allSubLayers = new Set(olLayer.get("subLayers"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ function GroupLayer({

const handleLayerItemClick = () => {
if (layerIsToggled) {
layerSwitcherDispatch.setLayerVisibility(layerId, false);
layerSwitcherDispatch.setLayerVisibility(layerId, false, subLayersToShow);
} else {
layerSwitcherDispatch.setLayerVisibility(layerId, true);
layerSwitcherDispatch.setLayerVisibility(layerId, true, subLayersToShow);
}
};

Expand Down
12 changes: 10 additions & 2 deletions apps/client/src/plugins/LayerSwitcher/components/LayerGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,17 @@ const LayerGroup = ({
toggleState={toggleState}
clickHandler={() => {
if (isToggled) {
layerSwitcherDispatch.setGroupVisibility(groupId, false);
layerSwitcherDispatch.setGroupVisibility(
groupId,
false,
filterHits
);
} else {
layerSwitcherDispatch.setGroupVisibility(groupId, true);
layerSwitcherDispatch.setGroupVisibility(
groupId,
true,
filterHits
);
}
}}
/>
Expand Down