2 //= require numbered_pagination
3 //= require leaflet.sidebar
4 //= require leaflet.sidebar-pane
5 //= require leaflet.locate
6 //= require leaflet.layers
7 //= require leaflet.legend
8 //= require leaflet.note
9 //= require leaflet.share
10 //= require leaflet.query
11 //= require index/contextmenu
12 //= require index/initializations
13 //= require index/layers/data
14 //= require index/layers/notes
17 OSM.initializations = [];
20 const map = new L.OSM.Map("map", {
27 OSM.loadSidebarContent = function (path, callback) {
28 const atomSelector = "link[type=\"application/atom+xml\"]";
30 map.setSidebarOverlaid(false);
32 $("#sidebar_loader").prop("hidden", false).addClass("delayed-fade-in");
34 // Prevent caching the XHR response as a full-page URL
35 // https://github.com/openstreetmap/openstreetmap-website/issues/5663
36 const xhrPath = path + `${path.includes("?") ? "&" : "?"}xhr=1`;
41 fetch(xhrPath, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
44 $("#sidebar_loader").removeClass("delayed-fade-in").prop("hidden", true);
46 const title = response.headers.get("X-Page-Title");
47 if (title) document.title = decodeURIComponent(title);
49 return response.text();
52 const content = $(html);
54 $("head").find(atomSelector).remove();
56 $("head").append(content.filter(atomSelector));
58 $("#sidebar_content").html(content.not(atomSelector));
66 const token = $("head").data("oauthToken");
67 if (token) OSM.oauth = { authorization: "Bearer " + token };
69 const params = OSM.mapParams();
71 map.attributionControl.setPrefix("");
73 map.updateLayers(params.layers);
75 map.on("baselayerchange", function (e) {
76 if (map.getZoom() > e.layer.options.maxZoom) {
77 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
81 const sidebar = L.OSM.sidebar("#map-ui")
84 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
86 function addControlGroup(controls) {
87 for (const control of controls) control.addTo(map);
89 const firstContainer = controls[0].getContainer();
90 $(firstContainer).find(".control-button").first()
91 .addClass("control-button-first");
93 const lastContainer = controls[controls.length - 1].getContainer();
94 $(lastContainer).find(".control-button").last()
95 .addClass("control-button-last");
99 L.OSM.zoom({ position }),
100 L.OSM.locate({ position })
107 layers: map.baseLayers
109 L.OSM.legend({ position, sidebar }),
118 L.OSM.note({ position, sidebar })
122 L.OSM.query({ position, sidebar })
128 OSM.initializations.forEach(func => func(map));
130 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
131 OSM.initializeNotesLayer(map);
132 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
133 map.addLayer(map.noteLayer);
136 OSM.initializeDataLayer(map);
137 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
138 map.addLayer(map.dataLayer);
141 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
142 map.addLayer(map.gpsLayer);
146 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
148 const expires = new Date();
149 const thisYear = expires.getFullYear();
150 expires.setFullYear(thisYear + 10);
152 const updateCookieAndLinks = function () {
154 map.getCenter().wrap(),
159 OSM.cookies.set("_osm_location", OSM.locationCookie(map), { expires });
161 for (const e of ["moveend", "baselayerchange", "overlayadd", "overlayremove"]) {
162 map.on(e, updateCookieAndLinks);
165 if (OSM.cookies.get("_osm_welcome") !== "hide") {
166 $(".welcome").addClass("d-md-block");
169 $(".welcome .btn-close").on("click", function () {
170 $(".welcome").removeClass("d-md-block");
171 OSM.cookies.set("_osm_welcome", "hide", { expires });
174 expires.setFullYear(thisYear + 1);
176 $("#banner .btn-close").on("click", function (e) {
177 const cookieId = e.target.id;
178 $("#banner").removeClass("d-md-block");
181 OSM.cookies.set(cookieId, "hide", { expires });
186 const matomoLayerHandler = function (e) {
187 if (e.layer.options) {
188 const goal = OSM.MATOMO.goals[e.layer.options.layerId];
191 $("body").trigger("matomogoal", goal);
195 map.on("baselayerchange", matomoLayerHandler);
196 map.on("overlayadd", matomoLayerHandler);
200 map.fitBounds(params.bounds);
202 map.setView([params.lat, params.lon], params.zoom);
205 if (params.marker && params.mrad) {
206 L.circle([params.mlat, params.mlon], { radius: params.mrad }).addTo(map);
207 } else if (params.marker) {
208 L.marker([params.mlat, params.mlon], { icon: OSM.getMarker({ color: "var(--marker-blue)" }) }).addTo(map);
211 function remoteEditHandler(bbox, object) {
212 const remoteEditHost = "http://127.0.0.1:8111",
213 osmHost = location.protocol + "//" + location.host,
214 query = new URLSearchParams({
215 left: bbox.getWest() - 0.0001,
216 top: bbox.getNorth() + 0.0001,
217 right: bbox.getEast() + 0.0001,
218 bottom: bbox.getSouth() - 0.0001
221 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
222 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query)
224 if (object && object.type === "note") {
225 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
226 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
230 OSM.showAlert(OSM.i18n.t("javascripts.remote_edit.failed.title"),
231 OSM.i18n.t("javascripts.remote_edit.failed.body"));
234 function sendRemoteEditCommand(url) {
235 return fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) });
241 $("a[data-editor=remote]").click(function (e) {
242 const params = OSM.mapParams(this.search);
243 remoteEditHandler(map.getBounds(), params.object);
247 if (new URLSearchParams(location.search).get("edit_help")) {
252 title: OSM.i18n.t("javascripts.edit_help")
256 $("body").one("click", function () {
257 $("#editanchor").tooltip("hide");
261 OSM.router = OSM.Router(map, {
264 "/directions": "directions",
266 "/note/new": "new_note",
267 "/history/friends": "history",
268 "/history/nearby": "history",
269 "/history": "history",
270 "/user/:display_name/history": "history",
272 "/node/:id(/history)": { module: "index_element", part: m => m.mappedElement("node") },
273 "/node/:id/history/:version": { module: "index_element", part: m => m.mappedElement("node") },
274 "/way/:id(/history)": { module: "index_element", part: m => m.mappedElement("way") },
275 "/way/:id/history/:version": { module: "index_element", part: m => m.element("way") },
276 "/relation/:id(/history)": { module: "index_element", part: m => m.mappedElement("relation") },
277 "/relation/:id/history/:version": { module: "index_element", part: m => m.element("relation") },
278 "/changeset/:id": "changeset",
280 "/account/home": "home"
283 if (OSM.preferred_editor === "remote" && location.pathname === "/edit") {
284 remoteEditHandler(map.getBounds(), params.object);
285 OSM.router.setCurrentPath("/");
290 $(document).on("click", "a", function (e) {
291 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
295 // Open links in a new tab as normal.
296 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
300 // Open local anchor links as normal.
301 if ($(this).attr("href")?.startsWith("#")) {
305 // Ignore cross-protocol and cross-origin links.
306 const url = new URL($(this).attr("href"), location);
307 if (location.protocol !== url.protocol || location.host !== url.host) {
311 if (OSM.router.route(url.pathname + url.search + url.hash)) {
313 if (url.pathname !== "/directions") {
314 $("header").addClass("closed");
319 $(document).on("click", "#sidebar .sidebar-close-controls button", function () {
320 $(".search_form input[name=query]").val("");
321 OSM.router.route("/" + OSM.formatHash(map));