1 //= require @maptiler/maplibre-gl-omt-language
2 //= require maplibre/map
3 //= require maplibre/i18n
5 L.OSM.layers = function (options) {
6 const control = L.OSM.sidebarPane(options, "layers", "javascripts.map.layers.title", "javascripts.map.layers.header");
8 control.onAddPane = function (map, button, $ui, toggle) {
9 const layers = options.layers;
11 control.onContentLoaded = function () {
12 $ui.find(".base-layers>div").each(initBaseLayer);
15 control.loadContent();
17 function initBaseLayer() {
18 const [container, input, item] = this.children;
19 const layer = layers.find(l => l.options.layerId === container.dataset.layer);
20 input.checked = map.hasLayer(layer);
22 map.whenReady(function () {
29 const center = map.getCenter();
30 miniMap = new OSM.MapLibre.Map({
32 style: layer.options.style,
34 attributionControl: false,
36 zoomSnap: layer.options.isVectorStyle ? 0 : 1,
37 center: [center.lng, center.lat],
38 zoom: getZoomForMiniMap()
41 if (layer.options.layerId === "openmaptiles_osm") {
42 OSM.MapLibre.setOMTMapLanguage(miniMap);
45 map.on("moveend", moved);
49 map.off("moveend", moved);
54 const center = map.getCenter();
55 const zoom = getZoomForMiniMap();
56 miniMap.easeTo({ center: [center.lng, center.lat], zoom });
59 function getZoomForMiniMap() {
60 return Math.max(Math.floor(map.getZoom() - 3), -1);
64 $(input).on("click", function () {
65 for (const other of layers) {
66 if (other !== layer) {
67 map.removeLayer(other);
73 $(item).on("dblclick", toggle);
75 map.on("baselayerchange", function () {
76 input.checked = map.hasLayer(layer);
80 function initOverlays() {
81 $ui.find(".overlay-layers div.form-check").each(function () {
83 const layer = map[this.dataset.layerId];
84 const input = this.firstElementChild.firstElementChild;
85 $(item).tooltip("disable");
87 let checked = map.hasLayer(layer);
89 input.checked = checked;
91 $(input).on("change", function () {
92 checked = input.checked;
93 layer.cancelLoading?.();
98 map.removeLayer(layer);
99 $(`#layers-${name}-loading`).remove();
103 map.on("overlayadd overlayremove", function () {
104 input.checked = map.hasLayer(layer);
107 map.on("zoomend", function () {
108 const disabled = map.getBounds().getSize() >= item.dataset.maxArea;
109 input.disabled = disabled;
111 if (disabled && input.checked) {
114 } else if (!disabled && !input.checked && checked) {
118 item.classList.toggle("disabled", disabled);
119 $(item).tooltip(disabled ? "enable" : "disable");