Move query state to common
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good

This commit is contained in:
Willem Dantuma 2020-02-19 11:15:31 +01:00
parent 3baf0b15c7
commit 17af34ff94
3 changed files with 67 additions and 0 deletions

View File

@ -39,6 +39,7 @@ import { IItemTask, ItemTask } from './models/itemTask';
import { IListItem } from './models/list.item';
import { ITypeaheadItem } from './models/typeahead.item';
import { IUser } from './models/user';
import { IQueryState } from './models/query.state';
import { ICodeListItem } from './models/code.list.item';
import * as commonActions from './actions/app-common.actions';
import * as commonReducers from './reducers/app-common.reducer';
@ -67,6 +68,7 @@ export {
ITypeaheadItem,
IUser,
ICodeListItem,
IQueryState,
commonActions,
commonReducers,
IAuthconfigFactory,

View File

@ -0,0 +1,12 @@
export interface IQueryState {
itemCode: string;
parentCode: string;
level: number;
itemType: string;
query: string;
tags: string;
bboxFilter: boolean;
startDate: Date;
endDate: Date;
bbox: number[];
}

View File

@ -0,0 +1,53 @@
import { Injectable, Query } from '@angular/core';
import { IQueryState } from '../models/query.state';
@Injectable({
providedIn: 'root',
})
export class StateSerializerService {
serialize(queryState: IQueryState): string {
var state = "";
state += (queryState.itemCode) ? queryState.itemCode : "";
state += ";";
state += (queryState.parentCode) ? queryState.parentCode + ";" + queryState.level.toString() : ";";
state += ";";
state += (queryState.itemType) ? queryState.itemType : "";
state += ";";
state += (queryState.query) ? encodeURIComponent( queryState.query ) : "";
state += ";";
state += (queryState.tags) ? encodeURIComponent( queryState.tags ) : "";
state += ";";
state += (queryState.bboxFilter) ? "1" : "0";
state += ";";
state += (queryState.startDate) ? JSON.stringify(queryState.startDate) : "";
state += ";";
state += (queryState.endDate) ? JSON.stringify(queryState.endDate) : "";
state += ";";
state += (queryState.bboxFilter && queryState.bbox && queryState.bbox.length>0) ? JSON.stringify(queryState.bbox) : "";
return btoa(state);
}
deserialize(queryState: string): IQueryState {
var state: IQueryState = { itemCode: null, parentCode: null, level: 1, itemType: null, query: null, tags: null, bboxFilter: false, startDate: null, endDate: null,bbox:[] };
var stateParts = atob(queryState).split(";");
if (stateParts.length == 10) {
if (stateParts[0] != "") state.itemCode = stateParts[0];
if (stateParts[1] != "") {
state.parentCode = stateParts[1];
state.level = parseInt(stateParts[2]);
}
if (stateParts[3] != "") state.itemType = stateParts[3];
if (stateParts[4] != "") state.query = decodeURIComponent(stateParts[4]);
if (stateParts[5] != "") state.tags = decodeURIComponent( stateParts[5]);
if (stateParts[6] != "") state.bboxFilter = parseInt(stateParts[6]) == 1;
if (stateParts[7] != "") state.startDate = new Date(Date.parse(JSON.parse(stateParts[7])));
if (stateParts[8] != "") state.endDate = new Date(Date.parse(JSON.parse(stateParts[8])));
if (stateParts[9] != "") state.bbox = JSON.parse(stateParts[9]);
}
return state;
}
}