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