Skip to content

Commit

Permalink
feat(search-bar): fill search bar when feature is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Feb 28, 2018
1 parent 531b188 commit 73d0cdd
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/lib/feature/shared/feature.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export enum FeatureFormat {
GeoJSON,
JSON
}

export enum SourceFeatureType {
Query = <any> 'Query',
Search = <any> 'Search'
}
3 changes: 2 additions & 1 deletion src/lib/feature/shared/feature.interface.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/query/shared/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -223,6 +224,7 @@ export class QueryService {
return {
id: undefined,
source: undefined,
sourceType: SourceFeatureType.Query,
type: FeatureType.Feature,
format: FeatureFormat.GeoJSON,
title: undefined,
Expand Down
21 changes: 19 additions & 2 deletions src/lib/search/search-bar/search-bar.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Component, OnInit, Input, Output,
EventEmitter, ViewChild, ElementRef,
EventEmitter, ViewChild, ElementRef, ChangeDetectorRef,
OnDestroy, ChangeDetectionStrategy } from '@angular/core';
import { FloatLabelType } from '@angular/material'

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({
Expand Down Expand Up @@ -76,22 +77,38 @@ export class SearchBarComponent implements OnInit, OnDestroy {
private readonly invalidKeys = ['Control', 'Shift', 'Alt'];
private stream$ = new Subject<string>();
private stream$$: Subscription;
private selectedFeature$$: Subscription;

@Output() search = new EventEmitter<string>();

@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) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/search/search-sources/datasource-search-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/search/search-sources/icherche-search-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/search/search-sources/nominatim-search-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 73d0cdd

Please sign in to comment.