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
26 $(document).ready(function () {
27 var map = new L.OSM.Map("map", {
34 OSM.loadSidebarContent = function (path, callback) {
35 var content_path = path;
37 map.setSidebarOverlaid(false);
39 $("#sidebar_loader").show().addClass("delayed-fade-in");
47 complete: function (xhr) {
49 $("#sidebar_loader").removeClass("delayed-fade-in").hide();
51 var content = $(xhr.responseText);
53 if (xhr.getResponseHeader("X-Page-Title")) {
54 var title = xhr.getResponseHeader("X-Page-Title");
55 document.title = decodeURIComponent(title);
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\"]"));
74 var params = OSM.mapParams();
76 map.attributionControl.setPrefix("");
78 map.updateLayers(params.layers);
80 map.on("baselayerchange", function (e) {
81 if (map.getZoom() > e.layer.options.maxZoom) {
82 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
86 var sidebar = L.OSM.sidebar("#map-ui")
89 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
91 function addControlGroup(controls) {
92 for (const control of controls) control.addTo(map);
94 var firstContainer = controls[0].getContainer();
95 $(firstContainer).find(".control-button").first()
96 .addClass("control-button-first");
98 var lastContainer = controls[controls.length - 1].getContainer();
99 $(lastContainer).find(".control-button").last()
100 .addClass("control-button-last");
104 L.OSM.zoom({ position: position }),
105 L.OSM.locate({ position: position })
111 layers: map.baseLayers,
119 "position": position,
142 OSM.initializeContextMenu(map);
144 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
145 OSM.initializeNotesLayer(map);
146 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
147 map.addLayer(map.noteLayer);
150 OSM.initializeDataLayer(map);
151 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
152 map.addLayer(map.dataLayer);
155 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
156 map.addLayer(map.gpsLayer);
160 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
162 var expiry = new Date();
163 expiry.setYear(expiry.getFullYear() + 10);
165 map.on("moveend baselayerchange overlayadd overlayremove", function () {
167 map.getCenter().wrap(),
172 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
175 if (Cookies.get("_osm_welcome") !== "hide") {
176 $(".welcome").removeAttr("hidden");
179 $(".welcome .btn-close").on("click", function () {
180 $(".welcome").hide();
181 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
184 var bannerExpiry = new Date();
185 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
187 $("#banner .btn-close").on("click", function (e) {
188 var cookieId = e.target.id;
192 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
197 map.on("baselayerchange overlayadd", function (e) {
198 if (e.layer.options) {
199 var goal = OSM.MATOMO.goals[e.layer.options.layerId];
202 $("body").trigger("matomogoal", goal);
209 map.fitBounds(params.bounds);
211 map.setView([params.lat, params.lon], params.zoom);
215 L.marker([params.mlat, params.mlon]).addTo(map);
218 $("#homeanchor").on("click", function (e) {
221 var data = $(this).data(),
222 center = L.latLng(data.lat, data.lon);
224 map.setView(center, data.zoom);
225 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
228 function remoteEditHandler(bbox, object) {
229 var remoteEditHost = "http://127.0.0.1:8111",
230 osmHost = location.protocol + "//" + location.host,
231 query = new URLSearchParams({
232 left: bbox.getWest() - 0.0001,
233 top: bbox.getNorth() + 0.0001,
234 right: bbox.getEast() + 0.0001,
235 bottom: bbox.getSouth() - 0.0001
238 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
239 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
240 if (object && object.type === "note") {
241 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
242 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
246 function sendRemoteEditCommand(url, callback) {
247 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
250 // eslint-disable-next-line no-alert
251 alert(I18n.t("site.index.remote_failed"));
258 $("a[data-editor=remote]").click(function (e) {
259 var params = OSM.mapParams(this.search);
260 remoteEditHandler(map.getBounds(), params.object);
264 if (OSM.params().edit_help) {
269 title: I18n.t("javascripts.edit_help")
273 $("body").one("click", function () {
274 $("#editanchor").tooltip("hide");
278 OSM.Index = function (map) {
281 page.pushstate = page.popstate = function () {
282 map.setSidebarOverlaid(true);
283 document.title = I18n.t("layouts.project_name.title");
286 page.load = function () {
287 const params = new URLSearchParams(location.search);
288 if (params.has("query")) {
289 $("#sidebar .search_form input[name=query]").value(params.get("query"));
291 if (!("autofocus" in document.createElement("input"))) {
292 $("#sidebar .search_form input[name=query]").focus();
294 return map.getState();
300 OSM.Browse = function (map, type) {
303 page.pushstate = page.popstate = function (path, id) {
304 OSM.loadSidebarContent(path, function () {
309 page.load = function (path, id) {
310 addObject(type, id, true);
313 function addObject(type, id, center) {
314 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
315 if (!window.location.hash && bounds.isValid() &&
316 (center || !map.getBounds().contains(bounds))) {
317 OSM.router.withoutMoveListener(function () {
318 map.fitBounds(bounds);
324 page.unload = function () {
331 OSM.OldBrowse = function () {
334 page.pushstate = page.popstate = function (path) {
335 OSM.loadSidebarContent(path);
341 var history = OSM.History(map);
343 OSM.router = OSM.Router(map, {
345 "/search": OSM.Search(map),
346 "/directions": OSM.Directions(map),
347 "/export": OSM.Export(map),
348 "/note/new": OSM.NewNote(map),
349 "/history/friends": history,
350 "/history/nearby": history,
352 "/user/:display_name/history": history,
353 "/note/:id": OSM.Note(map),
354 "/node/:id(/history)": OSM.Browse(map, "node"),
355 "/node/:id/history/:version": OSM.OldBrowse(),
356 "/way/:id(/history)": OSM.Browse(map, "way"),
357 "/way/:id/history/:version": OSM.OldBrowse(),
358 "/relation/:id(/history)": OSM.Browse(map, "relation"),
359 "/relation/:id/history/:version": OSM.OldBrowse(),
360 "/changeset/:id": OSM.Changeset(map),
361 "/query": OSM.Query(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));