]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Move leaflet to be a yarn managed asset
[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 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 piwik
17 //= require richtext
18 //= require querystring
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 querystring = require("querystring-component"),
29         href = link.href.split(/[?#]/)[0],
30         args = querystring.parse(link.search.substring(1)),
31         editlink = $(link).hasClass("editlink");
32
33     delete args.node;
34     delete args.way;
35     delete args.relation;
36     delete args.changeset;
37
38     if (object && editlink) {
39       args[object.type] = object.id;
40     }
41
42     var query = querystring.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     .off("click.minzoom")
64     .on("click.minzoom", function () { return !editDisabled; })
65     .toggleClass("disabled", editDisabled)
66     .attr("data-original-title", editDisabled ?
67       I18n.t("javascripts.site.edit_disabled_tooltip") : "");
68 };
69
70 window.maximiseMap = function () {
71   $("#content").addClass("maximised");
72 };
73
74 window.minimiseMap = function () {
75   $("#content").removeClass("maximised");
76 };
77
78 $(document).ready(function () {
79   var headerWidth = 0,
80       compactWidth = 0;
81
82   function updateHeader() {
83     var windowWidth = $(window).width();
84
85     if (windowWidth < compactWidth) {
86       $("body").removeClass("compact-nav").addClass("small-nav");
87     } else if (windowWidth < headerWidth) {
88       $("body").addClass("compact-nav").removeClass("small-nav");
89     } else {
90       $("body").removeClass("compact-nav").removeClass("small-nav");
91     }
92   }
93
94   /*
95    * Chrome 60 and later seem to fire the "ready" callback
96    * before the DOM is fully ready causing us to measure the
97    * wrong sizes for the header elements - use a 0ms timeout
98    * to defer the measurement slightly as a workaround.
99    */
100   setTimeout(function () {
101     $("header").children(":visible").each(function (i, e) {
102       headerWidth = headerWidth + $(e).outerWidth();
103     });
104
105     $("body").addClass("compact-nav");
106
107     $("header").children(":visible").each(function (i, e) {
108       compactWidth = compactWidth + $(e).outerWidth();
109     });
110
111     $("body").removeClass("compact-nav");
112
113     updateHeader();
114
115     $(window).resize(updateHeader);
116   }, 0);
117
118   $("#menu-icon").on("click", function (e) {
119     e.preventDefault();
120     $("header").toggleClass("closed");
121   });
122
123   $("nav.primary li a").on("click", function () {
124     $("header").toggleClass("closed");
125   });
126
127   var application_data = $("head").data();
128
129   I18n.default_locale = OSM.DEFAULT_LOCALE;
130   I18n.locale = application_data.locale;
131   I18n.fallbacks = true;
132
133   OSM.preferred_editor = application_data.preferredEditor;
134
135   if (application_data.user) {
136     OSM.user = application_data.user;
137
138     if (application_data.userHome) {
139       OSM.home = application_data.userHome;
140     }
141   }
142
143   if (application_data.location) {
144     OSM.location = application_data.location;
145   }
146 });