]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Merge remote-tracking branch 'upstream/pull/5621'
[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     queryArgs.delete("node");
52     queryArgs.delete("way");
53     queryArgs.delete("relation");
54     queryArgs.delete("changeset");
55     queryArgs.delete("note");
56
57     if (object && editlink) {
58       queryArgs.set(object.type, object.id);
59     }
60
61     const query = queryArgs.toString();
62     if (query) href += "?" + query;
63
64     const hashArgs = {
65       lat: loc.lat,
66       lon: "lon" in loc ? loc.lon : loc.lng,
67       zoom: zoom
68     };
69
70     if (layers && !editlink) {
71       hashArgs.layers = layers;
72     }
73
74     href += OSM.formatHash(hashArgs);
75
76     link.href = href;
77   });
78
79   // Disable the button group and also the buttons to avoid
80   // inconsistent behaviour when zooming
81   var editDisabled = zoom < 13;
82   $("#edit_tab")
83     .tooltip({ placement: "bottom" })
84     .tooltip(editDisabled ? "enable" : "disable")
85     .toggleClass("disabled", editDisabled)
86     .find("a")
87     .toggleClass("disabled", editDisabled);
88 };
89
90 $(document).ready(function () {
91   // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
92   // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
93   Turbo.session.drive = false;
94
95   var headerWidth = 0,
96       compactWidth = 0;
97
98   function updateHeader() {
99     var windowWidth = $(window).width();
100
101     if (windowWidth < compactWidth) {
102       $("body").removeClass("compact-nav").addClass("small-nav");
103     } else if (windowWidth < headerWidth) {
104       $("body").addClass("compact-nav").removeClass("small-nav");
105     } else {
106       $("body").removeClass("compact-nav").removeClass("small-nav");
107     }
108   }
109
110   /*
111    * Chrome 60 and later seem to fire the "ready" callback
112    * before the DOM is fully ready causing us to measure the
113    * wrong sizes for the header elements - use a 0ms timeout
114    * to defer the measurement slightly as a workaround.
115    */
116   setTimeout(function () {
117     $("header").children(":visible").each(function (i, e) {
118       headerWidth = headerWidth + $(e).outerWidth();
119     });
120
121     $("body").addClass("compact-nav");
122
123     $("header").children(":visible").each(function (i, e) {
124       compactWidth = compactWidth + $(e).outerWidth();
125     });
126
127     $("body").removeClass("compact-nav");
128
129     $("header").removeClass("text-nowrap");
130     $("header nav.secondary > ul").removeClass("flex-nowrap");
131
132     updateHeader();
133
134     $(window).resize(updateHeader);
135     $(document).on("turbo:render", updateHeader);
136   }, 0);
137
138   $("#menu-icon").on("click", function (e) {
139     e.preventDefault();
140     $("header").toggleClass("closed");
141   });
142
143   $("nav.primary li a").on("click", function () {
144     $("header").toggleClass("closed");
145   });
146
147   $("#edit_tab")
148     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
149 });