Skip to content

Commit

Permalink
perf: code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
aziz-access committed Nov 19, 2024
1 parent 10db31b commit aba0e07
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions packages/geo/src/lib/search/search-bar/search-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ export class SearchBarComponent implements OnInit, OnDestroy {
* Event emitted when the coords format setting is changed
*/
@Output() reverseSearchCoordsFormatStatus = new EventEmitter<boolean>();

/**
* test
* Map to add layer
*/
@Input() map: IgoMap;

/**
* Search term
*/
Expand Down Expand Up @@ -499,31 +501,34 @@ export class SearchBarComponent implements OnInit, OnDestroy {
*/

selectFirstElement() {
// look for the two highest score results
const results = this.store.all().sort((res1, res2) => {
return (res1.meta.score - res2.meta.score) * -1;
});
const results = this.store.all();
// find the highest result score
const result = results.reduce(
(max: SearchResult, result) =>
result.meta.score > max.meta.score ? result : max,
results[0]
);

//Take the first element (feature or layer) to make a focus or view it on the map
if (results.length) {
const result = results[0];
this.store.state.update(result, { focused: true, selected: true }, true);

if (result.meta.dataType === FEATURE) {
const feature = (result as SearchResult<Feature>).data;
this.map.searchResultsOverlay.setFeatures(
[feature] satisfies Feature[],
FeatureMotion.Default
);
} else if (result.meta.dataType === LAYER) {
const layerOptions = (result as SearchResult<LayerOptions>).data;
if (layerOptions.sourceOptions.optionsFromApi === undefined) {
layerOptions.sourceOptions.optionsFromApi = true;
}
this.layerService.createAsyncLayer(result.data).subscribe((layer) => {
this.map.addLayer(layer);
});
if (!result) {
return;
}

this.store.state.update(result, { focused: true, selected: true }, true);

if (result.meta.dataType === FEATURE) {
const feature = (result as SearchResult<Feature>).data;
this.map.searchResultsOverlay.setFeatures(
[feature] satisfies Feature[],
FeatureMotion.Default
);
} else if (result.meta.dataType === LAYER) {
const layerOptions = (result as SearchResult<LayerOptions>).data;
if (layerOptions.sourceOptions.optionsFromApi === undefined) {
layerOptions.sourceOptions.optionsFromApi = true;
}
this.layerService.createAsyncLayer(layerOptions).subscribe((layer) => {
this.map.addLayer(layer);
});
}
}
}

0 comments on commit aba0e07

Please sign in to comment.