Skip to content

Commit

Permalink
WIP highlight feature
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinFabre-ods committed Jan 10, 2024
1 parent 1f7c971 commit b59663e
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions packages/visualizations/src/components/MapPoi/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ export default class MapPOI {
});
}

private setLayerLayoutProperty(layerId: string, name: string, value: unknown) {
this.queue((map) => {
map.setLayoutProperty(layerId, name, value );
});
}

/** Add a feature state and edit layer layout property to sort on top the active feature */
private highlightFeature(feature: ActiveFeatureType) {
if(!feature) return;
const {layer} = feature;
this.updateFeatureState(feature, POPUP_FEATURE_STATE_KEY, true);
this.setLayerLayoutProperty(layer.id, `${layer.type}-sort-key`, [
'case',
["==", ["id"], feature.id], 1,
0
]);

}

private unhighlightFeature(feature: ActiveFeatureType) {
if(!feature) return;
const {layer} = feature;
this.updateFeatureState(feature, POPUP_FEATURE_STATE_KEY, false);
this.setLayerLayoutProperty(layer.id, `${layer.type}-sort-key`, 0);
}

/** Initialize a resize observer to always fit the map to its container */
private initializeMapResizer(map: maplibregl.Map, container: HTMLElement) {
// Set a resizeObserver to resize map on container size changes
Expand Down Expand Up @@ -172,15 +198,15 @@ export default class MapPOI {
}

private navigateToFeature(direction: number) {
this.updateFeatureState(this.activeFeature, POPUP_FEATURE_STATE_KEY, false);
this.unhighlightFeature(this.activeFeature);
const activeFeatureIndex = this.availableFeaturesOnClick.indexOf(this.activeFeature);
this.activeFeature = this.availableFeaturesOnClick[activeFeatureIndex + direction];
this.updatePopupContent();
this.updatePopupDisplay();
if (this.activeFeature?.geometry.type === 'Point') {
this.popup.setLngLat(this.activeFeature?.geometry.coordinates as LngLatLike);
}
this.updateFeatureState(this.activeFeature, POPUP_FEATURE_STATE_KEY, true);
this.highlightFeature(this.activeFeature);
}

private renderFeaturesNavigationControls() {
Expand Down Expand Up @@ -307,7 +333,7 @@ export default class MapPOI {
});

// Removing feature state for the obsolete active feature.
this.updateFeatureState(this.activeFeature, POPUP_FEATURE_STATE_KEY, false);
this.unhighlightFeature(this.activeFeature);
const hasFeatures = !!features.length;
// Current rule: use the first feature to build the popup.
// TO DO: Create a menu to display a list of feature to choose from.
Expand Down Expand Up @@ -340,7 +366,7 @@ export default class MapPOI {

this.updatePopupContent();
this.updatePopupDisplay();
this.updateFeatureState(this.activeFeature, POPUP_FEATURE_STATE_KEY, true);
this.highlightFeature(this.activeFeature);
}

initialize(
Expand Down Expand Up @@ -520,7 +546,7 @@ export default class MapPOI {

constructor() {
this.popup.on('close', () => {
this.updateFeatureState(this.activeFeature, POPUP_FEATURE_STATE_KEY, false);
this.unhighlightFeature(this.activeFeature);
this.onPopupDisplayUpdate(this.activePopupDisplay, null);
this.activePopupDisplay = null;
this.activeFeature = null;
Expand Down

0 comments on commit b59663e

Please sign in to comment.