]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Add OSM.i18n as an alias for I18n
[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   OSM.i18n = I18n;
23
24   I18n.default_locale = OSM.DEFAULT_LOCALE;
25   I18n.locale = locale;
26
27   // '-' are replaced with '_' in https://github.com/eemeli/make-plural/tree/main/packages/plurals
28   const pluralizer = plurals[locale.replace(/\W+/g, "_")] || plurals[locale.split("-")[0]];
29   if (pluralizer) {
30     I18n.pluralization[locale] = (count) => [pluralizer(count), "other"];
31   }
32
33   OSM.preferred_editor = application_data.preferredEditor;
34   OSM.preferred_languages = application_data.preferredLanguages;
35
36   if (application_data.user) {
37     OSM.user = application_data.user;
38
39     if (application_data.userHome) {
40       OSM.home = application_data.userHome;
41     }
42   }
43
44   if (application_data.location) {
45     OSM.location = application_data.location;
46   }
47 }
48
49 /*
50  * Called as the user scrolls/zooms around to manipulate hrefs of the
51  * view tab and various other links
52  */
53 window.updateLinks = function (loc, zoom, layers, object) {
54   $(".geolink").each(function (index, link) {
55     let href = link.href.split(/[?#]/)[0];
56     const queryArgs = new URLSearchParams(link.search),
57           editlink = $(link).hasClass("editlink");
58
59     for (const arg of ["node", "way", "relation", "changeset", "note"]) {
60       queryArgs.delete(arg);
61     }
62
63     if (object && editlink) {
64       queryArgs.set(object.type, object.id);
65     }
66
67     const query = queryArgs.toString();
68     if (query) href += "?" + query;
69
70     const hashArgs = {
71       lat: loc.lat,
72       lon: "lon" in loc ? loc.lon : loc.lng,
73       zoom: zoom
74     };
75
76     if (layers && !editlink) {
77       hashArgs.layers = layers;
78     }
79
80     href += OSM.formatHash(hashArgs);
81
82     link.href = href;
83   });
84
85   // Disable the button group and also the buttons to avoid
86   // inconsistent behaviour when zooming
87   const editDisabled = zoom < 13;
88   $("#edit_tab")
89     .tooltip({ placement: "bottom" })
90     .tooltip(editDisabled ? "enable" : "disable")
91     .toggleClass("disabled", editDisabled)
92     .find("a")
93     .toggleClass("disabled", editDisabled);
94 };
95
96 $(function () {
97   // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
98   // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
99   Turbo.session.drive = false;
100
101   const $expandedSecondaryMenu = $("header nav.secondary > ul"),
102         $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
103         secondaryMenuItems = [],
104         breakpointWidth = 768;
105   let moreItemWidth = 0;
106
107   OSM.csrf = {};
108   OSM.csrf[($("meta[name=csrf-param]").attr("content"))] = $("meta[name=csrf-token]").attr("content");
109
110   function updateHeader() {
111     const windowWidth = $(window).width();
112
113     if (windowWidth < breakpointWidth) {
114       $("body").addClass("small-nav");
115       expandAllSecondaryMenuItems();
116     } else {
117       $("body").removeClass("small-nav");
118       const availableWidth = $expandedSecondaryMenu.width();
119       secondaryMenuItems.forEach(function (item) {
120         $(item[0]).remove();
121       });
122       let runningWidth = 0,
123           i = 0,
124           requiredWidth;
125       for (; i < secondaryMenuItems.length; i++) {
126         runningWidth += secondaryMenuItems[i][1];
127         if (i < secondaryMenuItems.length - 1) {
128           requiredWidth = runningWidth + moreItemWidth;
129         } else {
130           requiredWidth = runningWidth;
131         }
132         if (requiredWidth > availableWidth) {
133           break;
134         }
135         expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
136       }
137       for (; i < secondaryMenuItems.length; i++) {
138         collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
139       }
140     }
141   }
142
143   function expandAllSecondaryMenuItems() {
144     secondaryMenuItems.forEach(function (item) {
145       expandSecondaryMenuItem($(item[0]));
146     });
147   }
148
149   function expandSecondaryMenuItem($item) {
150     $item.children("a")
151       .removeClass("dropdown-item")
152       .addClass("nav-link")
153       .addClass(function () {
154         return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
155       });
156     $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
157     toggleCompactSecondaryNav();
158   }
159
160   function collapseSecondaryMenuItem($item) {
161     $item.children("a")
162       .addClass("dropdown-item")
163       .removeClass("nav-link text-secondary text-secondary-emphasis");
164     $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
165     toggleCompactSecondaryNav();
166   }
167
168   function toggleCompactSecondaryNav() {
169     $("#compact-secondary-nav").toggle(
170       $collapsedSecondaryMenu.find("li").length > 0
171     );
172   }
173
174   /*
175    * Chrome 60 and later seem to fire the "ready" callback
176    * before the DOM is fully ready causing us to measure the
177    * wrong sizes for the header elements - use a 0ms timeout
178    * to defer the measurement slightly as a workaround.
179    */
180   setTimeout(function () {
181     $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
182       secondaryMenuItems.push([this, $(this).width()]);
183     });
184     moreItemWidth = $("#compact-secondary-nav").width();
185
186     updateHeader();
187
188     $(window).resize(updateHeader);
189     $(document).on("turbo:render", updateHeader);
190   }, 0);
191
192   $("#menu-icon").on("click", function (e) {
193     e.preventDefault();
194     $("header").toggleClass("closed");
195   });
196
197   $("nav.primary li a").on("click", function () {
198     $("header").toggleClass("closed");
199   });
200
201   $("#edit_tab")
202     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
203 });