diff --git a/static/js/map/CustomOverlay.js b/static/js/map/CustomOverlay.js new file mode 100644 index 0000000..dfe96f9 --- /dev/null +++ b/static/js/map/CustomOverlay.js @@ -0,0 +1,40 @@ + +var customOverlays = {}; + +try { + customOverlays = JSON.parse(localStorage["mapserver-customOverlays"]) +} catch (e){} + +function save(){ + localStorage["mapserver-customOverlays"] = JSON.stringify(customOverlays); +} + +export default function(map, overlays){ + + Object.keys(customOverlays) + .filter(name => overlays[name]) + .forEach(name => { + const layer = overlays[name]; + + if (customOverlays[name] && !map.hasLayer(layer)){ + //Add + map.addLayer(layer); + } + + if (!customOverlays[name] && map.hasLayer(layer)){ + //Remove + map.removeLayer(layer); + } + }); + + map.on('overlayadd', e => { + customOverlays[e.name] = true; + save(); + }); + + map.on('overlayremove', e => { + customOverlays[e.name] = false; + save(); + }); + +} diff --git a/static/js/map/MapComponent.js b/static/js/map/MapComponent.js index 7b3f8e1..46c1c75 100644 --- a/static/js/map/MapComponent.js +++ b/static/js/map/MapComponent.js @@ -4,6 +4,7 @@ import CoordinatesDisplay from './CoordinatesDisplay.js'; import WorldInfoDisplay from './WorldInfoDisplay.js'; import SearchControl from './SearchControl.js'; import Overlaysetup from './Overlaysetup.js'; +import CustomOverlay from './CustomOverlay.js'; import layerManager from './LayerManager.js'; import config from '../config.js'; @@ -33,6 +34,7 @@ export default { //All overlays Overlaysetup(cfg, map, overlays, wsChannel, layerManager); + CustomOverlay(map, overlays); new CoordinatesDisplay({ position: 'bottomleft' }).addTo(map); new WorldInfoDisplay(wsChannel, { position: 'bottomright' }).addTo(map); @@ -54,8 +56,6 @@ export default { map.on('zoomend', updateHash); map.on('moveend', updateHash); map.on('baselayerchange', updateHash); - - //TODO: overlay persistence (state, localstorage) }, onbeforeupdate(newVnode, oldVnode) {