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