Store mapstate in localstorage
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good

This commit is contained in:
Willem Dantuma 2020-01-06 18:36:47 +01:00
parent 50d1d9645a
commit 58f7c99ddc
2 changed files with 23 additions and 2 deletions

View File

@ -61,13 +61,32 @@ import { LayerVectorImageComponent } from './components/aol/layer-vector-image/l
import { StateSerializerService } from './services/state-serializer.service'; import { StateSerializerService } from './services/state-serializer.service';
import { GeolocationService } from './services/geolocation.service'; import { GeolocationService } from './services/geolocation.service';
import {DeviceOrientationService} from './services/device-orientation.service'; import {DeviceOrientationService} from './services/device-orientation.service';
import { localStorageSync } from 'ngrx-store-localstorage';
import { WidgetStatusComponent } from './components/widget-status/widget-status.component'; import { WidgetStatusComponent } from './components/widget-status/widget-status.component';
import { ForChild} from './components/for-item/for-child.decorator'; import { ForChild} from './components/for-item/for-child.decorator';
import {ForItemType } from './components/for-item/for-itemtype.decorator'; import {ForItemType } from './components/for-item/for-itemtype.decorator';
import { ForSourceTask} from './components/for-item/for-sourcetask.decorator'; import { ForSourceTask} from './components/for-item/for-sourcetask.decorator';
import { PanToLocation} from './components/aol/pan-to-location/pan-to-location.component'; import { PanToLocation} from './components/aol/pan-to-location/pan-to-location.component';
export function LocalStorageSync(reducer: ActionReducer<any>): ActionReducer<any> {
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<MetaReducer<any, any>> = [LocalStorageSync];
export { export {
mapEffects, mapEffects,
@ -132,7 +151,7 @@ export {
CommonModule, CommonModule,
AngularOpenlayersModule, AngularOpenlayersModule,
MapRoutingModule, MapRoutingModule,
StoreModule.forFeature(MODULE_NAME, mapReducers.reducer), StoreModule.forFeature(MODULE_NAME, mapReducers.reducer,{metaReducers:metaReducers}),
EffectsModule.forFeature([mapEffects.MapEffects]), EffectsModule.forFeature([mapEffects.MapEffects]),
NgbModule, NgbModule,
FormsModule, FormsModule,
@ -227,6 +246,7 @@ export {
] ]
}) })
export class AppCommonMapModule { export class AppCommonMapModule {
static forRoot(): ModuleWithProviders { static forRoot(): ModuleWithProviders {
return { return {

View File

@ -175,6 +175,7 @@ export class MapComponent implements OnInit, OnDestroy,AfterViewInit {
newMapState = { xCenter: xCenter, yCenter: yCenter, zoom: zoom, rotation: rotation, baseLayerCode: baseLayer } newMapState = { xCenter: xCenter, yCenter: yCenter, zoom: zoom, rotation: rotation, baseLayerCode: baseLayer }
mapStateChanged = this.lastMapState != JSON.stringify(newMapState) && this.stateSetCount == 0; mapStateChanged = this.lastMapState != JSON.stringify(newMapState) && this.stateSetCount == 0;
this.lastMapState = JSON.stringify(newMapState); this.lastMapState = JSON.stringify(newMapState);
window.localStorage.setItem("FarmMapsCommonMap_mapState",this.lastMapState);
} }
if (params.has("queryState")) { if (params.has("queryState")) {
let queryState = params.get("queryState"); let queryState = params.get("queryState");