]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Update to make-plural 8.x
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery3
2 //= require jquery_ujs
3 //= require jquery.throttle-debounce
4 //= require js-cookie/dist/js.cookie
5 //= require popper
6 //= require bootstrap-sprockets
7 //= require osm
8 //= require leaflet/dist/leaflet-src
9 //= require leaflet.osm
10 //= require leaflet.shortbread
11 //= require leaflet.maptiler
12 //= require leaflet.map
13 //= require leaflet.zoom
14 //= require leaflet.locationfilter
15 //= require i18n
16 //= require matomo
17 //= require richtext
18 //= require language_selector
19
20 {
21   const application_data = $("head").data();
22   const locale = application_data.locale;
23
24   OSM.i18n.defaultLocale = OSM.DEFAULT_LOCALE;
25   OSM.i18n.locale = application_data.locale;
26
27   import(OSM.MAKE_PLURAL_CARDINALS).then((plurals) => {
28     // '-' are replaced with '_' in https://github.com/eemeli/make-plural/tree/main/packages/plurals
29     const pluralizer = plurals[locale.replace(/\W+/g, "_")] || plurals[locale.split("-")[0]];
30     if (pluralizer) {
31       OSM.i18n.pluralization.register(locale, (_, count) => [pluralizer(count), "other"]);
32     }
33   });
34
35   OSM.preferred_editor = application_data.preferredEditor;
36   OSM.preferred_languages = application_data.preferredLanguages;
37
38   if (application_data.user) {
39     OSM.user = application_data.user;
40
41     if (application_data.userHome) {
42       OSM.home = application_data.userHome;
43     }
44   }
45
46   if (application_data.location) {
47     OSM.location = application_data.location;
48   }
49 }
50
51 /*
52  * Called as the user scrolls/zooms around to manipulate hrefs of the
53  * view tab and various other links
54  */
55 window.updateLinks = function (loc, zoom, layers, object) {
56   $(".geolink").each(function (index, link) {
57     let href = link.href.split(/[?#]/)[0];
58     const queryArgs = new URLSearchParams(link.search),
59           editlink = $(link).hasClass("editlink");
60
61     for (const arg of ["node", "way", "relation", "changeset", "note"]) {
62       queryArgs.delete(arg);
63     }
64
65     if (object && editlink) {
66       queryArgs.set(object.type, object.id);
67     }
68
69     const query = queryArgs.toString();
70     if (query) href += "?" + query;
71
72     const hashArgs = {
73       lat: loc.lat,
74       lon: "lon" in loc ? loc.lon : loc.lng,
75       zoom: zoom
76     };
77
78     if (layers && !editlink) {
79       hashArgs.layers = layers;
80     }
81
82     href += OSM.formatHash(hashArgs);
83
84     link.href = href;
85   });
86
87   // Disable the button group and also the buttons to avoid
88   // inconsistent behaviour when zooming
89   const editDisabled = zoom < 13;
90   $("#edit_tab")
91     .tooltip({ placement: "bottom" })
92     .tooltip(editDisabled ? "enable" : "disable")
93     .toggleClass("disabled", editDisabled)
94     .find("a")
95     .toggleClass("disabled", editDisabled);
96 };
97
98 $(function () {
99   // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
100   // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
101   Turbo.session.drive = false;
102
103   const $expandedSecondaryMenu = $("header nav.secondary > ul"),
104         $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
105         secondaryMenuItems = [],
106         breakpointWidth = 768;
107   let moreItemWidth = 0;
108
109   OSM.csrf = {};
110   OSM.csrf[($("meta[name=csrf-param]").attr("content"))] = $("meta[name=csrf-token]").attr("content");
111
112   function updateHeader() {
113     const windowWidth = $(window).width();
114
115     if (windowWidth < breakpointWidth) {
116       $("body").addClass("small-nav");
117       expandAllSecondaryMenuItems();
118     } else {
119       $("body").removeClass("small-nav");
120       const availableWidth = $expandedSecondaryMenu.width();
121       secondaryMenuItems.forEach(function (item) {
122         $(item[0]).remove();
123       });
124       let runningWidth = 0,
125           i = 0,
126           requiredWidth;
127       for (; i < secondaryMenuItems.length; i++) {
128         runningWidth += secondaryMenuItems[i][1];
129         if (i < secondaryMenuItems.length - 1) {
130           requiredWidth = runningWidth + moreItemWidth;
131         } else {
132           requiredWidth = runningWidth;
133         }
134         if (requiredWidth > availableWidth) {
135           break;
136         }
137         expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
138       }
139       for (; i < secondaryMenuItems.length; i++) {
140         collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
141       }
142     }
143   }
144
145   function expandAllSecondaryMenuItems() {
146     secondaryMenuItems.forEach(function (item) {
147       expandSecondaryMenuItem($(item[0]));
148     });
149   }
150
151   function expandSecondaryMenuItem($item) {
152     $item.children("a")
153       .removeClass("dropdown-item")
154       .addClass("nav-link")
155       .addClass(function () {
156         return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
157       });
158     $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
159     toggleCompactSecondaryNav();
160   }
161
162   function collapseSecondaryMenuItem($item) {
163     $item.children("a")
164       .addClass("dropdown-item")
165       .removeClass("nav-link text-secondary text-secondary-emphasis");
166     $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
167     toggleCompactSecondaryNav();
168   }
169
170   function toggleCompactSecondaryNav() {
171     $("#compact-secondary-nav").toggle(
172       $collapsedSecondaryMenu.find("li").length > 0
173     );
174   }
175
176   /*
177    * Chrome 60 and later seem to fire the "ready" callback
178    * before the DOM is fully ready causing us to measure the
179    * wrong sizes for the header elements - use a 0ms timeout
180    * to defer the measurement slightly as a workaround.
181    */
182   setTimeout(function () {
183     $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
184       secondaryMenuItems.push([this, $(this).width()]);
185     });
186     moreItemWidth = $("#compact-secondary-nav").width();
187
188     updateHeader();
189
190     $(window).resize(updateHeader);
191     $(document).on("turbo:render", updateHeader);
192   }, 0);
193
194   $("#menu-icon").on("click", function (e) {
195     e.preventDefault();
196     $("header").toggleClass("closed");
197   });
198
199   $("nav.primary li a").on("click", function () {
200     $("header").toggleClass("closed");
201   });
202
203   $("#edit_tab")
204     .attr("title", OSM.i18n.t("javascripts.site.edit_disabled_tooltip"));
205
206   document.addEventListener("turbo:frame-load", function (event) {
207     if (event.target.id === "select_language_list") {
208       const $search = $("#language_search");
209       if ($search.length) {
210         $search.off("input");
211         $search.on("input", function () {
212           const query = $(this).val().toLowerCase();
213           $(".language-item").each(function () {
214             const text = $(this).text().toLowerCase();
215             $(this).toggle(text.indexOf(query) > -1);
216           });
217         });
218       }
219       $search
220         .val("")
221         .trigger("input")
222         .trigger("focus");
223     }
224   });
225
226   $("#select_language_dialog").on("shown.bs.modal", function () {
227     $("#language_search")
228       .val("")
229       .trigger("input")
230       .trigger("focus");
231     const $frame = $("#select_language_list");
232     const originalSrc = new URL($frame.attr("src"), window.location.origin);
233
234     // Set `source` query param to current page path + query string
235     originalSrc.searchParams.set("source", window.location.pathname + window.location.search);
236
237     if ($frame.attr("src") !== originalSrc.toString()) {
238       $frame.attr("src", originalSrc.toString());
239     }
240   });
241 });