1 //= require @maptiler/maplibre-gl-omt-language
2 //= require maplibre/map
3 //= require maplibre/styles
4 //= require maplibre/i18n
6 L.OSM.layers = function (options) {
7 const control = L.OSM.sidebarPane(options, "layers", "javascripts.map.layers.title", "javascripts.map.layers.header");
9 control.onAddPane = function (map, button, $ui, toggle) {
10 const layers = options.layers;
12 control.onContentLoaded = function () {
13 $ui.find(".base-layers>div").each(initBaseLayer);
16 control.loadContent();
18 function initBaseLayer() {
19 const [container, input, item] = this.children;
20 const layer = layers.find(l => l.options.layerId === container.dataset.layer);
21 input.checked = map.hasLayer(layer);
23 map.whenReady(function () {
24 const styleId = layer.options.leafletOsmId;
25 const style = OSM.MapLibre.Styles[styleId](layer.options);
26 const layerDefinition = OSM.LAYER_DEFINITIONS.find(l => l.leafletOsmId === styleId || l.leafletOsmDarkId === styleId);
27 const miniMap = new OSM.MapLibre.Map({
31 attributionControl: false,
33 zoomSnap: layerDefinition.isVectorStyle ? 0 : 1
36 if (styleId === "OpenMapTiles") {
37 OSM.MapLibre.setOMTMapLanguage(miniMap);
47 map.on("moveend", moved);
51 map.off("moveend", moved);
58 function setView(animate = true) {
59 const center = map.getCenter();
60 const zoom = Math.max(Math.floor(map.getZoom() - 3), -1);
62 miniMap.easeTo({ center: [center.lng, center.lat], zoom });
64 miniMap.jumpTo({ center: [center.lng, center.lat], zoom });
69 $(input).on("click", function () {
70 for (const other of layers) {
71 if (other !== layer) {
72 map.removeLayer(other);
78 $(item).on("dblclick", toggle);
80 map.on("baselayerchange", function () {
81 input.checked = map.hasLayer(layer);
85 function initOverlays() {
86 $ui.find(".overlay-layers div.form-check").each(function () {
88 const layer = map[this.dataset.layerId];
89 const input = this.firstElementChild.firstElementChild;
90 $(item).tooltip("disable");
92 let checked = map.hasLayer(layer);
94 input.checked = checked;
96 $(input).on("change", function () {
97 checked = input.checked;
98 layer.cancelLoading?.();
103 map.removeLayer(layer);
104 $(`#layers-${name}-loading`).remove();
108 map.on("overlayadd overlayremove", function () {
109 input.checked = map.hasLayer(layer);
112 map.on("zoomend", function () {
113 const disabled = map.getBounds().getSize() >= item.dataset.maxArea;
114 input.disabled = disabled;
116 if (disabled && input.checked) {
119 } else if (!disabled && !input.checked && checked) {
123 item.classList.toggle("disabled", disabled);
124 $(item).tooltip(disabled ? "enable" : "disable");