]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Remove bs-custom-file-input javascript plugin
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery3
2 //= require jquery_ujs
3 //= require jquery.timers
4 //= require jquery.throttle-debounce
5 //= require js-cookie/dist/js.cookie
6 //= require popper
7 //= require bootstrap-sprockets
8 //= require osm
9 //= require leaflet/dist/leaflet-src
10 //= require leaflet.osm
11 //= require leaflet.map
12 //= require leaflet.zoom
13 //= require leaflet.locationfilter
14 //= require i18n
15 //= require oauth
16 //= require matomo
17 //= require richtext
18 //= require qs/dist/qs
19
20 /*
21  * Called as the user scrolls/zooms around to manipulate hrefs of the
22  * view tab and various other links
23  */
24 window.updateLinks = function (loc, zoom, layers, object) {
25   $(".geolink").each(function (index, link) {
26     var href = link.href.split(/[?#]/)[0],
27         args = Qs.parse(link.search.substring(1)),
28         editlink = $(link).hasClass("editlink");
29
30     delete args.node;
31     delete args.way;
32     delete args.relation;
33     delete args.changeset;
34     delete args.note;
35
36     if (object && editlink) {
37       args[object.type] = object.id;
38     }
39
40     var query = Qs.stringify(args);
41     if (query) href += "?" + query;
42
43     args = {
44       lat: loc.lat,
45       lon: "lon" in loc ? loc.lon : loc.lng,
46       zoom: zoom
47     };
48
49     if (layers && !editlink) {
50       args.layers = layers;
51     }
52
53     href += OSM.formatHash(args);
54
55     link.href = href;
56   });
57
58   // Disable the button group and also the buttons to avoid
59   // inconsistent behaviour when zooming
60   var editDisabled = zoom < 13;
61   $("#edit_tab")
62     .tooltip({ placement: "bottom" })
63     .tooltip(editDisabled ? "enable" : "disable")
64     .toggleClass("disabled", editDisabled)
65     .find("a")
66     .toggleClass("disabled", editDisabled);
67 };
68
69 $(document).ready(function () {
70   // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
71   // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
72   Turbo.session.drive = false;
73
74   var headerWidth = 0,
75       compactWidth = 0;
76
77   function updateHeader() {
78     var windowWidth = $(window).width();
79
80     if (windowWidth < compactWidth) {
81       $("body").removeClass("compact-nav").addClass("small-nav");
82     } else if (windowWidth < headerWidth) {
83       $("body").addClass("compact-nav").removeClass("small-nav");
84     } else {
85       $("body").removeClass("compact-nav").removeClass("small-nav");
86     }
87   }
88
89   /*
90    * Chrome 60 and later seem to fire the "ready" callback
91    * before the DOM is fully ready causing us to measure the
92    * wrong sizes for the header elements - use a 0ms timeout
93    * to defer the measurement slightly as a workaround.
94    */
95   setTimeout(function () {
96     $("header").children(":visible").each(function (i, e) {
97       headerWidth = headerWidth + $(e).outerWidth();
98     });
99
100     $("body").addClass("compact-nav");
101
102     $("header").children(":visible").each(function (i, e) {
103       compactWidth = compactWidth + $(e).outerWidth();
104     });
105
106     $("body").removeClass("compact-nav");
107
108     $("header").removeClass("text-nowrap");
109
110     updateHeader();
111
112     $(window).resize(updateHeader);
113     $(document).on("turbo:render", updateHeader);
114   }, 0);
115
116   $("#menu-icon").on("click", function (e) {
117     e.preventDefault();
118     $("header").toggleClass("closed");
119   });
120
121   $("nav.primary li a").on("click", function () {
122     $("header").toggleClass("closed");
123   });
124
125   var application_data = $("head").data();
126
127   I18n.default_locale = OSM.DEFAULT_LOCALE;
128   I18n.locale = application_data.locale;
129   I18n.fallbacks = true;
130
131   OSM.preferred_editor = application_data.preferredEditor;
132   OSM.preferred_languages = application_data.preferredLanguages;
133
134   if (application_data.user) {
135     OSM.user = application_data.user;
136
137     if (application_data.userHome) {
138       OSM.home = application_data.userHome;
139     }
140   }
141
142   if (application_data.location) {
143     OSM.location = application_data.location;
144   }
145
146   $("#edit_tab")
147     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
148 });