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 controls.forEach(function (control) {
104 var firstContainer = controls[0].getContainer();
105 $(firstContainer).find(".control-button").first()
106 .addClass("control-button-first");
108 var lastContainer = controls[controls.length - 1].getContainer();
109 $(lastContainer).find(".control-button").last()
110 .addClass("control-button-last");
114 L.OSM.zoom({ position: position }),
115 L.OSM.locate({ position: position })
121 layers: map.baseLayers,
129 "position": position,
152 OSM.initializeContextMenu(map);
154 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
155 OSM.initializeNotesLayer(map);
156 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
157 map.addLayer(map.noteLayer);
160 OSM.initializeDataLayer(map);
161 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
162 map.addLayer(map.dataLayer);
165 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
166 map.addLayer(map.gpsLayer);
170 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
172 var expiry = new Date();
173 expiry.setYear(expiry.getFullYear() + 10);
175 map.on("moveend baselayerchange overlayadd overlayremove", function () {
177 map.getCenter().wrap(),
182 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
185 if (Cookies.get("_osm_welcome") !== "hide") {
186 $(".welcome").removeAttr("hidden");
189 $(".welcome .btn-close").on("click", function () {
190 $(".welcome").hide();
191 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
194 var bannerExpiry = new Date();
195 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
197 $("#banner .btn-close").on("click", function (e) {
198 var cookieId = e.target.id;
202 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
207 map.on("baselayerchange overlayadd", function (e) {
208 if (e.layer.options) {
209 var goal = OSM.MATOMO.goals[e.layer.options.layerId];
212 $("body").trigger("matomogoal", goal);
219 map.fitBounds(params.bounds);
221 map.setView([params.lat, params.lon], params.zoom);
225 L.marker([params.mlat, params.mlon]).addTo(map);
228 $("#homeanchor").on("click", function (e) {
231 var data = $(this).data(),
232 center = L.latLng(data.lat, data.lon);
234 map.setView(center, data.zoom);
235 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
238 function remoteEditHandler(bbox, object) {
239 var remoteEditHost = "http://127.0.0.1:8111",
240 osmHost = location.protocol + "//" + location.host,
241 query = new URLSearchParams({
242 left: bbox.getWest() - 0.0001,
243 top: bbox.getNorth() + 0.0001,
244 right: bbox.getEast() + 0.0001,
245 bottom: bbox.getSouth() - 0.0001
248 if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
249 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
250 if (object && object.type === "note") {
251 const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
252 sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
256 function sendRemoteEditCommand(url, callback) {
257 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
260 alert(I18n.t("site.index.remote_failed"));
267 $("a[data-editor=remote]").click(function (e) {
268 var params = OSM.mapParams(this.search);
269 remoteEditHandler(map.getBounds(), params.object);
273 if (OSM.params().edit_help) {
278 title: I18n.t("javascripts.edit_help")
282 $("body").one("click", function () {
283 $("#editanchor").tooltip("hide");
287 OSM.Index = function (map) {
290 page.pushstate = page.popstate = function () {
291 map.setSidebarOverlaid(true);
292 document.title = I18n.t("layouts.project_name.title");
295 page.load = function () {
296 const params = new URLSearchParams(location.search);
297 if (params.has("query")) {
298 $("#sidebar .search_form input[name=query]").value(params.get("query"));
300 if (!("autofocus" in document.createElement("input"))) {
301 $("#sidebar .search_form input[name=query]").focus();
303 return map.getState();
309 OSM.Browse = function (map, type) {
312 page.pushstate = page.popstate = function (path, id) {
313 OSM.loadSidebarContent(path, function () {
318 page.load = function (path, id) {
319 addObject(type, id, true);
322 function addObject(type, id, center) {
323 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
324 if (!window.location.hash && bounds.isValid() &&
325 (center || !map.getBounds().contains(bounds))) {
326 OSM.router.withoutMoveListener(function () {
327 map.fitBounds(bounds);
333 page.unload = function () {
340 OSM.OldBrowse = function () {
343 page.pushstate = page.popstate = function (path) {
344 OSM.loadSidebarContent(path);
350 var history = OSM.History(map);
352 OSM.router = OSM.Router(map, {
354 "/search": OSM.Search(map),
355 "/directions": OSM.Directions(map),
356 "/export": OSM.Export(map),
357 "/note/new": OSM.NewNote(map),
358 "/history/friends": history,
359 "/history/nearby": history,
361 "/user/:display_name/history": history,
362 "/note/:id": OSM.Note(map),
363 "/node/:id(/history)": OSM.Browse(map, "node"),
364 "/node/:id/history/:version": OSM.OldBrowse(),
365 "/way/:id(/history)": OSM.Browse(map, "way"),
366 "/way/:id/history/:version": OSM.OldBrowse(),
367 "/relation/:id(/history)": OSM.Browse(map, "relation"),
368 "/relation/:id/history/:version": OSM.OldBrowse(),
369 "/changeset/:id": OSM.Changeset(map),
370 "/query": OSM.Query(map)
373 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
374 remoteEditHandler(map.getBounds(), params.object);
375 OSM.router.setCurrentPath("/");
380 $(document).on("click", "a", function (e) {
381 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
385 // Open links in a new tab as normal.
386 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
390 // Ignore cross-protocol and cross-origin links.
391 if (location.protocol !== this.protocol || location.host !== this.host) {
395 if (OSM.router.route(this.pathname + this.search + this.hash)) {
397 if (this.pathname !== "/directions") {
398 $("header").addClass("closed");
403 $(document).on("click", "#sidebar_content .btn-close", function () {
404 OSM.router.route("/" + OSM.formatHash(map));