Restore active layers (#205)

This commit is contained in:
Norbert Renner 2019-06-18 09:09:55 +02:00
parent 708b35f642
commit ef41a9e5ff
4 changed files with 93 additions and 31 deletions

View file

@ -59,15 +59,17 @@ BR.ControlLayers = L.Control.Layers.extend({
}); });
}, },
activateLayer: function(layer) { activateLayer: function(obj) {
this._map.addLayer(layer); if (!this._map.hasLayer(obj.layer)) {
this._map.addLayer(obj.layer);
}
}, },
activateFirstLayer: function() { activateFirstLayer: function() {
for (var i = 0; i < this._layers.length; i++) { for (var i = 0; i < this._layers.length; i++) {
var obj = this._layers[i]; var obj = this._layers[i];
if (!obj.overlay) { if (!obj.overlay) {
this._map.addLayer(obj.layer); this.activateLayer(obj);
break; break;
} }
} }
@ -77,7 +79,7 @@ BR.ControlLayers = L.Control.Layers.extend({
var baseLayers = this.getBaseLayers(); var baseLayers = this.getBaseLayers();
var obj = baseLayers[index]; var obj = baseLayers[index];
this.activateLayer(obj.layer); this.activateLayer(obj);
}, },
_addLayer: function(layer, name, overlay) { _addLayer: function(layer, name, overlay) {

View file

@ -26,6 +26,26 @@ BR.LayersTab = BR.ControlLayers.extend({
return this; 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() { initButtons: function() {
var expandTree = function(e) { var expandTree = function(e) {
this.jstree.open_all(); this.jstree.open_all();
@ -298,9 +318,7 @@ BR.LayersTab = BR.ControlLayers.extend({
if (!overlaysOnly || (overlaysOnly && obj.overlay)) { if (!overlaysOnly || (overlaysOnly && obj.overlay)) {
var hasLayer = !!this._getLayer(L.Util.stamp(obj.layer)); var hasLayer = !!this._getLayer(L.Util.stamp(obj.layer));
if (hasLayer) { if (hasLayer) {
if (!this._map.hasLayer(obj.layer)) { this.activateLayer(obj);
this._map.addLayer(obj.layer);
}
} else if (!obj.overlay) { } else if (!obj.overlay) {
// saved base layer has been removed during preview, select first // saved base layer has been removed during preview, select first
this.activateFirstLayer(); this.activateFirstLayer();
@ -379,6 +397,59 @@ BR.LayersTab = BR.ControlLayers.extend({
this.removePreviewBounds(); this.removePreviewBounds();
this.removePreviewLayer(); this.removePreviewLayer();
this.restoreActiveLayers(); 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);
}
}
}
}
} }
}); });

View file

@ -292,6 +292,12 @@
router.setOptions(routingOptions.getOptions()); router.setOptions(routingOptions.getOptions());
profile.update(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 onHashChangeCb = function(url) {
var url2params = function(s) { var url2params = function(s) {
s = s.replace(/;/g, '|'); s = s.replace(/;/g, '|');

View file

@ -92,7 +92,9 @@
var layerString = decodeURIComponent(layerEncoded); var layerString = decodeURIComponent(layerEncoded);
if (layerString) { if (layerString) {
obj = this._getLayerFromString(layerString); obj = this.options.layersControl.getLayerFromString(
layerString
);
} }
return obj; return obj;
@ -138,7 +140,7 @@
layers.forEach( layers.forEach(
L.bind(function(obj, index, array) { L.bind(function(obj, index, array) {
if (obj) { if (obj) {
layersControl.activateLayer(obj.layer); layersControl.activateLayer(obj);
if (obj && !obj.overlay) { if (obj && !obj.overlay) {
added = true; added = true;
} }
@ -156,34 +158,15 @@
var objList = this.options.layersControl.getActiveLayers(); var objList = this.options.layersControl.getActiveLayers();
var layerList = objList.map( var layerList = objList.map(
L.bind(function(obj) { L.bind(function(obj) {
return encodeURIComponent(this._toLayerString(obj)); return encodeURIComponent(
this.options.layersControl.toLayerString(obj)
);
}, this) }, this)
); );
return layerList.join(this.options.layerSeparator); 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) { removeFrom: function(map) {
if (this.changeTimeout) { if (this.changeTimeout) {
clearTimeout(this.changeTimeout); clearTimeout(this.changeTimeout);