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);
53 var router, stateChange;
55 if (window.history && window.history.pushState) {
56 stateChange = function(state, hash) {
57 window.history.replaceState(state, document.title, hash);
60 // Set a non-null initial state, so that the e.originalEvent.state
61 // check below works correctly when going back to the initial page.
62 stateChange(OSM.parseHash(currentHash), currentPath + currentHash);
64 $(window).on('popstate', function(e) {
65 if (!e.originalEvent.state) return; // Is it a real popstate event or just a hash change?
66 var path = window.location.pathname + window.location.search;
67 if (path === currentPath) return;
68 currentRoute.run('unload');
70 currentRoute = routes.recognize(currentPath);
71 currentRoute.run('popstate', currentPath);
72 var state = e.originalEvent.state;
74 map.setView(state.center, state.zoom, {animate: false});
75 map.updateLayers(state.layers);
79 router = function (url) {
80 var path = url.replace(/#.*/, ''),
81 route = routes.recognize(path);
82 if (!route) return false;
83 window.history.pushState(OSM.parseHash(url) || {}, document.title, url);
84 currentRoute.run('unload');
87 currentRoute.run('pushstate', currentPath);
91 stateChange = function(state, hash) {
92 window.location.replace(hash);
95 router = function (url) {
96 window.location.assign(url);
100 router.updateHash = function() {
101 var hash = OSM.formatHash(map);
102 if (hash === currentHash) return;
104 stateChange(OSM.parseHash(hash), hash);
107 router.hashUpdated = function() {
108 var hash = location.hash;
109 if (hash === currentHash) return;
111 var state = OSM.parseHash(hash);
113 map.setView(state.center, state.zoom);
114 map.updateLayers(state.layers);
115 stateChange(state, hash);
118 map.on('moveend baselayerchange overlaylayerchange', router.updateHash);
119 $(window).on('hashchange', router.hashUpdated);