]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/router.js
Merge branch 'master' into redesign
[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       return (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   var router, stateChange;
52
53   if (window.history && window.history.pushState) {
54     $(window).on('popstate', function(e) {
55       if (!e.originalEvent.state) return; // Is it a real popstate event or just a hash change?
56       var path = window.location.pathname + window.location.search;
57       if (path === currentPath) return;
58       currentRoute.run('unload');
59       currentPath = path;
60       currentRoute = routes.recognize(currentPath);
61       currentRoute.run('popstate', currentPath);
62       var state = e.originalEvent.state;
63       if (state.center) {
64         map.setView(state.center, state.zoom, {animate: false});
65         map.updateLayers(state.layers);
66       }
67     });
68
69     router = function (url) {
70       var path = url.replace(/#.*/, ''),
71         route = routes.recognize(path);
72       if (!route) return false;
73       window.history.pushState(OSM.parseHash(url) || {}, document.title, url);
74       currentRoute.run('unload');
75       currentPath = path;
76       currentRoute = route;
77       currentRoute.run('pushstate', currentPath);
78       return true;
79     };
80
81     router.stateChange = function(state) {
82       if (state.center) {
83         window.history.replaceState(state, document.title, OSM.formatHash(state));
84       } else {
85         window.history.replaceState(state, document.title, window.location);
86       }
87     };
88   } else {
89     router = function (url) {
90       window.location.assign(url);
91     };
92
93     router.stateChange = function(state) {
94       if (state.center) window.location.replace(OSM.formatHash(state));
95     };
96   }
97
98   router.updateHash = function() {
99     var hash = OSM.formatHash(map);
100     if (hash === currentHash) return;
101     currentHash = hash;
102     router.stateChange(OSM.parseHash(hash));
103   };
104
105   router.hashUpdated = function() {
106     var hash = location.hash;
107     if (hash === currentHash) return;
108     currentHash = hash;
109     var state = OSM.parseHash(hash);
110     if (!state) return;
111     map.setView(state.center, state.zoom);
112     map.updateLayers(state.layers);
113     router.stateChange(state, hash);
114   };
115
116   router.moveListenerOn = function() {
117     map.on('moveend', router.updateHash);
118   };
119
120   router.moveListenerOff = function() {
121     map.off('moveend', router.updateHash);
122   };
123
124   router.load = function() {
125     var loadState = currentRoute.run('load', currentPath);
126     router.stateChange(loadState || {});
127   };
128
129   map.on('moveend baselayerchange overlaylayerchange', router.updateHash);
130   $(window).on('hashchange', router.hashUpdated);
131
132   return router;
133 };