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
25 //= require qs/dist/qs
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");
42 // IE<10 doesn't respect Vary: X-Requested-With header, so
43 // prevent caching the XHR response as a full-page URL.
44 if (content_path.indexOf("?") >= 0) {
45 content_path += "&xhr=1";
47 content_path += "?xhr=1";
56 complete: function (xhr) {
58 $("#sidebar_loader").removeClass("delayed-fade-in").hide();
60 var content = $(xhr.responseText);
62 if (xhr.getResponseHeader("X-Page-Title")) {
63 var title = xhr.getResponseHeader("X-Page-Title");
64 document.title = decodeURIComponent(title);
68 .find("link[type=\"application/atom+xml\"]")
72 .append(content.filter("link[type=\"application/atom+xml\"]"));
74 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
83 var params = OSM.mapParams();
85 map.attributionControl.setPrefix("");
87 map.updateLayers(params.layers);
89 map.on("baselayerchange", function (e) {
90 if (map.getZoom() > e.layer.options.maxZoom) {
91 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
95 var sidebar = L.OSM.sidebar("#map-ui")
98 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
100 function addControlGroup(controls) {
101 controls.forEach(function (control) {
105 var firstContainer = controls[0].getContainer();
106 $(firstContainer).find(".control-button").first()
107 .addClass("control-button-first");
109 var lastContainer = controls[controls.length - 1].getContainer();
110 $(lastContainer).find(".control-button").last()
111 .addClass("control-button-last");
115 L.OSM.zoom({ position: position }),
116 L.OSM.locate({ position: position })
122 layers: map.baseLayers,
130 "position": position,
153 OSM.initializeContextMenu(map);
155 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
156 OSM.initializeNotesLayer(map);
157 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
158 map.addLayer(map.noteLayer);
161 OSM.initializeDataLayer(map);
162 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
163 map.addLayer(map.dataLayer);
166 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
167 map.addLayer(map.gpsLayer);
171 $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
173 var expiry = new Date();
174 expiry.setYear(expiry.getFullYear() + 10);
176 map.on("moveend layeradd layerremove", function () {
178 map.getCenter().wrap(),
183 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
186 if (Cookies.get("_osm_welcome") !== "hide") {
187 $(".welcome").removeAttr("hidden");
190 $(".welcome .btn-close").on("click", function () {
191 $(".welcome").hide();
192 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
195 var bannerExpiry = new Date();
196 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
198 $("#banner .btn-close").on("click", function (e) {
199 var cookieId = e.target.id;
203 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
208 map.on("layeradd", function (e) {
209 if (e.layer.options) {
210 var goal = OSM.MATOMO.goals[e.layer.options.keyid];
213 $("body").trigger("matomogoal", goal);
220 map.fitBounds(params.bounds);
222 map.setView([params.lat, params.lon], params.zoom);
226 L.marker([params.mlat, params.mlon]).addTo(map);
229 $("#homeanchor").on("click", function (e) {
232 var data = $(this).data(),
233 center = L.latLng(data.lat, data.lon);
235 map.setView(center, data.zoom);
236 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
239 function remoteEditHandler(bbox, object) {
240 var remoteEditHost = "http://127.0.0.1:8111",
241 osmHost = location.protocol + "//" + location.host,
243 left: bbox.getWest() - 0.0001,
244 top: bbox.getNorth() + 0.0001,
245 right: bbox.getEast() + 0.0001,
246 bottom: bbox.getSouth() - 0.0001
249 if (object && object.type !== "note") query.select = object.type + object.id; // can't select notes
250 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + Qs.stringify(query), function () {
251 if (object && object.type === "note") {
252 var noteQuery = { url: osmHost + OSM.apiUrl(object) };
253 sendRemoteEditCommand(remoteEditHost + "/import?" + Qs.stringify(noteQuery));
257 function sendRemoteEditCommand(url, callback) {
258 fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
261 alert(I18n.t("site.index.remote_failed"));
268 $("a[data-editor=remote]").click(function (e) {
269 var params = OSM.mapParams(this.search);
270 remoteEditHandler(map.getBounds(), params.object);
274 if (OSM.params().edit_help) {
279 title: I18n.t("javascripts.edit_help")
283 $("body").one("click", function () {
284 $("#editanchor").tooltip("hide");
288 OSM.Index = function (map) {
291 page.pushstate = page.popstate = function () {
292 map.setSidebarOverlaid(true);
293 document.title = I18n.t("layouts.project_name.title");
296 page.load = function () {
297 var params = Qs.parse(location.search.substring(1));
299 $("#sidebar .search_form input[name=query]").value(params.query);
301 if (!("autofocus" in document.createElement("input"))) {
302 $("#sidebar .search_form input[name=query]").focus();
304 return map.getState();
310 OSM.Browse = function (map, type) {
313 page.pushstate = page.popstate = function (path, id) {
314 OSM.loadSidebarContent(path, function () {
319 page.load = function (path, id) {
320 addObject(type, id, true);
323 function addObject(type, id, center) {
324 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
325 if (!window.location.hash && bounds.isValid() &&
326 (center || !map.getBounds().contains(bounds))) {
327 OSM.router.withoutMoveListener(function () {
328 map.fitBounds(bounds);
334 page.unload = function () {
341 OSM.OldBrowse = function () {
344 page.pushstate = page.popstate = function (path) {
345 OSM.loadSidebarContent(path);
351 var history = OSM.History(map);
353 OSM.router = OSM.Router(map, {
355 "/search": OSM.Search(map),
356 "/directions": OSM.Directions(map),
357 "/export": OSM.Export(map),
358 "/note/new": OSM.NewNote(map),
359 "/history/friends": history,
360 "/history/nearby": history,
362 "/user/:display_name/history": history,
363 "/note/:id": OSM.Note(map),
364 "/node/:id(/history)": OSM.Browse(map, "node"),
365 "/node/:id/history/:version": OSM.OldBrowse(),
366 "/way/:id(/history)": OSM.Browse(map, "way"),
367 "/way/:id/history/:version": OSM.OldBrowse(),
368 "/relation/:id(/history)": OSM.Browse(map, "relation"),
369 "/relation/:id/history/:version": OSM.OldBrowse(),
370 "/changeset/:id": OSM.Changeset(map),
371 "/query": OSM.Query(map)
374 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
375 remoteEditHandler(map.getBounds(), params.object);
376 OSM.router.setCurrentPath("/");
381 $(document).on("click", "a", function (e) {
382 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
386 // Open links in a new tab as normal.
387 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
391 // Ignore cross-protocol and cross-origin links.
392 if (location.protocol !== this.protocol || location.host !== this.host) {
396 if (OSM.router.route(this.pathname + this.search + this.hash)) {
398 if (this.pathname !== "/directions") {
399 $("header").addClass("closed");
404 $(document).on("click", "#sidebar_content .btn-close", function () {
405 OSM.router.route("/" + OSM.formatHash(map));