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.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 index/element
29 const map = new L.OSM.Map("map", {
36 OSM.loadSidebarContent = function (path, callback) {
37 let content_path = path;
39 map.setSidebarOverlaid(false);
41 $("#sidebar_loader").prop("hidden", false).addClass("delayed-fade-in");
43 // Prevent caching the XHR response as a full-page URL
44 // https://github.com/openstreetmap/openstreetmap-website/issues/5663
45 if (content_path.indexOf("?") >= 0) {
46 content_path += "&xhr=1";
48 content_path += "?xhr=1";
54 fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
57 $("#sidebar_loader").removeClass("delayed-fade-in").prop("hidden", true);
59 const title = response.headers.get("X-Page-Title");
60 if (title) document.title = decodeURIComponent(title);
62 return response.text();
65 const content = $(html);
68 .find("link[type=\"application/atom+xml\"]")
72 .append(content.filter("link[type=\"application/atom+xml\"]"));
74 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
82 const token = $("head").data("oauthToken");
83 if (token) OSM.oauth = { authorization: "Bearer " + token };
85 const params = OSM.mapParams();
87 map.attributionControl.setPrefix("");
89 map.updateLayers(params.layers);
91 map.on("baselayerchange", function (e) {
92 if (map.getZoom() > e.layer.options.maxZoom) {
93 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
97 const sidebar = L.OSM.sidebar("#map-ui")
100 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
102 function addControlGroup(controls) {
103 for (const control of controls) control.addTo(map);
105 const firstContainer = controls[0].getContainer();
106 $(firstContainer).find(".control-button").first()
107 .addClass("control-button-first");
109 const lastContainer = controls[controls.length - 1].getContainer();
110 $(lastContainer).find(".control-button").last()
111 .addClass("control-button-last");
115 L.OSM.zoom({ position }),
116 L.OSM.locate({ position })
123 layers: map.baseLayers
125 L.OSM.legend({ position, sidebar }),
134 L.OSM.note({ position, sidebar })
138 L.OSM.query({ position, sidebar })
144 OSM.initializeContextMenu(map);
146 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
147 OSM.initializeNotesLayer(map);
148 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
149 map.addLayer(map.noteLayer);
152 OSM.initializeDataLayer(map);
153 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
154 map.addLayer(map.dataLayer);
157 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
158 map.addLayer(map.gpsLayer);
162 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
164 const expiry = new Date();
165 expiry.setYear(expiry.getFullYear() + 10);
167 map.on("moveend baselayerchange overlayadd overlayremove", function () {
169 map.getCenter().wrap(),
174 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
177 if (Cookies.get("_osm_welcome") !== "hide") {
178 $(".welcome").removeAttr("hidden");
181 $(".welcome .btn-close").on("click", function () {
182 $(".welcome").hide();
183 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
186 const bannerExpiry = new Date();
187 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
189 $("#banner .btn-close").on("click", function (e) {
190 const cookieId = e.target.id;
194 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
199 map.on("baselayerchange overlayadd", function (e) {
200 if (e.layer.options) {
201 const goal = OSM.MATOMO.goals[e.layer.options.layerId];
204 $("body").trigger("matomogoal", goal);
211 map.fitBounds(params.bounds);
213 map.setView([params.lat, params.lon], params.zoom);
216 if (params.marker && params.mrad) {
217 L.circle([params.mlat, params.mlon], { radius: params.mrad }).addTo(map);
218 } else if (params.marker) {
219 L.marker([params.mlat, params.mlon], { icon: OSM.getMarker({ color: "var(--marker-blue)" }) }).addTo(map);
222 function remoteEditHandler(bbox, object) {
223 const remoteEditHost = "http://127.0.0.1:8111",
224 osmHost = location.protocol + "//" + location.host,
225 query = new URLSearchParams({
226 left: bbox.getWest() - 0.0001,
227 top: bbox.getNorth() + 0.0001,
228 right: bbox.getEast() + 0.0001,
229 bottom: bbox.getSouth() - 0.0001
232 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
233 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query)
235 if (object && object.type === "note") {
236 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
237 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
241 OSM.showAlert(OSM.i18n.t("javascripts.remote_edit.failed.title"),
242 OSM.i18n.t("javascripts.remote_edit.failed.body"));
245 function sendRemoteEditCommand(url) {
246 return fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) });
252 $("a[data-editor=remote]").click(function (e) {
253 const params = OSM.mapParams(this.search);
254 remoteEditHandler(map.getBounds(), params.object);
258 if (new URLSearchParams(location.search).get("edit_help")) {
263 title: OSM.i18n.t("javascripts.edit_help")
267 $("body").one("click", function () {
268 $("#editanchor").tooltip("hide");
272 OSM.Index = function (map) {
275 page.pushstate = page.popstate = function () {
276 map.setSidebarOverlaid(true);
277 document.title = OSM.i18n.t("layouts.project_name.title");
280 page.load = function () {
281 const params = new URLSearchParams(location.search);
282 if (params.has("query")) {
283 $("#sidebar .search_form input[name=query]").value(params.get("query"));
285 return map.getState();
291 OSM.router = OSM.Router(map, {
293 "/search": OSM.Search,
294 "/directions": OSM.Directions,
295 "/export": OSM.Export,
296 "/note/new": OSM.NewNote,
297 "/history/friends": OSM.History,
298 "/history/nearby": OSM.History,
299 "/history": OSM.History,
300 "/user/:display_name/history": OSM.History,
301 "/note/:id": OSM.Note,
302 "/node/:id(/history)": OSM.MappedElement("node"),
303 "/node/:id/history/:version": OSM.MappedElement("node"),
304 "/way/:id(/history)": OSM.MappedElement("way"),
305 "/way/:id/history/:version": OSM.Element("way"),
306 "/relation/:id(/history)": OSM.MappedElement("relation"),
307 "/relation/:id/history/:version": OSM.Element("relation"),
308 "/changeset/:id": OSM.Changeset,
310 "/account/home": OSM.Home
313 if (OSM.preferred_editor === "remote" && location.pathname === "/edit") {
314 remoteEditHandler(map.getBounds(), params.object);
315 OSM.router.setCurrentPath("/");
320 $(document).on("click", "a", function (e) {
321 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
325 // Open links in a new tab as normal.
326 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
330 // Open local anchor links as normal.
331 if ($(this).attr("href")?.startsWith("#")) {
335 // Ignore cross-protocol and cross-origin links.
336 const url = new URL($(this).attr("href"), location);
337 if (location.protocol !== url.protocol || location.host !== url.host) {
341 if (OSM.router.route(url.pathname + url.search + url.hash)) {
343 if (url.pathname !== "/directions") {
344 $("header").addClass("closed");
349 $(document).on("click", "#sidebar .sidebar-close-controls button", function () {
350 $(".search_form input[name=query]").val("");
351 OSM.router.route("/" + OSM.formatHash(map));