]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Merge remote-tracking branch 'upstream/pull/2134'
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery3
2 //= require jquery_ujs
3 //= require jquery.timers
4 //= require jquery.cookie
5 //= require jquery.throttle-debounce
6 //= require bootstrap.tooltip
7 //= require bootstrap.dropdown
8 //= require osm
9 //= require leaflet
10 //= require leaflet.osm
11 //= require leaflet.map
12 //= require leaflet.zoom
13 //= require leaflet.locationfilter
14 //= require i18n
15 //= require oauth
16 //= require piwik
17 //= require richtext
18 //= require querystring
19
20 var querystring = require('querystring-component');
21
22 /*
23  * Called as the user scrolls/zooms around to maniplate 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 = querystring.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 = querystring.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   $('#edit_tab')
61     .tooltip({placement: 'bottom'})
62     .off('click.minzoom')
63     .on('click.minzoom', function() { return !editDisabled; })
64     .toggleClass('disabled', editDisabled)
65     .attr('data-original-title', editDisabled ?
66       I18n.t('javascripts.site.edit_disabled_tooltip') : '');
67 };
68
69 window.maximiseMap = function () {
70   $("#content").addClass("maximised");
71 };
72
73 window.minimiseMap = function () {
74   $("#content").removeClass("maximised");
75 };
76
77 $(document).ready(function () {
78   var headerWidth = 0,
79       compactWidth = 0;
80
81   function updateHeader() {
82     var windowWidth = $(window).width();
83
84     if (windowWidth < compactWidth) {
85       $("body").removeClass("compact").addClass("small");
86     } else if (windowWidth < headerWidth) {
87       $("body").addClass("compact").removeClass("small");
88     } else {
89       $("body").removeClass("compact").removeClass("small");
90     }
91   }
92
93   /*
94    * Chrome 60 and later seem to fire the "ready" callback
95    * before the DOM is fully ready causing us to measure the
96    * wrong sizes for the header elements - use a 0ms timeout
97    * to defer the measurement slightly as a workaround.
98    */
99   setTimeout(function () {
100     $("header").children(":visible").each(function (i,e) {
101       headerWidth = headerWidth + $(e).outerWidth();
102     });
103
104     $("body").addClass("compact");
105
106     $("header").children(":visible").each(function (i,e) {
107       compactWidth = compactWidth + $(e).outerWidth();
108     });
109
110     $("body").removeClass("compact");
111
112     updateHeader();
113
114     $(window).resize(updateHeader);
115   }, 0);
116
117   $("#menu-icon").on("click", function(e) {
118     e.preventDefault();
119     $("header").toggleClass("closed");
120   });
121
122   $("nav.primary li a").on("click", function() {
123     $("header").toggleClass("closed");
124   });
125
126   var application_data = $("head").data();
127
128   I18n.default_locale = OSM.DEFAULT_LOCALE;
129   I18n.locale = application_data.locale;
130   I18n.fallbacks = true;
131
132   OSM.preferred_editor = application_data.preferredEditor;
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 });