]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Show alert on account home page if home location is not set
[rails.git] / app / assets / javascripts / index.js
1 //= require_self
2 //= require leaflet.sidebar
3 //= require leaflet.sidebar-pane
4 //= require leaflet.locatecontrol/dist/L.Control.Locate.umd
5 //= require leaflet.locate
6 //= require leaflet.layers
7 //= require leaflet.key
8 //= require leaflet.note
9 //= require leaflet.share
10 //= require leaflet.polyline
11 //= require leaflet.query
12 //= require leaflet.contextmenu
13 //= require index/contextmenu
14 //= require index/search
15 //= require index/layers/data
16 //= require index/export
17 //= require index/layers/notes
18 //= require index/history
19 //= require index/note
20 //= require index/new_note
21 //= require index/directions
22 //= require index/changeset
23 //= require index/query
24 //= require index/home
25 //= require router
26
27 $(document).ready(function () {
28   var map = new L.OSM.Map("map", {
29     zoomControl: false,
30     layerControl: false,
31     contextmenu: true,
32     worldCopyJump: true
33   });
34
35   OSM.loadSidebarContent = function (path, callback) {
36     var content_path = path;
37
38     map.setSidebarOverlaid(false);
39
40     $("#sidebar_loader").show().addClass("delayed-fade-in");
41
42     $("#sidebar_content")
43       .empty();
44
45     fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
46       .then(response => {
47         $("#flash").empty();
48         $("#sidebar_loader").removeClass("delayed-fade-in").hide();
49
50         const title = response.headers.get("X-Page-Title");
51         if (title) document.title = decodeURIComponent(title);
52
53         return response.text();
54       })
55       .then(html => {
56         const content = $(html);
57
58         $("head")
59           .find("link[type=\"application/atom+xml\"]")
60           .remove();
61
62         $("head")
63           .append(content.filter("link[type=\"application/atom+xml\"]"));
64
65         $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
66
67         if (callback) {
68           callback();
69         }
70       });
71   };
72
73   var params = OSM.mapParams();
74
75   map.attributionControl.setPrefix("");
76
77   map.updateLayers(params.layers);
78
79   map.on("baselayerchange", function (e) {
80     if (map.getZoom() > e.layer.options.maxZoom) {
81       map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
82     }
83   });
84
85   var sidebar = L.OSM.sidebar("#map-ui")
86     .addTo(map);
87
88   var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
89
90   function addControlGroup(controls) {
91     for (const control of controls) control.addTo(map);
92
93     var firstContainer = controls[0].getContainer();
94     $(firstContainer).find(".control-button").first()
95       .addClass("control-button-first");
96
97     var lastContainer = controls[controls.length - 1].getContainer();
98     $(lastContainer).find(".control-button").last()
99       .addClass("control-button-last");
100   }
101
102   addControlGroup([
103     L.OSM.zoom({ position: position }),
104     L.OSM.locate({ position: position })
105   ]);
106
107   addControlGroup([
108     L.OSM.layers({
109       position: position,
110       layers: map.baseLayers,
111       sidebar: sidebar
112     }),
113     L.OSM.key({
114       position: position,
115       sidebar: sidebar
116     }),
117     L.OSM.share({
118       "position": position,
119       "sidebar": sidebar,
120       "short": true
121     })
122   ]);
123
124   addControlGroup([
125     L.OSM.note({
126       position: position,
127       sidebar: sidebar
128     })
129   ]);
130
131   addControlGroup([
132     L.OSM.query({
133       position: position,
134       sidebar: sidebar
135     })
136   ]);
137
138   L.control.scale()
139     .addTo(map);
140
141   OSM.initializeContextMenu(map);
142
143   if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
144     OSM.initializeNotesLayer(map);
145     if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
146       map.addLayer(map.noteLayer);
147     }
148
149     OSM.initializeDataLayer(map);
150     if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
151       map.addLayer(map.dataLayer);
152     }
153
154     if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
155       map.addLayer(map.gpsLayer);
156     }
157   }
158
159   $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
160
161   var expiry = new Date();
162   expiry.setYear(expiry.getFullYear() + 10);
163
164   map.on("moveend baselayerchange overlayadd overlayremove", function () {
165     updateLinks(
166       map.getCenter().wrap(),
167       map.getZoom(),
168       map.getLayersCode(),
169       map._object);
170
171     Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
172   });
173
174   if (Cookies.get("_osm_welcome") !== "hide") {
175     $(".welcome").removeAttr("hidden");
176   }
177
178   $(".welcome .btn-close").on("click", function () {
179     $(".welcome").hide();
180     Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
181   });
182
183   var bannerExpiry = new Date();
184   bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
185
186   $("#banner .btn-close").on("click", function (e) {
187     var cookieId = e.target.id;
188     $("#banner").hide();
189     e.preventDefault();
190     if (cookieId) {
191       Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
192     }
193   });
194
195   if (OSM.MATOMO) {
196     map.on("baselayerchange overlayadd", function (e) {
197       if (e.layer.options) {
198         var goal = OSM.MATOMO.goals[e.layer.options.layerId];
199
200         if (goal) {
201           $("body").trigger("matomogoal", goal);
202         }
203       }
204     });
205   }
206
207   if (params.bounds) {
208     map.fitBounds(params.bounds);
209   } else {
210     map.setView([params.lat, params.lon], params.zoom);
211   }
212
213   if (params.marker) {
214     L.marker([params.mlat, params.mlon]).addTo(map);
215   }
216
217   function remoteEditHandler(bbox, object) {
218     var remoteEditHost = "http://127.0.0.1:8111",
219         osmHost = location.protocol + "//" + location.host,
220         query = new URLSearchParams({
221           left: bbox.getWest() - 0.0001,
222           top: bbox.getNorth() + 0.0001,
223           right: bbox.getEast() + 0.0001,
224           bottom: bbox.getSouth() - 0.0001
225         });
226
227     if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
228     sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
229       if (object && object.type === "note") {
230         const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
231         sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
232       }
233     });
234
235     function sendRemoteEditCommand(url, callback) {
236       fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
237         .then(callback)
238         .catch(function () {
239           // eslint-disable-next-line no-alert
240           alert(I18n.t("site.index.remote_failed"));
241         });
242     }
243
244     return false;
245   }
246
247   $("a[data-editor=remote]").click(function (e) {
248     var params = OSM.mapParams(this.search);
249     remoteEditHandler(map.getBounds(), params.object);
250     e.preventDefault();
251   });
252
253   if (OSM.params().edit_help) {
254     $("#editanchor")
255       .removeAttr("title")
256       .tooltip({
257         placement: "bottom",
258         title: I18n.t("javascripts.edit_help")
259       })
260       .tooltip("show");
261
262     $("body").one("click", function () {
263       $("#editanchor").tooltip("hide");
264     });
265   }
266
267   OSM.Index = function (map) {
268     var page = {};
269
270     page.pushstate = page.popstate = function () {
271       map.setSidebarOverlaid(true);
272       document.title = I18n.t("layouts.project_name.title");
273     };
274
275     page.load = function () {
276       const params = new URLSearchParams(location.search);
277       if (params.has("query")) {
278         $("#sidebar .search_form input[name=query]").value(params.get("query"));
279       }
280       if (!("autofocus" in document.createElement("input"))) {
281         $("#sidebar .search_form input[name=query]").focus();
282       }
283       return map.getState();
284     };
285
286     return page;
287   };
288
289   OSM.Browse = function (map, type) {
290     var page = {};
291
292     page.pushstate = page.popstate = function (path, id) {
293       OSM.loadSidebarContent(path, function () {
294         addObject(type, id);
295       });
296     };
297
298     page.load = function (path, id) {
299       addObject(type, id, true);
300     };
301
302     function addObject(type, id, center) {
303       map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
304         if (!window.location.hash && bounds.isValid() &&
305             (center || !map.getBounds().contains(bounds))) {
306           OSM.router.withoutMoveListener(function () {
307             map.fitBounds(bounds);
308           });
309         }
310       });
311     }
312
313     page.unload = function () {
314       map.removeObject();
315     };
316
317     return page;
318   };
319
320   OSM.OldBrowse = function () {
321     var page = {};
322
323     page.pushstate = page.popstate = function (path) {
324       OSM.loadSidebarContent(path);
325     };
326
327     return page;
328   };
329
330   var history = OSM.History(map);
331
332   OSM.router = OSM.Router(map, {
333     "/": OSM.Index(map),
334     "/search": OSM.Search(map),
335     "/directions": OSM.Directions(map),
336     "/export": OSM.Export(map),
337     "/note/new": OSM.NewNote(map),
338     "/history/friends": history,
339     "/history/nearby": history,
340     "/history": history,
341     "/user/:display_name/history": history,
342     "/note/:id": OSM.Note(map),
343     "/node/:id(/history)": OSM.Browse(map, "node"),
344     "/node/:id/history/:version": OSM.OldBrowse(),
345     "/way/:id(/history)": OSM.Browse(map, "way"),
346     "/way/:id/history/:version": OSM.OldBrowse(),
347     "/relation/:id(/history)": OSM.Browse(map, "relation"),
348     "/relation/:id/history/:version": OSM.OldBrowse(),
349     "/changeset/:id": OSM.Changeset(map),
350     "/query": OSM.Query(map),
351     "/account/home": OSM.Home(map)
352   });
353
354   if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
355     remoteEditHandler(map.getBounds(), params.object);
356     OSM.router.setCurrentPath("/");
357   }
358
359   OSM.router.load();
360
361   $(document).on("click", "a", function (e) {
362     if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
363       return;
364     }
365
366     // Open links in a new tab as normal.
367     if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
368       return;
369     }
370
371     // Ignore cross-protocol and cross-origin links.
372     if (location.protocol !== this.protocol || location.host !== this.host) {
373       return;
374     }
375
376     if (OSM.router.route(this.pathname + this.search + this.hash)) {
377       e.preventDefault();
378       if (this.pathname !== "/directions") {
379         $("header").addClass("closed");
380       }
381     }
382   });
383
384   $(document).on("click", "#sidebar_content .btn-close", function () {
385     OSM.router.route("/" + OSM.formatHash(map));
386   });
387 });