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();
31 miniMap = new OSM.MapLibre.Map({
33 style: layer.options.style,
35 attributionControl: false,
37 zoomSnap: layer.options.isVectorStyle ? 0 : 1,
38 center: [center.lng, center.lat],
39 zoom: getZoomForMiniMap()
45 if (layer.options.layerId === "openmaptiles_osm") {
46 OSM.MapLibre.setOMTMapLanguage(miniMap);
49 map.on("moveend", moved);
53 // miniMap can be falsy if webgl is not supported
55 map.off("moveend", moved);
61 const center = map.getCenter();
62 const zoom = getZoomForMiniMap();
63 miniMap.easeTo({ center: [center.lng, center.lat], zoom });
66 function getZoomForMiniMap() {
67 return Math.max(Math.floor(map.getZoom() - 3), -1);
71 $(input).on("click", function () {
72 for (const other of layers) {
73 if (other !== layer) {
74 map.removeLayer(other);
80 $(item).on("dblclick", toggle);
82 map.on("baselayerchange", function () {
83 input.checked = map.hasLayer(layer);
87 function initOverlays() {
88 $ui.find(".overlay-layers div.form-check").each(function () {
90 const layer = map[this.dataset.layerId];
91 const input = this.firstElementChild.firstElementChild;
92 $(item).tooltip("disable");
94 let checked = map.hasLayer(layer);
96 input.checked = checked;
98 $(input).on("change", function () {
99 checked = input.checked;
100 layer.cancelLoading?.();
105 map.removeLayer(layer);
106 $(`#layers-${name}-loading`).remove();
110 map.on("overlayadd overlayremove", function () {
111 input.checked = map.hasLayer(layer);
114 map.on("zoomend", function () {
115 const disabled = map.getBounds().getSize() >= item.dataset.maxArea;
116 input.disabled = disabled;
118 if (disabled && input.checked) {
121 } else if (!disabled && !input.checked && checked) {
125 item.classList.toggle("disabled", disabled);
126 $(item).tooltip(disabled ? "enable" : "disable");