]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Load pluralizers
[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.map
11 //= require leaflet.zoom
12 //= require leaflet.locationfilter
13 //= require i18n
14 //= require make-plural/cardinals
15 //= require matomo
16 //= require richtext
17
18 {
19   const application_data = $("head").data();
20   const locale = application_data.locale;
21
22   I18n.default_locale = OSM.DEFAULT_LOCALE;
23   I18n.locale = locale;
24   I18n.fallbacks = true;
25
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]];
28   if (pluralizer) {
29     I18n.pluralization[locale] = (count) => [pluralizer(count), "other"];
30   }
31
32   OSM.preferred_editor = application_data.preferredEditor;
33   OSM.preferred_languages = application_data.preferredLanguages;
34
35   if (application_data.user) {
36     OSM.user = application_data.user;
37
38     if (application_data.userHome) {
39       OSM.home = application_data.userHome;
40     }
41   }
42
43   if (application_data.location) {
44     OSM.location = application_data.location;
45   }
46 }
47
48 /*
49  * Called as the user scrolls/zooms around to manipulate hrefs of the
50  * view tab and various other links
51  */
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");
57
58     for (const arg of ["node", "way", "relation", "changeset", "note"]) {
59       queryArgs.delete(arg);
60     }
61
62     if (object && editlink) {
63       queryArgs.set(object.type, object.id);
64     }
65
66     const query = queryArgs.toString();
67     if (query) href += "?" + query;
68
69     const hashArgs = {
70       lat: loc.lat,
71       lon: "lon" in loc ? loc.lon : loc.lng,
72       zoom: zoom
73     };
74
75     if (layers && !editlink) {
76       hashArgs.layers = layers;
77     }
78
79     href += OSM.formatHash(hashArgs);
80
81     link.href = href;
82   });
83
84   // Disable the button group and also the buttons to avoid
85   // inconsistent behaviour when zooming
86   const editDisabled = zoom < 13;
87   $("#edit_tab")
88     .tooltip({ placement: "bottom" })
89     .tooltip(editDisabled ? "enable" : "disable")
90     .toggleClass("disabled", editDisabled)
91     .find("a")
92     .toggleClass("disabled", editDisabled);
93 };
94
95 $(function () {
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;
99
100   const $expandedSecondaryMenu = $("header nav.secondary > ul"),
101         $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
102         secondaryMenuItems = [],
103         breakpointWidth = 768;
104   let moreItemWidth = 0;
105
106   OSM.csrf = {};
107   OSM.csrf[($("meta[name=csrf-param]").attr("content"))] = $("meta[name=csrf-token]").attr("content");
108
109   function updateHeader() {
110     const windowWidth = $(window).width();
111
112     if (windowWidth < breakpointWidth) {
113       $("body").addClass("small-nav");
114       expandAllSecondaryMenuItems();
115     } else {
116       $("body").removeClass("small-nav");
117       const availableWidth = $expandedSecondaryMenu.width();
118       secondaryMenuItems.forEach(function (item) {
119         $(item[0]).remove();
120       });
121       let runningWidth = 0,
122           i = 0,
123           requiredWidth;
124       for (; i < secondaryMenuItems.length; i++) {
125         runningWidth += secondaryMenuItems[i][1];
126         if (i < secondaryMenuItems.length - 1) {
127           requiredWidth = runningWidth + moreItemWidth;
128         } else {
129           requiredWidth = runningWidth;
130         }
131         if (requiredWidth > availableWidth) {
132           break;
133         }
134         expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
135       }
136       for (; i < secondaryMenuItems.length; i++) {
137         collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
138       }
139     }
140   }
141
142   function expandAllSecondaryMenuItems() {
143     secondaryMenuItems.forEach(function (item) {
144       expandSecondaryMenuItem($(item[0]));
145     });
146   }
147
148   function expandSecondaryMenuItem($item) {
149     $item.children("a")
150       .removeClass("dropdown-item")
151       .addClass("nav-link")
152       .addClass(function () {
153         return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
154       });
155     $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
156     toggleCompactSecondaryNav();
157   }
158
159   function collapseSecondaryMenuItem($item) {
160     $item.children("a")
161       .addClass("dropdown-item")
162       .removeClass("nav-link text-secondary text-secondary-emphasis");
163     $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
164     toggleCompactSecondaryNav();
165   }
166
167   function toggleCompactSecondaryNav() {
168     $("#compact-secondary-nav").toggle(
169       $collapsedSecondaryMenu.find("li").length > 0
170     );
171   }
172
173   /*
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.
178    */
179   setTimeout(function () {
180     $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
181       secondaryMenuItems.push([this, $(this).width()]);
182     });
183     moreItemWidth = $("#compact-secondary-nav").width();
184
185     updateHeader();
186
187     $(window).resize(updateHeader);
188     $(document).on("turbo:render", updateHeader);
189   }, 0);
190
191   $("#menu-icon").on("click", function (e) {
192     e.preventDefault();
193     $("header").toggleClass("closed");
194   });
195
196   $("nav.primary li a").on("click", function () {
197     $("header").toggleClass("closed");
198   });
199
200   $("#edit_tab")
201     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
202 });