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