Skip to content

Commit 4776759

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

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

assets/src/modules/Tooltip.js

Lines changed: 64 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,13 @@ export default class Tooltip {
3232
this._activeTooltipLayer;
3333
this._tooltipLayers = new Map();
3434
this.activeLayerOrder = null;
35+
36+
mainLizmap.state.rootMapGroup.addListener(
37+
evt => {
38+
this._applyFilter(evt.name);
39+
},
40+
'layer.filter.token.changed'
41+
);
3542
}
3643

3744
/**
@@ -103,6 +110,7 @@ export default class Tooltip {
103110

104111
if (tooltipLayer) {
105112
this._activeTooltipLayer = tooltipLayer;
113+
this._applyFilter(layerName);
106114
} else {
107115
const url = `${lizUrls.service.replace('service?','features/tooltips?')}&layerId=${layerTooltipCfg.id}`;
108116

@@ -116,15 +124,18 @@ export default class Tooltip {
116124
stroke: stroke,
117125
});
118126

127+
// Initially hidden, will be set to 1 when features are loaded and filter is applied
128+
// to avoid visual flickering
129+
// Using the visible property of the layer does not work
119130
this._activeTooltipLayer = new VectorLayer({
131+
opacity: 0,
120132
source: new VectorSource({
121133
url: url,
122134
format: new GeoJSON(),
123135
}),
124136
style: vectorStyle
125137
});
126138

127-
128139
// Handle points layers with QGIS style
129140
if (this._displayLayerStyle) {
130141
const wmsParams = {
@@ -154,6 +165,7 @@ export default class Tooltip {
154165

155166
// Load tooltip layer
156167
this._activeTooltipLayer.once('sourceready', () => {
168+
this._applyFilter(layerName);
157169
mainEventDispatcher.dispatch('tooltip.loaded');
158170
});
159171

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

0 commit comments

Comments
 (0)