]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Drop bowser, which is no longer used
[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       popup: I18n.t("javascripts.map.locate.popup")
113     }
114   }).addTo(map);
115
116   var locateContainer = locate.getContainer();
117
118   $(locateContainer)
119     .removeClass("leaflet-control-locate leaflet-bar")
120     .addClass("control-locate")
121     .children("a")
122     .attr("href", "#")
123     .removeClass("leaflet-bar-part leaflet-bar-part-single")
124     .addClass("control-button");
125
126   var sidebar = L.OSM.sidebar("#map-ui")
127     .addTo(map);
128
129   L.OSM.layers({
130     position: position,
131     layers: map.baseLayers,
132     sidebar: sidebar
133   }).addTo(map);
134
135   L.OSM.key({
136     position: position,
137     sidebar: sidebar
138   }).addTo(map);
139
140   L.OSM.share({
141     "position": position,
142     "sidebar": sidebar,
143     "short": true
144   }).addTo(map);
145
146   L.OSM.note({
147     position: position,
148     sidebar: sidebar
149   }).addTo(map);
150
151   L.OSM.query({
152     position: position,
153     sidebar: sidebar
154   }).addTo(map);
155
156   L.control.scale()
157     .addTo(map);
158
159   OSM.initializeContextMenu(map);
160
161   if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
162     OSM.initializeNotes(map);
163     if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
164       map.addLayer(map.noteLayer);
165     }
166
167     OSM.initializeBrowse(map);
168     if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
169       map.addLayer(map.dataLayer);
170     }
171
172     if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
173       map.addLayer(map.gpsLayer);
174     }
175   }
176
177   var placement = $("html").attr("dir") === "rtl" ? "right" : "left";
178   $(".leaflet-control .control-button").tooltip({ placement: placement, container: "body" });
179
180   var expiry = new Date();
181   expiry.setYear(expiry.getFullYear() + 10);
182
183   map.on("moveend layeradd layerremove", function () {
184     updateLinks(
185       map.getCenter().wrap(),
186       map.getZoom(),
187       map.getLayersCode(),
188       map._object);
189
190     $.removeCookie("_osm_location");
191     $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
192   });
193
194   if ($.cookie("_osm_welcome") !== "hide") {
195     $(".welcome").addClass("visible");
196   }
197
198   $(".welcome .close-wrap").on("click", function () {
199     $(".welcome").removeClass("visible");
200     $.cookie("_osm_welcome", "hide", { expires: expiry, path: "/" });
201   });
202
203   var bannerExpiry = new Date();
204   bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
205
206   $("#banner .close-wrap").on("click", function (e) {
207     var cookieId = e.target.id;
208     $("#banner").hide();
209     e.preventDefault();
210     if (cookieId) {
211       $.cookie(cookieId, "hide", { expires: bannerExpiry, path: "/" });
212     }
213   });
214
215   if (OSM.PIWIK) {
216     map.on("layeradd", function (e) {
217       if (e.layer.options) {
218         var goal = OSM.PIWIK.goals[e.layer.options.keyid];
219
220         if (goal) {
221           $("body").trigger("piwikgoal", goal);
222         }
223       }
224     });
225   }
226
227   if (params.bounds) {
228     map.fitBounds(params.bounds);
229   } else {
230     map.setView([params.lat, params.lon], params.zoom);
231   }
232
233   if (params.marker) {
234     L.marker([params.mlat, params.mlon]).addTo(map);
235   }
236
237   $("#homeanchor").on("click", function (e) {
238     e.preventDefault();
239
240     var data = $(this).data(),
241         center = L.latLng(data.lat, data.lon);
242
243     map.setView(center, data.zoom);
244     L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
245   });
246
247   function remoteEditHandler(bbox, object) {
248     var loaded = false,
249         url,
250         query = {
251           left: bbox.getWest() - 0.0001,
252           top: bbox.getNorth() + 0.0001,
253           right: bbox.getEast() + 0.0001,
254           bottom: bbox.getSouth() - 0.0001
255         };
256
257     url = "http://127.0.0.1:8111/load_and_zoom?";
258
259     if (object) query.select = object.type + object.id;
260
261     var iframe = $("<iframe>")
262       .hide()
263       .appendTo("body")
264       .attr("src", url + querystring.stringify(query))
265       .on("load", function () {
266         $(this).remove();
267         loaded = true;
268       });
269
270     setTimeout(function () {
271       if (!loaded) {
272         alert(I18n.t("site.index.remote_failed"));
273         iframe.remove();
274       }
275     }, 1000);
276
277     return false;
278   }
279
280   $("a[data-editor=remote]").click(function (e) {
281     var params = OSM.mapParams(this.search);
282     remoteEditHandler(map.getBounds(), params.object);
283     e.preventDefault();
284   });
285
286   if (OSM.params().edit_help) {
287     $("#editanchor")
288       .removeAttr("title")
289       .tooltip({
290         placement: "bottom",
291         title: I18n.t("javascripts.edit_help")
292       })
293       .tooltip("show");
294
295     $("body").one("click", function () {
296       $("#editanchor").tooltip("hide");
297     });
298   }
299
300   OSM.Index = function (map) {
301     var page = {};
302
303     page.pushstate = page.popstate = function () {
304       map.setSidebarOverlaid(true);
305       document.title = I18n.t("layouts.project_name.title");
306     };
307
308     page.load = function () {
309       var params = querystring.parse(location.search.substring(1));
310       if (params.query) {
311         $("#sidebar .search_form input[name=query]").value(params.query);
312       }
313       if (!("autofocus" in document.createElement("input"))) {
314         $("#sidebar .search_form input[name=query]").focus();
315       }
316       return map.getState();
317     };
318
319     return page;
320   };
321
322   OSM.Browse = function (map, type) {
323     var page = {};
324
325     page.pushstate = page.popstate = function (path, id) {
326       OSM.loadSidebarContent(path, function () {
327         addObject(type, id);
328       });
329     };
330
331     page.load = function (path, id) {
332       addObject(type, id, true);
333     };
334
335     function addObject(type, id, center) {
336       map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
337         if (!window.location.hash && bounds.isValid() &&
338             (center || !map.getBounds().contains(bounds))) {
339           OSM.router.withoutMoveListener(function () {
340             map.fitBounds(bounds);
341           });
342         }
343       });
344
345       $(".colour-preview-box").each(function () {
346         $(this).css("background-color", $(this).data("colour"));
347       });
348     }
349
350     page.unload = function () {
351       map.removeObject();
352     };
353
354     return page;
355   };
356
357   var history = OSM.History(map);
358
359   OSM.router = OSM.Router(map, {
360     "/": OSM.Index(map),
361     "/search": OSM.Search(map),
362     "/directions": OSM.Directions(map),
363     "/export": OSM.Export(map),
364     "/note/new": OSM.NewNote(map),
365     "/history/friends": history,
366     "/history/nearby": history,
367     "/history": history,
368     "/user/:display_name/history": history,
369     "/note/:id": OSM.Note(map),
370     "/node/:id(/history)": OSM.Browse(map, "node"),
371     "/way/:id(/history)": OSM.Browse(map, "way"),
372     "/relation/:id(/history)": OSM.Browse(map, "relation"),
373     "/changeset/:id": OSM.Changeset(map),
374     "/query": OSM.Query(map)
375   });
376
377   if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
378     remoteEditHandler(map.getBounds(), params.object);
379     OSM.router.setCurrentPath("/");
380   }
381
382   OSM.router.load();
383
384   $(document).on("click", "a", function (e) {
385     if (e.isDefaultPrevented() || e.isPropagationStopped()) {
386       return;
387     }
388
389     // Open links in a new tab as normal.
390     if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
391       return;
392     }
393
394     // Ignore cross-protocol and cross-origin links.
395     if (location.protocol !== this.protocol || location.host !== this.host) {
396       return;
397     }
398
399     if (OSM.router.route(this.pathname + this.search + this.hash)) {
400       e.preventDefault();
401     }
402   });
403 });