]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Move OAuth requests to fetch
[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   var 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 $(document).ready(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   function updateHeader() {
99     var windowWidth = $(window).width();
100
101     if (windowWidth < breakpointWidth) {
102       $("body").addClass("small-nav");
103       expandAllSecondaryMenuItems();
104     } else {
105       $("body").removeClass("small-nav");
106       const availableWidth = $expandedSecondaryMenu.width();
107       secondaryMenuItems.forEach(function (item) {
108         $(item[0]).remove();
109       });
110       let runningWidth = 0,
111           i = 0,
112           requiredWidth;
113       for (; i < secondaryMenuItems.length; i++) {
114         runningWidth += secondaryMenuItems[i][1];
115         if (i < secondaryMenuItems.length - 1) {
116           requiredWidth = runningWidth + moreItemWidth;
117         } else {
118           requiredWidth = runningWidth;
119         }
120         if (requiredWidth > availableWidth) {
121           break;
122         }
123         expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
124       }
125       for (; i < secondaryMenuItems.length; i++) {
126         collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
127       }
128     }
129   }
130
131   function expandAllSecondaryMenuItems() {
132     secondaryMenuItems.forEach(function (item) {
133       expandSecondaryMenuItem($(item[0]));
134     });
135   }
136
137   function expandSecondaryMenuItem($item) {
138     $item.children("a")
139       .removeClass("dropdown-item")
140       .addClass("nav-link")
141       .addClass(function () {
142         return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
143       });
144     $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
145     toggleCompactSecondaryNav();
146   }
147
148   function collapseSecondaryMenuItem($item) {
149     $item.children("a")
150       .addClass("dropdown-item")
151       .removeClass("nav-link text-secondary text-secondary-emphasis");
152     $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
153     toggleCompactSecondaryNav();
154   }
155
156   function toggleCompactSecondaryNav() {
157     $("#compact-secondary-nav").toggle(
158       $collapsedSecondaryMenu.find("li").length > 0
159     );
160   }
161
162   /*
163    * Chrome 60 and later seem to fire the "ready" callback
164    * before the DOM is fully ready causing us to measure the
165    * wrong sizes for the header elements - use a 0ms timeout
166    * to defer the measurement slightly as a workaround.
167    */
168   setTimeout(function () {
169     $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
170       secondaryMenuItems.push([this, $(this).width()]);
171     });
172     moreItemWidth = $("#compact-secondary-nav").width();
173
174     updateHeader();
175
176     $(window).resize(updateHeader);
177     $(document).on("turbo:render", updateHeader);
178   }, 0);
179
180   $("#menu-icon").on("click", function (e) {
181     e.preventDefault();
182     $("header").toggleClass("closed");
183   });
184
185   $("nav.primary li a").on("click", function () {
186     $("header").toggleClass("closed");
187   });
188
189   $("#edit_tab")
190     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
191 });