UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

58 lines (57 loc) • 2.22 kB
import LayoutSerializer from './layoutserializer.js'; /** * The layouts the display modes of the previous versions correspond to. * * <code>globe.display</code> was replaced by <code>state.layout</code>, but the states that were * shared before that change still contain it and have to keep working. Such a state never contains * a layout, so there is no risk of overwriting a layout that was deserialized before. */ const LEGACY_DISPLAYS = { '2D': { type: '1x1', cells: ['map-2d'], sizes: { columns: [100], rows: [100] } }, '3D': { type: '1x1', cells: ['map-3d'], sizes: { columns: [100], rows: [100] } }, '2D/3D': { type: '2x1', cells: ['map-2d', 'map-3d'], sizes: { columns: [45, 55], rows: [100] } } }; export default class GlobeSerializer { context; constructor(context) { this.context = context; } get globe() { return this.context.stateManager.state.globe; } brainSerialize(globe) { return JSON.stringify({ shadows: globe.shadows, shadowsTimestamp: globe.shadowsTimestamp, camera: globe.camera }); } brainDeserialize(str) { const parsedGlobe = JSON.parse(str); const globe = this.globe; if (parsedGlobe.shadows !== undefined) { globe.shadows = parsedGlobe.shadows; } if (parsedGlobe.shadowsTimestamp) { globe.shadowsTimestamp = parsedGlobe.shadowsTimestamp; } globe.camera = parsedGlobe.camera ?? null; globe.loaded = false; this.deserializeLegacyDisplay(parsedGlobe.display); } /** * Applies the layout a display mode of the previous versions corresponds to. * TODO REG: Remove the legacy code when not used any more * */ deserializeLegacyDisplay(display) { if (display === undefined) { return; } const legacyLayout = LEGACY_DISPLAYS[display]; if (!legacyLayout) { console.warn(`${display} is not a known display mode. The shared display mode will be ignored.`); return; } new LayoutSerializer(this.context).applyLayout(legacyLayout.type, legacyLayout.cells, legacyLayout.sizes); } }