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
14 //= require make-plural/cardinals
19 const application_data = $("head").data();
20 const locale = application_data.locale;
22 I18n.default_locale = OSM.DEFAULT_LOCALE;
24 I18n.fallbacks = true;
26 // '-' are replaced with '_' in https://github.com/eemeli/make-plural/tree/main/packages/plurals
27 const pluralizer = plurals[locale.replace(/\W+/g, "_")] || plurals[locale.split("-")[0]];
29 I18n.pluralization[locale] = (count) => [pluralizer(count), "other"];
32 OSM.preferred_editor = application_data.preferredEditor;
33 OSM.preferred_languages = application_data.preferredLanguages;
35 if (application_data.user) {
36 OSM.user = application_data.user;
38 if (application_data.userHome) {
39 OSM.home = application_data.userHome;
43 if (application_data.location) {
44 OSM.location = application_data.location;
49 * Called as the user scrolls/zooms around to manipulate hrefs of the
50 * view tab and various other links
52 window.updateLinks = function (loc, zoom, layers, object) {
53 $(".geolink").each(function (index, link) {
54 let href = link.href.split(/[?#]/)[0];
55 const queryArgs = new URLSearchParams(link.search),
56 editlink = $(link).hasClass("editlink");
58 for (const arg of ["node", "way", "relation", "changeset", "note"]) {
59 queryArgs.delete(arg);
62 if (object && editlink) {
63 queryArgs.set(object.type, object.id);
66 const query = queryArgs.toString();
67 if (query) href += "?" + query;
71 lon: "lon" in loc ? loc.lon : loc.lng,
75 if (layers && !editlink) {
76 hashArgs.layers = layers;
79 href += OSM.formatHash(hashArgs);
84 // Disable the button group and also the buttons to avoid
85 // inconsistent behaviour when zooming
86 const editDisabled = zoom < 13;
88 .tooltip({ placement: "bottom" })
89 .tooltip(editDisabled ? "enable" : "disable")
90 .toggleClass("disabled", editDisabled)
92 .toggleClass("disabled", editDisabled);
96 // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
97 // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
98 Turbo.session.drive = false;
100 const $expandedSecondaryMenu = $("header nav.secondary > ul"),
101 $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
102 secondaryMenuItems = [],
103 breakpointWidth = 768;
104 let moreItemWidth = 0;
107 OSM.csrf[($("meta[name=csrf-param]").attr("content"))] = $("meta[name=csrf-token]").attr("content");
109 function updateHeader() {
110 const windowWidth = $(window).width();
112 if (windowWidth < breakpointWidth) {
113 $("body").addClass("small-nav");
114 expandAllSecondaryMenuItems();
116 $("body").removeClass("small-nav");
117 const availableWidth = $expandedSecondaryMenu.width();
118 secondaryMenuItems.forEach(function (item) {
121 let runningWidth = 0,
124 for (; i < secondaryMenuItems.length; i++) {
125 runningWidth += secondaryMenuItems[i][1];
126 if (i < secondaryMenuItems.length - 1) {
127 requiredWidth = runningWidth + moreItemWidth;
129 requiredWidth = runningWidth;
131 if (requiredWidth > availableWidth) {
134 expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
136 for (; i < secondaryMenuItems.length; i++) {
137 collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
142 function expandAllSecondaryMenuItems() {
143 secondaryMenuItems.forEach(function (item) {
144 expandSecondaryMenuItem($(item[0]));
148 function expandSecondaryMenuItem($item) {
150 .removeClass("dropdown-item")
151 .addClass("nav-link")
152 .addClass(function () {
153 return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
155 $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
156 toggleCompactSecondaryNav();
159 function collapseSecondaryMenuItem($item) {
161 .addClass("dropdown-item")
162 .removeClass("nav-link text-secondary text-secondary-emphasis");
163 $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
164 toggleCompactSecondaryNav();
167 function toggleCompactSecondaryNav() {
168 $("#compact-secondary-nav").toggle(
169 $collapsedSecondaryMenu.find("li").length > 0
174 * Chrome 60 and later seem to fire the "ready" callback
175 * before the DOM is fully ready causing us to measure the
176 * wrong sizes for the header elements - use a 0ms timeout
177 * to defer the measurement slightly as a workaround.
179 setTimeout(function () {
180 $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
181 secondaryMenuItems.push([this, $(this).width()]);
183 moreItemWidth = $("#compact-secondary-nav").width();
187 $(window).resize(updateHeader);
188 $(document).on("turbo:render", updateHeader);
191 $("#menu-icon").on("click", function (e) {
193 $("header").toggleClass("closed");
196 $("nav.primary li a").on("click", function () {
197 $("header").toggleClass("closed");
201 .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));