]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/router.js
fbad1e28c1695f90d37b5a40bef573f491e1afd1
[rails.git] / app / assets / javascripts / router.js
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;
6
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 : '([^\/]+)';
13         })
14         .replace(splatParam, '(.*?)') + '(?:\\?.*)?$');
15
16     var route = {};
17
18     route.match = function(path) {
19       return regexp.test(path);
20     };
21
22     route.run = function(action, path) {
23       var params = [];
24
25       if (path) {
26         params = regexp.exec(path).map(function(param, i) {
27           return (i > 0 && param) ? decodeURIComponent(param) : param;
28         });
29       }
30
31       (controller[action] || $.noop).apply(controller, params);
32     };
33
34     return route;
35   }
36
37   var routes = [];
38   for (var r in rts)
39     routes.push(Route(r, rts[r]));
40
41   routes.recognize = function(path) {
42     for (var i = 0; i < this.length; i++) {
43       if (this[i].match(path)) return this[i];
44     }
45   };
46
47   var currentPath = window.location.pathname + window.location.search,
48     currentRoute = routes.recognize(currentPath),
49     currentHash = location.hash || OSM.formatHash(map);
50
51   currentRoute.run('load', currentPath);
52
53   var stateChange;
54
55   map.on('moveend baselayerchange overlaylayerchange', function() {
56     var hash = OSM.formatHash(map);
57     if (hash === currentHash) return;
58     currentHash = hash;
59     stateChange(OSM.parseHash(hash), hash);
60   });
61
62   $(window).on('hashchange', function() {
63     var hash = location.hash;
64     if (hash === currentHash) return;
65     currentHash = hash;
66     var state = OSM.parseHash(hash);
67     if (!state) return;
68     map.setView(state.center, state.zoom);
69     map.updateLayers(state.layers);
70     stateChange(state, hash);
71   });
72
73   if (window.history && window.history.pushState) {
74     stateChange = function(state, hash) {
75       window.history.replaceState(state, document.title, hash);
76     };
77
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);
81
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');
87       currentPath = path;
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);
93     });
94
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');
101       currentPath = path;
102       currentRoute = route;
103       currentRoute.run('pushstate', currentPath);
104       return true;
105     }
106   } else {
107     stateChange = function(state, hash) {
108       window.location.replace(hash);
109     };
110
111     return function (url) {
112       window.location.assign(url);
113     }
114   }
115 };