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 var iframe = $("<iframe>");
259 var timeoutId = setTimeout(function () {
260 alert(I18n.t("site.index.remote_failed"));
268 .on("load", function () {
269 clearTimeout(timeoutId);
271 if (callback) callback();
278 $("a[data-editor=remote]").click(function (e) {
279 var params = OSM.mapParams(this.search);
280 remoteEditHandler(map.getBounds(), params.object);
284 if (OSM.params().edit_help) {
289 title: I18n.t("javascripts.edit_help")
293 $("body").one("click", function () {
294 $("#editanchor").tooltip("hide");
298 OSM.Index = function (map) {
301 page.pushstate = page.popstate = function () {
302 map.setSidebarOverlaid(true);
303 document.title = I18n.t("layouts.project_name.title");
306 page.load = function () {
307 var params = Qs.parse(location.search.substring(1));
309 $("#sidebar .search_form input[name=query]").value(params.query);
311 if (!("autofocus" in document.createElement("input"))) {
312 $("#sidebar .search_form input[name=query]").focus();
314 return map.getState();
320 OSM.Browse = function (map, type) {
323 page.pushstate = page.popstate = function (path, id) {
324 OSM.loadSidebarContent(path, function () {
329 page.load = function (path, id) {
330 addObject(type, id, true);
333 function addObject(type, id, center) {
334 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
335 if (!window.location.hash && bounds.isValid() &&
336 (center || !map.getBounds().contains(bounds))) {
337 OSM.router.withoutMoveListener(function () {
338 map.fitBounds(bounds);
344 page.unload = function () {
351 OSM.OldBrowse = function () {
354 page.pushstate = page.popstate = function (path) {
355 OSM.loadSidebarContent(path);
361 var history = OSM.History(map);
363 OSM.router = OSM.Router(map, {
365 "/search": OSM.Search(map),
366 "/directions": OSM.Directions(map),
367 "/export": OSM.Export(map),
368 "/note/new": OSM.NewNote(map),
369 "/history/friends": history,
370 "/history/nearby": history,
372 "/user/:display_name/history": history,
373 "/note/:id": OSM.Note(map),
374 "/node/:id(/history)": OSM.Browse(map, "node"),
375 "/node/:id/history/:version": OSM.OldBrowse(),
376 "/way/:id(/history)": OSM.Browse(map, "way"),
377 "/way/:id/history/:version": OSM.OldBrowse(),
378 "/relation/:id(/history)": OSM.Browse(map, "relation"),
379 "/relation/:id/history/:version": OSM.OldBrowse(),
380 "/changeset/:id": OSM.Changeset(map),
381 "/query": OSM.Query(map)
384 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
385 remoteEditHandler(map.getBounds(), params.object);
386 OSM.router.setCurrentPath("/");
391 $(document).on("click", "a", function (e) {
392 if (e.isDefaultPrevented() || e.isPropagationStopped() || $(e.target).data("turbo")) {
396 // Open links in a new tab as normal.
397 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
401 // Ignore cross-protocol and cross-origin links.
402 if (location.protocol !== this.protocol || location.host !== this.host) {
406 if (OSM.router.route(this.pathname + this.search + this.hash)) {
408 if (this.pathname !== "/directions") {
409 $("header").addClass("closed");
414 $(document).on("click", "#sidebar_content .btn-close", function () {
415 OSM.router.route("/" + OSM.formatHash(map));