]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Merge branch 'master' into tooltips-enable-disable
[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
37     if (object && editlink) {
38       args[object.type] = object.id;
39     }
40
41     var query = Qs.stringify(args);
42     if (query) href += "?" + query;
43
44     args = {
45       lat: loc.lat,
46       lon: "lon" in loc ? loc.lon : loc.lng,
47       zoom: zoom
48     };
49
50     if (layers && !editlink) {
51       args.layers = layers;
52     }
53
54     href += OSM.formatHash(args);
55
56     link.href = href;
57   });
58
59   var editDisabled = zoom < 13;
60   var editTab = $("#edit_tab");
61   editTab
62     // Disable the button group and also the buttons to avoid
63     // inconsistent behaviour when zooming
64     .toggleClass("disabled", editDisabled)
65     .find("a")
66     .toggleClass("disabled", editDisabled);
67   var editTooltip = bootstrap.Tooltip.getOrCreateInstance(editTab[0], { placement: "bottom" });
68   if (editDisabled) {
69     editTooltip.enable();
70   } else {
71     editTooltip.disable();
72   }
73 };
74
75 window.maximiseMap = function () {
76   $("#content").addClass("maximised");
77 };
78
79 window.minimiseMap = function () {
80   $("#content").removeClass("maximised");
81 };
82
83 $(document).ready(function () {
84   $("#edit_tab")
85     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
86
87   var headerWidth = 0,
88       compactWidth = 0;
89
90   function updateHeader() {
91     var windowWidth = $(window).width();
92
93     if (windowWidth < compactWidth) {
94       $("body").removeClass("compact-nav").addClass("small-nav");
95     } else if (windowWidth < headerWidth) {
96       $("body").addClass("compact-nav").removeClass("small-nav");
97     } else {
98       $("body").removeClass("compact-nav").removeClass("small-nav");
99     }
100   }
101
102   /*
103    * Chrome 60 and later seem to fire the "ready" callback
104    * before the DOM is fully ready causing us to measure the
105    * wrong sizes for the header elements - use a 0ms timeout
106    * to defer the measurement slightly as a workaround.
107    */
108   setTimeout(function () {
109     $("header").children(":visible").each(function (i, e) {
110       headerWidth = headerWidth + $(e).outerWidth();
111     });
112
113     $("body").addClass("compact-nav");
114
115     $("header").children(":visible").each(function (i, e) {
116       compactWidth = compactWidth + $(e).outerWidth();
117     });
118
119     $("body").removeClass("compact-nav");
120
121     updateHeader();
122
123     $(window).resize(updateHeader);
124   }, 0);
125
126   $("#menu-icon").on("click", function (e) {
127     e.preventDefault();
128     $("header").toggleClass("closed");
129   });
130
131   $("nav.primary li a").on("click", function () {
132     $("header").toggleClass("closed");
133   });
134
135   var application_data = $("head").data();
136
137   I18n.default_locale = OSM.DEFAULT_LOCALE;
138   I18n.locale = application_data.locale;
139   I18n.fallbacks = true;
140
141   OSM.preferred_editor = application_data.preferredEditor;
142
143   if (application_data.user) {
144     OSM.user = application_data.user;
145
146     if (application_data.userHome) {
147       OSM.home = application_data.userHome;
148     }
149   }
150
151   if (application_data.location) {
152     OSM.location = application_data.location;
153   }
154 });