From ef41a9e5ff69ca959887b18769db28092bc43e0f Mon Sep 17 00:00:00 2001 From: Norbert Renner Date: Tue, 18 Jun 2019 09:09:55 +0200 Subject: [PATCH] Restore active layers (#205) --- js/control/Control.Layers.js | 10 +++-- js/control/LayersTab.js | 77 +++++++++++++++++++++++++++++++++-- js/index.js | 6 +++ js/plugin/leaflet-fullHash.js | 31 ++++---------- 4 files changed, 93 insertions(+), 31 deletions(-) diff --git a/js/control/Control.Layers.js b/js/control/Control.Layers.js index 158a1f6..9bb34fa 100644 --- a/js/control/Control.Layers.js +++ b/js/control/Control.Layers.js @@ -59,15 +59,17 @@ BR.ControlLayers = L.Control.Layers.extend({ }); }, - activateLayer: function(layer) { - this._map.addLayer(layer); + activateLayer: function(obj) { + if (!this._map.hasLayer(obj.layer)) { + this._map.addLayer(obj.layer); + } }, activateFirstLayer: function() { for (var i = 0; i < this._layers.length; i++) { var obj = this._layers[i]; if (!obj.overlay) { - this._map.addLayer(obj.layer); + this.activateLayer(obj); break; } } @@ -77,7 +79,7 @@ BR.ControlLayers = L.Control.Layers.extend({ var baseLayers = this.getBaseLayers(); var obj = baseLayers[index]; - this.activateLayer(obj.layer); + this.activateLayer(obj); }, _addLayer: function(layer, name, overlay) { diff --git a/js/control/LayersTab.js b/js/control/LayersTab.js index ab1aaad..e577c66 100644 --- a/js/control/LayersTab.js +++ b/js/control/LayersTab.js @@ -26,6 +26,26 @@ BR.LayersTab = BR.ControlLayers.extend({ return this; }, + onAdd: function(map) { + BR.ControlLayers.prototype.onAdd.call(this, map); + + map.on( + 'baselayerchange overlayadd overlayremove', + this.storeActiveLayers, + this + ); + }, + + onRemove: function(map) { + BR.ControlLayers.prototype.onRemove.call(this, map); + + map.off( + 'baselayerchange overlayadd overlayremove', + this.storeActiveLayers, + this + ); + }, + initButtons: function() { var expandTree = function(e) { this.jstree.open_all(); @@ -298,9 +318,7 @@ BR.LayersTab = BR.ControlLayers.extend({ if (!overlaysOnly || (overlaysOnly && obj.overlay)) { var hasLayer = !!this._getLayer(L.Util.stamp(obj.layer)); if (hasLayer) { - if (!this._map.hasLayer(obj.layer)) { - this._map.addLayer(obj.layer); - } + this.activateLayer(obj); } else if (!obj.overlay) { // saved base layer has been removed during preview, select first this.activateFirstLayer(); @@ -379,6 +397,59 @@ BR.LayersTab = BR.ControlLayers.extend({ this.removePreviewBounds(); this.removePreviewLayer(); this.restoreActiveLayers(); + }, + + toLayerString: function(obj) { + return obj.layer.id ? obj.layer.id : obj.name; + }, + + getLayerFromString: function(layerString) { + var obj = this.getLayerById(layerString); + + if (!obj) { + // fallback to name for custom and config layers + obj = this.getLayer(layerString); + + if (!obj) { + // legacy layer name support + obj = this.getLayerByLegacyName(layerString); + } + } + + return obj; + }, + + storeActiveLayers: function() { + if (BR.Util.localStorageAvailable()) { + var objList = this.getActiveLayers(); + var idList = objList.map( + L.bind(function(obj) { + return this.toLayerString(obj); + }, this) + ); + var str = JSON.stringify(idList); + + localStorage.setItem('map/activeLayers', str); + } + }, + + loadActiveLayers: function() { + if (BR.Util.localStorageAvailable()) { + var item = localStorage.getItem('map/activeLayers'); + + if (item) { + var idList = JSON.parse(item); + + for (var i = 0; i < idList.length; i++) { + var id = idList[i]; + var obj = this.getLayerFromString(id); + + if (obj) { + this.activateLayer(obj); + } + } + } + } } }); diff --git a/js/index.js b/js/index.js index f9402bd..1b36642 100644 --- a/js/index.js +++ b/js/index.js @@ -292,6 +292,12 @@ router.setOptions(routingOptions.getOptions()); profile.update(routingOptions.getOptions()); + // restore active layers from local storage when called without hash + // (check before hash plugin init) + if (!location.hash) { + layersControl.loadActiveLayers(); + } + var onHashChangeCb = function(url) { var url2params = function(s) { s = s.replace(/;/g, '|'); diff --git a/js/plugin/leaflet-fullHash.js b/js/plugin/leaflet-fullHash.js index e24e95d..cc7c47b 100644 --- a/js/plugin/leaflet-fullHash.js +++ b/js/plugin/leaflet-fullHash.js @@ -92,7 +92,9 @@ var layerString = decodeURIComponent(layerEncoded); if (layerString) { - obj = this._getLayerFromString(layerString); + obj = this.options.layersControl.getLayerFromString( + layerString + ); } return obj; @@ -138,7 +140,7 @@ layers.forEach( L.bind(function(obj, index, array) { if (obj) { - layersControl.activateLayer(obj.layer); + layersControl.activateLayer(obj); if (obj && !obj.overlay) { added = true; } @@ -156,34 +158,15 @@ var objList = this.options.layersControl.getActiveLayers(); var layerList = objList.map( L.bind(function(obj) { - return encodeURIComponent(this._toLayerString(obj)); + return encodeURIComponent( + this.options.layersControl.toLayerString(obj) + ); }, this) ); return layerList.join(this.options.layerSeparator); }, - _toLayerString: function(obj) { - return obj.layer.id ? obj.layer.id : obj.name; - }, - - _getLayerFromString: function(layerString) { - var layersControl = this.options.layersControl; - var obj = layersControl.getLayerById(layerString); - - if (!obj) { - // fallback to name for custom and config layers - obj = layersControl.getLayer(layerString); - - if (!obj) { - // legacy layer name support - obj = layersControl.getLayerByLegacyName(layerString); - } - } - - return obj; - }, - removeFrom: function(map) { if (this.changeTimeout) { clearTimeout(this.changeTimeout);