3 //= require jquery.throttle-debounce
4 //= require js-cookie/dist/js.cookie
6 //= require bootstrap-sprockets
8 //= require leaflet/dist/leaflet-src
9 //= require leaflet.osm
10 //= require leaflet.map
11 //= require leaflet.zoom
12 //= require leaflet.locationfilter
18 const application_data = $("head").data();
20 I18n.default_locale = OSM.DEFAULT_LOCALE;
21 I18n.locale = application_data.locale;
22 I18n.fallbacks = true;
24 OSM.preferred_editor = application_data.preferredEditor;
25 OSM.preferred_languages = application_data.preferredLanguages;
27 if (application_data.user) {
28 OSM.user = application_data.user;
30 if (application_data.userHome) {
31 OSM.home = application_data.userHome;
35 if (application_data.location) {
36 OSM.location = application_data.location;
41 * Called as the user scrolls/zooms around to manipulate hrefs of the
42 * view tab and various other links
44 window.updateLinks = function (loc, zoom, layers, object) {
45 $(".geolink").each(function (index, link) {
46 let href = link.href.split(/[?#]/)[0];
47 const queryArgs = new URLSearchParams(link.search),
48 editlink = $(link).hasClass("editlink");
50 for (const arg of ["node", "way", "relation", "changeset", "note"]) {
51 queryArgs.delete(arg);
54 if (object && editlink) {
55 queryArgs.set(object.type, object.id);
58 const query = queryArgs.toString();
59 if (query) href += "?" + query;
63 lon: "lon" in loc ? loc.lon : loc.lng,
67 if (layers && !editlink) {
68 hashArgs.layers = layers;
71 href += OSM.formatHash(hashArgs);
76 // Disable the button group and also the buttons to avoid
77 // inconsistent behaviour when zooming
78 const editDisabled = zoom < 13;
80 .tooltip({ placement: "bottom" })
81 .tooltip(editDisabled ? "enable" : "disable")
82 .toggleClass("disabled", editDisabled)
84 .toggleClass("disabled", editDisabled);
88 // NB: Turns Turbo Drive off by default. Turbo Drive must be opt-in on a per-link and per-form basis
89 // See https://turbo.hotwired.dev/reference/drive#turbo.session.drive
90 Turbo.session.drive = false;
92 const $expandedSecondaryMenu = $("header nav.secondary > ul"),
93 $collapsedSecondaryMenu = $("#compact-secondary-nav > ul"),
94 secondaryMenuItems = [],
95 breakpointWidth = 768;
96 let moreItemWidth = 0;
99 OSM.csrf[($("meta[name=csrf-param]").attr("content"))] = $("meta[name=csrf-token]").attr("content");
101 function updateHeader() {
102 const windowWidth = $(window).width();
104 if (windowWidth < breakpointWidth) {
105 $("body").addClass("small-nav");
106 expandAllSecondaryMenuItems();
108 $("body").removeClass("small-nav");
109 const availableWidth = $expandedSecondaryMenu.width();
110 secondaryMenuItems.forEach(function (item) {
113 let runningWidth = 0,
116 for (; i < secondaryMenuItems.length; i++) {
117 runningWidth += secondaryMenuItems[i][1];
118 if (i < secondaryMenuItems.length - 1) {
119 requiredWidth = runningWidth + moreItemWidth;
121 requiredWidth = runningWidth;
123 if (requiredWidth > availableWidth) {
126 expandSecondaryMenuItem($(secondaryMenuItems[i][0]));
128 for (; i < secondaryMenuItems.length; i++) {
129 collapseSecondaryMenuItem($(secondaryMenuItems[i][0]));
134 function expandAllSecondaryMenuItems() {
135 secondaryMenuItems.forEach(function (item) {
136 expandSecondaryMenuItem($(item[0]));
140 function expandSecondaryMenuItem($item) {
142 .removeClass("dropdown-item")
143 .addClass("nav-link")
144 .addClass(function () {
145 return $(this).hasClass("active") ? "text-secondary-emphasis" : "text-secondary";
147 $item.addClass("nav-item").insertBefore("#compact-secondary-nav");
148 toggleCompactSecondaryNav();
151 function collapseSecondaryMenuItem($item) {
153 .addClass("dropdown-item")
154 .removeClass("nav-link text-secondary text-secondary-emphasis");
155 $item.removeClass("nav-item").appendTo($collapsedSecondaryMenu);
156 toggleCompactSecondaryNav();
159 function toggleCompactSecondaryNav() {
160 $("#compact-secondary-nav").toggle(
161 $collapsedSecondaryMenu.find("li").length > 0
166 * Chrome 60 and later seem to fire the "ready" callback
167 * before the DOM is fully ready causing us to measure the
168 * wrong sizes for the header elements - use a 0ms timeout
169 * to defer the measurement slightly as a workaround.
171 setTimeout(function () {
172 $expandedSecondaryMenu.find("li:not(#compact-secondary-nav)").each(function () {
173 secondaryMenuItems.push([this, $(this).width()]);
175 moreItemWidth = $("#compact-secondary-nav").width();
179 $(window).resize(updateHeader);
180 $(document).on("turbo:render", updateHeader);
183 $("#menu-icon").on("click", function (e) {
185 $("header").toggleClass("closed");
188 $("nav.primary li a").on("click", function () {
189 $("header").toggleClass("closed");
193 .attr("title", I18n.t("javascripts.site.edit_disabled_tooltip"));