maplibre-gl-layer-control 0.17.5 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -162,6 +162,90 @@ class CustomLayerRegistry {
162
162
  }
163
163
  return false;
164
164
  }
165
+ /**
166
+ * Get the layer groups of every adapter. When two adapters define the same
167
+ * group ID, the first registered adapter's group wins.
168
+ * @returns The groups, in adapter order
169
+ */
170
+ getGroups() {
171
+ const groups = [];
172
+ const seen = /* @__PURE__ */ new Set();
173
+ this.adapters.forEach((adapter) => {
174
+ var _a;
175
+ for (const group of ((_a = adapter.getGroups) == null ? void 0 : _a.call(adapter)) ?? []) {
176
+ if (seen.has(group.id)) continue;
177
+ seen.add(group.id);
178
+ groups.push(group);
179
+ }
180
+ });
181
+ return groups;
182
+ }
183
+ /**
184
+ * Get the ID of the group a custom layer belongs to.
185
+ * @param layerId The layer ID
186
+ * @returns The group ID, or undefined when the layer is ungrouped
187
+ */
188
+ getLayerGroupId(layerId) {
189
+ var _a;
190
+ const adapter = this.getAdapterForLayer(layerId);
191
+ return (_a = adapter == null ? void 0 : adapter.getLayerGroupId) == null ? void 0 : _a.call(adapter, layerId);
192
+ }
193
+ /**
194
+ * Get the adapter that defines a group.
195
+ * @param groupId The group ID
196
+ * @returns The adapter or null if no adapter defines the group
197
+ */
198
+ getAdapterForGroup(groupId) {
199
+ var _a;
200
+ for (const adapter of this.adapters.values()) {
201
+ if ((_a = adapter.getGroups) == null ? void 0 : _a.call(adapter).some((group) => group.id === groupId)) {
202
+ return adapter;
203
+ }
204
+ }
205
+ return null;
206
+ }
207
+ /**
208
+ * Set visibility of a group.
209
+ * @param groupId The group ID
210
+ * @param visible Whether the group should be visible
211
+ * @returns true if the operation was handled by an adapter
212
+ */
213
+ setGroupVisibility(groupId, visible) {
214
+ const adapter = this.getAdapterForGroup(groupId);
215
+ if (adapter == null ? void 0 : adapter.setGroupVisibility) {
216
+ adapter.setGroupVisibility(groupId, visible);
217
+ return true;
218
+ }
219
+ return false;
220
+ }
221
+ /**
222
+ * Set opacity of a group.
223
+ * @param groupId The group ID
224
+ * @param opacity The opacity value (0-1)
225
+ * @returns true if the operation was handled by an adapter
226
+ */
227
+ setGroupOpacity(groupId, opacity) {
228
+ const adapter = this.getAdapterForGroup(groupId);
229
+ if (adapter == null ? void 0 : adapter.setGroupOpacity) {
230
+ adapter.setGroupOpacity(groupId, opacity);
231
+ return true;
232
+ }
233
+ return false;
234
+ }
235
+ /**
236
+ * Record that a group was collapsed or expanded.
237
+ * @param groupId The group ID
238
+ * @param collapsed Whether the group is now collapsed
239
+ * @returns true if the operation was handled by an adapter
240
+ */
241
+ setGroupCollapsed(groupId, collapsed) {
242
+ const adapter = this.getAdapterForGroup(groupId);
243
+ if (adapter == null ? void 0 : adapter.setGroupCollapsed) {
244
+ adapter.setGroupCollapsed(groupId, collapsed);
245
+ return true;
246
+ }
247
+ return false;
248
+ }
165
249
  /**
166
250
  * Subscribe to layer changes across all adapters.
167
251
  * @param callback Function called when layers are added or removed
@@ -707,6 +791,10 @@ class LayerControl {
707
791
  __publicField(this, "customLayerUnsubscribe", null);
708
792
  __publicField(this, "removedCustomLayerIds", /* @__PURE__ */ new Set());
709
793
  __publicField(this, "nativeLayerGroups", /* @__PURE__ */ new Map());
794
+ /** Layer groups from the custom layer adapters, as of the last panel build */
795
+ __publicField(this, "groupStates", /* @__PURE__ */ new Map());
796
+ /** Group IDs, parents, and memberships the panel was last built from */
797
+ __publicField(this, "groupStructureKey", "");
710
798
  __publicField(this, "basemapStyleUrl", null);
711
799
  __publicField(this, "basemapLayerIds", null);
712
800
  __publicField(this, "widthFrame", null);
@@ -1433,6 +1521,9 @@ class LayerControl {
1433
1521
  * Set visibility of all layers
1434
1522
  */
1435
1523
  setAllLayersVisibility(visible) {
1524
+ this.groupStates.forEach((_group, groupId) => {
1525
+ this.setGroupVisibility(groupId, visible);
1526
+ });
1436
1527
  Object.keys(this.state.layerStates).forEach((layerId) => {
1437
1528
  this.toggleLayerVisibility(layerId, visible);
1438
1529
  const itemEl = this.panel.querySelector(`[data-layer-id="${layerId}"]`);
@@ -1579,7 +1670,10 @@ class LayerControl {
1579
1670
  * Build layer items (called initially and when layers change)
1580
1671
  */
1581
1672
  buildLayerItems() {
1582
- const existingItems = this.panel.querySelectorAll(".layer-control-item");
1673
+ var _a;
1674
+ const existingItems = this.panel.querySelectorAll(
1675
+ ".layer-control-item, .layer-control-group"
1676
+ );
1583
1677
  existingItems.forEach((item) => item.remove());
1584
1678
  this.styleEditors.clear();
1585
1679
  const orderedLayerIds = this.getUserLayerIdsInMapOrder();
@@ -1592,20 +1686,302 @@ class LayerControl {
1592
1686
  if (this.state.layerStates["Background"]) {
1593
1687
  orderedLayerIds.push("Background");
1594
1688
  }
1689
+ this.groupStates = new Map(
1690
+ (((_a = this.customLayerRegistry) == null ? void 0 : _a.getGroups()) ?? []).map((group) => [
1691
+ group.id,
1692
+ { ...group }
1693
+ ])
1694
+ );
1695
+ this.groupStructureKey = this.computeGroupStructureKey(this.groupStates);
1696
+ const groupContainers = /* @__PURE__ */ new Map();
1595
1697
  orderedLayerIds.forEach((layerId) => {
1596
1698
  const state = this.state.layerStates[layerId];
1597
1699
  if (!state) {
1598
1700
  return;
1599
1701
  }
1600
1702
  if (layerId === "Background" || this.targetLayers.length === 0 || this.targetLayers.includes(layerId)) {
1601
- this.addLayerItem(layerId, state);
1703
+ const container = layerId === "Background" ? this.panel : this.getGroupContainer(
1704
+ this.getLayerGroupIdOf(layerId),
1705
+ groupContainers,
1706
+ /* @__PURE__ */ new Set()
1707
+ );
1708
+ this.addLayerItem(layerId, state, container);
1709
+ }
1710
+ });
1711
+ }
1712
+ /**
1713
+ * Get the ID of the group a layer belongs to, ignoring a group ID that no
1714
+ * adapter defines.
1715
+ * @param layerId The layer ID
1716
+ * @returns The group ID, or undefined when the layer is ungrouped
1717
+ */
1718
+ getLayerGroupIdOf(layerId) {
1719
+ var _a;
1720
+ if (this.groupStates.size === 0) return void 0;
1721
+ const groupId = (_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerGroupId(layerId);
1722
+ return groupId !== void 0 && this.groupStates.has(groupId) ? groupId : void 0;
1723
+ }
1724
+ /**
1725
+ * Get the element a group's rows go into, creating the group (and any
1726
+ * enclosing groups) on first use.
1727
+ * @param groupId The group ID, or undefined for the panel's top level
1728
+ * @param containers Group child containers already created in this build
1729
+ * @param visiting Groups on the current parent chain, to stop on a cycle
1730
+ * @returns The element to append the group's rows to
1731
+ */
1732
+ getGroupContainer(groupId, containers, visiting) {
1733
+ if (groupId === void 0) return this.panel;
1734
+ const existing = containers.get(groupId);
1735
+ if (existing) return existing;
1736
+ const group = this.groupStates.get(groupId);
1737
+ if (!group || visiting.has(groupId)) return this.panel;
1738
+ visiting.add(groupId);
1739
+ const parentId = group.parentId !== void 0 && this.groupStates.has(group.parentId) ? group.parentId : void 0;
1740
+ const parent = this.getGroupContainer(parentId, containers, visiting);
1741
+ const { element, children } = this.createGroupItem(group);
1742
+ parent.appendChild(element);
1743
+ containers.set(groupId, children);
1744
+ return children;
1745
+ }
1746
+ /**
1747
+ * Summarize the group structure the panel is built from: each group's ID
1748
+ * and parent, and each layer's group. A change to any of these needs a
1749
+ * rebuild; a change to a group's name, visibility, opacity, or collapsed
1750
+ * state does not.
1751
+ * @param groups The groups to summarize
1752
+ * @returns A string that changes whenever the structure does
1753
+ */
1754
+ computeGroupStructureKey(groups) {
1755
+ if (groups.size === 0) return "";
1756
+ const memberships = Object.keys(this.state.layerStates).filter((layerId) => layerId !== "Background").map((layerId) => {
1757
+ var _a;
1758
+ return [
1759
+ layerId,
1760
+ ((_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerGroupId(layerId)) ?? null
1761
+ ];
1762
+ });
1763
+ return JSON.stringify({
1764
+ groups: Array.from(groups.values(), (group) => [
1765
+ group.id,
1766
+ group.parentId ?? null
1767
+ ]),
1768
+ memberships
1769
+ });
1770
+ }
1771
+ /**
1772
+ * Create the element for a layer group: a header row with a collapse
1773
+ * toggle, visibility checkbox, name, and opacity slider, followed by a
1774
+ * container for the group's layers and nested groups.
1775
+ * @param group The group to render
1776
+ * @returns The group element and its child container
1777
+ */
1778
+ createGroupItem(group) {
1779
+ const element = document.createElement("div");
1780
+ element.className = "layer-control-group";
1781
+ element.setAttribute("data-group-id", group.id);
1782
+ const row = document.createElement("div");
1783
+ row.className = "layer-control-row layer-control-group-row";
1784
+ if (this.enableDragAndDrop) {
1785
+ row.appendChild(this.createDisabledDragHandle());
1786
+ }
1787
+ const checkbox = document.createElement("input");
1788
+ checkbox.type = "checkbox";
1789
+ checkbox.className = "layer-control-checkbox layer-control-group-checkbox";
1790
+ checkbox.addEventListener("change", () => {
1791
+ this.setGroupVisibility(group.id, checkbox.checked);
1792
+ });
1793
+ row.appendChild(checkbox);
1794
+ if (this.showLayerSymbol) {
1795
+ const symbol = document.createElement("span");
1796
+ symbol.className = "layer-control-symbol layer-control-group-symbol";
1797
+ symbol.innerHTML = `<svg viewBox="0 0 16 16" aria-hidden="true">
1798
+ <path d="M1.5 3.5h4.5l1.5 1.5h7v8.5h-13z" fill="#f2c94c" stroke="#b8902a" stroke-width="1"/>
1799
+ </svg>`;
1800
+ symbol.title = "Layer group";
1801
+ row.appendChild(symbol);
1802
+ }
1803
+ const name = document.createElement("span");
1804
+ name.className = "layer-control-name layer-control-group-name";
1805
+ row.appendChild(name);
1806
+ if (this.showOpacitySlider) {
1807
+ const opacity = document.createElement("input");
1808
+ opacity.type = "range";
1809
+ opacity.className = "layer-control-opacity layer-control-group-opacity";
1810
+ opacity.min = "0";
1811
+ opacity.max = "1";
1812
+ opacity.step = "0.01";
1813
+ opacity.addEventListener("mousedown", () => {
1814
+ this.state.userInteractingWithSlider = true;
1815
+ });
1816
+ opacity.addEventListener("mouseup", () => {
1817
+ this.state.userInteractingWithSlider = false;
1818
+ });
1819
+ opacity.addEventListener("input", () => {
1820
+ this.setGroupOpacity(group.id, parseFloat(opacity.value));
1821
+ });
1822
+ row.appendChild(opacity);
1823
+ }
1824
+ const toggle = document.createElement("button");
1825
+ toggle.type = "button";
1826
+ toggle.className = "layer-control-group-toggle";
1827
+ toggle.innerHTML = `<svg viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
1828
+ <path d="M6 4l4 4-4 4z"/>
1829
+ </svg>`;
1830
+ toggle.addEventListener("click", (e) => {
1831
+ var _a;
1832
+ e.stopPropagation();
1833
+ this.setGroupCollapsed(group.id, !((_a = this.groupStates.get(group.id)) == null ? void 0 : _a.collapsed));
1834
+ });
1835
+ row.appendChild(toggle);
1836
+ const children = document.createElement("div");
1837
+ children.className = "layer-control-group-children";
1838
+ element.appendChild(row);
1839
+ element.appendChild(children);
1840
+ this.updateGroupElement(element, group);
1841
+ return { element, children };
1842
+ }
1843
+ /**
1844
+ * Show a group's state on its element.
1845
+ * @param element The group element
1846
+ * @param group The group state to show
1847
+ */
1848
+ updateGroupElement(element, group) {
1849
+ element.classList.toggle("collapsed", group.collapsed);
1850
+ element.classList.toggle("layer-control-group-hidden", !group.visible);
1851
+ const row = element.querySelector(":scope > .layer-control-group-row");
1852
+ if (!row) return;
1853
+ const toggle = row.querySelector(
1854
+ ".layer-control-group-toggle"
1855
+ );
1856
+ if (toggle) {
1857
+ toggle.setAttribute("aria-expanded", String(!group.collapsed));
1858
+ toggle.title = group.collapsed ? "Expand group" : "Collapse group";
1859
+ toggle.setAttribute(
1860
+ "aria-label",
1861
+ `${group.collapsed ? "Expand" : "Collapse"} ${group.name}`
1862
+ );
1863
+ }
1864
+ const checkbox = row.querySelector(
1865
+ ".layer-control-group-checkbox"
1866
+ );
1867
+ if (checkbox) checkbox.checked = group.visible;
1868
+ const name = row.querySelector(".layer-control-group-name");
1869
+ if (name) {
1870
+ name.textContent = group.name;
1871
+ name.title = group.name;
1872
+ }
1873
+ const opacity = row.querySelector(
1874
+ ".layer-control-group-opacity"
1875
+ );
1876
+ if (opacity) {
1877
+ opacity.value = String(group.opacity);
1878
+ opacity.title = `Opacity: ${Math.round(group.opacity * 100)}%`;
1879
+ }
1880
+ }
1881
+ /**
1882
+ * Get the element of a rendered group.
1883
+ * @param groupId The group ID
1884
+ * @returns The group element, or null when the group is not rendered
1885
+ */
1886
+ getGroupElement(groupId) {
1887
+ const groups = this.panel.querySelectorAll(".layer-control-group");
1888
+ for (const element of Array.from(groups)) {
1889
+ if (element.dataset.groupId === groupId) {
1890
+ return element;
1602
1891
  }
1892
+ }
1893
+ return null;
1894
+ }
1895
+ /**
1896
+ * Remove group elements left without any layer row, e.g. after their last
1897
+ * layer was removed.
1898
+ */
1899
+ pruneEmptyGroups() {
1900
+ const groups = Array.from(this.panel.querySelectorAll(".layer-control-group"));
1901
+ for (const element of groups.reverse()) {
1902
+ if (!element.querySelector(".layer-control-item")) element.remove();
1903
+ }
1904
+ }
1905
+ /**
1906
+ * Set a group's visibility from the panel and report it to its adapter.
1907
+ * @param groupId The group ID
1908
+ * @param visible Whether the group should be visible
1909
+ */
1910
+ setGroupVisibility(groupId, visible) {
1911
+ var _a;
1912
+ const group = this.groupStates.get(groupId);
1913
+ if (!group) return;
1914
+ group.visible = visible;
1915
+ const element = this.getGroupElement(groupId);
1916
+ if (element) this.updateGroupElement(element, group);
1917
+ this.state.isStyleOperationInProgress = true;
1918
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupVisibility(groupId, visible);
1919
+ setTimeout(() => {
1920
+ this.state.isStyleOperationInProgress = false;
1921
+ }, 200);
1922
+ }
1923
+ /**
1924
+ * Set a group's opacity from the panel and report it to its adapter.
1925
+ * @param groupId The group ID
1926
+ * @param opacity The opacity value (0-1)
1927
+ */
1928
+ setGroupOpacity(groupId, opacity) {
1929
+ var _a;
1930
+ const group = this.groupStates.get(groupId);
1931
+ if (!group) return;
1932
+ group.opacity = opacity;
1933
+ const element = this.getGroupElement(groupId);
1934
+ if (element) this.updateGroupElement(element, group);
1935
+ this.state.isStyleOperationInProgress = true;
1936
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupOpacity(groupId, opacity);
1937
+ setTimeout(() => {
1938
+ this.state.isStyleOperationInProgress = false;
1939
+ }, 200);
1940
+ }
1941
+ /**
1942
+ * Collapse or expand a group in the panel and report it to its adapter.
1943
+ * @param groupId The group ID
1944
+ * @param collapsed Whether the group should be collapsed
1945
+ */
1946
+ setGroupCollapsed(groupId, collapsed) {
1947
+ var _a;
1948
+ const group = this.groupStates.get(groupId);
1949
+ if (!group) return;
1950
+ group.collapsed = collapsed;
1951
+ const element = this.getGroupElement(groupId);
1952
+ if (element) this.updateGroupElement(element, group);
1953
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupCollapsed(groupId, collapsed);
1954
+ }
1955
+ /**
1956
+ * Re-read the layer groups from the custom layer adapters. Call this after
1957
+ * groups change outside the control. The panel is rebuilt when a group was
1958
+ * added, removed, or re-parented, or a layer changed group; otherwise the
1959
+ * group rows' name, visibility, opacity, and collapsed state are updated in
1960
+ * place.
1961
+ */
1962
+ refreshGroups() {
1963
+ var _a;
1964
+ if (!this.panel) return;
1965
+ const next = new Map(
1966
+ (((_a = this.customLayerRegistry) == null ? void 0 : _a.getGroups()) ?? []).map((group) => [
1967
+ group.id,
1968
+ { ...group }
1969
+ ])
1970
+ );
1971
+ if (this.computeGroupStructureKey(next) !== this.groupStructureKey) {
1972
+ this.buildLayerItems();
1973
+ return;
1974
+ }
1975
+ this.groupStates = next;
1976
+ next.forEach((group, groupId) => {
1977
+ const element = this.getGroupElement(groupId);
1978
+ if (element) this.updateGroupElement(element, group);
1603
1979
  });
1604
1980
  }
1605
1981
  /**
1606
1982
  * Add a single layer item to the panel
1607
1983
  */
1608
- addLayerItem(layerId, state) {
1984
+ addLayerItem(layerId, state, container = this.panel) {
1609
1985
  const item = document.createElement("div");
1610
1986
  item.className = "layer-control-item";
1611
1987
  item.setAttribute("data-layer-id", layerId);
@@ -1689,7 +2065,7 @@ class LayerControl {
1689
2065
  this.showContextMenu(layerId, e.clientX, e.clientY);
1690
2066
  });
1691
2067
  }
1692
- this.panel.appendChild(item);
2068
+ container.appendChild(item);
1693
2069
  }
1694
2070
  /**
1695
2071
  * Create a symbol element for a layer
@@ -3586,6 +3962,8 @@ class LayerControl {
3586
3962
  }
3587
3963
  if (layersAdded) {
3588
3964
  this.buildLayerItems();
3965
+ } else {
3966
+ this.pruneEmptyGroups();
3589
3967
  }
3590
3968
  } catch (error) {
3591
3969
  console.warn("Failed to check for new layers:", error);
@@ -3955,6 +4333,10 @@ class LayerControl {
3955
4333
  */
3956
4334
  moveLayerUp(layerId) {
3957
4335
  var _a, _b;
4336
+ if (this.groupStates.size > 0) {
4337
+ this.moveLayerWithinGroup(layerId, "up");
4338
+ return;
4339
+ }
3958
4340
  const layerIds = this.getUserLayerIdsInMapOrder();
3959
4341
  const index = layerIds.indexOf(layerId);
3960
4342
  if (index <= 0) return;
@@ -3989,6 +4371,10 @@ class LayerControl {
3989
4371
  */
3990
4372
  moveLayerToTop(layerId) {
3991
4373
  var _a, _b;
4374
+ if (this.groupStates.size > 0) {
4375
+ this.moveLayerWithinGroup(layerId, "top");
4376
+ return;
4377
+ }
3992
4378
  const layerIds = this.getUserLayerIdsInMapOrder();
3993
4379
  const index = layerIds.indexOf(layerId);
3994
4380
  if (index <= 0) return;
@@ -4020,6 +4406,10 @@ class LayerControl {
4020
4406
  */
4021
4407
  moveLayerDown(layerId) {
4022
4408
  var _a, _b;
4409
+ if (this.groupStates.size > 0) {
4410
+ this.moveLayerWithinGroup(layerId, "down");
4411
+ return;
4412
+ }
4023
4413
  const layerIds = this.getUserLayerIdsInMapOrder();
4024
4414
  const index = layerIds.indexOf(layerId);
4025
4415
  if (index < 0 || index >= layerIds.length - 1) return;
@@ -4056,6 +4446,10 @@ class LayerControl {
4056
4446
  */
4057
4447
  moveLayerToBottom(layerId) {
4058
4448
  var _a, _b;
4449
+ if (this.groupStates.size > 0) {
4450
+ this.moveLayerWithinGroup(layerId, "bottom");
4451
+ return;
4452
+ }
4059
4453
  const layerIds = this.getUserLayerIdsInMapOrder();
4060
4454
  if (layerIds.length <= 1) return;
4061
4455
  const index = layerIds.indexOf(layerId);
@@ -4090,6 +4484,82 @@ class LayerControl {
4090
4484
  this.buildLayerItems();
4091
4485
  (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
4092
4486
  }
4487
+ /**
4488
+ * Move a layer within its own group (or among the top-level rows), so the
4489
+ * context-menu moves never carry it out of its group. A nested group counts
4490
+ * as one block: moving up or down steps past the whole block.
4491
+ * @param layerId The layer ID
4492
+ * @param direction Where to move the layer within its group
4493
+ */
4494
+ moveLayerWithinGroup(layerId, direction) {
4495
+ var _a;
4496
+ const order = this.getUserLayerIdsInMapOrder();
4497
+ const groupId = this.getLayerGroupIdOf(layerId);
4498
+ const blocks = [];
4499
+ for (const id of order) {
4500
+ const key = this.getChildKeyWithin(id, groupId);
4501
+ if (key === null) continue;
4502
+ const lastBlock = blocks[blocks.length - 1];
4503
+ if (lastBlock && lastBlock.key === key && key !== `layer:${id}`) {
4504
+ lastBlock.ids.push(id);
4505
+ } else {
4506
+ blocks.push({ key, ids: [id] });
4507
+ }
4508
+ }
4509
+ const position = blocks.findIndex((block2) => block2.key === `layer:${layerId}`);
4510
+ const last = blocks.length - 1;
4511
+ if (position < 0) return;
4512
+ if ((direction === "up" || direction === "top") && position === 0) return;
4513
+ if ((direction === "down" || direction === "bottom") && position === last) {
4514
+ return;
4515
+ }
4516
+ const above = direction === "up" || direction === "top";
4517
+ const block = {
4518
+ up: blocks[position - 1],
4519
+ top: blocks[0],
4520
+ down: blocks[position + 1],
4521
+ bottom: blocks[last]
4522
+ }[direction];
4523
+ const anchor = above ? block.ids[0] : block.ids[block.ids.length - 1];
4524
+ const next = order.filter((id) => id !== layerId);
4525
+ const anchorIndex = next.indexOf(anchor);
4526
+ next.splice(above ? anchorIndex : anchorIndex + 1, 0, layerId);
4527
+ const newLayerStates = {};
4528
+ if (this.state.layerStates["Background"]) {
4529
+ newLayerStates["Background"] = this.state.layerStates["Background"];
4530
+ }
4531
+ next.forEach((id) => {
4532
+ if (this.state.layerStates[id]) {
4533
+ newLayerStates[id] = this.state.layerStates[id];
4534
+ }
4535
+ });
4536
+ this.state.layerStates = newLayerStates;
4537
+ this.applyLayerOrderToMap(next);
4538
+ this.buildLayerItems();
4539
+ (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, next);
4540
+ }
4541
+ /**
4542
+ * Identify which row of a group a layer falls under: the layer itself when
4543
+ * it belongs to the group directly, or the nested group that holds it.
4544
+ * @param layerId The layer ID
4545
+ * @param groupId The group, or undefined for the panel's top level
4546
+ * @returns `layer:<id>` or `group:<id>`, or null when the layer is not
4547
+ * inside the group at all
4548
+ */
4549
+ getChildKeyWithin(layerId, groupId) {
4550
+ var _a;
4551
+ let current = this.getLayerGroupIdOf(layerId);
4552
+ if (current === groupId) return `layer:${layerId}`;
4553
+ const visited = /* @__PURE__ */ new Set();
4554
+ while (current !== void 0 && !visited.has(current)) {
4555
+ visited.add(current);
4556
+ const parentId = (_a = this.groupStates.get(current)) == null ? void 0 : _a.parentId;
4557
+ const parent = parentId !== void 0 && this.groupStates.has(parentId) ? parentId : void 0;
4558
+ if (parent === groupId) return `group:${current}`;
4559
+ current = parent;
4560
+ }
4561
+ return null;
4562
+ }
4093
4563
  /**
4094
4564
  * Show inline remove confirmation instead of browser confirm() dialog.
4095
4565
  * Works in Jupyter notebooks where native dialogs may not appear.
@@ -4174,6 +4644,7 @@ class LayerControl {
4174
4644
  if (itemEl) {
4175
4645
  itemEl.remove();
4176
4646
  }
4647
+ this.pruneEmptyGroups();
4177
4648
  (_b = this.onLayerRemove) == null ? void 0 : _b.call(this, layerId);
4178
4649
  setTimeout(() => {
4179
4650
  this.state.isStyleOperationInProgress = false;
@@ -4276,10 +4747,9 @@ class LayerControl {
4276
4747
  clone.style.top = `${currentTop + deltaY}px`;
4277
4748
  this.state.drag.startY = e.clientY;
4278
4749
  this.state.drag.currentY = e.clientY;
4279
- const items = Array.from(
4280
- this.panel.querySelectorAll(".layer-control-item:not(.dragging)")
4281
- ).filter(
4282
- (item) => item.dataset.layerId !== "Background"
4750
+ const scope = placeholder.parentElement;
4751
+ const items = Array.from((scope == null ? void 0 : scope.children) ?? []).filter(
4752
+ (item) => (item.classList.contains("layer-control-item") || item.classList.contains("layer-control-group")) && !item.classList.contains("dragging") && item.dataset.layerId !== "Background"
4283
4753
  );
4284
4754
  for (const item of items) {
4285
4755
  const itemRect = item.getBoundingClientRect();
@@ -4343,8 +4813,7 @@ class LayerControl {
4343
4813
  * Apply UI order to map layers
4344
4814
  */
4345
4815
  applyUIOrderToMap() {
4346
- var _a, _b;
4347
- this.state.isStyleOperationInProgress = true;
4816
+ var _a;
4348
4817
  const items = this.panel.querySelectorAll(".layer-control-item");
4349
4818
  const uiLayerIds = [];
4350
4819
  items.forEach((item) => {
@@ -4353,6 +4822,16 @@ class LayerControl {
4353
4822
  uiLayerIds.push(layerId);
4354
4823
  }
4355
4824
  });
4825
+ this.applyLayerOrderToMap(uiLayerIds);
4826
+ (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, uiLayerIds);
4827
+ }
4828
+ /**
4829
+ * Restack the map's MapLibre layers to match a panel order.
4830
+ * @param uiLayerIds User layer IDs top-to-bottom, as the panel lists them
4831
+ */
4832
+ applyLayerOrderToMap(uiLayerIds) {
4833
+ var _a;
4834
+ this.state.isStyleOperationInProgress = true;
4356
4835
  const style = this.map.getStyle();
4357
4836
  const mapLibreLayerIds = new Set(((_a = style == null ? void 0 : style.layers) == null ? void 0 : _a.map((l) => l.id)) || []);
4358
4837
  for (let i = 0; i < uiLayerIds.length; i++) {
@@ -4372,7 +4851,6 @@ class LayerControl {
4372
4851
  } catch (e) {
4373
4852
  }
4374
4853
  }
4375
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, uiLayerIds);
4376
4854
  setTimeout(() => {
4377
4855
  this.state.isStyleOperationInProgress = false;
4378
4856
  }, 200);