Skip to content

Commit

Permalink
fix(POI Maps): improve popup removals
Browse files Browse the repository at this point in the history
Popup can be removed if its source disappear
  • Loading branch information
KevinFabre-ods committed Sep 21, 2023
1 parent 72d155e commit 59ea8cf
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions packages/visualizations/src/components/MapPoi/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class MapPOI {
/** Array of layer IDs that are not from the base style of the map */
private layerIds: string[] = [];

/** Array of source IDs used in the map */
private sourceIds: string[] = [];

/** A navigation control for the map. */
private navigationControl = new maplibregl.NavigationControl({ showCompass: false });

Expand Down Expand Up @@ -82,9 +85,11 @@ export default class MapPOI {
/** Event handler for popup close event. */
private onPopupClose() {
this.popupFeatures.forEach(({ source, sourceLayer, id }) => {
this.queue((map) => {
map.setFeatureState({ source, sourceLayer, id }, { 'popup-feature': false });
});
if (this.sourceIds.includes(source)) {
this.queue((map) => {
map.setFeatureState({ source, sourceLayer, id }, { 'popup-feature': false });
});
}
});
this.popupFeatures = [];
}
Expand All @@ -111,8 +116,12 @@ export default class MapPOI {
// Get the popup configuration for a layer
const popupConfiguration = this.popupsConfiguration[layerId];

// If no popup configuration for a layer, we remove the popup
if (!popupConfiguration) {
/*
* We remove the popup if:
* - no popup configuration for a layer
* - popup's source is no longer used in the map
*/
if (!popupConfiguration || !this.sourceIds.includes(source)) {
this.popup.remove();
this.popupFeatures = [];
return;
Expand All @@ -125,7 +134,6 @@ export default class MapPOI {
}

this.popup.setHTML(getLoadingContent());

getContent(featureId, properties).then((content) => {
this.popup.setHTML(content);
});
Expand Down Expand Up @@ -188,6 +196,7 @@ export default class MapPOI {
});
}
this.layerIds = layers.map(({ id }) => id);
this.sourceIds = Object.keys(sources);
});
}

Expand Down

0 comments on commit 59ea8cf

Please sign in to comment.