]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Lessen code repetition
[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   var headerWidth = 0,
94       compactWidth = 0;
95
96   function updateHeader() {
97     var windowWidth = $(window).width();
98
99     if (windowWidth < compactWidth) {
100       $("body").removeClass("compact-nav").addClass("small-nav");
101     } else if (windowWidth < headerWidth) {
102       $("body").addClass("compact-nav").removeClass("small-nav");
103     } else {
104       $("body").removeClass("compact-nav").removeClass("small-nav");
105     }
106   }
107
108   /*
109    * Chrome 60 and later seem to fire the "ready" callback
110    * before the DOM is fully ready causing us to measure the
111    * wrong sizes for the header elements - use a 0ms timeout
112    * to defer the measurement slightly as a workaround.
113    */
114   setTimeout(function () {
115     $("header").children(":visible").each(function (i, e) {
116       headerWidth = headerWidth + $(e).outerWidth();
117     });
118
119     $("body").addClass("compact-nav");
120
121     $("header").children(":visible").each(function (i, e) {
122       compactWidth = compactWidth + $(e).outerWidth();
123     });
124
125     $("body").removeClass("compact-nav");
126
127     $("header").removeClass("text-nowrap");
128     $("header nav.secondary > ul").removeClass("flex-nowrap");
129
130     updateHeader();
131
132     $(window).resize(updateHeader);
133     $(document).on("turbo:render", updateHeader);
134   }, 0);
135
136   $("#menu-icon").on("click", function (e) {
137     e.preventDefault();
138     $("header").toggleClass("closed");
139   });
140
141   $("nav.primary li a").on("click", function () {
142     $("header").toggleClass("closed");
143   });
144
145   $("#edit_tab")
146     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
147 });