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 $("#homeanchor").on("click", function (e) {
220 var data = $(this).data(),
221 center = L.latLng(data.lat, data.lon);
223 map.setView(center, data.zoom);
224 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
227 function remoteEditHandler(bbox, object) {
228 var remoteEditHost = "http://127.0.0.1:8111",
229 osmHost = location.protocol + "//" + location.host,
230 query = new URLSearchParams({
231 left: bbox.getWest() - 0.0001,
232 top: bbox.getNorth() + 0.0001,
233 right: bbox.getEast() + 0.0001,
234 bottom: bbox.getSouth() - 0.0001
237 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
238 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
239 if (object && object.type === "note") {
240 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
241 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
245 function sendRemoteEditCommand(url, callback) {
246 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
249 // eslint-disable-next-line no-alert
250 alert(I18n.t("site.index.remote_failed"));
257 $("a[data-editor=remote]").click(function (e) {
258 var params = OSM.mapParams(this.search);
259 remoteEditHandler(map.getBounds(), params.object);
263 if (OSM.params().edit_help) {
268 title: I18n.t("javascripts.edit_help")
272 $("body").one("click", function () {
273 $("#editanchor").tooltip("hide");
277 OSM.Index = function (map) {
280 page.pushstate = page.popstate = function () {
281 map.setSidebarOverlaid(true);
282 document.title = I18n.t("layouts.project_name.title");
285 page.load = function () {
286 const params = new URLSearchParams(location.search);
287 if (params.has("query")) {
288 $("#sidebar .search_form input[name=query]").value(params.get("query"));
290 if (!("autofocus" in document.createElement("input"))) {
291 $("#sidebar .search_form input[name=query]").focus();
293 return map.getState();
299 OSM.Browse = function (map, type) {
302 page.pushstate = page.popstate = function (path, id) {
303 OSM.loadSidebarContent(path, function () {
308 page.load = function (path, id) {
309 addObject(type, id, true);
312 function addObject(type, id, center) {
313 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
314 if (!window.location.hash && bounds.isValid() &&
315 (center || !map.getBounds().contains(bounds))) {
316 OSM.router.withoutMoveListener(function () {
317 map.fitBounds(bounds);
323 page.unload = function () {
330 OSM.OldBrowse = function () {
333 page.pushstate = page.popstate = function (path) {
334 OSM.loadSidebarContent(path);
340 var history = OSM.History(map);
342 OSM.router = OSM.Router(map, {
344 "/search": OSM.Search(map),
345 "/directions": OSM.Directions(map),
346 "/export": OSM.Export(map),
347 "/note/new": OSM.NewNote(map),
348 "/history/friends": history,
349 "/history/nearby": history,
351 "/user/:display_name/history": history,
352 "/note/:id": OSM.Note(map),
353 "/node/:id(/history)": OSM.Browse(map, "node"),
354 "/node/:id/history/:version": OSM.OldBrowse(),
355 "/way/:id(/history)": OSM.Browse(map, "way"),
356 "/way/:id/history/:version": OSM.OldBrowse(),
357 "/relation/:id(/history)": OSM.Browse(map, "relation"),
358 "/relation/:id/history/:version": OSM.OldBrowse(),
359 "/changeset/:id": OSM.Changeset(map),
360 "/query": OSM.Query(map),
361 "/account/home": OSM.Home(map)
364 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
365 remoteEditHandler(map.getBounds(), params.object);
366 OSM.router.setCurrentPath("/");
371 $(document).on("click", "a", function (e) {
372 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
376 // Open links in a new tab as normal.
377 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
381 // Ignore cross-protocol and cross-origin links.
382 if (location.protocol !== this.protocol || location.host !== this.host) {
386 if (OSM.router.route(this.pathname + this.search + this.hash)) {
388 if (this.pathname !== "/directions") {
389 $("header").addClass("closed");
394 $(document).on("click", "#sidebar_content .btn-close", function () {
395 OSM.router.route("/" + OSM.formatHash(map));