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");
44 fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
47 $("#sidebar_loader").removeClass("delayed-fade-in").hide();
49 const title = response.headers.get("X-Page-Title");
50 if (title) document.title = decodeURIComponent(title);
52 return response.text();
55 const content = $(html);
58 .find("link[type=\"application/atom+xml\"]")
62 .append(content.filter("link[type=\"application/atom+xml\"]"));
64 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
72 var params = OSM.mapParams();
74 map.attributionControl.setPrefix("");
76 map.updateLayers(params.layers);
78 map.on("baselayerchange", function (e) {
79 if (map.getZoom() > e.layer.options.maxZoom) {
80 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
84 var sidebar = L.OSM.sidebar("#map-ui")
87 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
89 function addControlGroup(controls) {
90 for (const control of controls) control.addTo(map);
92 var firstContainer = controls[0].getContainer();
93 $(firstContainer).find(".control-button").first()
94 .addClass("control-button-first");
96 var lastContainer = controls[controls.length - 1].getContainer();
97 $(lastContainer).find(".control-button").last()
98 .addClass("control-button-last");
102 L.OSM.zoom({ position: position }),
103 L.OSM.locate({ position: position })
109 layers: map.baseLayers,
117 "position": position,
140 OSM.initializeContextMenu(map);
142 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
143 OSM.initializeNotesLayer(map);
144 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
145 map.addLayer(map.noteLayer);
148 OSM.initializeDataLayer(map);
149 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
150 map.addLayer(map.dataLayer);
153 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
154 map.addLayer(map.gpsLayer);
158 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
160 var expiry = new Date();
161 expiry.setYear(expiry.getFullYear() + 10);
163 map.on("moveend baselayerchange overlayadd overlayremove", function () {
165 map.getCenter().wrap(),
170 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
173 if (Cookies.get("_osm_welcome") !== "hide") {
174 $(".welcome").removeAttr("hidden");
177 $(".welcome .btn-close").on("click", function () {
178 $(".welcome").hide();
179 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
182 var bannerExpiry = new Date();
183 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
185 $("#banner .btn-close").on("click", function (e) {
186 var cookieId = e.target.id;
190 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
195 map.on("baselayerchange overlayadd", function (e) {
196 if (e.layer.options) {
197 var goal = OSM.MATOMO.goals[e.layer.options.layerId];
200 $("body").trigger("matomogoal", goal);
207 map.fitBounds(params.bounds);
209 map.setView([params.lat, params.lon], params.zoom);
213 L.marker([params.mlat, params.mlon]).addTo(map);
216 $("#homeanchor").on("click", function (e) {
219 var data = $(this).data(),
220 center = L.latLng(data.lat, data.lon);
222 map.setView(center, data.zoom);
223 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
226 function remoteEditHandler(bbox, object) {
227 var remoteEditHost = "http://127.0.0.1:8111",
228 osmHost = location.protocol + "//" + location.host,
229 query = new URLSearchParams({
230 left: bbox.getWest() - 0.0001,
231 top: bbox.getNorth() + 0.0001,
232 right: bbox.getEast() + 0.0001,
233 bottom: bbox.getSouth() - 0.0001
236 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
237 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
238 if (object && object.type === "note") {
239 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
240 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
244 function sendRemoteEditCommand(url, callback) {
245 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
248 // eslint-disable-next-line no-alert
249 alert(I18n.t("site.index.remote_failed"));
256 $("a[data-editor=remote]").click(function (e) {
257 var params = OSM.mapParams(this.search);
258 remoteEditHandler(map.getBounds(), params.object);
262 if (OSM.params().edit_help) {
267 title: I18n.t("javascripts.edit_help")
271 $("body").one("click", function () {
272 $("#editanchor").tooltip("hide");
276 OSM.Index = function (map) {
279 page.pushstate = page.popstate = function () {
280 map.setSidebarOverlaid(true);
281 document.title = I18n.t("layouts.project_name.title");
284 page.load = function () {
285 const params = new URLSearchParams(location.search);
286 if (params.has("query")) {
287 $("#sidebar .search_form input[name=query]").value(params.get("query"));
289 if (!("autofocus" in document.createElement("input"))) {
290 $("#sidebar .search_form input[name=query]").focus();
292 return map.getState();
298 OSM.Browse = function (map, type) {
301 page.pushstate = page.popstate = function (path, id) {
302 OSM.loadSidebarContent(path, function () {
307 page.load = function (path, id) {
308 addObject(type, id, true);
311 function addObject(type, id, center) {
312 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
313 if (!window.location.hash && bounds.isValid() &&
314 (center || !map.getBounds().contains(bounds))) {
315 OSM.router.withoutMoveListener(function () {
316 map.fitBounds(bounds);
322 page.unload = function () {
329 OSM.OldBrowse = function () {
332 page.pushstate = page.popstate = function (path) {
333 OSM.loadSidebarContent(path);
339 var history = OSM.History(map);
341 OSM.router = OSM.Router(map, {
343 "/search": OSM.Search(map),
344 "/directions": OSM.Directions(map),
345 "/export": OSM.Export(map),
346 "/note/new": OSM.NewNote(map),
347 "/history/friends": history,
348 "/history/nearby": history,
350 "/user/:display_name/history": history,
351 "/note/:id": OSM.Note(map),
352 "/node/:id(/history)": OSM.Browse(map, "node"),
353 "/node/:id/history/:version": OSM.OldBrowse(),
354 "/way/:id(/history)": OSM.Browse(map, "way"),
355 "/way/:id/history/:version": OSM.OldBrowse(),
356 "/relation/:id(/history)": OSM.Browse(map, "relation"),
357 "/relation/:id/history/:version": OSM.OldBrowse(),
358 "/changeset/:id": OSM.Changeset(map),
359 "/query": OSM.Query(map)
362 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
363 remoteEditHandler(map.getBounds(), params.object);
364 OSM.router.setCurrentPath("/");
369 $(document).on("click", "a", function (e) {
370 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
374 // Open links in a new tab as normal.
375 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
379 // Ignore cross-protocol and cross-origin links.
380 if (location.protocol !== this.protocol || location.host !== this.host) {
384 if (OSM.router.route(this.pathname + this.search + this.hash)) {
386 if (this.pathname !== "/directions") {
387 $("header").addClass("closed");
392 $(document).on("click", "#sidebar_content .btn-close", function () {
393 OSM.router.route("/" + OSM.formatHash(map));