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