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 () {
23 const miniMap = new OSM.MapLibre.Map({
25 style: layer.options.style,
27 attributionControl: false,
29 zoomSnap: layer.options.isVectorStyle ? 0 : 1
32 if (layer.options.layerId === "openmaptiles_osm") {
33 OSM.MapLibre.setOMTMapLanguage(miniMap);
43 map.on("moveend", moved);
47 map.off("moveend", moved);
54 function setView(animate = true) {
55 const center = map.getCenter();
56 const zoom = Math.max(Math.floor(map.getZoom() - 3), -1);
58 miniMap.easeTo({ center: [center.lng, center.lat], zoom });
60 miniMap.jumpTo({ center: [center.lng, center.lat], zoom });
65 $(input).on("click", function () {
66 for (const other of layers) {
67 if (other !== layer) {
68 map.removeLayer(other);
74 $(item).on("dblclick", toggle);
76 map.on("baselayerchange", function () {
77 input.checked = map.hasLayer(layer);
81 function initOverlays() {
82 $ui.find(".overlay-layers div.form-check").each(function () {
84 const layer = map[this.dataset.layerId];
85 const input = this.firstElementChild.firstElementChild;
86 $(item).tooltip("disable");
88 let checked = map.hasLayer(layer);
90 input.checked = checked;
92 $(input).on("change", function () {
93 checked = input.checked;
94 layer.cancelLoading?.();
99 map.removeLayer(layer);
100 $(`#layers-${name}-loading`).remove();
104 map.on("overlayadd overlayremove", function () {
105 input.checked = map.hasLayer(layer);
108 map.on("zoomend", function () {
109 const disabled = map.getBounds().getSize() >= item.dataset.maxArea;
110 input.disabled = disabled;
112 if (disabled && input.checked) {
115 } else if (!disabled && !input.checked && checked) {
119 item.classList.toggle("disabled", disabled);
120 $(item).tooltip(disabled ? "enable" : "disable");