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) {
28 map.setSidebarOverlaid(false);
30 return new Promise((resolve, reject) => {
31 $("#sidebar_content_frame")
32 .one("turbo:frame-render", event => {
33 const response = event.originalEvent.detail.fetchResponse.response;
35 const title = response.headers.get("X-Page-Title");
36 if (title) document.title = decodeURIComponent(title);
41 reject(new Error(`HTTP Error ${response.status} ${response.statusText}`));
48 $("#sidebar_content_frame")
49 .on("turbo:before-fetch-response", () => $("#flash").empty())
50 .on("turbo:before-frame-render", event => {
51 const atomSelector = "link[type='application/atom+xml']";
52 $("head").find(atomSelector).remove();
54 $(event.originalEvent.detail.newFrame)
59 const token = $("head").data("oauthToken");
60 if (token) OSM.oauth = { authorization: "Bearer " + token };
62 const params = OSM.mapParams();
64 map.attributionControl.setPrefix("");
66 map.updateLayers(params.layers);
68 map.on("baselayerchange", function (e) {
69 if (map.getZoom() > e.layer.options.maxZoom) {
70 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
74 const sidebar = L.OSM.sidebar("#map-ui")
77 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
79 function addControlGroup(controls) {
80 for (const control of controls) control.addTo(map);
82 const firstContainer = controls[0].getContainer();
83 $(firstContainer).find(".control-button").first()
84 .addClass("control-button-first");
86 const lastContainer = controls[controls.length - 1].getContainer();
87 $(lastContainer).find(".control-button").last()
88 .addClass("control-button-last");
92 L.OSM.zoom({ position }),
93 L.OSM.locate({ position })
100 layers: map.baseLayers
102 L.OSM.legend({ position, sidebar }),
111 L.OSM.note({ position, sidebar })
115 L.OSM.query({ position, sidebar })
121 OSM.initializations.forEach(func => func(map));
123 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
124 OSM.initializeNotesLayer(map);
125 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
126 map.addLayer(map.noteLayer);
129 OSM.initializeDataLayer(map);
130 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
131 map.addLayer(map.dataLayer);
134 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
135 map.addLayer(map.gpsLayer);
139 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
141 const expires = new Date();
142 const thisYear = expires.getFullYear();
143 expires.setFullYear(thisYear + 10);
145 const updateCookieAndLinks = function () {
147 map.getCenter().wrap(),
152 OSM.cookies.set("_osm_location", OSM.locationCookie(map), { expires });
154 for (const e of ["moveend", "baselayerchange", "overlayadd", "overlayremove"]) {
155 map.on(e, updateCookieAndLinks);
158 if (OSM.cookies.get("_osm_welcome") !== "hide") {
159 $(".welcome").addClass("d-md-block");
162 $(".welcome .btn-close").on("click", function () {
163 $(".welcome").removeClass("d-md-block");
164 OSM.cookies.set("_osm_welcome", "hide", { expires });
167 expires.setFullYear(thisYear + 1);
169 $("#banner .btn-close").on("click", function (e) {
170 const cookieId = e.target.id;
171 $("#banner").removeClass("d-md-block");
174 OSM.cookies.set(cookieId, "hide", { expires });
179 const matomoLayerHandler = function (e) {
180 if (e.layer.options) {
181 const goal = OSM.MATOMO.goals[e.layer.options.layerId];
184 $("body").trigger("matomogoal", goal);
188 map.on("baselayerchange", matomoLayerHandler);
189 map.on("overlayadd", matomoLayerHandler);
193 map.fitBounds(params.bounds);
195 map.setView([params.lat, params.lon], params.zoom);
198 if (params.marker && params.mrad) {
199 L.circle([params.mlat, params.mlon], { radius: params.mrad }).addTo(map);
200 } else if (params.marker) {
201 L.marker([params.mlat, params.mlon], { icon: OSM.getMarker({ color: "var(--marker-blue)" }) }).addTo(map);
204 function remoteEditHandler(bbox, object) {
205 const remoteEditHost = "http://127.0.0.1:8111",
206 osmHost = location.protocol + "//" + location.host,
207 query = new URLSearchParams({
208 left: bbox.getWest() - 0.0001,
209 top: bbox.getNorth() + 0.0001,
210 right: bbox.getEast() + 0.0001,
211 bottom: bbox.getSouth() - 0.0001
214 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
215 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query)
217 if (object && object.type === "note") {
218 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
219 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
223 OSM.showAlert(OSM.i18n.t("javascripts.remote_edit.failed.title"),
224 OSM.i18n.t("javascripts.remote_edit.failed.body"));
227 function sendRemoteEditCommand(url) {
228 return fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) });
234 $("a[data-editor=remote]").click(function (e) {
235 const params = OSM.mapParams(this.search);
236 remoteEditHandler(map.getBounds(), params.object);
240 if (new URLSearchParams(location.search).get("edit_help")) {
245 title: OSM.i18n.t("javascripts.edit_help")
249 $("body").one("click", function () {
250 $("#editanchor").tooltip("hide");
254 OSM.router = OSM.Router(map, {
257 "/directions": "directions",
259 "/note/new": "new_note",
260 "/history/friends": "history",
261 "/history/nearby": "history",
262 "/history": "history",
263 "/user/:display_name/history": "history",
265 "/node/:id(/history)": { module: "index_element", part: m => m.mappedElement("node") },
266 "/node/:id/history/:version": { module: "index_element", part: m => m.mappedElement("node") },
267 "/way/:id(/history)": { module: "index_element", part: m => m.mappedElement("way") },
268 "/way/:id/history/:version": { module: "index_element", part: m => m.element("way") },
269 "/relation/:id(/history)": { module: "index_element", part: m => m.mappedElement("relation") },
270 "/relation/:id/history/:version": { module: "index_element", part: m => m.element("relation") },
271 "/changeset/:id": "changeset",
273 "/account/home": "home"
276 if (OSM.preferred_editor === "remote" && location.pathname === "/edit") {
277 remoteEditHandler(map.getBounds(), params.object);
278 OSM.router.setCurrentPath("/");
283 $(document).on("click", "a", function (e) {
284 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
288 // Open links in a new tab as normal.
289 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
293 // Open local anchor links as normal.
294 if ($(this).attr("href")?.startsWith("#")) {
298 // Ignore cross-protocol and cross-origin links.
299 const url = new URL($(this).attr("href"), location);
300 if (location.protocol !== url.protocol || location.host !== url.host) {
304 if (OSM.router.route(url.pathname + url.search + url.hash)) {
306 if (url.pathname !== "/directions") {
307 $(".navbar-toggler:not(.collapsed)").trigger("click");
312 $(document).on("click", "#sidebar .sidebar-close-controls button", function () {
313 $(".search_form input[name=query]").val("");
314 OSM.router.route("/" + OSM.formatHash(map));