Generate hash-based permalinks

This commit is contained in:
John Firebaugh 2013-07-24 10:09:56 -07:00
parent b28511faca
commit 3c22a53c93
3 changed files with 23 additions and 42 deletions

View file

@ -59,41 +59,22 @@ function remoteEditHandler(bbox, select) {
* view tab and various other links * view tab and various other links
*/ */
function updatelinks(loc, zoom, layers, bounds, object) { function updatelinks(loc, zoom, layers, bounds, object) {
var precision = zoomPrecision(zoom); $(".geolink").each(function(index, link) {
bounds = normalBounds(bounds);
var lat = loc.lat.toFixed(precision),
lon = (loc.lon || loc.lng).toFixed(precision);
if (bounds) {
var minlon = bounds.getWest().toFixed(precision),
minlat = bounds.getSouth().toFixed(precision),
maxlon = bounds.getEast().toFixed(precision),
maxlat = bounds.getNorth().toFixed(precision);
}
$(".geolink").each(setGeolink);
function setGeolink(index, link) {
var base = link.href.split('?')[0], var base = link.href.split('?')[0],
qs = link.href.split('?')[1], args = querystring.parse(link.search.substring(1));
args = querystring.parse(qs);
if ($(link).hasClass("llz")) {
$.extend(args, {
lat: lat,
lon: lon,
zoom: zoom
});
} else if (minlon && $(link).hasClass("bbox")) {
$.extend(args, {
bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
});
}
if (bounds && $(link).hasClass("bbox")) args.bbox = normalBounds(bounds).toBBoxString();
if (layers && $(link).hasClass("layers")) args.layers = layers; if (layers && $(link).hasClass("layers")) args.layers = layers;
if (object && $(link).hasClass("object")) args[object.type] = object.id; if (object && $(link).hasClass("object")) args[object.type] = object.id;
var href = base + '?' + querystring.stringify(args);
if ($(link).hasClass("llz")) {
href += OSM.formatHash({lat: loc.lat, lon: loc.lon || loc.lng, zoom: zoom});
}
link.href = href;
var minzoom = $(link).data("minzoom"); var minzoom = $(link).data("minzoom");
if (minzoom) { if (minzoom) {
var name = link.id.replace(/anchor$/, ""); var name = link.id.replace(/anchor$/, "");
@ -112,8 +93,7 @@ function updatelinks(loc, zoom, layers, bounds, object) {
}); });
} }
} }
link.href = base + '?' + querystring.stringify(args); });
}
} }
// generate a cookie-safe string of map state // generate a cookie-safe string of map state

View file

@ -29,22 +29,16 @@ L.extend(L.Map.prototype, {
}, },
getUrl: function(marker) { getUrl: function(marker) {
var center = this.getCenter(), var precision = zoomPrecision(this.getZoom()),
zoom = this.getZoom(), params = { layers: this.getLayersCode() };
precision = zoomPrecision(zoom),
params = {
lat: center.lat.toFixed(precision),
lon: center.lng.toFixed(precision),
zoom: zoom,
layers: this.getLayersCode()
};
if (marker && this.hasLayer(marker)) { if (marker && this.hasLayer(marker)) {
params.mlat = marker.getLatLng().lat.toFixed(precision); params.mlat = marker.getLatLng().lat.toFixed(precision);
params.mlon = marker.getLatLng().lng.toFixed(precision); params.mlon = marker.getLatLng().lng.toFixed(precision);
} }
return 'http://' + OSM.SERVER_URL + '/?' + querystring.stringify(params); return 'http://' + OSM.SERVER_URL + '/?' + querystring.stringify(params) +
OSM.formatHash({lat: this.getCenter().lat, lon: this.getCenter().lng, zoom: this.getZoom()});
}, },
getShortUrl: function(marker) { getShortUrl: function(marker) {

View file

@ -130,5 +130,12 @@ OSM = {
} }
return mapParams; return mapParams;
},
formatHash: function(args) {
var precision = zoomPrecision(args.zoom);
return '#' + args.zoom +
'/' + args.lat.toFixed(precision) +
'/' + args.lon.toFixed(precision);
} }
}; };