Skip to content

Commit

Permalink
use arrow notation
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed May 15, 2018
1 parent dac6bae commit 0e60d71
Showing 1 changed file with 70 additions and 52 deletions.
122 changes: 70 additions & 52 deletions src/lib/query/shared/query.directive.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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[] = [];
Expand All @@ -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);
}
Expand All @@ -69,65 +81,71 @@ 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,
projection: this.map.projection,
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<Feature[]>) => {
return query$.subscribe(
(features: Feature[]) => this.query.emit({
return query$.subscribe((features: Feature[]) =>
this.query.emit({
features: parsedClickedFeatures.concat(features),
event: event
})
Expand Down

0 comments on commit 0e60d71

Please sign in to comment.