From 73d0cdd68784ff94a2541ff739a855444a05fb48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Barbeau?= Date: Wed, 28 Feb 2018 13:40:16 -0500 Subject: [PATCH] feat(search-bar): fill search bar when feature is selected --- src/lib/feature/shared/feature.enum.ts | 5 +++++ src/lib/feature/shared/feature.interface.ts | 3 ++- src/lib/query/shared/query.service.ts | 4 +++- .../search/search-bar/search-bar.component.ts | 21 +++++++++++++++++-- .../datasource-search-source.ts | 3 ++- .../search-sources/icherche-search-source.ts | 3 ++- .../search-sources/nominatim-search-source.ts | 3 ++- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/lib/feature/shared/feature.enum.ts b/src/lib/feature/shared/feature.enum.ts index 1547c15a4e..ebeb24345c 100644 --- a/src/lib/feature/shared/feature.enum.ts +++ b/src/lib/feature/shared/feature.enum.ts @@ -8,3 +8,8 @@ export enum FeatureFormat { GeoJSON, JSON } + +export enum SourceFeatureType { + Query = 'Query', + Search = 'Search' +} diff --git a/src/lib/feature/shared/feature.interface.ts b/src/lib/feature/shared/feature.interface.ts index 7bca17b18e..57006f2aa1 100644 --- a/src/lib/feature/shared/feature.interface.ts +++ b/src/lib/feature/shared/feature.interface.ts @@ -1,10 +1,11 @@ import { AnyDataSourceContext } from '../../datasource'; -import { FeatureType, FeatureFormat } from './feature.enum'; +import { FeatureType, FeatureFormat, SourceFeatureType } from './feature.enum'; export interface Feature { id: string; source: string; + sourceType?: SourceFeatureType; type: FeatureType; title: string; format?: FeatureFormat; diff --git a/src/lib/query/shared/query.service.ts b/src/lib/query/shared/query.service.ts index 19fb38189e..ca6ceef7fc 100644 --- a/src/lib/query/shared/query.service.ts +++ b/src/lib/query/shared/query.service.ts @@ -3,7 +3,7 @@ import { HttpClient } from '@angular/common/http'; import { Subscription } from 'rxjs/Subscription'; import { uuid } from '../../utils/uuid'; -import { Feature, FeatureType, FeatureFormat, +import { Feature, FeatureType, FeatureFormat, SourceFeatureType, FeatureService } from '../../feature'; import { DataSource, QueryableDataSource } from '../../datasource'; import { Layer } from '../../layer'; @@ -76,6 +76,7 @@ export class QueryService { return Object.assign(feature, { id: uuid(), source: dataSource.title, + sourceType: SourceFeatureType.Query, title: title ? title : `${dataSource.title} (${index + 1})`, projection: options.projection }); @@ -223,6 +224,7 @@ export class QueryService { return { id: undefined, source: undefined, + sourceType: SourceFeatureType.Query, type: FeatureType.Feature, format: FeatureFormat.GeoJSON, title: undefined, diff --git a/src/lib/search/search-bar/search-bar.component.ts b/src/lib/search/search-bar/search-bar.component.ts index c26c664c29..1b5d8a77d3 100644 --- a/src/lib/search/search-bar/search-bar.component.ts +++ b/src/lib/search/search-bar/search-bar.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit, Input, Output, - EventEmitter, ViewChild, ElementRef, + EventEmitter, ViewChild, ElementRef, ChangeDetectorRef, OnDestroy, ChangeDetectionStrategy } from '@angular/core'; import { FloatLabelType } from '@angular/material' @@ -7,6 +7,7 @@ import { Subject } from 'rxjs/Subject'; import { Subscription } from 'rxjs/Subscription'; import { debounceTime, distinctUntilChanged } from 'rxjs/operators'; +import { FeatureService, SourceFeatureType, FeatureType } from '../../feature'; import { SearchService } from '../shared'; @Component({ @@ -76,22 +77,38 @@ export class SearchBarComponent implements OnInit, OnDestroy { private readonly invalidKeys = ['Control', 'Shift', 'Alt']; private stream$ = new Subject(); private stream$$: Subscription; + private selectedFeature$$: Subscription; @Output() search = new EventEmitter(); @ViewChild('input') input: ElementRef; - constructor(private searchService: SearchService) {} + constructor( + private searchService: SearchService, + private featureService: FeatureService, + private changeDetectorRef: ChangeDetectorRef + ) {} ngOnInit(): void { this.stream$$ = this.stream$.pipe( debounceTime(this._debounce), distinctUntilChanged() ).subscribe((term: string) => this.handleTermChanged(term)); + + this.selectedFeature$$ = this.featureService.selectedFeature$.subscribe( + (feature) => { + if (feature && feature.type === FeatureType.Feature && + feature.sourceType === SourceFeatureType.Search) { + this.term = feature.title; + this.changeDetectorRef.markForCheck(); + } + } + ); } ngOnDestroy() { this.stream$$.unsubscribe(); + this.selectedFeature$$.unsubscribe(); } keyup(event: KeyboardEvent) { diff --git a/src/lib/search/search-sources/datasource-search-source.ts b/src/lib/search/search-sources/datasource-search-source.ts index 58190c37e2..5fc4a4d2e5 100644 --- a/src/lib/search/search-sources/datasource-search-source.ts +++ b/src/lib/search/search-sources/datasource-search-source.ts @@ -3,7 +3,7 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import { ConfigService, Message, LanguageService } from '../../core'; -import { Feature, FeatureType } from '../../feature'; +import { Feature, FeatureType, SourceFeatureType } from '../../feature'; import { SearchSource } from './search-source'; import { SearchSourceOptions } from './search-source.interface'; @@ -79,6 +79,7 @@ export class DataSourceSearchSource extends SearchSource { return { id: result.id, source: this.getName(), + sourceType: SourceFeatureType.Search, type: FeatureType.DataSource, title: result.source.title, title_html: result.highlight.title, diff --git a/src/lib/search/search-sources/icherche-search-source.ts b/src/lib/search/search-sources/icherche-search-source.ts index 9bdbc4cdd3..311a10907f 100644 --- a/src/lib/search/search-sources/icherche-search-source.ts +++ b/src/lib/search/search-sources/icherche-search-source.ts @@ -3,7 +3,7 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import { ConfigService, Message } from '../../core'; -import { Feature, FeatureType, FeatureFormat} from '../../feature'; +import { Feature, FeatureType, FeatureFormat, SourceFeatureType} from '../../feature'; import { SearchSource } from './search-source'; import { SearchSourceOptions } from './search-source.interface'; @@ -72,6 +72,7 @@ export class IChercheSearchSource extends SearchSource { return { id: result._id, source: IChercheSearchSource._name, + sourceType: SourceFeatureType.Search, type: FeatureType.Feature, format: FeatureFormat.GeoJSON, title: result.properties.recherche, diff --git a/src/lib/search/search-sources/nominatim-search-source.ts b/src/lib/search/search-sources/nominatim-search-source.ts index 69a09316dd..ac0ff76c08 100644 --- a/src/lib/search/search-sources/nominatim-search-source.ts +++ b/src/lib/search/search-sources/nominatim-search-source.ts @@ -3,7 +3,7 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import { ConfigService, Message } from '../../core'; -import { Feature, FeatureType, FeatureFormat} from '../../feature'; +import { Feature, FeatureType, FeatureFormat, SourceFeatureType} from '../../feature'; import { SearchSource } from './search-source'; import { SearchSourceOptions } from './search-source.interface'; @@ -61,6 +61,7 @@ export class NominatimSearchSource extends SearchSource { return { id: result.place_id, source: NominatimSearchSource._name, + sourceType: SourceFeatureType.Search, type: FeatureType.Feature, format: FeatureFormat.GeoJSON, title: result.display_name,