2 OSM.Router implements pushState-based navigation for the main page and
3 other pages that use a sidebar+map based layout (export, search results,
4 history, and browse pages).
6 For browsers without pushState, it falls back to full page loads, which all
7 of the above pages support.
9 The router is initialized with a set of routes: a mapping of URL path templates
10 to route controller objects. Path templates can contain placeholders
11 (`/note/:id`) and optional segments (`/:type/:id(/history)`).
13 Route controller objects can define four methods that are called at defined
16 * The `load` method is called by the router when a path which matches the
17 route's path template is loaded via a normal full page load. It is passed
18 as arguments the URL path plus any matching arguments for placeholders
21 * The `pushstate` method is called when a page which matches the route's path
22 template is loaded via pushState. It is passed the same arguments as `load`.
24 * The `popstate` method is called when returning to a previously
25 pushState-loaded page via popstate (i.e. browser back/forward buttons).
27 * The `unload` method is called on the exiting route controller when navigating
28 via pushState or popstate to another route.
30 Note that while `load` is not called by the router for pushState-based loads,
31 it's frequently useful for route controllers to call it manually inside their
32 definition of the `pushstate` and `popstate` methods.
34 An instance of OSM.Router is assigned to `OSM.router`. To navigate to a new page
35 via pushState (with automatic full-page load fallback), call `OSM.router.route`:
37 OSM.router.route('/way/1234');
39 If `route` is passed a path that matches one of the path templates, it performs
40 the appropriate actions and returns true. Otherwise it returns false.
42 OSM.Router also handles updating the hash portion of the URL containing transient
43 map state such as the position and zoom level. Some route controllers may wish to
44 temporarily suppress updating the hash (for example, to omit the hash on pages
45 such as `/way/1234` unless the map is moved). This can be done by using
46 `OSM.router.withoutMoveListener` to run a block of code that may update
47 move the map without the hash changing.
49 OSM.Router = function (map, rts) {
50 var escapeRegExp = /[-{}[\]+?.,\\^$|#\s]/g;
51 var optionalParam = /\((.*?)\)/g;
52 var namedParam = /(\(\?)?:\w+/g;
53 var splatParam = /\*\w+/g;
55 function Route(path, controller) {
56 var regexp = new RegExp("^" +
57 path.replace(escapeRegExp, "\\$&")
58 .replace(optionalParam, "(?:$1)?")
59 .replace(namedParam, function (match, optional) {
60 return optional ? match : "([^/]+)";
62 .replace(splatParam, "(.*?)") + "(?:\\?.*)?$");
66 route.match = function (path) {
67 return regexp.test(path);
70 route.run = function (action, path) {
74 params = regexp.exec(path).map(function (param, i) {
75 return (i > 0 && param) ? decodeURIComponent(param) : param;
79 params = params.concat(Array.prototype.slice.call(arguments, 2));
81 return (controller[action] || $.noop).apply(controller, params);
89 routes.push(new Route(r, rts[r]));
92 routes.recognize = function (path) {
93 for (var i = 0; i < this.length; i++) {
94 if (this[i].match(path)) return this[i];
98 var currentPath = window.location.pathname.replace(/(.)\/$/, "$1") + window.location.search,
99 currentRoute = routes.recognize(currentPath),
100 currentHash = location.hash || OSM.formatHash(map);
104 function updateSecondaryNav() {
105 $("header nav.secondary > ul > li > a").each(function () {
106 var active = $(this).attr("href") === window.location.pathname;
109 .toggleClass("text-secondary", !active)
110 .toggleClass("text-secondary-emphasis", active);
114 $(window).on("popstate", function (e) {
115 if (!e.originalEvent.state) return; // Is it a real popstate event or just a hash change?
116 var path = window.location.pathname + window.location.search,
117 route = routes.recognize(path);
118 if (path === currentPath) return;
119 currentRoute.run("unload", null, route === currentRoute);
121 currentRoute = route;
122 currentRoute.run("popstate", currentPath);
123 updateSecondaryNav();
124 map.setState(e.originalEvent.state, { animate: false });
127 router.route = function (url) {
128 var path = url.replace(/#.*/, ""),
129 route = routes.recognize(path);
130 if (!route) return false;
131 currentRoute.run("unload", null, route === currentRoute);
132 var state = OSM.parseHash(url);
134 window.history.pushState(state, document.title, url);
136 currentRoute = route;
137 currentRoute.run("pushstate", currentPath);
138 updateSecondaryNav();
142 router.replace = function (url) {
143 window.history.replaceState(OSM.parseHash(url), document.title, url);
146 router.stateChange = function (state) {
148 window.history.replaceState(state, document.title, OSM.formatHash(state));
150 window.history.replaceState(state, document.title, window.location);
154 router.updateHash = function () {
155 var hash = OSM.formatHash(map);
156 if (hash === currentHash) return;
158 router.stateChange(OSM.parseHash(hash));
161 router.hashUpdated = function () {
162 var hash = location.hash;
163 if (hash === currentHash) return;
165 var state = OSM.parseHash(hash);
167 router.stateChange(state, hash);
170 router.withoutMoveListener = function (callback) {
171 function disableMoveListener() {
172 map.off("moveend", router.updateHash);
173 map.once("moveend", function () {
174 map.on("moveend", router.updateHash);
178 map.once("movestart", disableMoveListener);
180 map.off("movestart", disableMoveListener);
183 router.load = function () {
184 var loadState = currentRoute.run("load", currentPath);
185 router.stateChange(loadState || {});
188 router.setCurrentPath = function (path) {
190 currentRoute = routes.recognize(currentPath);
193 map.on("moveend baselayerchange overlaylayerchange", router.updateHash);
194 $(window).on("hashchange", router.hashUpdated);