From 07976676decf321bffb3e1f3cb6782a15dae5ade Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 3 Dec 2013 10:47:38 -0800 Subject: [PATCH] Restore support for #layers=X with no map parameter https://trac.openstreetmap.org/ticket/5056 --- app/assets/javascripts/leaflet.map.js.erb | 5 +++ app/assets/javascripts/osm.js.erb | 38 +++++++---------------- app/assets/javascripts/router.js | 12 ++----- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/app/assets/javascripts/leaflet.map.js.erb b/app/assets/javascripts/leaflet.map.js.erb index f65325e37..af7ce202d 100644 --- a/app/assets/javascripts/leaflet.map.js.erb +++ b/app/assets/javascripts/leaflet.map.js.erb @@ -243,6 +243,11 @@ L.OSM.Map = L.Map.extend({ zoom: this.getZoom(), layers: this.getLayersCode() } + }, + + setState: function(state, options) { + if (state.center) this.setView(state.center, state.zoom, options); + this.updateLayers(state.layers); } }); diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index 7b5df1865..922bd2639 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -111,41 +111,25 @@ OSM = { }, parseHash: function(hash) { + var args = {}; + var i = hash.indexOf('#'); if (i < 0) { - return false; + return args; } - hash = hash.substr(i + 1); - - if (hash === '') { - return false; - } + hash = querystring.parse(hash.substr(i + 1)); - hash = querystring.parse(hash); - - if (hash.map === undefined) { - return false; - } + var map = (hash.map || '').split('/'), + zoom = parseInt(map[0], 10), + lat = parseFloat(map[1]), + lon = parseFloat(map[2]); - var args = hash.map.split("/"); - if (args.length !== 3) { - return false; + if (!isNaN(zoom) && !isNaN(lat) && !isNaN(lon)) { + args.center = new L.LatLng(lat, lon); + args.zoom = zoom; } - var zoom = parseInt(args[0], 10), - lat = parseFloat(args[1]), - lon = parseFloat(args[2]); - - if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) { - return false; - } - - args = { - center: new L.LatLng(lat, lon), - zoom: zoom - }; - if (hash.layers) { args.layers = hash.layers; } diff --git a/app/assets/javascripts/router.js b/app/assets/javascripts/router.js index cadb6b628..200edd266 100644 --- a/app/assets/javascripts/router.js +++ b/app/assets/javascripts/router.js @@ -106,18 +106,14 @@ OSM.Router = function(map, rts) { currentPath = path; currentRoute = routes.recognize(currentPath); currentRoute.run('popstate', currentPath); - var state = e.originalEvent.state; - if (state.center) { - map.setView(state.center, state.zoom, {animate: false}); - map.updateLayers(state.layers); - } + map.setState(e.originalEvent.state, {animate: false}); }); router.route = function (url) { var path = url.replace(/#.*/, ''), route = routes.recognize(path); if (!route) return false; - window.history.pushState(OSM.parseHash(url) || {}, document.title, url); + window.history.pushState(OSM.parseHash(url), document.title, url); currentRoute.run('unload'); currentPath = path; currentRoute = route; @@ -154,9 +150,7 @@ OSM.Router = function(map, rts) { if (hash === currentHash) return; currentHash = hash; var state = OSM.parseHash(hash); - if (!state) return; - map.setView(state.center, state.zoom); - map.updateLayers(state.layers); + map.setState(state); router.stateChange(state, hash); }; -- 2.39.5