From 0e60d71dabf4b98452505ccf2d830ddc68944883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Barbeau?= Date: Tue, 15 May 2018 14:43:48 -0400 Subject: [PATCH] use arrow notation --- src/lib/query/shared/query.directive.ts | 122 ++++++++++++++---------- 1 file changed, 70 insertions(+), 52 deletions(-) diff --git a/src/lib/query/shared/query.directive.ts b/src/lib/query/shared/query.directive.ts index c279b34b11..1b751d9677 100644 --- a/src/lib/query/shared/query.directive.ts +++ b/src/lib/query/shared/query.directive.ts @@ -1,5 +1,12 @@ -import { Directive, Self, Input, Output, EventEmitter, - OnDestroy, AfterViewInit } from '@angular/core'; +import { + Directive, + Self, + Input, + Output, + EventEmitter, + OnDestroy, + AfterViewInit +} from '@angular/core'; import { Subscription } from 'rxjs/Subscription'; import { Observable } from 'rxjs/Observable'; @@ -17,7 +24,6 @@ import { LanguageService } from '../../core'; selector: '[igoQuery]' }) export class QueryDirective implements AfterViewInit, OnDestroy { - private queryLayers: Layer[]; private queryLayers$$: Subscription; private queries$$: Subscription[] = []; @@ -27,24 +33,30 @@ export class QueryDirective implements AfterViewInit, OnDestroy { } @Input() - get waitForAllQueries(): boolean { return this._waitForAllQueries; } + get waitForAllQueries(): boolean { + return this._waitForAllQueries; + } set waitForAllQueries(value: boolean) { this._waitForAllQueries = value; } private _waitForAllQueries: boolean = false; - @Output() query = new EventEmitter<{ - features: Feature[] | Feature[][], - event: ol.MapBrowserEvent + @Output() + query = new EventEmitter<{ + features: Feature[] | Feature[][]; + event: ol.MapBrowserEvent; }>(); - constructor(@Self() private component: MapBrowserComponent, - private queryService: QueryService, - private languageService: LanguageService) {} + constructor( + @Self() private component: MapBrowserComponent, + private queryService: QueryService, + private languageService: LanguageService + ) {} ngAfterViewInit() { - this.queryLayers$$ = this.component.map.layers$ - .subscribe((layers: Layer[]) => this.handleLayersChange(layers)); + this.queryLayers$$ = this.component.map.layers$.subscribe( + (layers: Layer[]) => this.handleLayersChange(layers) + ); this.map.ol.on('singleclick', this.handleMapClick, this); } @@ -69,42 +81,47 @@ export class QueryDirective implements AfterViewInit, OnDestroy { private handleMapClick(event: ol.MapBrowserEvent) { this.unsubscribeQueries(); - const clickedFeatures: ol.Feature[] = [] + const clickedFeatures: ol.Feature[] = []; const format = new ol.format.GeoJSON(); const mapProjection = this.map.projection; - this.map.ol.forEachFeatureAtPixel(event.pixel, - function(feature: ol.Feature, layer: ol.layer.Layer) { - if (layer.getZIndex() !== 999) { - let title; - if (layer.get('title') !== undefined) { - title = layer.get('title') - } else { - title = this.map.layers.filter((f) => f['zIndex'] === layer.getZIndex())[0] - .dataSource['options']['title'] - } - feature.set('clickedTitle', title) - clickedFeatures.push(feature) - } - }.bind(this)); - const featuresGeoJSON = JSON.parse( - format.writeFeatures(clickedFeatures, { - dataProjection: 'EPSG:4326', - featureProjection: mapProjection - }) - ); - let i = 0; - let parsedClickedFeatures: Feature[] = []; - parsedClickedFeatures = featuresGeoJSON.features.map(f => - Object.assign({}, f, { - source: this.languageService.translate.instant('igo.clickOnMap.clickedFeature'), - id: f.properties.clickedTitle + ' ' + String(i++), - icon: 'mouse', - title: f.properties.clickedTitle - }) - ); - parsedClickedFeatures.forEach(element => { - delete element.properties['clickedTitle']; - }); + this.map.ol.forEachFeatureAtPixel( + event.pixel, + (feature: ol.Feature, layer: ol.layer.Layer) => { + if (layer.getZIndex() !== 999) { + let title; + if (layer.get('title') !== undefined) { + title = layer.get('title'); + } else { + title = this.map.layers.filter( + f => f['zIndex'] === layer.getZIndex() + )[0].dataSource['options']['title']; + } + feature.set('clickedTitle', title); + clickedFeatures.push(feature); + } + } + ); + const featuresGeoJSON = JSON.parse( + format.writeFeatures(clickedFeatures, { + dataProjection: 'EPSG:4326', + featureProjection: mapProjection + }) + ); + let i = 0; + let parsedClickedFeatures: Feature[] = []; + parsedClickedFeatures = featuresGeoJSON.features.map(f => + Object.assign({}, f, { + source: this.languageService.translate.instant( + 'igo.clickOnMap.clickedFeature' + ), + id: f.properties.clickedTitle + ' ' + String(i++), + icon: 'mouse', + title: f.properties.clickedTitle + }) + ); + parsedClickedFeatures.forEach(element => { + delete element.properties['clickedTitle']; + }); const view = this.map.ol.getView(); const queries$ = this.queryService.query(this.queryLayers, { coordinates: event.coordinate, @@ -112,22 +129,23 @@ export class QueryDirective implements AfterViewInit, OnDestroy { resolution: view.getResolution() }); if (queries$.length === 0) { - this.query.emit({features: parsedClickedFeatures, event: event}) + this.query.emit({ features: parsedClickedFeatures, event: event }); } else { if (this.waitForAllQueries) { this.queries$$.push( - forkJoin(...queries$).subscribe( - (features: Feature[][]) => + forkJoin(...queries$).subscribe((features: Feature[][]) => this.query.emit({ - features: features.filter((f) => f.length > 0).concat(parsedClickedFeatures), + features: features + .filter(f => f.length > 0) + .concat(parsedClickedFeatures), event: event }) ) ); } else { this.queries$$ = queries$.map((query$: Observable) => { - return query$.subscribe( - (features: Feature[]) => this.query.emit({ + return query$.subscribe((features: Feature[]) => + this.query.emit({ features: parsedClickedFeatures.concat(features), event: event })