Skip to content

Commit

Permalink
fixed legend update on filter change by adding await to check for ren…
Browse files Browse the repository at this point in the history
…dered items. also broke some logic out into new function getRenderedLayerIds and waitForLayerChangeRender
  • Loading branch information
acatchpole committed Jan 10, 2024
1 parent 3427db1 commit 7cc589f
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions app/frontend/src/aquifers/components/SingleAquiferMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,51 @@ export default {
filter.splice(1, 0, ['==', ['get', 'aquifer_id'], this.aquiferId], false)
return filter
},
layersChanged (layerId, show) {
// Turn the layer's visibility on / off
this.map.setLayoutProperty(layerId, 'visibility', show ? 'visible' : 'none');
//this will update the legend but needs to wait for entries to render/disappear
setTimeout(() => { this.updateMapLegendBasedOnVisibleElements(); }, 100);
async layersChanged(layerId, show) {
try {
// Turn the layer's visibility on / off
this.map.setLayoutProperty(layerId, 'visibility', show ? 'visible' : 'none');
// Wait for the layer change to be rendered
await this.waitForLayerChangeRender(layerId, show);
// Update the legend based on visible elements
this.updateMapLegendBasedOnVisibleElements();
} catch (error) {
console.error('Error in layersChanged:', error);
// Handle the error as needed (log, throw, etc.)
throw error;
}
},
async waitForLayerChangeRender(layerId, show) {
const maxAttempts = 10; // Adjust the number of attempts as needed
let attempts = 0;
let hasChangeBeenRendered = false;
while (!hasChangeBeenRendered && attempts < maxAttempts) {
const visibleLayerIds = this.getRenderedLayerIds();
if (show) {
hasChangeBeenRendered = visibleLayerIds.has(layerId);
} else {
hasChangeBeenRendered = !visibleLayerIds.has(layerId);
}
if (!hasChangeBeenRendered) {
// Add a delay before checking again (e.g., 100ms)
await new Promise(resolve => setTimeout(resolve, 100));
attempts++;
}
}
if (!hasChangeBeenRendered) {
console.warn('Timeout waiting for layer change to be rendered.');
// Handle the timeout scenario (e.g., log, throw, etc.)
}
},
zoomToAquifer (fitBoundsOptions) {
if (!this.geom) { return }
Expand Down Expand Up @@ -402,14 +439,8 @@ export default {
},
updateMapLegendBasedOnVisibleElements() {
//gets a list of rendered objects and then updates the legend to only display entries for items that are currently rendered
const visibleFeatures = this.map.queryRenderedFeatures();
const uniqueRenderedLayerIds = new Set();
visibleFeatures.forEach(item => {
if (item.layer.id){
uniqueRenderedLayerIds.add(item.layer.id);
}
});
console.log(uniqueRenderedLayerIds);
const uniqueRenderedLayerIds = this.getRenderedLayerIds();
//as cadastrals are a raster layer rather than a vector layer like the others, so seperate logic is required.
this.mapLayers.forEach(layerObj =>{
if(uniqueRenderedLayerIds.has(layerObj.id) && layerObj.id != DATABC_CADASTREL_LAYER_ID){
Expand All @@ -428,6 +459,16 @@ export default {
}
this.legendControl.update();
},
getRenderedLayerIds(){
const visibleFeatures = (this.map.queryRenderedFeatures());
const uniqueRenderedLayerIds = new Set();
visibleFeatures.forEach(item => {
if (item.layer.id){
uniqueRenderedLayerIds.add(item.layer.id);
}
});
return uniqueRenderedLayerIds;
},
listenForMapMovement () {
const startEvents = ['zoomstart', 'movestart']
startEvents.forEach(eventName => {
Expand Down

0 comments on commit 7cc589f

Please sign in to comment.