Skip to content

Commit dd84fd7

Browse files
fix: enable hash control over individual sublayers (#1635)
* fix: enable hash to control sub layers * chore: move functions to proper location
1 parent dd22022 commit dd84fd7

File tree

3 files changed

+64
-65
lines changed

3 files changed

+64
-65
lines changed

apps/client/src/models/AppModel.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import VectorLayer from "ol/layer/Vector";
3838
import VectorSource from "ol/source/Vector";
3939
import { Icon, Fill, Stroke, Style } from "ol/style";
4040

41+
import { setOLSubLayers } from "../utils/groupLayers";
42+
4143
class AppModel {
4244
/**
4345
* Initialize new AddModel
@@ -1072,17 +1074,16 @@ class AppModel {
10721074
if (wantedGl[layer]) {
10731075
// In addition, this looks like a group layer that has
10741076
// its sublayers specified and we should take care of that too
1075-
this.globalObserver.publish("layerswitcher.showLayer", {
1076-
layer: olLayer,
1077-
subLayersToShow: wantedGl[layer]?.split(","),
1078-
});
1077+
const subLayersToShow = wantedGl[layer]?.split(",");
1078+
setOLSubLayers(olLayer, subLayersToShow);
10791079
}
10801080
// On the other hand, if the layer to be shown does not exist in 'wantedGl',
10811081
// it means that we should show ALL the sublayers.
10821082
// For that we must publish the event slightly differently. (Also, see
10831083
// where we subscribe to layerswitcher.showLayer for further understanding.)
10841084
else {
1085-
this.globalObserver.publish("layerswitcher.showLayer", olLayer);
1085+
const allSubLayers = olLayer.get("allSubLayers");
1086+
setOLSubLayers(olLayer, allSubLayers);
10861087
}
10871088
}
10881089
// That's it for group layer. The other layers, the "normal"
@@ -1107,6 +1108,7 @@ class AppModel {
11071108
} else if (olLayer.get("layerType") === "group") {
11081109
// Tell the LayerSwitcher about it
11091110
this.globalObserver.publish("layerswitcher.hideLayer", olLayer);
1111+
olLayer.setVisible(false);
11101112
} else {
11111113
olLayer.setVisible(false);
11121114
}
@@ -1124,10 +1126,9 @@ class AppModel {
11241126
const olLayer = this.map
11251127
.getAllLayers()
11261128
.find((l) => l.get("name") === key);
1127-
this.globalObserver.publish("layerswitcher.showLayer", {
1128-
layer: olLayer,
1129-
subLayersToShow: wantedGl[key]?.split(","),
1130-
});
1129+
1130+
const subLayersToShow = wantedGl[key]?.split(",");
1131+
setOLSubLayers(olLayer, subLayersToShow);
11311132
}
11321133
}
11331134

@@ -1151,14 +1152,13 @@ class AppModel {
11511152
// Determine how we should call the layerswitcher.showLayer event.
11521153
// A: No sublayers specified for layer in 'wantedGl'. That means show ALL sublayers.
11531154
// B: Sublayers found in 'wantedGl'. Set visibility accordingly.
1154-
const param =
1155-
wantedGl[layer] === undefined
1156-
? olLayer
1157-
: {
1158-
layer: olLayer,
1159-
subLayersToShow: wantedGl[layer]?.split(","),
1160-
};
1161-
this.globalObserver.publish("layerswitcher.showLayer", param);
1155+
if (wantedGl[layer] === undefined) {
1156+
const allSubLayers = olLayer.get("allSubLayers");
1157+
setOLSubLayers(olLayer, allSubLayers);
1158+
} else {
1159+
const subLayersToShow = wantedGl[layer]?.split(",");
1160+
setOLSubLayers(olLayer, subLayersToShow);
1161+
}
11621162
}
11631163
});
11641164
}

apps/client/src/plugins/LayerSwitcher/LayerSwitcherProvider.js

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import LayerSwitcherView from "./LayerSwitcherView.js";
1212
import { useLayerZoomWarningSnackbar } from "./useLayerZoomWarningSnackbar";
1313
import { functionalOk as functionalCookieOk } from "../../models/Cookie";
1414
import LocalStorageHelper from "../../utils/LocalStorageHelper";
15+
import { setOLSubLayers, getAllLayerIdsInGroup } from "../../utils/groupLayers";
1516

1617
export const QUICK_ACCESS_KEY = "quickAccess";
1718
export const QUICK_ACCESS_LS_KEY = "quickAccessLayers";
@@ -96,54 +97,6 @@ const LayerZoomVisibleSnackbarProvider = ({ children, layers }) => {
9697
);
9798
};
9899

99-
const setOLSubLayers = (olLayer, visibleSubLayersArray) => {
100-
if (visibleSubLayersArray.length === 0) {
101-
// Fix underlying source
102-
olLayer.getSource().updateParams({
103-
// Ensure that the list of sublayers is emptied (otherwise they'd be
104-
// "remembered" the next time user toggles group)
105-
LAYERS: "",
106-
// Remove any filters
107-
CQL_FILTER: null,
108-
});
109-
110-
// Hide the layer in OL
111-
olLayer.setVisible(false);
112-
} else {
113-
// Set LAYERS and STYLES so that the exact sublayers that are needed
114-
// will be visible
115-
olLayer.getSource().updateParams({
116-
// join(), so we always provide a string as value to LAYERS
117-
LAYERS: visibleSubLayersArray.join(),
118-
// Filter STYLES to only contain styles for currently visible layers,
119-
// and maintain the order from layersInfo (it's crucial that the order
120-
// of STYLES corresponds exactly to the order of LAYERS!)
121-
STYLES: Object.entries(olLayer.layersInfo)
122-
.filter((k) => visibleSubLayersArray.indexOf(k[0]) !== -1)
123-
.map((l) => l[1].style)
124-
.join(","),
125-
CQL_FILTER: null,
126-
});
127-
olLayer.set("subLayers", visibleSubLayersArray);
128-
olLayer.setVisible(true);
129-
}
130-
};
131-
132-
// TODO move to common. Copied from LayerGroup.js
133-
const getAllLayerIdsInGroup = (group) => {
134-
if (!group) {
135-
return [];
136-
}
137-
138-
if (!group.children) {
139-
return [group.id];
140-
} else {
141-
return group.children.flatMap((c) => {
142-
return getAllLayerIdsInGroup(c);
143-
});
144-
}
145-
};
146-
147100
const getGroupConfigById = (tree, groupId) => {
148101
if (!tree) {
149102
return null;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export const setOLSubLayers = (olLayer, visibleSubLayersArray) => {
2+
if (visibleSubLayersArray.length === 0) {
3+
// Fix underlying source
4+
olLayer.getSource().updateParams({
5+
// Ensure that the list of sublayers is emptied (otherwise they'd be
6+
// "remembered" the next time user toggles group)
7+
LAYERS: "",
8+
// Remove any filters
9+
CQL_FILTER: null,
10+
});
11+
12+
// Hide the layer in OL
13+
olLayer.setVisible(false);
14+
} else {
15+
// Set LAYERS and STYLES so that the exact sublayers that are needed
16+
// will be visible
17+
olLayer.getSource().updateParams({
18+
// join(), so we always provide a string as value to LAYERS
19+
LAYERS: visibleSubLayersArray.join(),
20+
// Filter STYLES to only contain styles for currently visible layers,
21+
// and maintain the order from layersInfo (it's crucial that the order
22+
// of STYLES corresponds exactly to the order of LAYERS!)
23+
STYLES: Object.entries(olLayer.layersInfo)
24+
.filter((k) => visibleSubLayersArray.indexOf(k[0]) !== -1)
25+
.map((l) => l[1].style)
26+
.join(","),
27+
CQL_FILTER: null,
28+
});
29+
olLayer.set("subLayers", visibleSubLayersArray);
30+
olLayer.setVisible(true);
31+
}
32+
};
33+
34+
export const getAllLayerIdsInGroup = (group) => {
35+
if (!group) {
36+
return [];
37+
}
38+
39+
if (!group.children) {
40+
return [group.id];
41+
} else {
42+
return group.children.flatMap((c) => {
43+
return getAllLayerIdsInGroup(c);
44+
});
45+
}
46+
};

0 commit comments

Comments
 (0)