]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.legend.js
Move /key to /panes/legend
[rails.git] / app / assets / javascripts / leaflet.legend.js
1 L.OSM.legend = function (options) {
2   const control = L.OSM.sidebarPane(options, "legend", "javascripts.key.title", "javascripts.key.title");
3
4   control.onAddPane = function (map, button, $ui) {
5     $ui
6       .on("show", () => {
7         map.on("zoomend", update);
8         update();
9       })
10       .on("hide", () => {
11         map.off("zoomend", update);
12       });
13
14     map.on("baselayerchange", updateButton);
15
16     updateButton();
17
18     control.onContentLoaded = update;
19     $ui.one("show", control.loadContent);
20
21     function updateButton() {
22       const disabled = !map.getMapBaseLayer().options.hasLegend;
23       button
24         .toggleClass("disabled", disabled)
25         .attr("data-bs-original-title",
26               OSM.i18n.t(disabled ?
27                 "javascripts.key.tooltip_disabled" :
28                 "javascripts.key.tooltip"));
29     }
30
31     function update() {
32       const layerId = map.getMapBaseLayerId(),
33             zoom = map.getZoom();
34
35       $("#legend [data-layer]").each(function () {
36         const data = $(this).data();
37         $(this).toggle(
38           layerId === data.layer &&
39           (!data.zoomMin || zoom >= data.zoomMin) &&
40           (!data.zoomMax || zoom <= data.zoomMax)
41         );
42       });
43     }
44   };
45
46   return control;
47 };