From 58f7c99ddc311108d4b4e2a13270f1b745078867 Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Mon, 6 Jan 2020 18:36:47 +0100 Subject: [PATCH] Store mapstate in localstorage --- .../src/fm-map/common-map.module.ts | 24 +++++++++++++++++-- .../fm-map/components/map/map.component.ts | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/projects/common-map/src/fm-map/common-map.module.ts b/projects/common-map/src/fm-map/common-map.module.ts index 1db6307..7b6b2fa 100644 --- a/projects/common-map/src/fm-map/common-map.module.ts +++ b/projects/common-map/src/fm-map/common-map.module.ts @@ -61,13 +61,32 @@ import { LayerVectorImageComponent } from './components/aol/layer-vector-image/l import { StateSerializerService } from './services/state-serializer.service'; import { GeolocationService } from './services/geolocation.service'; import {DeviceOrientationService} from './services/device-orientation.service'; -import { localStorageSync } from 'ngrx-store-localstorage'; import { WidgetStatusComponent } from './components/widget-status/widget-status.component'; import { ForChild} from './components/for-item/for-child.decorator'; import {ForItemType } from './components/for-item/for-itemtype.decorator'; import { ForSourceTask} from './components/for-item/for-sourcetask.decorator'; import { PanToLocation} from './components/aol/pan-to-location/pan-to-location.component'; +export function LocalStorageSync(reducer: ActionReducer): ActionReducer { + const r = function(state, action) { + const r2 = reducer(state, action); + + if(action.type == "@ngrx/store/update-reducers") { + let ms = window.localStorage.getItem(MODULE_NAME+"_mapState"); + if(ms) { + r2["mapState"] = JSON.parse(ms); + } + } + if(action.type == "[Map] MapState" || action.type == "[Map] SetState") { + window.localStorage.setItem(MODULE_NAME + "_mapState",JSON.stringify(r2["mapState"])); + } + + return r2; + }; + return r; + } + +const metaReducers: Array> = [LocalStorageSync]; export { mapEffects, @@ -132,7 +151,7 @@ export { CommonModule, AngularOpenlayersModule, MapRoutingModule, - StoreModule.forFeature(MODULE_NAME, mapReducers.reducer), + StoreModule.forFeature(MODULE_NAME, mapReducers.reducer,{metaReducers:metaReducers}), EffectsModule.forFeature([mapEffects.MapEffects]), NgbModule, FormsModule, @@ -227,6 +246,7 @@ export { ] }) + export class AppCommonMapModule { static forRoot(): ModuleWithProviders { return { diff --git a/projects/common-map/src/fm-map/components/map/map.component.ts b/projects/common-map/src/fm-map/components/map/map.component.ts index 05159c3..7de5ad3 100644 --- a/projects/common-map/src/fm-map/components/map/map.component.ts +++ b/projects/common-map/src/fm-map/components/map/map.component.ts @@ -175,6 +175,7 @@ export class MapComponent implements OnInit, OnDestroy,AfterViewInit { newMapState = { xCenter: xCenter, yCenter: yCenter, zoom: zoom, rotation: rotation, baseLayerCode: baseLayer } mapStateChanged = this.lastMapState != JSON.stringify(newMapState) && this.stateSetCount == 0; this.lastMapState = JSON.stringify(newMapState); + window.localStorage.setItem("FarmMapsCommonMap_mapState",this.lastMapState); } if (params.has("queryState")) { let queryState = params.get("queryState");