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
26 $(document).ready(function () {
27 var map = new L.OSM.Map("map", {
34 OSM.loadSidebarContent = function (path, callback) {
35 var content_path = path;
37 map.setSidebarOverlaid(false);
39 $("#sidebar_loader").show().addClass("delayed-fade-in");
41 // Prevent caching the XHR response as a full-page URL
42 // https://github.com/openstreetmap/openstreetmap-website/issues/5663
43 if (content_path.indexOf("?") >= 0) {
44 content_path += "&xhr=1";
46 content_path += "?xhr=1";
52 fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
55 $("#sidebar_loader").removeClass("delayed-fade-in").hide();
57 const title = response.headers.get("X-Page-Title");
58 if (title) document.title = decodeURIComponent(title);
60 return response.text();
63 const content = $(html);
66 .find("link[type=\"application/atom+xml\"]")
70 .append(content.filter("link[type=\"application/atom+xml\"]"));
72 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
80 var params = OSM.mapParams();
82 map.attributionControl.setPrefix("");
84 map.updateLayers(params.layers);
86 map.on("baselayerchange", function (e) {
87 if (map.getZoom() > e.layer.options.maxZoom) {
88 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
92 var sidebar = L.OSM.sidebar("#map-ui")
95 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
97 function addControlGroup(controls) {
98 for (const control of controls) control.addTo(map);
100 var firstContainer = controls[0].getContainer();
101 $(firstContainer).find(".control-button").first()
102 .addClass("control-button-first");
104 var lastContainer = controls[controls.length - 1].getContainer();
105 $(lastContainer).find(".control-button").last()
106 .addClass("control-button-last");
110 L.OSM.zoom({ position: position }),
111 L.OSM.locate({ position: position })
117 layers: map.baseLayers,
125 "position": position,
148 OSM.initializeContextMenu(map);
150 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
151 OSM.initializeNotesLayer(map);
152 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
153 map.addLayer(map.noteLayer);
156 OSM.initializeDataLayer(map);
157 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
158 map.addLayer(map.dataLayer);
161 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
162 map.addLayer(map.gpsLayer);
166 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
168 var expiry = new Date();
169 expiry.setYear(expiry.getFullYear() + 10);
171 map.on("moveend baselayerchange overlayadd overlayremove", function () {
173 map.getCenter().wrap(),
178 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
181 if (Cookies.get("_osm_welcome") !== "hide") {
182 $(".welcome").removeAttr("hidden");
185 $(".welcome .btn-close").on("click", function () {
186 $(".welcome").hide();
187 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
190 var bannerExpiry = new Date();
191 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
193 $("#banner .btn-close").on("click", function (e) {
194 var cookieId = e.target.id;
198 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
203 map.on("baselayerchange overlayadd", function (e) {
204 if (e.layer.options) {
205 var goal = OSM.MATOMO.goals[e.layer.options.layerId];
208 $("body").trigger("matomogoal", goal);
215 map.fitBounds(params.bounds);
217 map.setView([params.lat, params.lon], params.zoom);
221 L.marker([params.mlat, params.mlon]).addTo(map);
224 $("#homeanchor").on("click", function (e) {
227 var data = $(this).data(),
228 center = L.latLng(data.lat, data.lon);
230 map.setView(center, data.zoom);
231 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
234 function remoteEditHandler(bbox, object) {
235 var remoteEditHost = "http://127.0.0.1:8111",
236 osmHost = location.protocol + "//" + location.host,
237 query = new URLSearchParams({
238 left: bbox.getWest() - 0.0001,
239 top: bbox.getNorth() + 0.0001,
240 right: bbox.getEast() + 0.0001,
241 bottom: bbox.getSouth() - 0.0001
244 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
245 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
246 if (object && object.type === "note") {
247 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
248 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
252 function sendRemoteEditCommand(url, callback) {
253 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
256 // eslint-disable-next-line no-alert
257 alert(I18n.t("site.index.remote_failed"));
264 $("a[data-editor=remote]").click(function (e) {
265 var params = OSM.mapParams(this.search);
266 remoteEditHandler(map.getBounds(), params.object);
270 if (OSM.params().edit_help) {
275 title: I18n.t("javascripts.edit_help")
279 $("body").one("click", function () {
280 $("#editanchor").tooltip("hide");
284 OSM.Index = function (map) {
287 page.pushstate = page.popstate = function () {
288 map.setSidebarOverlaid(true);
289 document.title = I18n.t("layouts.project_name.title");
292 page.load = function () {
293 const params = new URLSearchParams(location.search);
294 if (params.has("query")) {
295 $("#sidebar .search_form input[name=query]").value(params.get("query"));
297 if (!("autofocus" in document.createElement("input"))) {
298 $("#sidebar .search_form input[name=query]").focus();
300 return map.getState();
306 OSM.Browse = function (map, type) {
309 page.pushstate = page.popstate = function (path, id) {
310 OSM.loadSidebarContent(path, function () {
315 page.load = function (path, id) {
316 addObject(type, id, true);
319 function addObject(type, id, center) {
320 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
321 if (!window.location.hash && bounds.isValid() &&
322 (center || !map.getBounds().contains(bounds))) {
323 OSM.router.withoutMoveListener(function () {
324 map.fitBounds(bounds);
330 page.unload = function () {
337 OSM.OldBrowse = function () {
340 page.pushstate = page.popstate = function (path) {
341 OSM.loadSidebarContent(path);
347 var history = OSM.History(map);
349 OSM.router = OSM.Router(map, {
351 "/search": OSM.Search(map),
352 "/directions": OSM.Directions(map),
353 "/export": OSM.Export(map),
354 "/note/new": OSM.NewNote(map),
355 "/history/friends": history,
356 "/history/nearby": history,
358 "/user/:display_name/history": history,
359 "/note/:id": OSM.Note(map),
360 "/node/:id(/history)": OSM.Browse(map, "node"),
361 "/node/:id/history/:version": OSM.OldBrowse(),
362 "/way/:id(/history)": OSM.Browse(map, "way"),
363 "/way/:id/history/:version": OSM.OldBrowse(),
364 "/relation/:id(/history)": OSM.Browse(map, "relation"),
365 "/relation/:id/history/:version": OSM.OldBrowse(),
366 "/changeset/:id": OSM.Changeset(map),
367 "/query": OSM.Query(map)
370 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
371 remoteEditHandler(map.getBounds(), params.object);
372 OSM.router.setCurrentPath("/");
377 $(document).on("click", "a", function (e) {
378 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
382 // Open links in a new tab as normal.
383 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
387 // Ignore cross-protocol and cross-origin links.
388 if (location.protocol !== this.protocol || location.host !== this.host) {
392 if (OSM.router.route(this.pathname + this.search + this.hash)) {
394 if (this.pathname !== "/directions") {
395 $("header").addClass("closed");
400 $(document).on("click", "#sidebar_content .btn-close", function () {
401 OSM.router.route("/" + OSM.formatHash(map));