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