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
19 const application_data = $("head").data();
21 I18n.default_locale = OSM.DEFAULT_LOCALE;
22 I18n.locale = application_data.locale;
23 I18n.fallbacks = true;
25 OSM.preferred_editor = application_data.preferredEditor;
26 OSM.preferred_languages = application_data.preferredLanguages;
28 if (application_data.user) {
29 OSM.user = application_data.user;
31 if (application_data.userHome) {
32 OSM.home = application_data.userHome;
36 if (application_data.location) {
37 OSM.location = application_data.location;
42 * Called as the user scrolls/zooms around to manipulate hrefs of the
43 * view tab and various other links
45 window.updateLinks = function (loc, zoom, layers, object) {
46 $(".geolink").each(function (index, link) {
47 let href = link.href.split(/[?#]/)[0];
48 const queryArgs = new URLSearchParams(link.search),
49 editlink = $(link).hasClass("editlink");
51 for (const arg of ["node", "way", "relation", "changeset", "note"]) {
52 queryArgs.delete(arg);
55 if (object && editlink) {
56 queryArgs.set(object.type, object.id);
59 const query = queryArgs.toString();
60 if (query) href += "?" + query;
64 lon: "lon" in loc ? loc.lon : loc.lng,
68 if (layers && !editlink) {
69 hashArgs.layers = layers;
72 href += OSM.formatHash(hashArgs);
77 // Disable the button group and also the buttons to avoid
78 // inconsistent behaviour when zooming
79 var editDisabled = zoom < 13;
81 .tooltip({ placement: "bottom" })
82 .tooltip(editDisabled ? "enable" : "disable")
83 .toggleClass("disabled", editDisabled)
85 .toggleClass("disabled", editDisabled);
88 $(document).ready(function () {
89 // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
90 // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
91 Turbo.session.drive = false;
93 const $expandedSecondaryMenu = $("header nav.secondary > ul"),
94 $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
95 secondaryMenuItems = [],
96 breakpointWidth = 768;
97 let moreItemWidth = 0;
99 function updateHeader() {
100 var windowWidth = $(window).width();
102 if (windowWidth < breakpointWidth) {
103 $("body").addClass("small-nav");
104 expandAllSecondaryMenuItems();
106 $("body").removeClass("small-nav");
107 const availableWidth = $expandedSecondaryMenu.width();
108 secondaryMenuItems.forEach(function (item) {
111 let runningWidth = 0,
114 for (; i < secondaryMenuItems.length; i++) {
115 runningWidth += secondaryMenuItems[i][1];
116 if (i < secondaryMenuItems.length - 1) {
117 requiredWidth = runningWidth + moreItemWidth;
119 requiredWidth = runningWidth;
121 if (requiredWidth > availableWidth) {
124 expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
126 for (; i < secondaryMenuItems.length; i++) {
127 collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
132 function expandAllSecondaryMenuItems() {
133 secondaryMenuItems.forEach(function (item) {
134 expandSecondaryMenuItem($(item[0]));
138 function expandSecondaryMenuItem($item) {
140 .removeClass("dropdown-item")
141 .addClass("nav-link")
142 .addClass(function () {
143 return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
145 $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
146 toggleCompactSecondaryNav();
149 function collapseSecondaryMenuItem($item) {
151 .addClass("dropdown-item")
152 .removeClass("nav-link text-secondary text-secondary-emphasis");
153 $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
154 toggleCompactSecondaryNav();
157 function toggleCompactSecondaryNav() {
158 $("#compact-secondary-nav").toggle(
159 $collapsedSecondaryMenu.find("li").length > 0
164 * Chrome 60 and later seem to fire the "ready" callback
165 * before the DOM is fully ready causing us to measure the
166 * wrong sizes for the header elements - use a 0ms timeout
167 * to defer the measurement slightly as a workaround.
169 setTimeout(function () {
170 $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
171 secondaryMenuItems.push([this, $(this).width()]);
173 moreItemWidth = $("#compact-secondary-nav").width();
177 $(window).resize(updateHeader);
178 $(document).on("turbo:render", updateHeader);
181 $("#menu-icon").on("click", function (e) {
183 $("header").toggleClass("closed");
186 $("nav.primary li a").on("click", function () {
187 $("header").toggleClass("closed");
191 .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));