Skip to content

Commit

Permalink
feat(geo): provide a method to select the featuremotion when adding s…
Browse files Browse the repository at this point in the history
…earchresult as overlay (#1587)

* feat(geo): provide a method to select the featuremotion when adding searchresult as overlay

* fix(geo/search): adjust type and comment

* wip

---------

Co-authored-by: Alexandre Caron <[email protected]>
  • Loading branch information
pelord and alecarn authored Jan 19, 2024
1 parent 45917e8 commit afe56d5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/geo/src/lib/overlay/shared/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Overlay<T extends MapBase = MapBase> {
/**
* Add a feature to the overlay and, optionally, move to it
* @param feature Feature
* @param motion Optional: Apply this motion to the map view
* @param motion Optional: Apply this motion to the map view (default on FeatureMotion.Default)
*/
addFeature(feature: Feature, motion: FeatureMotion = FeatureMotion.Default) {
this.addFeatures([feature], motion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class SearchResultsToolComponent implements OnInit, OnDestroy {
abstractResult.meta.style.setZIndex(2000 + zIndexOffset);
this.map.searchResultsOverlay.addFeature(
abstractResult,
FeatureMotion.None
this.searchState.featureMotion.focus
);
if (trigger === 'focused') {
this.abstractFocusedResult = abstractResult;
Expand Down Expand Up @@ -471,7 +471,7 @@ export class SearchResultsToolComponent implements OnInit, OnDestroy {
}
this.map.searchResultsOverlay.addFeature(
result.data as Feature,
FeatureMotion.None
this.searchState.featureMotion.focus
);
}
}
Expand Down Expand Up @@ -510,7 +510,7 @@ export class SearchResultsToolComponent implements OnInit, OnDestroy {
*/
onResultSelect(result: SearchResult) {
this.map.searchResultsOverlay.dataSource.ol.clear();
this.tryAddFeatureToMap(result);
this.tryAddFeatureToMap(result, this.searchState.featureMotion.selected);
this.searchState.setSelectedResult(result);

if (this.topPanelState === 'initial') {
Expand Down Expand Up @@ -630,8 +630,12 @@ export class SearchResultsToolComponent implements OnInit, OnDestroy {
/**
* Try to add a feature to the map overlay
* @param result A search result that could be a feature
* @param motion A FeatureMotion to trigger when adding the searchresult to the map search overlay
*/
private tryAddFeatureToMap(result: SearchResult) {
private tryAddFeatureToMap(
result: SearchResult,
motion: FeatureMotion = FeatureMotion.Default
) {
if (result.meta.dataType !== FEATURE) {
return undefined;
}
Expand All @@ -651,7 +655,7 @@ export class SearchResultsToolComponent implements OnInit, OnDestroy {
)
);

this.map.searchResultsOverlay.addFeature(feature);
this.map.searchResultsOverlay.addFeature(feature, motion);
}

isScrolledIntoView(elemSource, elem) {
Expand Down
19 changes: 19 additions & 0 deletions packages/integration/src/lib/search/search.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ConfigService, StorageService } from '@igo2/core';
import {
CommonVectorStyleOptions,
Feature,
FeatureMotion,
FeatureStore,
FeatureWorkspace,
OverlayStyleOptions,
Expand All @@ -23,6 +24,14 @@ import { BehaviorSubject, Subscription } from 'rxjs';
import { MapState } from '../map';
import { WorkspaceState } from '../workspace/workspace.state';

/**
* Define the FeatureMotion to apply when adding the SearchResult to the map as an overlay.
*/
export interface SearchFeatureMotion {
selected?: FeatureMotion;
focus?: FeatureMotion;
}

/**
* Service that holds the state of the search module
*/
Expand All @@ -38,6 +47,16 @@ export class SearchState {
public focusedOrResolution$$: Subscription;
public selectedOrResolution$$: Subscription;

/**
* Default feature motion are:
* on selection = FeatureMotion.Default and
* on focus = FeatureMotion.None
*/
public featureMotion: SearchFeatureMotion = {
selected: FeatureMotion.Default,
focus: FeatureMotion.None
};

readonly searchTermSplitter$: BehaviorSubject<string> = new BehaviorSubject(
'|'
);
Expand Down
2 changes: 1 addition & 1 deletion projects/demo/src/app/geo/search/search.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
placeholder="false"
[settingsChange$]="settingsChange$"
(resultFocus)="onResultFocus($event)"
(resultSelect)="onResultFocus($event)"
(resultSelect)="onResultSelect($event)"
(moreResults)="onSearch($event)"
>
<ng-template #igoSearchItemToolbar let-result="result">
Expand Down
27 changes: 18 additions & 9 deletions projects/demo/src/app/geo/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ViewChild
} from '@angular/core';

import { Action, ActionStore, EntityStore } from '@igo2/common';
import { ActionStore, EntityStore } from '@igo2/common';
import { MediaService, StorageService } from '@igo2/core';
import {
FEATURE,
Expand Down Expand Up @@ -140,15 +140,27 @@ export class AppSearchComponent implements OnInit, OnDestroy {
* @param result A search result that could be a feature
*/
onResultFocus(result: SearchResult<Feature>): void {
this.tryAddFeatureToMap(result);
this.selectedFeature = (result satisfies SearchResult<Feature>).data;
this.tryAddFeatureToMap(result, this.searchState.featureMotion.focus);
this.selectedFeature = result.data;
}
/**
* Try to add a feature to the map when it's being selected
* @internal
* @param result A search result that could be a feature
*/
onResultSelect(result: SearchResult<Feature>): void {
this.tryAddFeatureToMap(result, this.searchState.featureMotion.selected);
this.selectedFeature = result.data;
}

/**
* Try to add a feature to the map overlay
* @param layer A search result that could be a feature
*/
private tryAddFeatureToMap(layer: SearchResult<Feature>): void | undefined {
private tryAddFeatureToMap(
layer: SearchResult<Feature>,
motion: FeatureMotion = FeatureMotion.Default
): void | undefined {
if (layer.meta.dataType !== FEATURE) {
return undefined;
}
Expand All @@ -158,10 +170,7 @@ export class AppSearchComponent implements OnInit, OnDestroy {
return;
}

this.map.searchResultsOverlay.setFeatures(
[layer.data] satisfies Feature[],
FeatureMotion.Default
);
this.map.searchResultsOverlay.setFeatures([layer.data], motion);
}

ngOnInit(): void {
Expand All @@ -181,7 +190,7 @@ export class AppSearchComponent implements OnInit, OnDestroy {
title: 'googleStreetView',
handler: () => this.openGoogleStreetView(this.lonlat)
}
] satisfies Action[]);
]);
}

ngOnDestroy(): void {
Expand Down

0 comments on commit afe56d5

Please sign in to comment.