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