@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
38 lines (37 loc) • 1.46 kB
JavaScript
import DrawingFeature from './drawingFeature.js';
import DrawingState from './drawingState.js';
export default class DrawingSerializer {
context;
constructor(context) {
this.context = context;
}
get state() {
return this.context.stateManager.state;
}
brainSerialize(drawingState) {
const serializedDrawings = [];
for (const feature of drawingState.features) {
serializedDrawings.push(feature.serialize());
}
return JSON.stringify(serializedDrawings);
}
brainDeserialize(str, stateLocation) {
const serializedFeatures = JSON.parse(str);
if (serializedFeatures) {
let drawingState = new DrawingState();
if (this.state.extendedState[stateLocation]) {
// First, delete existing drawing features in the state before adding new ones.
// This will trigger removal of the ol features in the map.
drawingState = this.state.extendedState[stateLocation];
drawingState.features.splice(0, drawingState.features.length);
}
else {
this.state.extendedState[stateLocation] = drawingState;
}
serializedFeatures.forEach((f) => DrawingFeature.deserialize(f, this.context, drawingState));
}
else {
throw new Error(`Cannot deserialize the drawing state for ${stateLocation}`);
}
}
}