]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Merge remote-tracking branch 'upstream/pull/3736'
[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 //= require bs-custom-file-input
20 //= require bs-custom-file-input-init
21
22 /*
23  * Called as the user scrolls/zooms around to manipulate hrefs of the
24  * view tab and various other links
25  */
26 window.updateLinks = function (loc, zoom, layers, object) {
27   $(".geolink").each(function (index, link) {
28     var href = link.href.split(/[?#]/)[0],
29         args = Qs.parse(link.search.substring(1)),
30         editlink = $(link).hasClass("editlink");
31
32     delete args.node;
33     delete args.way;
34     delete args.relation;
35     delete args.changeset;
36     delete args.note;
37
38     if (object && editlink) {
39       args[object.type] = object.id;
40     }
41
42     var query = Qs.stringify(args);
43     if (query) href += "?" + query;
44
45     args = {
46       lat: loc.lat,
47       lon: "lon" in loc ? loc.lon : loc.lng,
48       zoom: zoom
49     };
50
51     if (layers && !editlink) {
52       args.layers = layers;
53     }
54
55     href += OSM.formatHash(args);
56
57     link.href = href;
58   });
59
60   var editDisabled = zoom < 13;
61   $("#edit_tab")
62     .tooltip({ placement: "bottom" })
63     .attr("data-bs-original-title", editDisabled ?
64       I18n.t("javascripts.site.edit_disabled_tooltip") : "")
65     // Disable the button group and also the buttons to avoid
66     // inconsistent behaviour when zooming
67     .toggleClass("disabled", editDisabled)
68     .find("a")
69     .toggleClass("disabled", editDisabled);
70 };
71
72 window.maximiseMap = function () {
73   $("#content").addClass("maximised");
74 };
75
76 window.minimiseMap = function () {
77   $("#content").removeClass("maximised");
78 };
79
80 $(document).ready(function () {
81   var headerWidth = 0,
82       compactWidth = 0;
83
84   function updateHeader() {
85     var windowWidth = $(window).width();
86
87     if (windowWidth < compactWidth) {
88       $("body").removeClass("compact-nav").addClass("small-nav");
89     } else if (windowWidth < headerWidth) {
90       $("body").addClass("compact-nav").removeClass("small-nav");
91     } else {
92       $("body").removeClass("compact-nav").removeClass("small-nav");
93     }
94   }
95
96   /*
97    * Chrome 60 and later seem to fire the "ready" callback
98    * before the DOM is fully ready causing us to measure the
99    * wrong sizes for the header elements - use a 0ms timeout
100    * to defer the measurement slightly as a workaround.
101    */
102   setTimeout(function () {
103     $("header").children(":visible").each(function (i, e) {
104       headerWidth = headerWidth + $(e).outerWidth();
105     });
106
107     $("body").addClass("compact-nav");
108
109     $("header").children(":visible").each(function (i, e) {
110       compactWidth = compactWidth + $(e).outerWidth();
111     });
112
113     $("body").removeClass("compact-nav");
114
115     updateHeader();
116
117     $(window).resize(updateHeader);
118   }, 0);
119
120   $("#menu-icon").on("click", function (e) {
121     e.preventDefault();
122     $("header").toggleClass("closed");
123   });
124
125   $("nav.primary li a").on("click", function () {
126     $("header").toggleClass("closed");
127   });
128
129   var application_data = $("head").data();
130
131   I18n.default_locale = OSM.DEFAULT_LOCALE;
132   I18n.locale = application_data.locale;
133   I18n.fallbacks = true;
134
135   OSM.preferred_editor = application_data.preferredEditor;
136
137   if (application_data.user) {
138     OSM.user = application_data.user;
139
140     if (application_data.userHome) {
141       OSM.home = application_data.userHome;
142     }
143   }
144
145   if (application_data.location) {
146     OSM.location = application_data.location;
147   }
148 });