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 maplibre-gl/dist/maplibre-gl
11 //= require leaflet.maplibre
12 //= require leaflet.shortbread
14 //= require leaflet.maptiler
15 //= require leaflet.map
16 //= require leaflet.zoom
17 //= require leaflet.locationfilter
20 //= require language_selector
23 const application_data = $("head").data();
24 const locale = application_data.locale;
26 OSM.i18n.defaultLocale = OSM.DEFAULT_LOCALE;
27 OSM.i18n.locale = application_data.locale;
29 import(OSM.MAKE_PLURAL_CARDINALS).then((plurals) => {
30 // '-' are replaced with '_' in https://github.com/eemeli/make-plural/tree/main/packages/plurals
31 const pluralizer = plurals[locale.replace(/\W+/g, "_")] || plurals[locale.split("-")[0]];
33 OSM.i18n.pluralization.register(locale, (_, count) => [pluralizer(count), "other"]);
37 OSM.preferred_editor = application_data.preferredEditor;
38 OSM.preferred_languages = application_data.preferredLanguages;
40 if (application_data.user) {
41 OSM.user = application_data.user;
43 if (application_data.userHome) {
44 OSM.home = application_data.userHome;
48 if (application_data.location) {
49 OSM.location = application_data.location;
54 * Called as the user scrolls/zooms around to manipulate hrefs of the
55 * view tab and various other links
57 window.updateLinks = function (loc, zoom, layers, object) {
58 $(".geolink").each(function (index, link) {
59 let href = link.href.split(/[?#]/)[0];
60 const queryArgs = new URLSearchParams(link.search),
61 editlink = $(link).hasClass("editlink");
63 for (const arg of ["node", "way", "relation", "changeset", "note"]) {
64 queryArgs.delete(arg);
67 if (object && editlink) {
68 queryArgs.set(object.type, object.id);
71 const query = queryArgs.toString();
72 if (query) href += "?" + query;
76 lon: "lon" in loc ? loc.lon : loc.lng,
80 if (layers && !editlink) {
81 hashArgs.layers = layers;
84 href += OSM.formatHash(hashArgs);
89 // Disable the button group and also the buttons to avoid
90 // inconsistent behaviour when zooming
91 const editDisabled = zoom < 13;
93 .tooltip({ placement: "bottom" })
94 .tooltip(editDisabled ? "enable" : "disable")
95 .toggleClass("disabled", editDisabled)
97 .toggleClass("disabled", editDisabled);
101 // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
102 // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
103 Turbo.session.drive = false;
105 const $expandedSecondaryMenu = $("header nav.secondary > ul"),
106 $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
107 secondaryMenuItems = [],
108 breakpointWidth = 768;
109 let moreItemWidth = 0;
112 OSM.csrf[($("meta[name=csrf-param]").attr("content"))] = $("meta[name=csrf-token]").attr("content");
114 function updateHeader() {
115 const windowWidth = $(window).width();
117 if (windowWidth < breakpointWidth) {
118 expandAllSecondaryMenuItems();
120 if (secondaryMenuItems.length === 0) {
121 $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
122 secondaryMenuItems.push([this, $(this).width()]);
124 moreItemWidth = $("#compact-secondary-nav").width();
126 const availableWidth = $expandedSecondaryMenu.width();
127 secondaryMenuItems.forEach(function (item) {
130 let runningWidth = 0,
133 for (; i < secondaryMenuItems.length; i++) {
134 runningWidth += secondaryMenuItems[i][1];
135 if (i < secondaryMenuItems.length - 1) {
136 requiredWidth = runningWidth + moreItemWidth;
138 requiredWidth = runningWidth;
140 if (requiredWidth > availableWidth) {
143 expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
145 for (; i < secondaryMenuItems.length; i++) {
146 collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
151 function expandAllSecondaryMenuItems() {
152 secondaryMenuItems.forEach(function (item) {
153 expandSecondaryMenuItem($(item[0]));
157 function expandSecondaryMenuItem($item) {
159 .removeClass("dropdown-item")
160 .addClass("nav-link")
161 .addClass(function () {
162 return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
164 $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
165 toggleCompactSecondaryNav();
168 function collapseSecondaryMenuItem($item) {
170 .addClass("dropdown-item")
171 .removeClass("nav-link text-secondary text-secondary-emphasis");
172 $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
173 toggleCompactSecondaryNav();
176 function toggleCompactSecondaryNav() {
177 $("#compact-secondary-nav").toggle(
178 $collapsedSecondaryMenu.find("li").length > 0
183 * Chrome 60 and later seem to fire the "ready" callback
184 * before the DOM is fully ready causing us to measure the
185 * wrong sizes for the header elements - use a 0ms timeout
186 * to defer the measurement slightly as a workaround.
188 setTimeout(function () {
191 $(window).resize(updateHeader);
192 $(document).on("turbo:render", updateHeader);
195 $("#menu-icon").on("click", function (e) {
197 $("header").toggleClass("closed");
200 $("nav.primary li a").on("click", function () {
201 $("header").toggleClass("closed");
205 .attr("title", OSM.i18n.t("javascripts.site.edit_disabled_tooltip"));
207 document.addEventListener("turbo:frame-load", function (event) {
208 if (event.target.id === "select_language_list") {
209 const $search = $("#language_search");
210 if ($search.length) {
211 $search.off("input");
212 $search.on("input", function () {
213 const query = $(this).val().toLowerCase();
214 $(".language-item").each(function () {
215 const text = $(this).text().toLowerCase();
216 $(this).toggle(text.indexOf(query) > -1);
227 $("#select_language_dialog").on("shown.bs.modal", function () {
228 $("#language_search")
232 const $frame = $("#select_language_list");
233 const originalSrc = new URL($frame.attr("src"), window.location.origin);
235 // Set `source` query param to current page path + query string
236 originalSrc.searchParams.set("source", window.location.pathname + window.location.search);
238 if ($frame.attr("src") !== originalSrc.toString()) {
239 $frame.attr("src", originalSrc.toString());