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
27 $(document).ready(function () {
28 const map = new L.OSM.Map("map", {
35 OSM.loadSidebarContent = function (path, callback) {
36 let content_path = path;
38 map.setSidebarOverlaid(false);
40 $("#sidebar_loader").show().addClass("delayed-fade-in");
42 // Prevent caching the XHR response as a full-page URL
43 // https://github.com/openstreetmap/openstreetmap-website/issues/5663
44 if (content_path.indexOf("?") >= 0) {
45 content_path += "&xhr=1";
47 content_path += "?xhr=1";
53 fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
56 $("#sidebar_loader").removeClass("delayed-fade-in").hide();
58 const title = response.headers.get("X-Page-Title");
59 if (title) document.title = decodeURIComponent(title);
61 return response.text();
64 const content = $(html);
67 .find("link[type=\"application/atom+xml\"]")
71 .append(content.filter("link[type=\"application/atom+xml\"]"));
73 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
81 const params = OSM.mapParams();
83 map.attributionControl.setPrefix("");
85 map.updateLayers(params.layers);
87 map.on("baselayerchange", function (e) {
88 if (map.getZoom() > e.layer.options.maxZoom) {
89 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
93 const sidebar = L.OSM.sidebar("#map-ui")
96 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
98 function addControlGroup(controls) {
99 for (const control of controls) control.addTo(map);
101 const firstContainer = controls[0].getContainer();
102 $(firstContainer).find(".control-button").first()
103 .addClass("control-button-first");
105 const lastContainer = controls[controls.length - 1].getContainer();
106 $(lastContainer).find(".control-button").last()
107 .addClass("control-button-last");
111 L.OSM.zoom({ position: position }),
112 L.OSM.locate({ position: position })
118 layers: map.baseLayers,
126 "position": position,
149 OSM.initializeContextMenu(map);
151 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
152 OSM.initializeNotesLayer(map);
153 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
154 map.addLayer(map.noteLayer);
157 OSM.initializeDataLayer(map);
158 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
159 map.addLayer(map.dataLayer);
162 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
163 map.addLayer(map.gpsLayer);
167 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
169 const expiry = new Date();
170 expiry.setYear(expiry.getFullYear() + 10);
172 map.on("moveend baselayerchange overlayadd overlayremove", function () {
174 map.getCenter().wrap(),
179 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
182 if (Cookies.get("_osm_welcome") !== "hide") {
183 $(".welcome").removeAttr("hidden");
186 $(".welcome .btn-close").on("click", function () {
187 $(".welcome").hide();
188 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
191 const bannerExpiry = new Date();
192 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
194 $("#banner .btn-close").on("click", function (e) {
195 const cookieId = e.target.id;
199 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
204 map.on("baselayerchange overlayadd", function (e) {
205 if (e.layer.options) {
206 const goal = OSM.MATOMO.goals[e.layer.options.layerId];
209 $("body").trigger("matomogoal", goal);
216 map.fitBounds(params.bounds);
218 map.setView([params.lat, params.lon], params.zoom);
222 L.marker([params.mlat, params.mlon]).addTo(map);
225 function remoteEditHandler(bbox, object) {
226 const remoteEditHost = "http://127.0.0.1:8111",
227 osmHost = location.protocol + "//" + location.host,
228 query = new URLSearchParams({
229 left: bbox.getWest() - 0.0001,
230 top: bbox.getNorth() + 0.0001,
231 right: bbox.getEast() + 0.0001,
232 bottom: bbox.getSouth() - 0.0001
235 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
236 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
237 if (object && object.type === "note") {
238 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
239 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
243 function sendRemoteEditCommand(url, callback) {
244 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
247 // eslint-disable-next-line no-alert
248 alert(I18n.t("site.index.remote_failed"));
255 $("a[data-editor=remote]").click(function (e) {
256 const params = OSM.mapParams(this.search);
257 remoteEditHandler(map.getBounds(), params.object);
261 if (OSM.params().edit_help) {
266 title: I18n.t("javascripts.edit_help")
270 $("body").one("click", function () {
271 $("#editanchor").tooltip("hide");
275 OSM.Index = function (map) {
278 page.pushstate = page.popstate = function () {
279 map.setSidebarOverlaid(true);
280 document.title = I18n.t("layouts.project_name.title");
283 page.load = function () {
284 const params = new URLSearchParams(location.search);
285 if (params.has("query")) {
286 $("#sidebar .search_form input[name=query]").value(params.get("query"));
288 if (!("autofocus" in document.createElement("input"))) {
289 $("#sidebar .search_form input[name=query]").focus();
291 return map.getState();
297 OSM.Browse = function (map, type) {
300 page.pushstate = page.popstate = function (path, id) {
301 OSM.loadSidebarContent(path, function () {
306 page.load = function (path, id) {
307 addObject(type, id, true);
310 function addObject(type, id, center) {
311 const hashParams = OSM.parseHash(window.location.hash);
312 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
313 if (!hashParams.center && bounds.isValid() &&
314 (center || !map.getBounds().contains(bounds))) {
315 OSM.router.withoutMoveListener(function () {
316 map.fitBounds(bounds);
322 page.unload = function () {
329 OSM.OldBrowse = function () {
332 page.pushstate = page.popstate = function (path) {
333 OSM.loadSidebarContent(path);
339 const history = OSM.History(map);
341 OSM.router = OSM.Router(map, {
343 "/search": OSM.Search(map),
344 "/directions": OSM.Directions(map),
345 "/export": OSM.Export(map),
346 "/note/new": OSM.NewNote(map),
347 "/history/friends": history,
348 "/history/nearby": history,
350 "/user/:display_name/history": history,
351 "/note/:id": OSM.Note(map),
352 "/node/:id(/history)": OSM.Browse(map, "node"),
353 "/node/:id/history/:version": OSM.OldBrowse(),
354 "/way/:id(/history)": OSM.Browse(map, "way"),
355 "/way/:id/history/:version": OSM.OldBrowse(),
356 "/relation/:id(/history)": OSM.Browse(map, "relation"),
357 "/relation/:id/history/:version": OSM.OldBrowse(),
358 "/changeset/:id": OSM.Changeset(map),
359 "/query": OSM.Query(map),
360 "/account/home": OSM.Home(map)
363 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
364 remoteEditHandler(map.getBounds(), params.object);
365 OSM.router.setCurrentPath("/");
370 $(document).on("click", "a", function (e) {
371 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
375 // Open links in a new tab as normal.
376 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
380 // Ignore cross-protocol and cross-origin links.
381 if (location.protocol !== this.protocol || location.host !== this.host) {
385 if (OSM.router.route(this.pathname + this.search + this.hash)) {
387 if (this.pathname !== "/directions") {
388 $("header").addClass("closed");
393 $(document).on("click", "#sidebar_content .btn-close", function () {
394 OSM.router.route("/" + OSM.formatHash(map));