]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Add empty account home page
[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   $("#homeanchor").on("click", function (e) {
218     e.preventDefault();
219
220     var data = $(this).data(),
221         center = L.latLng(data.lat, data.lon);
222
223     map.setView(center, data.zoom);
224     L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
225   });
226
227   function remoteEditHandler(bbox, object) {
228     var remoteEditHost = "http://127.0.0.1:8111",
229         osmHost = location.protocol + "//" + location.host,
230         query = new URLSearchParams({
231           left: bbox.getWest() - 0.0001,
232           top: bbox.getNorth() + 0.0001,
233           right: bbox.getEast() + 0.0001,
234           bottom: bbox.getSouth() - 0.0001
235         });
236
237     if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
238     sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
239       if (object && object.type === "note") {
240         const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
241         sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
242       }
243     });
244
245     function sendRemoteEditCommand(url, callback) {
246       fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
247         .then(callback)
248         .catch(function () {
249           // eslint-disable-next-line no-alert
250           alert(I18n.t("site.index.remote_failed"));
251         });
252     }
253
254     return false;
255   }
256
257   $("a[data-editor=remote]").click(function (e) {
258     var params = OSM.mapParams(this.search);
259     remoteEditHandler(map.getBounds(), params.object);
260     e.preventDefault();
261   });
262
263   if (OSM.params().edit_help) {
264     $("#editanchor")
265       .removeAttr("title")
266       .tooltip({
267         placement: "bottom",
268         title: I18n.t("javascripts.edit_help")
269       })
270       .tooltip("show");
271
272     $("body").one("click", function () {
273       $("#editanchor").tooltip("hide");
274     });
275   }
276
277   OSM.Index = function (map) {
278     var page = {};
279
280     page.pushstate = page.popstate = function () {
281       map.setSidebarOverlaid(true);
282       document.title = I18n.t("layouts.project_name.title");
283     };
284
285     page.load = function () {
286       const params = new URLSearchParams(location.search);
287       if (params.has("query")) {
288         $("#sidebar .search_form input[name=query]").value(params.get("query"));
289       }
290       if (!("autofocus" in document.createElement("input"))) {
291         $("#sidebar .search_form input[name=query]").focus();
292       }
293       return map.getState();
294     };
295
296     return page;
297   };
298
299   OSM.Browse = function (map, type) {
300     var page = {};
301
302     page.pushstate = page.popstate = function (path, id) {
303       OSM.loadSidebarContent(path, function () {
304         addObject(type, id);
305       });
306     };
307
308     page.load = function (path, id) {
309       addObject(type, id, true);
310     };
311
312     function addObject(type, id, center) {
313       map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
314         if (!window.location.hash && bounds.isValid() &&
315             (center || !map.getBounds().contains(bounds))) {
316           OSM.router.withoutMoveListener(function () {
317             map.fitBounds(bounds);
318           });
319         }
320       });
321     }
322
323     page.unload = function () {
324       map.removeObject();
325     };
326
327     return page;
328   };
329
330   OSM.OldBrowse = function () {
331     var page = {};
332
333     page.pushstate = page.popstate = function (path) {
334       OSM.loadSidebarContent(path);
335     };
336
337     return page;
338   };
339
340   var history = OSM.History(map);
341
342   OSM.router = OSM.Router(map, {
343     "/": OSM.Index(map),
344     "/search": OSM.Search(map),
345     "/directions": OSM.Directions(map),
346     "/export": OSM.Export(map),
347     "/note/new": OSM.NewNote(map),
348     "/history/friends": history,
349     "/history/nearby": history,
350     "/history": history,
351     "/user/:display_name/history": history,
352     "/note/:id": OSM.Note(map),
353     "/node/:id(/history)": OSM.Browse(map, "node"),
354     "/node/:id/history/:version": OSM.OldBrowse(),
355     "/way/:id(/history)": OSM.Browse(map, "way"),
356     "/way/:id/history/:version": OSM.OldBrowse(),
357     "/relation/:id(/history)": OSM.Browse(map, "relation"),
358     "/relation/:id/history/:version": OSM.OldBrowse(),
359     "/changeset/:id": OSM.Changeset(map),
360     "/query": OSM.Query(map),
361     "/account/home": OSM.Home(map)
362   });
363
364   if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
365     remoteEditHandler(map.getBounds(), params.object);
366     OSM.router.setCurrentPath("/");
367   }
368
369   OSM.router.load();
370
371   $(document).on("click", "a", function (e) {
372     if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
373       return;
374     }
375
376     // Open links in a new tab as normal.
377     if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
378       return;
379     }
380
381     // Ignore cross-protocol and cross-origin links.
382     if (location.protocol !== this.protocol || location.host !== this.host) {
383       return;
384     }
385
386     if (OSM.router.route(this.pathname + this.search + this.hash)) {
387       e.preventDefault();
388       if (this.pathname !== "/directions") {
389         $("header").addClass("closed");
390       }
391     }
392   });
393
394   $(document).on("click", "#sidebar_content .btn-close", function () {
395     OSM.router.route("/" + OSM.formatHash(map));
396   });
397 });