]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Merge remote-tracking branch 'upstream/pull/5201'
[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 //= require language_selector
18
19 {
20   const application_data = $("head").data();
21   const locale = application_data.locale;
22
23   OSM.i18n.defaultLocale = OSM.DEFAULT_LOCALE;
24   OSM.i18n.locale = application_data.locale;
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     OSM.i18n.pluralization.register(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       notCollapsibleItemsWidth = 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 = notCollapsibleItemsWidth,
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):not(.not-collapsible)").each(function () {
182       secondaryMenuItems.push([this, $(this).width()]);
183     });
184     moreItemWidth = $("#compact-secondary-nav").width();
185     notCollapsibleItemsWidth = $expandedSecondaryMenu
186       .find("li.not-collapsible")
187       .toArray()
188       .reduce((accWidth, item) => accWidth + $(item).outerWidth(), 0);
189
190     updateHeader();
191
192     $(window).resize(updateHeader);
193     $(document).on("turbo:render", updateHeader);
194   }, 0);
195
196   $("#menu-icon").on("click", function (e) {
197     e.preventDefault();
198     $("header").toggleClass("closed");
199   });
200
201   $("nav.primary li a").on("click", function () {
202     $("header").toggleClass("closed");
203   });
204
205   $("#edit_tab")
206     .attr("title", OSM.i18n.t("javascripts.site.edit_disabled_tooltip"));
207 });