Skip to content

Commit 306b67e

Browse files
committed
Update tooltip layers when a filter is applied
1 parent 15434fb commit 306b67e

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

assets/src/modules/Tooltip.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @copyright 2024 3Liz
55
* @license MPL-2.0
66
*/
7-
import { mainEventDispatcher } from '../modules/Globals.js';
7+
import { mainEventDispatcher, mainLizmap } from '../modules/Globals.js';
88
import { TooltipLayersConfig } from './config/Tooltip.js';
99
import WMS from '../modules/WMS.js';
1010
import GeoJSON from 'ol/format/GeoJSON.js';
@@ -32,6 +32,15 @@ export default class Tooltip {
3232
this._activeTooltipLayer;
3333
this._tooltipLayers = new Map();
3434
this.activeLayerOrder = null;
35+
36+
this._lizmap3.events.on({
37+
layerFilterParamChanged: evt => {
38+
this._applyFilter(evt.featureType);
39+
},
40+
layerFilteredFeaturesChanged: evt => {
41+
this._applyFilter(evt.featureType);
42+
}
43+
});
3544
}
3645

3746
/**
@@ -103,6 +112,7 @@ export default class Tooltip {
103112

104113
if (tooltipLayer) {
105114
this._activeTooltipLayer = tooltipLayer;
115+
this._applyFilter(layerName);
106116
} else {
107117
const url = `${lizUrls.service.replace('service?','features/tooltips?')}&layerId=${layerTooltipCfg.id}`;
108118

@@ -116,15 +126,18 @@ export default class Tooltip {
116126
stroke: stroke,
117127
});
118128

129+
// Initially hidden, will be set to 1 when features are loaded and filter is applied
130+
// to avoid visual flickering
131+
// Using the visible property of the layer does not work
119132
this._activeTooltipLayer = new VectorLayer({
133+
opacity: 0,
120134
source: new VectorSource({
121135
url: url,
122136
format: new GeoJSON(),
123137
}),
124138
style: vectorStyle
125139
});
126140

127-
128141
// Handle points layers with QGIS style
129142
if (this._displayLayerStyle) {
130143
const wmsParams = {
@@ -154,6 +167,7 @@ export default class Tooltip {
154167

155168
// Load tooltip layer
156169
this._activeTooltipLayer.once('sourceready', () => {
170+
this._applyFilter(layerName);
157171
mainEventDispatcher.dispatch('tooltip.loaded');
158172
});
159173

@@ -270,4 +284,54 @@ export default class Tooltip {
270284
this.activeLayerOrder = null;
271285
mainEventDispatcher.dispatch('tooltip.deactivated');
272286
}
287+
288+
_applyFilter(layerName) {
289+
const tooltipLayer = this._tooltipLayers.get(layerName);
290+
291+
if (!tooltipLayer) {
292+
// No tooltip layer for this feature type
293+
return;
294+
}
295+
296+
const expFilter = mainLizmap.state.rootMapGroup.getMapLayerByName(layerName).itemState.expressionFilter;
297+
let featureIds = [];
298+
299+
const hideFilteredFeatures = () => {
300+
for (const feature of tooltipLayer.getSource().getFeatures()) {
301+
// If the feature id is not in the list, hide it
302+
if (featureIds.length === 0 || featureIds.includes(feature.getId())) {
303+
feature.setStyle(null);
304+
} else {
305+
feature.setStyle(new Style({}));
306+
}
307+
}
308+
// Display the layer now all styles are applied
309+
tooltipLayer.setOpacity(1);
310+
};
311+
312+
if(!expFilter) {
313+
hideFilteredFeatures();
314+
return;
315+
}
316+
317+
if (expFilter.startsWith('$id IN ')) {
318+
re = /[() ]/g;
319+
featureIds = expFilter.replace('$id IN ', '').replace(re, '').split(',').map(Number);
320+
hideFilteredFeatures();
321+
} else {
322+
const wfsParams = {
323+
TYPENAME: layerName,
324+
// No geometry needed
325+
GEOMETRYNAME: 'none',
326+
// Force to return only the featureId
327+
PROPERTYNAME: 'no_feature_properties',
328+
// Filter
329+
EXP_FILTER: expFilter
330+
};
331+
mainLizmap.wfs.getFeature(wfsParams).then(result => {
332+
featureIds = result.features.map(f => parseInt(f.id.split('.')[1]));
333+
hideFilteredFeatures();
334+
});
335+
}
336+
}
273337
}

0 commit comments

Comments
 (0)