]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Supply hints for OSRM
[rails.git] / app / assets / javascripts / index.js
1 //= require_self
2 //= require leaflet.sidebar
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require leaflet.polyline
9 //= require index/search
10 //= require index/browse
11 //= require index/export
12 //= require index/notes
13 //= require index/history
14 //= require index/note
15 //= require index/new_note
16 //= require index/directions
17 //= require router
18
19 $(document).ready(function () {
20   var loaderTimeout;
21
22   OSM.loadSidebarContent = function(path, callback) {
23     map.setSidebarOverlaid(false);
24
25     clearTimeout(loaderTimeout);
26
27     loaderTimeout = setTimeout(function() {
28       $('#sidebar_loader').show();
29     }, 200);
30
31     // IE<10 doesn't respect Vary: X-Requested-With header, so
32     // prevent caching the XHR response as a full-page URL.
33     if (path.indexOf('?') >= 0) {
34       path += '&xhr=1'
35     } else {
36       path += '?xhr=1'
37     }
38
39     $('#sidebar_content')
40       .empty();
41
42     $.ajax({
43       url: path,
44       dataType: "html",
45       complete: function(xhr) {
46         clearTimeout(loaderTimeout);
47         $('#flash').empty();
48         $('#sidebar_loader').hide();
49
50         var content = $(xhr.responseText);
51
52         if (xhr.getResponseHeader('X-Page-Title')) {
53           var title = xhr.getResponseHeader('X-Page-Title');
54           document.title = decodeURIComponent(escape(title));
55         }
56
57         $('head')
58           .find('link[type="application/atom+xml"]')
59           .remove();
60
61         $('head')
62           .append(content.filter('link[type="application/atom+xml"]'));
63
64         $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
65
66         if (callback) {
67           callback();
68         }
69       }
70     });
71   };
72
73   var params = OSM.mapParams();
74
75   var map = new L.OSM.Map("map", {
76     zoomControl: false,
77     layerControl: false
78   });
79
80   map.attributionControl.setPrefix('');
81
82   map.updateLayers(params.layers);
83
84   map.on("baselayerchange", function (e) {
85     if (map.getZoom() > e.layer.options.maxZoom) {
86       map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
87     }
88   });
89
90   var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
91
92   L.OSM.zoom({position: position})
93     .addTo(map);
94
95   L.control.locate({
96     position: position,
97     strings: {
98       title: I18n.t('javascripts.map.locate.title'),
99       popup: I18n.t('javascripts.map.locate.popup')
100     }
101   }).addTo(map);
102
103   var sidebar = L.OSM.sidebar('#map-ui')
104     .addTo(map);
105
106   L.OSM.layers({
107     position: position,
108     layers: map.baseLayers,
109     sidebar: sidebar
110   }).addTo(map);
111
112   L.OSM.key({
113     position: position,
114     sidebar: sidebar
115   }).addTo(map);
116
117   L.OSM.share({
118     position: position,
119     sidebar: sidebar,
120     short: true
121   }).addTo(map);
122
123   L.OSM.note({
124     position: position,
125     sidebar: sidebar
126   }).addTo(map);
127
128   L.control.scale()
129     .addTo(map);
130
131   if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
132     initializeNotes(map);
133     if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
134       map.addLayer(map.noteLayer);
135     }
136
137     initializeBrowse(map);
138     if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
139       map.addLayer(map.dataLayer);
140     }
141   }
142
143   $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
144
145   var expiry = new Date();
146   expiry.setYear(expiry.getFullYear() + 10);
147
148   map.on('moveend layeradd layerremove', function() {
149     updateLinks(
150       map.getCenter().wrap(),
151       map.getZoom(),
152       map.getLayersCode(),
153       map._object);
154
155     $.removeCookie("_osm_location");
156     $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
157   });
158
159   if ($.cookie('_osm_welcome') == 'hide') {
160     $('.welcome').hide();
161   }
162
163   $('.welcome .close').on('click', function() {
164     $('.welcome').hide();
165     $.cookie("_osm_welcome", 'hide', { expires: expiry });
166   });
167
168   if (OSM.PIWIK) {
169     map.on('layeradd', function (e) {
170       if (e.layer.options) {
171         var goal = OSM.PIWIK.goals[e.layer.options.keyid];
172
173         if (goal) {
174           $('body').trigger('piwikgoal', goal);
175         }
176       }
177     });
178   }
179
180   if (params.bounds) {
181     map.fitBounds(params.bounds);
182   } else {
183     map.setView([params.lat, params.lon], params.zoom);
184   }
185
186   var marker = L.marker([0, 0], {icon: getUserIcon()});
187
188   if (params.marker) {
189     marker.setLatLng([params.mlat, params.mlon]).addTo(map);
190   }
191
192   $("#homeanchor").on("click", function(e) {
193     e.preventDefault();
194
195     var data = $(this).data(),
196       center = L.latLng(data.lat, data.lon);
197
198     map.setView(center, data.zoom);
199     marker.setLatLng(center).addTo(map);
200   });
201
202   $("a[data-editor=remote]").click(function(e) {
203     var params = OSM.mapParams(this.search);
204     remoteEditHandler(map.getBounds(), params.object);
205     e.preventDefault();
206   });
207
208   if (OSM.params().edit_help) {
209     $('#editanchor')
210       .removeAttr('title')
211       .tooltip({
212         placement: 'bottom',
213         title: I18n.t('javascripts.edit_help')
214       })
215       .tooltip('show');
216
217     $('body').one('click', function() {
218       $('#editanchor').tooltip('hide');
219     });
220   }
221
222   OSM.Index = function(map) {
223     var page = {};
224
225     page.pushstate = page.popstate = function() {
226       map.setSidebarOverlaid(true);
227       document.title = I18n.t('layouts.project_name.title');
228     };
229
230     page.load = function() {
231       if (!("autofocus" in document.createElement("input"))) {
232         $("#sidebar .search_form input[name=query]").focus();
233       }
234       return map.getState();
235     };
236
237     return page;
238   };
239
240   OSM.Browse = function(map, type) {
241     var page = {};
242
243     page.pushstate = page.popstate = function(path, id) {
244       OSM.loadSidebarContent(path, function() {
245         addObject(type, id);
246       });
247     };
248
249     page.load = function(path, id) {
250       addObject(type, id, true);
251     };
252
253     function addObject(type, id, center) {
254       var bounds = map.addObject({type: type, id: parseInt(id)}, function(bounds) {
255         if (!window.location.hash && bounds.isValid()) {
256           OSM.router.moveListenerOff();
257           map.once('moveend', OSM.router.moveListenerOn);
258           if (center || !map.getBounds().contains(bounds)) map.fitBounds(bounds);
259         }
260       });
261     }
262
263     page.unload = function() {
264       map.removeObject();
265     };
266
267     return page;
268   };
269
270   var history = OSM.History(map);
271
272   OSM.router = OSM.Router(map, {
273     "/":                           OSM.Index(map),
274     "/search":                     OSM.Search(map),
275     "/directions":                 OSM.Directions(map),
276     "/export":                     OSM.Export(map),
277     "/note/new":                   OSM.NewNote(map),
278     "/history/friends":            history,
279     "/history/nearby":             history,
280     "/history":                    history,
281     "/user/:display_name/history": history,
282     "/note/:id":                   OSM.Note(map),
283     "/node/:id(/history)":         OSM.Browse(map, 'node'),
284     "/way/:id(/history)":          OSM.Browse(map, 'way'),
285     "/relation/:id(/history)":     OSM.Browse(map, 'relation'),
286     "/changeset/:id":              OSM.Browse(map, 'changeset')
287   });
288
289   if (OSM.preferred_editor == "remote" && document.location.pathname == "/edit") {
290     remoteEditHandler(map.getBounds(), params.object);
291     OSM.router.setCurrentPath("/");
292   }
293
294   OSM.router.load();
295
296   $(document).on("click", "a", function(e) {
297     if (e.isDefaultPrevented() || e.isPropagationStopped())
298       return;
299
300     // Open links in a new tab as normal.
301     if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
302       return;
303
304     // Ignore cross-protocol and cross-origin links.
305     if (location.protocol !== this.protocol || location.host !== this.host)
306       return;
307
308     if (OSM.router.route(this.pathname + this.search + this.hash))
309       e.preventDefault();
310   });
311 });