]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Merge pull request #4311 from tomhughes/query-locales
[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   // Disable the button group and also the buttons to avoid
61   // inconsistent behaviour when zooming
62   var editDisabled = zoom < 13;
63   $("#edit_tab")
64     .tooltip({ placement: "bottom" })
65     .tooltip(editDisabled ? "enable" : "disable")
66     .toggleClass("disabled", editDisabled)
67     .find("a")
68     .toggleClass("disabled", editDisabled);
69 };
70
71 $(document).ready(function () {
72   var headerWidth = 0,
73       compactWidth = 0;
74
75   function updateHeader() {
76     var windowWidth = $(window).width();
77
78     if (windowWidth < compactWidth) {
79       $("body").removeClass("compact-nav").addClass("small-nav");
80     } else if (windowWidth < headerWidth) {
81       $("body").addClass("compact-nav").removeClass("small-nav");
82     } else {
83       $("body").removeClass("compact-nav").removeClass("small-nav");
84     }
85   }
86
87   /*
88    * Chrome 60 and later seem to fire the "ready" callback
89    * before the DOM is fully ready causing us to measure the
90    * wrong sizes for the header elements - use a 0ms timeout
91    * to defer the measurement slightly as a workaround.
92    */
93   setTimeout(function () {
94     $("header").children(":visible").each(function (i, e) {
95       headerWidth = headerWidth + $(e).outerWidth();
96     });
97
98     $("body").addClass("compact-nav");
99
100     $("header").children(":visible").each(function (i, e) {
101       compactWidth = compactWidth + $(e).outerWidth();
102     });
103
104     $("body").removeClass("compact-nav");
105
106     updateHeader();
107
108     $(window).resize(updateHeader);
109   }, 0);
110
111   $("#menu-icon").on("click", function (e) {
112     e.preventDefault();
113     $("header").toggleClass("closed");
114   });
115
116   $("nav.primary li a").on("click", function () {
117     $("header").toggleClass("closed");
118   });
119
120   var application_data = $("head").data();
121
122   I18n.default_locale = OSM.DEFAULT_LOCALE;
123   I18n.locale = application_data.locale;
124   I18n.fallbacks = true;
125
126   OSM.preferred_editor = application_data.preferredEditor;
127   OSM.preferred_languages = application_data.preferredLanguages;
128
129   if (application_data.user) {
130     OSM.user = application_data.user;
131
132     if (application_data.userHome) {
133       OSM.home = application_data.userHome;
134     }
135   }
136
137   if (application_data.location) {
138     OSM.location = application_data.location;
139   }
140
141   $("#edit_tab")
142     .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));
143 });