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 var map = new L.OSM.Map("map", {
35 OSM.loadSidebarContent = function (path, callback) {
36 var content_path = path;
38 map.setSidebarOverlaid(false);
40 $("#sidebar_loader").show().addClass("delayed-fade-in");
45 fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
48 $("#sidebar_loader").removeClass("delayed-fade-in").hide();
50 const title = response.headers.get("X-Page-Title");
51 if (title) document.title = decodeURIComponent(title);
53 return response.text();
56 const content = $(html);
59 .find("link[type=\"application/atom+xml\"]")
63 .append(content.filter("link[type=\"application/atom+xml\"]"));
65 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
73 var params = OSM.mapParams();
75 map.attributionControl.setPrefix("");
77 map.updateLayers(params.layers);
79 map.on("baselayerchange", function (e) {
80 if (map.getZoom() > e.layer.options.maxZoom) {
81 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
85 var sidebar = L.OSM.sidebar("#map-ui")
88 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
90 function addControlGroup(controls) {
91 for (const control of controls) control.addTo(map);
93 var firstContainer = controls[0].getContainer();
94 $(firstContainer).find(".control-button").first()
95 .addClass("control-button-first");
97 var lastContainer = controls[controls.length - 1].getContainer();
98 $(lastContainer).find(".control-button").last()
99 .addClass("control-button-last");
103 L.OSM.zoom({ position: position }),
104 L.OSM.locate({ position: position })
110 layers: map.baseLayers,
118 "position": position,
141 OSM.initializeContextMenu(map);
143 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
144 OSM.initializeNotesLayer(map);
145 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
146 map.addLayer(map.noteLayer);
149 OSM.initializeDataLayer(map);
150 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
151 map.addLayer(map.dataLayer);
154 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
155 map.addLayer(map.gpsLayer);
159 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
161 var expiry = new Date();
162 expiry.setYear(expiry.getFullYear() + 10);
164 map.on("moveend baselayerchange overlayadd overlayremove", function () {
166 map.getCenter().wrap(),
171 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
174 if (Cookies.get("_osm_welcome") !== "hide") {
175 $(".welcome").removeAttr("hidden");
178 $(".welcome .btn-close").on("click", function () {
179 $(".welcome").hide();
180 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
183 var bannerExpiry = new Date();
184 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
186 $("#banner .btn-close").on("click", function (e) {
187 var cookieId = e.target.id;
191 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
196 map.on("baselayerchange overlayadd", function (e) {
197 if (e.layer.options) {
198 var goal = OSM.MATOMO.goals[e.layer.options.layerId];
201 $("body").trigger("matomogoal", goal);
208 map.fitBounds(params.bounds);
210 map.setView([params.lat, params.lon], params.zoom);
214 L.marker([params.mlat, params.mlon]).addTo(map);
217 function remoteEditHandler(bbox, object) {
218 var remoteEditHost = "http://127.0.0.1:8111",
219 osmHost = location.protocol + "//" + location.host,
220 query = new URLSearchParams({
221 left: bbox.getWest() - 0.0001,
222 top: bbox.getNorth() + 0.0001,
223 right: bbox.getEast() + 0.0001,
224 bottom: bbox.getSouth() - 0.0001
227 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
228 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
229 if (object && object.type === "note") {
230 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
231 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
235 function sendRemoteEditCommand(url, callback) {
236 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
239 // eslint-disable-next-line no-alert
240 alert(I18n.t("site.index.remote_failed"));
247 $("a[data-editor=remote]").click(function (e) {
248 var params = OSM.mapParams(this.search);
249 remoteEditHandler(map.getBounds(), params.object);
253 if (OSM.params().edit_help) {
258 title: I18n.t("javascripts.edit_help")
262 $("body").one("click", function () {
263 $("#editanchor").tooltip("hide");
267 OSM.Index = function (map) {
270 page.pushstate = page.popstate = function () {
271 map.setSidebarOverlaid(true);
272 document.title = I18n.t("layouts.project_name.title");
275 page.load = function () {
276 const params = new URLSearchParams(location.search);
277 if (params.has("query")) {
278 $("#sidebar .search_form input[name=query]").value(params.get("query"));
280 if (!("autofocus" in document.createElement("input"))) {
281 $("#sidebar .search_form input[name=query]").focus();
283 return map.getState();
289 OSM.Browse = function (map, type) {
292 page.pushstate = page.popstate = function (path, id) {
293 OSM.loadSidebarContent(path, function () {
298 page.load = function (path, id) {
299 addObject(type, id, true);
302 function addObject(type, id, center) {
303 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
304 if (!window.location.hash && bounds.isValid() &&
305 (center || !map.getBounds().contains(bounds))) {
306 OSM.router.withoutMoveListener(function () {
307 map.fitBounds(bounds);
313 page.unload = function () {
320 OSM.OldBrowse = function () {
323 page.pushstate = page.popstate = function (path) {
324 OSM.loadSidebarContent(path);
330 var history = OSM.History(map);
332 OSM.router = OSM.Router(map, {
334 "/search": OSM.Search(map),
335 "/directions": OSM.Directions(map),
336 "/export": OSM.Export(map),
337 "/note/new": OSM.NewNote(map),
338 "/history/friends": history,
339 "/history/nearby": history,
341 "/user/:display_name/history": history,
342 "/note/:id": OSM.Note(map),
343 "/node/:id(/history)": OSM.Browse(map, "node"),
344 "/node/:id/history/:version": OSM.OldBrowse(),
345 "/way/:id(/history)": OSM.Browse(map, "way"),
346 "/way/:id/history/:version": OSM.OldBrowse(),
347 "/relation/:id(/history)": OSM.Browse(map, "relation"),
348 "/relation/:id/history/:version": OSM.OldBrowse(),
349 "/changeset/:id": OSM.Changeset(map),
350 "/query": OSM.Query(map),
351 "/account/home": OSM.Home(map)
354 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
355 remoteEditHandler(map.getBounds(), params.object);
356 OSM.router.setCurrentPath("/");
361 $(document).on("click", "a", function (e) {
362 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
366 // Open links in a new tab as normal.
367 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
371 // Ignore cross-protocol and cross-origin links.
372 if (location.protocol !== this.protocol || location.host !== this.host) {
376 if (OSM.router.route(this.pathname + this.search + this.hash)) {
378 if (this.pathname !== "/directions") {
379 $("header").addClass("closed");
384 $(document).on("click", "#sidebar_content .btn-close", function () {
385 OSM.router.route("/" + OSM.formatHash(map));