1 OSM.Router = function(map, rts) {
2 var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
3 var optionalParam = /\((.*?)\)/g;
4 var namedParam = /(\(\?)?:\w+/g;
5 var splatParam = /\*\w+/g;
7 function Route(path, controller) {
8 var regexp = new RegExp('^' +
9 path.replace(escapeRegExp, '\\$&')
10 .replace(optionalParam, '(?:$1)?')
11 .replace(namedParam, function(match, optional){
12 return optional ? match : '([^\/]+)';
14 .replace(splatParam, '(.*?)') + '(?:\\?.*)?$');
18 route.match = function(path) {
19 return regexp.test(path);
22 route.run = function(action, path) {
26 params = regexp.exec(path).map(function(param, i) {
27 return (i > 0 && param) ? decodeURIComponent(param) : param;
31 (controller[action] || $.noop).apply(controller, params);
39 routes.push(Route(r, rts[r]));
41 routes.recognize = function(path) {
42 for (var i = 0; i < this.length; i++) {
43 if (this[i].match(path)) return this[i];
47 var currentPath = window.location.pathname + window.location.search,
48 currentRoute = routes.recognize(currentPath),
49 currentHash = location.hash || OSM.formatHash(map);
51 currentRoute.run('load', currentPath);
55 map.on('moveend baselayerchange overlaylayerchange', function() {
56 var hash = OSM.formatHash(map);
57 if (hash === currentHash) return;
59 stateChange(OSM.parseHash(hash), hash);
62 $(window).on('hashchange', function() {
63 var hash = location.hash;
64 if (hash === currentHash) return;
66 var state = OSM.parseHash(hash);
68 map.setView(state.center, state.zoom);
69 map.updateLayers(state.layers);
70 stateChange(state, hash);
73 if (window.history && window.history.pushState) {
74 stateChange = function(state, hash) {
75 window.history.replaceState(state, document.title, hash);
78 // Set a non-null initial state, so that the e.originalEvent.state
79 // check below works correctly when going back to the initial page.
80 stateChange(OSM.parseHash(currentHash), currentPath + currentHash);
82 $(window).on('popstate', function(e) {
83 if (!e.originalEvent.state) return; // Is it a real popstate event or just a hash change?
84 var path = window.location.pathname + window.location.search;
85 if (path === currentPath) return;
86 currentRoute.run('unload');
88 currentRoute = routes.recognize(currentPath);
89 currentRoute.run('popstate', currentPath);
90 var state = e.originalEvent.state;
91 map.setView(state.center, state.zoom, {animate: false});
92 map.updateLayers(state.layers);
95 return function (url) {
96 var path = url.replace(/#.*/, ''),
97 route = routes.recognize(path);
98 if (!route) return false;
99 window.history.pushState(OSM.parseHash(url) || {}, document.title, url);
100 currentRoute.run('unload');
102 currentRoute = route;
103 currentRoute.run('pushstate', currentPath);
107 stateChange = function(state, hash) {
108 window.location.replace(hash);
111 return function (url) {
112 window.location.assign(url);