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");
41 // IE<10 doesn't respect Vary: X-Requested-With header, so
42 // prevent caching the XHR response as a full-page URL.
43 if (content_path.indexOf("?") >= 0) {
44 content_path += "&xhr=1";
46 content_path += "?xhr=1";
55 complete: function (xhr) {
57 $("#sidebar_loader").removeClass("delayed-fade-in").hide();
59 var content = $(xhr.responseText);
61 if (xhr.getResponseHeader("X-Page-Title")) {
62 var title = xhr.getResponseHeader("X-Page-Title");
63 document.title = decodeURIComponent(title);
67 .find("link[type=\"application/atom+xml\"]")
71 .append(content.filter("link[type=\"application/atom+xml\"]"));
73 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
82 var params = OSM.mapParams();
84 map.attributionControl.setPrefix("");
86 map.updateLayers(params.layers);
88 map.on("baselayerchange", function (e) {
89 if (map.getZoom() > e.layer.options.maxZoom) {
90 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
94 var sidebar = L.OSM.sidebar("#map-ui")
97 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
99 function addControlGroup(controls) {
100 for (const control of controls) control.addTo(map);
102 var firstContainer = controls[0].getContainer();
103 $(firstContainer).find(".control-button").first()
104 .addClass("control-button-first");
106 var lastContainer = controls[controls.length - 1].getContainer();
107 $(lastContainer).find(".control-button").last()
108 .addClass("control-button-last");
112 L.OSM.zoom({ position: position }),
113 L.OSM.locate({ position: position })
119 layers: map.baseLayers,
127 "position": position,
150 OSM.initializeContextMenu(map);
152 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
153 OSM.initializeNotesLayer(map);
154 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
155 map.addLayer(map.noteLayer);
158 OSM.initializeDataLayer(map);
159 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
160 map.addLayer(map.dataLayer);
163 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
164 map.addLayer(map.gpsLayer);
168 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
170 var expiry = new Date();
171 expiry.setYear(expiry.getFullYear() + 10);
173 map.on("moveend baselayerchange overlayadd overlayremove", function () {
175 map.getCenter().wrap(),
180 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
183 if (Cookies.get("_osm_welcome") !== "hide") {
184 $(".welcome").removeAttr("hidden");
187 $(".welcome .btn-close").on("click", function () {
188 $(".welcome").hide();
189 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
192 var bannerExpiry = new Date();
193 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
195 $("#banner .btn-close").on("click", function (e) {
196 var cookieId = e.target.id;
200 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
205 map.on("baselayerchange overlayadd", function (e) {
206 if (e.layer.options) {
207 var goal = OSM.MATOMO.goals[e.layer.options.layerId];
210 $("body").trigger("matomogoal", goal);
217 map.fitBounds(params.bounds);
219 map.setView([params.lat, params.lon], params.zoom);
223 L.marker([params.mlat, params.mlon]).addTo(map);
226 $("#homeanchor").on("click", function (e) {
229 var data = $(this).data(),
230 center = L.latLng(data.lat, data.lon);
232 map.setView(center, data.zoom);
233 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
236 function remoteEditHandler(bbox, object) {
237 var remoteEditHost = "http://127.0.0.1:8111",
238 osmHost = location.protocol + "//" + location.host,
239 query = new URLSearchParams({
240 left: bbox.getWest() - 0.0001,
241 top: bbox.getNorth() + 0.0001,
242 right: bbox.getEast() + 0.0001,
243 bottom: bbox.getSouth() - 0.0001
246 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
247 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
248 if (object && object.type === "note") {
249 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
250 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
254 function sendRemoteEditCommand(url, callback) {
255 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
258 alert(I18n.t("site.index.remote_failed"));
265 $("a[data-editor=remote]").click(function (e) {
266 var params = OSM.mapParams(this.search);
267 remoteEditHandler(map.getBounds(), params.object);
271 if (OSM.params().edit_help) {
276 title: I18n.t("javascripts.edit_help")
280 $("body").one("click", function () {
281 $("#editanchor").tooltip("hide");
285 OSM.Index = function (map) {
288 page.pushstate = page.popstate = function () {
289 map.setSidebarOverlaid(true);
290 document.title = I18n.t("layouts.project_name.title");
293 page.load = function () {
294 const params = new URLSearchParams(location.search);
295 if (params.has("query")) {
296 $("#sidebar .search_form input[name=query]").value(params.get("query"));
298 if (!("autofocus" in document.createElement("input"))) {
299 $("#sidebar .search_form input[name=query]").focus();
301 return map.getState();
307 OSM.Browse = function (map, type) {
310 page.pushstate = page.popstate = function (path, id) {
311 OSM.loadSidebarContent(path, function () {
316 page.load = function (path, id) {
317 addObject(type, id, true);
320 function addObject(type, id, center) {
321 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
322 if (!window.location.hash && bounds.isValid() &&
323 (center || !map.getBounds().contains(bounds))) {
324 OSM.router.withoutMoveListener(function () {
325 map.fitBounds(bounds);
331 page.unload = function () {
338 OSM.OldBrowse = function () {
341 page.pushstate = page.popstate = function (path) {
342 OSM.loadSidebarContent(path);
348 var history = OSM.History(map);
350 OSM.router = OSM.Router(map, {
352 "/search": OSM.Search(map),
353 "/directions": OSM.Directions(map),
354 "/export": OSM.Export(map),
355 "/note/new": OSM.NewNote(map),
356 "/history/friends": history,
357 "/history/nearby": history,
359 "/user/:display_name/history": history,
360 "/note/:id": OSM.Note(map),
361 "/node/:id(/history)": OSM.Browse(map, "node"),
362 "/node/:id/history/:version": OSM.OldBrowse(),
363 "/way/:id(/history)": OSM.Browse(map, "way"),
364 "/way/:id/history/:version": OSM.OldBrowse(),
365 "/relation/:id(/history)": OSM.Browse(map, "relation"),
366 "/relation/:id/history/:version": OSM.OldBrowse(),
367 "/changeset/:id": OSM.Changeset(map),
368 "/query": OSM.Query(map)
371 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
372 remoteEditHandler(map.getBounds(), params.object);
373 OSM.router.setCurrentPath("/");
378 $(document).on("click", "a", function (e) {
379 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
383 // Open links in a new tab as normal.
384 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
388 // Ignore cross-protocol and cross-origin links.
389 if (location.protocol !== this.protocol || location.host !== this.host) {
393 if (OSM.router.route(this.pathname + this.search + this.hash)) {
395 if (this.pathname !== "/directions") {
396 $("header").addClass("closed");
401 $(document).on("click", "#sidebar_content .btn-close", function () {
402 OSM.router.route("/" + OSM.formatHash(map));