3 //= require jquery.throttle-debounce
4 //= require js-cookie/dist/js.cookie
6 //= require bootstrap-sprockets
8 //= require leaflet/dist/leaflet-src
9 //= require leaflet.osm
10 //= require leaflet.map
11 //= require leaflet.zoom
12 //= require leaflet.locationfilter
18 const application_data = $("head").data();
20 I18n.default_locale = OSM.DEFAULT_LOCALE;
21 I18n.locale = application_data.locale;
22 I18n.fallbacks = true;
24 OSM.preferred_editor = application_data.preferredEditor;
25 OSM.preferred_languages = application_data.preferredLanguages;
27 if (application_data.user) {
28 OSM.user = application_data.user;
30 if (application_data.userHome) {
31 OSM.home = application_data.userHome;
35 if (application_data.location) {
36 OSM.location = application_data.location;
41 * Called as the user scrolls/zooms around to manipulate hrefs of the
42 * view tab and various other links
44 window.updateLinks = function (loc, zoom, layers, object) {
45 $(".geolink").each(function (index, link) {
46 let href = link.href.split(/[?#]/)[0];
47 const queryArgs = new URLSearchParams(link.search),
48 editlink = $(link).hasClass("editlink");
50 for (const arg of ["node", "way", "relation", "changeset", "note"]) {
51 queryArgs.delete(arg);
54 if (object && editlink) {
55 queryArgs.set(object.type, object.id);
58 const query = queryArgs.toString();
59 if (query) href += "?" + query;
63 lon: "lon" in loc ? loc.lon : loc.lng,
67 if (layers && !editlink) {
68 hashArgs.layers = layers;
71 href += OSM.formatHash(hashArgs);
76 // Disable the button group and also the buttons to avoid
77 // inconsistent behaviour when zooming
78 var editDisabled = zoom < 13;
80 .tooltip({ placement: "bottom" })
81 .tooltip(editDisabled ? "enable" : "disable")
82 .toggleClass("disabled", editDisabled)
84 .toggleClass("disabled", editDisabled);
87 $(document).ready(function () {
88 // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
89 // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
90 Turbo.session.drive = false;
92 const $expandedSecondaryMenu = $("header nav.secondary > ul"),
93 $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
94 secondaryMenuItems = [],
95 breakpointWidth = 768;
96 let moreItemWidth = 0;
98 function updateHeader() {
99 var windowWidth = $(window).width();
101 if (windowWidth < breakpointWidth) {
102 $("body").addClass("small-nav");
103 expandAllSecondaryMenuItems();
105 $("body").removeClass("small-nav");
106 const availableWidth = $expandedSecondaryMenu.width();
107 secondaryMenuItems.forEach(function (item) {
110 let runningWidth = 0,
113 for (; i < secondaryMenuItems.length; i++) {
114 runningWidth += secondaryMenuItems[i][1];
115 if (i < secondaryMenuItems.length - 1) {
116 requiredWidth = runningWidth + moreItemWidth;
118 requiredWidth = runningWidth;
120 if (requiredWidth > availableWidth) {
123 expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
125 for (; i < secondaryMenuItems.length; i++) {
126 collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
131 function expandAllSecondaryMenuItems() {
132 secondaryMenuItems.forEach(function (item) {
133 expandSecondaryMenuItem($(item[0]));
137 function expandSecondaryMenuItem($item) {
139 .removeClass("dropdown-item")
140 .addClass("nav-link")
141 .addClass(function () {
142 return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
144 $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
145 toggleCompactSecondaryNav();
148 function collapseSecondaryMenuItem($item) {
150 .addClass("dropdown-item")
151 .removeClass("nav-link text-secondary text-secondary-emphasis");
152 $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
153 toggleCompactSecondaryNav();
156 function toggleCompactSecondaryNav() {
157 $("#compact-secondary-nav").toggle(
158 $collapsedSecondaryMenu.find("li").length > 0
163 * Chrome 60 and later seem to fire the "ready" callback
164 * before the DOM is fully ready causing us to measure the
165 * wrong sizes for the header elements - use a 0ms timeout
166 * to defer the measurement slightly as a workaround.
168 setTimeout(function () {
169 $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
170 secondaryMenuItems.push([this, $(this).width()]);
172 moreItemWidth = $("#compact-secondary-nav").width();
176 $(window).resize(updateHeader);
177 $(document).on("turbo:render", updateHeader);
180 $("#menu-icon").on("click", function (e) {
182 $("header").toggleClass("closed");
185 $("nav.primary li a").on("click", function () {
186 $("header").toggleClass("closed");
190 .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));