1 L.OSM.layers = function (options) {
2 const control = L.OSM.sidebarPane(options, "layers", "javascripts.map.layers.title", "javascripts.map.layers.header");
4 control.onAddPane = function (map, button, $ui, toggle) {
5 const layers = options.layers;
7 control.onContentLoaded = function () {
8 $ui.find(".base-layers>div").each(initBaseLayer);
11 control.loadContent();
13 function initBaseLayer() {
14 const [container, input, item] = this.children;
15 const layer = layers.find(l => l.options.layerId === container.dataset.layer);
16 input.checked = map.hasLayer(layer);
18 map.whenReady(function () {
19 const miniMap = L.map(container, { attributionControl: false, zoomControl: false, keyboard: false })
20 .addLayer(new layer.constructor(layer.options));
22 miniMap.dragging.disable();
23 miniMap.touchZoom.disable();
24 miniMap.doubleClickZoom.disable();
25 miniMap.scrollWheelZoom.disable();
32 miniMap.invalidateSize();
33 setView({ animate: false });
34 map.on("moveend", moved);
38 map.off("moveend", moved);
45 function setView(options) {
46 miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0), options);
50 $(input).on("click", function () {
51 for (const other of layers) {
52 if (other !== layer) {
53 map.removeLayer(other);
59 $(item).on("dblclick", toggle);
61 map.on("baselayerchange", function () {
62 input.checked = map.hasLayer(layer);
66 function initOverlays() {
67 $ui.find(".overlay-layers div.form-check").each(function () {
69 const layer = map[this.dataset.layerId];
70 const input = this.firstElementChild.firstElementChild;
71 $(item).tooltip("disable");
73 let checked = map.hasLayer(layer);
75 input.checked = checked;
77 $(input).on("change", function () {
78 checked = input.checked;
79 if (layer.cancelLoading) {
80 layer.cancelLoading();
86 map.removeLayer(layer);
87 $(`#layers-${name}-loading`).remove();
91 map.on("overlayadd overlayremove", function () {
92 input.checked = map.hasLayer(layer);
95 map.on("zoomend", function () {
96 const disabled = map.getBounds().getSize() >= item.dataset.maxArea;
97 $(input).prop("disabled", disabled);
99 if (disabled && $(input).is(":checked")) {
100 $(input).prop("checked", false)
103 } else if (!disabled && !$(input).is(":checked") && checked) {
104 $(input).prop("checked", true)
109 .toggleClass("disabled", disabled)
110 .tooltip(disabled ? "enable" : "disable");