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