FarmMapsLib/projects/common-map/src/fm-map/effects/map.effects.ts

231 lines
8.8 KiB
TypeScript

import { Injectable, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { Store, Action } from '@ngrx/store';
import { Effect, Actions,ofType } from '@ngrx/effects';
import { Observable , of } from 'rxjs';
import { withLatestFrom, switchMap, map, catchError, mergeMap, delay} from 'rxjs/operators';
import {GeoJSON} from 'ol/format';
import {Feature} from 'ol';
import { getCenter, Extent, createEmpty, extend} from 'ol/extent';
import {Point} from 'ol/geom'
import * as mapActions from '../actions/map.actions';
import * as mapReducers from '../reducers/map.reducer';
import {commonReducers} from '@farmmaps/common';
import {commonActions} from '@farmmaps/common';
import { IListItem, IItem } from '@farmmaps/common';
import { FolderService, ItemService } from '@farmmaps/common';
import { tassign } from 'tassign';
@Injectable()
export class MapEffects {
private _format: GeoJSON;
private toPointFeature(feature: any): Feature {
var f = this._format.readFeature(feature);
var centroid = getCenter(f.getGeometry().getExtent());
f.setGeometry(new Point(centroid));
return f;
}
@Effect()
init$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.INIT),
withLatestFrom(this.store$.select(commonReducers.selectGetRootItems)),
switchMap(([action, rootItems]) => {
for (let rootItem of rootItems) {
if (rootItem.itemType == "UPLOADS_FOLDER") return of(new mapActions.SetParent(rootItem.code));
}
return [];
}
));
@Effect()
initBaseLayers$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.INIT),
withLatestFrom(this.store$.select(mapReducers.selectGetProjection)),
map(([action, projection]) => new mapActions.LoadBaseLayers(projection)));
@Effect()
loadBaseLayers$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.LOADBASELAYERS),
switchMap((action: mapActions.LoadBaseLayers) => {
return this.itemService$.getItemList("vnd.farmmaps.itemtype.layer", { "isBaseLayer": true }).pipe(
map((items: IItem[]) => new mapActions.LoadBaseLayersSuccess(items)),
catchError(error => of(new commonActions.Fail(error))));
}));
@Effect()
initRootItems$: Observable<Action> = this.actions$.pipe(
ofType(commonActions.INITROOTSUCCESS),
map((action) => new mapActions.Init()
));
@Effect()
startSearch$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.STARTSEARCH),
switchMap((action: mapActions.StartSearch) => {
console.debug("Start search");
var startDate = action.queryState.startDate;
var endDate = action.queryState.endDate;
var newAction:Observable<Action>;
if (action.queryState.itemCode || action.queryState.parentCode || action.queryState.itemType || action.queryState.query || action.queryState.tags) {
newAction= this.itemService$.getFeatures(action.queryState.bbox, "EPSG:3857", action.queryState.query, action.queryState.tags, startDate, endDate, action.queryState.itemType, action.queryState.parentCode).pipe(
switchMap((features: any) => {
for (let f of features.features) {
if (f.properties && f.properties["code"]) {
f.id = f.properties["code"];
}
}
return of(new mapActions.StartSearchSuccess(this._format.readFeatures(features), action.queryState));
}
),
catchError(error => of(new commonActions.Fail(error))));
} else {
newAction= of(new commonActions.Escape(true,false));
}
return newAction;
}));
@Effect()
startSearchSucces$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.STARTSEARCHSUCCESS),
mergeMap((action: mapActions.StartSearchSuccess) => {
if (action.query.bboxFilter) {
return [];
} else {
var extent = createEmpty();
if (extent) {
for (let f of action.features) {
extend(extent, (f as Feature).getGeometry().getExtent());
}
}
//return [];
return of(new mapActions.SetExtent(extent));
}
}));
@Effect()
selectItem$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.SELECTITEM),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItem)),
switchMap(([action, selectedItem]) => {
let a = action as mapActions.SelectItem;
let itemCode = selectedItem ? selectedItem.code : "";
if (a.itemCode != itemCode) {
return this.itemService$.getItem(a.itemCode).pipe(
map((item: IItem) => new mapActions.SelectItemSuccess(item)),
catchError(error => of(new commonActions.Fail(error))))
} else {
return [];
}
}
));
@Effect()
selectItemSuccess$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.SELECTITEMSUCCESS),
switchMap((action:mapActions.SelectItemSuccess) => {
return this.itemService$.getFeature(action.item.code, "EPSG:3857").pipe(
map((feature: IItem) => new mapActions.AddFeatureSuccess(this._format.readFeature(feature) )),
catchError(error => of(new commonActions.Fail(error))));
}
));
@Effect()
uploadedItemClick$: Observable<Action> = this.actions$.pipe(
ofType(commonActions.UPLOADEDFILECLICK),
switchMap((action: commonActions.UploadedFileClick) => of(new mapActions.DoQuery(tassign(mapReducers.initialState.query, {itemCode:action.itemCode})))
));
//@Effect()
//itemAdded$: Observable<Action> = this.actions$.pipe(
// ofType(commonActions.ITEMADDEDEVENT),
// withLatestFrom(this.store$.select(mapReducers.selectGetParentCode)),
// mergeMap(([action, parentCode]) => {
// let itemAddedAction = action as commonActions.ItemAddedEvent;
// if (parentCode && itemAddedAction.attributes["parentCode"] == parentCode) {
// return this.itemService$.getFeature(itemAddedAction.itemCode,"EPSG:3857").pipe(
// map((feature: Feature) => new mapActions.AddFeatureSuccess(this.toPointFeature(feature))),
// catchError(error => of(new commonActions.Fail(error))))
// } else
// return [
// ];
// }));
@Effect()
featureUpdate$: Observable<Action> = this.actions$.pipe(
ofType(commonActions.ITEMCHANGEDEVENT),
withLatestFrom(this.store$.select(mapReducers.selectGetFeatures)),
mergeMap(([action, features]) => {
let itemChangedAction = action as commonActions.ItemChangedEvent;
var feature: Feature = null;
for (let f of features) {
if (f.get("code") == itemChangedAction.itemCode) {
feature = f;
break;
}
}
if (feature) {
return this.itemService$.getFeature(itemChangedAction.itemCode, "EPSG:3857").pipe(
map((feature: any) => new mapActions.UpdateFeatureSuccess(this.toPointFeature(feature))),
catchError(error => of(new commonActions.Fail(error))));
} else {
return [];
}
}));
@Effect()
itemUpdate$: Observable<Action> = this.actions$.pipe(
ofType(commonActions.ITEMCHANGEDEVENT),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItem)),
mergeMap(([action, selectedItem]) => {
let itemChangedAction = action as commonActions.ItemChangedEvent;
if (selectedItem && selectedItem.code == itemChangedAction.itemCode) {
return this.itemService$.getItem(itemChangedAction.itemCode).pipe(
map((item: IItem) => new mapActions.SelectItemSuccess(item)),
catchError(error => of(new commonActions.Fail(error))));
} else {
return [];
}
}));
@Effect()
setQueryState$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.SETQUERYSTATE),
switchMap((action: mapActions.SetQueryState) => {
var newAction:Action;
if (action.queryState.itemCode && action.queryState.itemCode != "") {
newAction= new mapActions.SelectItem(action.queryState.itemCode);
} else {
newAction= new mapActions.StartSearch(action.queryState);
}
return of(newAction);
}));
@Effect()
setState$: Observable<Action> = this.actions$.pipe(
ofType(mapActions.SETSTATE),
switchMap((action: mapActions.SetState) => {
var newAction:Action;
if (action.queryState.itemCode && action.queryState.itemCode != "") {
newAction= new mapActions.SelectItem(action.queryState.itemCode);
} else {
newAction= new mapActions.StartSearch(action.queryState);
}
return of(newAction);
}));
constructor(private actions$: Actions, private store$: Store<mapReducers.State>, private folderService$: FolderService, private itemService$: ItemService) {
this._format = new GeoJSON();
}
}