From ecc84fbeff7d9450ca301860eb1089fc0a61df03 Mon Sep 17 00:00:00 2001 From: Marwin Hochfelsner <50826859+hlfan@users.noreply.github.com> Date: Mon, 22 Jun 2026 18:37:52 +0200 Subject: [PATCH] Reduce redundancy in router navigation handling --- app/assets/javascripts/router.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/router.js b/app/assets/javascripts/router.js index f7ddb571c..737e7541b 100644 --- a/app/assets/javascripts/router.js +++ b/app/assets/javascripts/router.js @@ -111,32 +111,34 @@ OSM.Router = function (map, rts) { }); } + function transition(action, path, route, beforeEnter = () => {}) { + if (!route) return false; + currentRoute.run("unload", null, route === currentRoute); + beforeEnter(); + currentPath = path; + currentRoute = route; + currentRoute.run(action, currentPath); + updateSecondaryNav(); + return true; + } + $(window).on("popstate", function (e) { if (!e.originalEvent.state) return; // Is it a real popstate event or just a hash change? const path = location.pathname + location.search, route = routes.recognize(path); if (path === currentPath) return; - currentRoute.run("unload", null, route === currentRoute); - currentPath = path; - currentRoute = route; - currentRoute.run("popstate", currentPath); - updateSecondaryNav(); - map.setState(e.originalEvent.state, { animate: false }); + const done = transition("popstate", path, route); + if (done) map.setState(e.originalEvent.state, { animate: false }); }); router.route = function (url) { const path = url.replace(/#.*/, ""), route = routes.recognize(path); - if (!route) return false; - currentRoute.run("unload", null, route === currentRoute); const state = OSM.parseHash(url); - map.setState(state); - window.history.pushState(state, document.title, url); - currentPath = path; - currentRoute = route; - currentRoute.run("pushstate", currentPath); - updateSecondaryNav(); - return true; + return transition("pushstate", path, route, () => { + map.setState(state); + window.history.pushState(state, document.title, url); + }); }; router.replace = function (url) { -- 2.47.3