Skip to content

Commit

Permalink
feat: refactor route planner libs
Browse files Browse the repository at this point in the history
  • Loading branch information
petrot committed Apr 6, 2018
1 parent 987ef59 commit a80d017
Show file tree
Hide file tree
Showing 27 changed files with 621 additions and 519 deletions.
5 changes: 5 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ import { ToasterModule, ToasterService } from 'angular2-toaster';

// Global styles
import './styles';
import { RoutePlannerService, RoutingControlService } from './shared/services/admin-map';
import { WaypointMarkerService } from './shared/services/admin-map/waypoint-marker.service';

const hikeModuleConfig = new HikeModuleConfig();
hikeModuleConfig.storeDomains = {
Expand Down Expand Up @@ -153,6 +155,9 @@ export class CustomRouterStateSerializer implements RouterStateSerializer<Router
HikeDataService,
ReverseGeocodingService,
AdminMapService,
RoutePlannerService,
RoutingControlService,
WaypointMarkerService,
PoiEditorService,
WikipediaPoiService,
OsmPoiService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { Component, ViewChild, OnInit, OnDestroy, AfterViewInit } from '@angular
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { Store } from '@ngrx/store';
import { State, hikeEditMapActions } from 'app/store';
import { State, hikeEditMapActions, adminMapActions } from 'app/store';
import { HikeEditRoutePlannerSelectors } from 'app/store/selectors';
import { LeafletMouseEvent } from 'leaflet';
import { Center, ISegment } from 'subrepos/gtrack-common-ngx';
import { AdminLeafletComponent } from 'app/shared/components/admin-leaflet';
import { AdminMapService } from 'app/shared/services';

import * as L from 'leaflet';
import { AdminMapService } from '../../../../shared/services';
import { LeafletMouseEvent } from 'leaflet';
import { WaypointMarkerService } from '../../../../shared/services/admin-map';

const CENTER = <Center>{
// London
Expand Down Expand Up @@ -53,6 +55,7 @@ export class HikeEditMapComponent implements OnInit, OnDestroy, AfterViewInit {
constructor(
private _store: Store<State>,
private _adminMapService: AdminMapService,
private _waypointMarkerService: WaypointMarkerService,
private _hikeEditRoutePlannerSelectors: HikeEditRoutePlannerSelectors
) {}

Expand All @@ -75,7 +78,7 @@ export class HikeEditMapComponent implements OnInit, OnDestroy, AfterViewInit {
this._destroy$.next(true);
this._destroy$.unsubscribe();

this.mapComponent.map.destroy();
this._store.dispatch(new adminMapActions.ResetMap());
}

ngAfterViewInit() {
Expand All @@ -88,7 +91,7 @@ export class HikeEditMapComponent implements OnInit, OnDestroy, AfterViewInit {
.on('click', (e: LeafletMouseEvent) => {
if (this.mode === 'routing') {
// TODO action
this.mapComponent.map.waypointMarker.addWaypoint(e.latlng);
this._waypointMarkerService.addWaypoint(e.latlng);
} else {
// console.log('todo _createCheckpoint');
// this._createCheckpoint(e.latlng);
Expand Down Expand Up @@ -133,10 +136,14 @@ export class HikeEditMapComponent implements OnInit, OnDestroy, AfterViewInit {
}

private _addBuffer() {
const _buffer = this.mapComponent.map.getBuffer();
if (_buffer) {
this._geoJsonOnMap = this.mapComponent.map.addGeoJSON(_buffer);
}
this.mapComponent.map
.getBuffer()
.map((buffer) => {
console.log('TEST _addBuffer ', buffer);
if (buffer) {
this._geoJsonOnMap = this.mapComponent.map.addGeoJSON(buffer);
}
});
}

private _removeBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { AdminMap, AdminMapService, AdminMapMarker } from 'app/shared/services/admin-map';
import { AdminMap, AdminMapService, AdminMapMarker, RoutePlannerService } from 'app/shared/services/admin-map';
import { PoiEditorService } from 'app/shared/services';
import { Poi, PoiSelectors } from 'subrepos/gtrack-common-ngx';
import { IPoi } from 'subrepos/provider-client';
import {
IExternalPoiType, IExternalPoi, IWikipediaPoi, IGooglePoi, IOsmPoi
IExternalPoiType, IExternalPoi, IWikipediaPoi, IGooglePoi, IOsmPoi, IGTrackPoi
} from 'app/shared/interfaces';
import {
State, hikeEditPoiActions, IExternalPoiListContextState, commonPoiActions, IHikeEditRoutePlannerState
Expand All @@ -34,6 +34,7 @@ export class HikeEditPoisExternalComponent implements OnInit, OnDestroy {
constructor(
private _store: Store<State>,
private _adminMapService: AdminMapService,
private _routePlannerService: RoutePlannerService,
private _hikeEditMapSelectors: HikeEditMapSelectors,
private _hikeEditPoiSelectors: HikeEditPoiSelectors,
private _poiSelectors: PoiSelectors,
Expand Down Expand Up @@ -61,35 +62,42 @@ export class HikeEditPoisExternalComponent implements OnInit, OnDestroy {
.select(this._hikeEditPoiSelectors.getHikeEditContextPropertySelector(this.poiType.subdomain, 'loaded'))
.takeUntil(this._destroy$)
.filter(loaded => !!loaded)
.subscribe((loaded: boolean) => {
// We have pure poi list on the store, now we have update some property
this._getSubdomainSelector(this.poiType.subdomain)
.take(1)
.switchMap((pois: IExternalPoi[]) => this._poiEditorService.organizePois(pois, this._map.routeInfo.getPath()))
.switchMap((pois: IExternalPoi[]) => this._poiEditorService.assignOnOffRoutePois(pois))
.switchMap((pois: IExternalPoi[]) => this._poiEditorService.handleElevation(pois))
.subscribe((pois) => {
// Refresh poi list on the store
this._updateSubdomainPois(pois);

// Refresh markers
this._poiEditorService.refreshPoiMarkers(this._map);

// Get gTrack pois for checking inGtrackDb
this._poiEditorService.getGTrackPois(this._map);
});
.switchMap(() => Observable.combineLatest(
this._getSubdomainSelector(this.poiType.subdomain).take(1),
this._store.select(this._hikeEditRoutePlannerSelectors.getPath)
))
.filter(([pois, path]: [IExternalPoi[], any]) => typeof pois !== 'undefined')
.switchMap(([pois, path]: [IExternalPoi[], any]) => {
return Observable.of(this._poiEditorService.organizePois(_.cloneDeep(pois), path));
})
.switchMap((pois: IExternalPoi[]) => this._poiEditorService.assignOnOffRoutePois(pois))
.switchMap((pois: IExternalPoi[]) => this._poiEditorService.handleElevation(pois))
.subscribe((pois) => {
// Refresh poi list on the store
this._updateSubdomainPois(pois);

// DIRTY FLAG TODO

// Get gTrack pois for checking inGtrackDb
if (pois.length > 0) {
// gTrack poi will call marker refresher!
this._poiEditorService.getGTrackPois(this._map);
} else {
// Refresh markers
console.log('Call refreshPoiMarkers 1');
this._poiEditorService.refreshPoiMarkers(this._map);
}
});

// Update inGtrackDb properties after common poi list has been refreshed
this._store.select(this._poiSelectors.getAllPois)
.takeUntil(this._destroy$)
.subscribe((gTRackPois) => {
this._getSubdomainSelector(this.poiType.subdomain)
.take(1)
.subscribe((pois) => {
this._patchSubdomainPois(this._poiEditorService.handleGTrackPois(pois, gTRackPois), ['id', 'inGtrackDb']);
});
});
Observable.combineLatest(
this._store.select(this._poiSelectors.getAllPois),
this._getSubdomainSelector(this.poiType.subdomain).take(1)
).subscribe(([gTrackPois, externalPois]: [IGTrackPoi[], IExternalPoi[]]) => {
if (gTrackPois.length > 0 && externalPois.length > 0) {
this._setSubdomainPoisInGtrackDb(this._poiEditorService.handleGTrackPois(externalPois, gTrackPois));
}
});

//
// Contexts
Expand Down Expand Up @@ -155,29 +163,42 @@ export class HikeEditPoisExternalComponent implements OnInit, OnDestroy {
}
}

private _patchSubdomainPois(pois, props: string[]) {
private _setSubdomainPoisInGtrackDb(pois) {
console.log('_patchSubdomainPois', this.poiType.subdomain);
switch (this.poiType.subdomain) {
case 'google':
this._store.dispatch(new hikeEditPoiActions.SetGooglePois({ pois: pois })); break;
this._store.dispatch(new hikeEditPoiActions.SetGooglePoisInGtrackDb({
properties: pois.map(p => _.pick(p, ['id', 'inGtrackDb']))
}));
break;
case 'wikipedia':
this._store.dispatch(new hikeEditPoiActions.SetWikipediaPois({ pois: pois })); break;
this._store.dispatch(new hikeEditPoiActions.SetWikipediaPoisInGtrackDb({
properties: pois.map(p => _.pick(p, ['id', 'inGtrackDb']))
}));
break;
case 'osmAmenity':
this._store.dispatch(new hikeEditPoiActions.SetOsmAmenityPois({ pois: pois })); break;
this._store.dispatch(new hikeEditPoiActions.SetOsmAmenityPoisInGtrackDb({
properties: pois.map(p => _.pick(p, ['id', 'inGtrackDb']))
}));
break;
case 'osmNatural':
this._store.dispatch(new hikeEditPoiActions.PatchOsmNaturalPois({
properties: pois.map(p => _.pick(p, props))
this._store.dispatch(new hikeEditPoiActions.SetOsmNaturalPoisInGtrackDb({
properties: pois.map(p => _.pick(p, ['id', 'inGtrackDb']))
}));
break;
case 'osmRoute':
this._store.dispatch(new hikeEditPoiActions.SetOsmRoutePois({ pois: pois })); break;
this._store.dispatch(new hikeEditPoiActions.SetOsmRoutePoisInGtrackDb({
properties: pois.map(p => _.pick(p, ['id', 'inGtrackDb']))
}));
break;
}
}

/**
* Get pois for the current subdomain
*/
public getPois() {
let _bounds = this._map.routeInfo.getSearchBounds();
let _bounds = this._routePlannerService.getSearchBounds();

if (_bounds) {
// Get pois for the current domain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class HikeEditPoisGTrackTableComponent {
this._store.dispatch(new hikeEditGeneralInfoActions.AddPoi({
poi: poi.id
}));

this._store.dispatch(new hikeEditPoiActions.SetDirty({
subdomain: 'gTrack',
dirty: true
}));
}

public openModal($event, poi: IGTrackPoi) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import {
PoiSelectors, CenterRadius, GeometryService, GeoSearchSelectors, Poi, PoiSaved, IGeoSearchContextState
PoiSelectors, CenterRadius, GeometryService, GeoSearchSelectors, Poi, PoiSaved, IGeoSearchContextState, IGeoSearchResponseItem
} from 'subrepos/gtrack-common-ngx';
import { IPoiStored, IPoi } from 'subrepos/provider-client';
import { AdminMap, AdminMapService, AdminMapMarker } from 'app/shared/services/admin-map';
Expand Down Expand Up @@ -56,24 +56,51 @@ export class HikeEditPoisGTrackComponent implements OnInit, OnDestroy {
});

// Get pois by id from geoSearch result
this._store
.select(this._geoSearchSelectors.getGeoSearch('gTrackPois'))
Observable
.combineLatest(
this._store.select(this._geoSearchSelectors.getGeoSearch('gTrackPois')),
this._store.select(this._poiSelectors.getPoiIds)
)
.takeUntil(this._destroy$)
.subscribe((searchData) => {
.debounceTime(100)
.subscribe(([searchData, inStorePoiIds]: [IGeoSearchResponseItem, string[]]) => {
if (searchData) {
this._store.dispatch(new commonPoiActions.LoadPois((<any>searchData).results));
const poiIds = _.difference((<any>searchData).results, _.intersection((<any>searchData).results, inStorePoiIds))

// Get only the not-loaded pois
if (poiIds && poiIds.length > 0) {
this._store.dispatch(new commonPoiActions.LoadPois(poiIds));
}
}
});

// Poi list based on geoSearch results
this.pois$ = this._store
.select(this._geoSearchSelectors.getGeoSearchResults<(IPoi)>('gTrackPois', this._poiSelectors.getAllPois))
.switchMap((pois: Poi[]) => this._poiEditorService.organizePois(_.cloneDeep(pois), this._map.routeInfo.getPath()))
.switchMap((pois: IGTrackPoi[]) => this._poiEditorService.handleHikeInclusion(pois));
this.pois$ = Observable
.combineLatest(
this._store.select(this._geoSearchSelectors.getGeoSearchResults<(IPoi)>('gTrackPois', this._poiSelectors.getAllPois)),
this._store.select(this._hikeEditRoutePlannerSelectors.getPath),
this._store.select(this._hikeEditPoiSelectors.getHikeEditContextPropertySelector('gTrack', 'dirty'))
)
.takeUntil(this._destroy$)
.debounceTime(100)
.filter(([pois, path, dirty]: [Poi[], any, boolean]) => typeof pois !== 'undefined')
.switchMap(([pois, path, dirty]: [Poi[], any, boolean]) => {
return Observable.of(this._poiEditorService.organizePois(_.cloneDeep(pois), path));
})
.switchMap((pois: IGTrackPoi[]) => {
this._store.dispatch(new hikeEditPoiActions.SetDirty({
subdomain: 'gTrack',
dirty: false
}));

return Observable.of(this._poiEditorService.handleHikeInclusion(pois));
});

this.pois$
.takeUntil(this._destroy$)
.debounceTime(100)
.subscribe((pois: Poi[]) => {
console.log('POI CAHNGES IN GTRAK');
// Refresh markers
this._poiEditorService.refreshPoiMarkers(this._map);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class HikeEditPoisHikeTableComponent {
this._store.dispatch(new hikeEditGeneralInfoActions.RemovePoi({
poi: poi.id
}));

this._store.dispatch(new hikeEditPoiActions.SetDirty({
subdomain: 'gTrack',
dirty: true
}));
}

public openModal($event, poi: IGTrackPoi) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,39 @@ export class HikeEditPoisHikeComponent implements OnInit, OnDestroy {
});

// Get pois by id
this._store
.select(this._hikeEditGeneralInfoSelectors.getPois)
Observable
.combineLatest(
this._store.select(this._hikeEditGeneralInfoSelectors.getPois),
this._store.select(this._poiSelectors.getPoiIds)
)
.takeUntil(this._destroy$)
.debounceTime(100)
.takeUntil(this._destroy$)
.delay(2000) // TODO wait for map.routeInfo with a better way
.subscribe((poiIds) => {
if (poiIds) {
.subscribe(([inHikePoiIds, inStorePoiIds]: [string[], string[]]) => {
const poiIds = _.difference(inHikePoiIds, _.intersection(inHikePoiIds, inStorePoiIds))

// Get only the not-loaded pois
if (poiIds && poiIds.length > 0) {
this._store.dispatch(new commonPoiActions.LoadPois(poiIds));
}
});

// Poi list
this.pois$ = this._store
.select(this._hikeEditGeneralInfoSelectors.getHikePois<(IPoi)>(this._poiSelectors.getAllPois))
.switchMap((pois: Poi[]) => this._poiEditorService.organizePois(_.cloneDeep(pois), this._map.routeInfo.getPath()))
// Poi list TODO uncomment
this.pois$ = Observable
.combineLatest(
this._store.select(this._hikeEditGeneralInfoSelectors.getHikePois<(IPoi)>(this._poiSelectors.getAllPois)),
this._store.select(this._hikeEditRoutePlannerSelectors.getPath)
)
.takeUntil(this._destroy$)
.debounceTime(100)
.filter(([pois, path]: [Poi[], any]) => typeof pois !== 'undefined')
.switchMap(([pois, path]: [Poi[], any]) => {
return Observable.of(this._poiEditorService.organizePois(_.cloneDeep(pois), path));
});

this.pois$
.takeUntil(this._destroy$)
.debounceTime(100)
.subscribe((pois: Poi[]) => {
// Refresh markers
this._poiEditorService.refreshPoiMarkers(this._map);
Expand Down
Loading

0 comments on commit a80d017

Please sign in to comment.