From: John Firebaugh Date: Thu, 14 Nov 2013 20:26:58 +0000 (-0800) Subject: Expose more router functions X-Git-Tag: live~5234^2~69 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/c9600ea323b52aab7d317e6ea5440ac2fdb185b4 Expose more router functions --- diff --git a/app/assets/javascripts/router.js b/app/assets/javascripts/router.js index 5b9cdad07..33182c292 100644 --- a/app/assets/javascripts/router.js +++ b/app/assets/javascripts/router.js @@ -50,25 +50,7 @@ OSM.Router = function(map, rts) { currentRoute.run('load', currentPath); - var stateChange; - - map.on('moveend baselayerchange overlaylayerchange', function() { - var hash = OSM.formatHash(map); - if (hash === currentHash) return; - currentHash = hash; - stateChange(OSM.parseHash(hash), hash); - }); - - $(window).on('hashchange', function() { - var hash = location.hash; - if (hash === currentHash) return; - currentHash = hash; - var state = OSM.parseHash(hash); - if (!state) return; - map.setView(state.center, state.zoom); - map.updateLayers(state.layers); - stateChange(state, hash); - }); + var router, stateChange; if (window.history && window.history.pushState) { stateChange = function(state, hash) { @@ -94,7 +76,7 @@ OSM.Router = function(map, rts) { } }); - return function (url) { + router = function (url) { var path = url.replace(/#.*/, ''), route = routes.recognize(path); if (!route) return false; @@ -110,8 +92,31 @@ OSM.Router = function(map, rts) { window.location.replace(hash); }; - return function (url) { + router = function (url) { window.location.assign(url); } } + + router.updateHash = function() { + var hash = OSM.formatHash(map); + if (hash === currentHash) return; + currentHash = hash; + stateChange(OSM.parseHash(hash), hash); + }; + + router.hashUpdated = function() { + var hash = location.hash; + if (hash === currentHash) return; + currentHash = hash; + var state = OSM.parseHash(hash); + if (!state) return; + map.setView(state.center, state.zoom); + map.updateLayers(state.layers); + stateChange(state, hash); + }; + + map.on('moveend baselayerchange overlaylayerchange', router.updateHash); + $(window).on('hashchange', router.hashUpdated); + + return router; };