Refactor hash layers
This commit is contained in:
parent
d83ffa3fbc
commit
f5c952a0eb
1 changed files with 38 additions and 29 deletions
|
|
@ -23,12 +23,7 @@
|
||||||
var zoom = parseInt(mapsArgs[0], 10),
|
var zoom = parseInt(mapsArgs[0], 10),
|
||||||
lat = parseFloat(mapsArgs[1]),
|
lat = parseFloat(mapsArgs[1]),
|
||||||
lon = parseFloat(mapsArgs[2]),
|
lon = parseFloat(mapsArgs[2]),
|
||||||
layersParam = mapsArgs[3],
|
layers = this.parseLayers(mapsArgs[3]),
|
||||||
// legacy support for '-' layer separator
|
|
||||||
layerSeparator = layersParam.indexOf('-') !== -1 ? '-' : this.options.layerSeparator,
|
|
||||||
layers = layersParam.split(layerSeparator).map(function (name) {
|
|
||||||
return decodeURIComponent(name);
|
|
||||||
}),
|
|
||||||
additional = args[1];
|
additional = args[1];
|
||||||
if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) {
|
if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -49,13 +44,13 @@
|
||||||
var center = map.getCenter(),
|
var center = map.getCenter(),
|
||||||
zoom = map.getZoom(),
|
zoom = map.getZoom(),
|
||||||
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)),
|
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)),
|
||||||
layers = this.getActiveLayers();
|
layers = this.formatLayers();
|
||||||
|
|
||||||
var params = [
|
var params = [
|
||||||
zoom,
|
zoom,
|
||||||
center.lat.toFixed(precision),
|
center.lat.toFixed(precision),
|
||||||
center.lng.toFixed(precision),
|
center.lng.toFixed(precision),
|
||||||
layers.join(this.options.layerSeparator)
|
layers
|
||||||
];
|
];
|
||||||
url = "#map=" + params.join("/");
|
url = "#map=" + params.join("/");
|
||||||
if (this.additionalCb != null) {
|
if (this.additionalCb != null) {
|
||||||
|
|
@ -90,11 +85,41 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getActiveLayers: function () {
|
parseLayers: function (layersParam) {
|
||||||
var objList = this.options.layersControl.getActiveLayers();
|
// legacy support for '-' layer separator
|
||||||
return objList.map(function (obj) {
|
var layerSeparator = layersParam.indexOf('-') !== -1 ? '-' : this.options.layerSeparator;
|
||||||
return encodeURIComponent(obj.name);
|
var layers = layersParam.split(layerSeparator).map(function (layerEncoded) {
|
||||||
|
return decodeURIComponent(layerEncoded);
|
||||||
});
|
});
|
||||||
|
return layers;
|
||||||
|
},
|
||||||
|
|
||||||
|
activateLayers: function (layers) {
|
||||||
|
var layersControl = this.options.layersControl;
|
||||||
|
var added = false;
|
||||||
|
|
||||||
|
layersControl.removeActiveLayers();
|
||||||
|
|
||||||
|
layers.forEach(function(name, index, array) {
|
||||||
|
if (!name) return;
|
||||||
|
var obj = layersControl.activateLayer(name);
|
||||||
|
if (obj && !obj.overlay) {
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!added) {
|
||||||
|
// if we couldn't add layers (removed or invalid name), add the default one
|
||||||
|
layersControl.activateFirstLayer();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
formatLayers: function () {
|
||||||
|
var objList = this.options.layersControl.getActiveLayers();
|
||||||
|
var layerList = objList.map(L.bind(function (obj) {
|
||||||
|
return encodeURIComponent(obj.name);
|
||||||
|
}, this));
|
||||||
|
return layerList.join(this.options.layerSeparator)
|
||||||
},
|
},
|
||||||
|
|
||||||
removeFrom: function(map) {
|
removeFrom: function(map) {
|
||||||
|
|
@ -145,24 +170,8 @@
|
||||||
this.movingMap = true;
|
this.movingMap = true;
|
||||||
|
|
||||||
this.map.setView(parsed.center, parsed.zoom);
|
this.map.setView(parsed.center, parsed.zoom);
|
||||||
var layers = parsed.layers,
|
|
||||||
layersControl = this.options.layersControl,
|
|
||||||
that = this;
|
|
||||||
|
|
||||||
layersControl.removeActiveLayers();
|
this.activateLayers(parsed.layers);
|
||||||
|
|
||||||
var added = false;
|
|
||||||
layers.forEach(function(element, index, array) {
|
|
||||||
if (!element) return;
|
|
||||||
var obj = layersControl.activateLayer(element);
|
|
||||||
if (obj && !obj.overlay) {
|
|
||||||
added = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (!added) {
|
|
||||||
// if we couldn't add layers (removed or invalid name), add the default one
|
|
||||||
layersControl.activateFirstLayer();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.onHashChangeCb != null) {
|
if (this.onHashChangeCb != null) {
|
||||||
this.onHashChangeCb(parsed.additional);
|
this.onHashChangeCb(parsed.additional);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue