]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
fbc76e4a8db29f442dccfeafd0904d51437f096e
[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 /*
21  * Called as the user scrolls/zooms around to maniplate 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 querystring = require("querystring-component"),
27         href = link.href.split(/[?#]/)[0],
28         args = querystring.parse(link.search.substring(1)),
29         editlink = $(link).hasClass("editlink");
30
31     delete args.node;
32     delete args.way;
33     delete args.relation;
34     delete args.changeset;
35
36     if (object && editlink) {
37       args[object.type] = object.id;
38     }
39
40     var query = querystring.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   var editDisabled = zoom < 13;
59   $("#edit_tab")
60     .tooltip({ placement: "bottom" })
61     .off("click.minzoom")
62     .on("click.minzoom", function () { return !editDisabled; })
63     .toggleClass("disabled", editDisabled)
64     .attr("data-original-title", editDisabled ?
65       I18n.t("javascripts.site.edit_disabled_tooltip") : "");
66 };
67
68 window.maximiseMap = function () {
69   $("#content").addClass("maximised");
70 };
71
72 window.minimiseMap = function () {
73   $("#content").removeClass("maximised");
74 };
75
76 $(document).ready(function () {
77   var headerWidth = 0,
78       compactWidth = 0;
79
80   function updateHeader() {
81     var windowWidth = $(window).width();
82
83     if (windowWidth < compactWidth) {
84       $("body").removeClass("compact").addClass("small");
85     } else if (windowWidth < headerWidth) {
86       $("body").addClass("compact").removeClass("small");
87     } else {
88       $("body").removeClass("compact").removeClass("small");
89     }
90   }
91
92   /*
93    * Chrome 60 and later seem to fire the "ready" callback
94    * before the DOM is fully ready causing us to measure the
95    * wrong sizes for the header elements - use a 0ms timeout
96    * to defer the measurement slightly as a workaround.
97    */
98   setTimeout(function () {
99     $("header").children(":visible").each(function (i, e) {
100       headerWidth = headerWidth + $(e).outerWidth();
101     });
102
103     $("body").addClass("compact");
104
105     $("header").children(":visible").each(function (i, e) {
106       compactWidth = compactWidth + $(e).outerWidth();
107     });
108
109     $("body").removeClass("compact");
110
111     updateHeader();
112
113     $(window).resize(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
133   if (application_data.user) {
134     OSM.user = application_data.user;
135
136     if (application_data.userHome) {
137       OSM.home = application_data.userHome;
138     }
139   }
140
141   if (application_data.location) {
142     OSM.location = application_data.location;
143   }
144 });