maplibre-gl-layer-control 0.17.4 → 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
@@ -43,6 +43,13 @@ class CustomLayerRegistry {
43
43
  });
44
44
  return ids;
45
45
  }
46
+ /**
47
+ * Get each adapter's layer IDs as a separate list, in adapter order.
48
+ * @returns One array of layer IDs per registered adapter
49
+ */
50
+ getLayerIdGroups() {
51
+ return Array.from(this.adapters.values(), (adapter) => adapter.getLayerIds());
52
+ }
46
53
  /**
47
54
  * Check if a layer ID is managed by any adapter.
48
55
  * @param layerId The layer ID to check
@@ -155,6 +162,90 @@ class CustomLayerRegistry {
155
162
  }
156
163
  return false;
157
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
+ }
158
249
  /**
159
250
  * Subscribe to layer changes across all adapters.
160
251
  * @param callback Function called when layers are added or removed
@@ -700,6 +791,10 @@ class LayerControl {
700
791
  __publicField(this, "customLayerUnsubscribe", null);
701
792
  __publicField(this, "removedCustomLayerIds", /* @__PURE__ */ new Set());
702
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", "");
703
798
  __publicField(this, "basemapStyleUrl", null);
704
799
  __publicField(this, "basemapLayerIds", null);
705
800
  __publicField(this, "widthFrame", null);
@@ -1426,6 +1521,9 @@ class LayerControl {
1426
1521
  * Set visibility of all layers
1427
1522
  */
1428
1523
  setAllLayersVisibility(visible) {
1524
+ this.groupStates.forEach((_group, groupId) => {
1525
+ this.setGroupVisibility(groupId, visible);
1526
+ });
1429
1527
  Object.keys(this.state.layerStates).forEach((layerId) => {
1430
1528
  this.toggleLayerVisibility(layerId, visible);
1431
1529
  const itemEl = this.panel.querySelector(`[data-layer-id="${layerId}"]`);
@@ -1572,7 +1670,10 @@ class LayerControl {
1572
1670
  * Build layer items (called initially and when layers change)
1573
1671
  */
1574
1672
  buildLayerItems() {
1575
- 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
+ );
1576
1677
  existingItems.forEach((item) => item.remove());
1577
1678
  this.styleEditors.clear();
1578
1679
  const orderedLayerIds = this.getUserLayerIdsInMapOrder();
@@ -1585,20 +1686,302 @@ class LayerControl {
1585
1686
  if (this.state.layerStates["Background"]) {
1586
1687
  orderedLayerIds.push("Background");
1587
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();
1588
1697
  orderedLayerIds.forEach((layerId) => {
1589
1698
  const state = this.state.layerStates[layerId];
1590
1699
  if (!state) {
1591
1700
  return;
1592
1701
  }
1593
1702
  if (layerId === "Background" || this.targetLayers.length === 0 || this.targetLayers.includes(layerId)) {
1594
- 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);
1595
1709
  }
1596
1710
  });
1597
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;
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);
1979
+ });
1980
+ }
1598
1981
  /**
1599
1982
  * Add a single layer item to the panel
1600
1983
  */
1601
- addLayerItem(layerId, state) {
1984
+ addLayerItem(layerId, state, container = this.panel) {
1602
1985
  const item = document.createElement("div");
1603
1986
  item.className = "layer-control-item";
1604
1987
  item.setAttribute("data-layer-id", layerId);
@@ -1682,7 +2065,7 @@ class LayerControl {
1682
2065
  this.showContextMenu(layerId, e.clientX, e.clientY);
1683
2066
  });
1684
2067
  }
1685
- this.panel.appendChild(item);
2068
+ container.appendChild(item);
1686
2069
  }
1687
2070
  /**
1688
2071
  * Create a symbol element for a layer
@@ -3579,6 +3962,8 @@ class LayerControl {
3579
3962
  }
3580
3963
  if (layersAdded) {
3581
3964
  this.buildLayerItems();
3965
+ } else {
3966
+ this.pruneEmptyGroups();
3582
3967
  }
3583
3968
  } catch (error) {
3584
3969
  console.warn("Failed to check for new layers:", error);
@@ -3859,6 +4244,7 @@ class LayerControl {
3859
4244
  * Includes both MapLibre layers and custom layers managed by adapters.
3860
4245
  */
3861
4246
  getUserLayerIdsInMapOrder() {
4247
+ var _a;
3862
4248
  const style = this.map.getStyle();
3863
4249
  if (!(style == null ? void 0 : style.layers)) return [];
3864
4250
  const mapLayerIds = style.layers.map((l) => l.id);
@@ -3866,11 +4252,11 @@ class LayerControl {
3866
4252
  const userLayerIds = Object.keys(this.state.layerStates).filter(
3867
4253
  (id) => id !== "Background"
3868
4254
  );
3869
- const mapIndexOf = (id) => {
3870
- var _a;
4255
+ const resolvedIndexOf = (id) => {
4256
+ var _a2;
3871
4257
  const direct = indexById.get(id);
3872
4258
  if (direct !== void 0) return direct;
3873
- const nativeIds = (_a = this.customLayerRegistry) == null ? void 0 : _a.getNativeLayerIds(id);
4259
+ const nativeIds = (_a2 = this.customLayerRegistry) == null ? void 0 : _a2.getNativeLayerIds(id);
3874
4260
  if (nativeIds && nativeIds.length > 0) {
3875
4261
  let best = -1;
3876
4262
  for (const nativeId of nativeIds) {
@@ -3879,14 +4265,45 @@ class LayerControl {
3879
4265
  }
3880
4266
  if (best !== -1) return best;
3881
4267
  }
3882
- return Number.POSITIVE_INFINITY;
4268
+ return void 0;
4269
+ };
4270
+ const anchors = /* @__PURE__ */ new Map();
4271
+ for (const group of ((_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerIdGroups()) ?? []) {
4272
+ const resolved = group.map(resolvedIndexOf);
4273
+ const known = resolved.filter((idx) => idx !== void 0);
4274
+ if (known.length === 0) continue;
4275
+ const ids = known[0] > known[known.length - 1] ? [...group].reverse() : group;
4276
+ const indexes = ids === group ? resolved : [...resolved].reverse();
4277
+ ids.forEach((id, position) => {
4278
+ if (indexes[position] !== void 0) return;
4279
+ for (let below = position - 1; below >= 0; below--) {
4280
+ const idx = indexes[below];
4281
+ if (idx !== void 0) {
4282
+ anchors.set(id, { mapIndex: idx, offset: position - below });
4283
+ return;
4284
+ }
4285
+ }
4286
+ for (let above = position + 1; above < ids.length; above++) {
4287
+ const idx = indexes[above];
4288
+ if (idx !== void 0) {
4289
+ anchors.set(id, { mapIndex: idx, offset: position - above });
4290
+ return;
4291
+ }
4292
+ }
4293
+ });
4294
+ }
4295
+ const stackKeyOf = (id) => {
4296
+ const resolved = resolvedIndexOf(id);
4297
+ if (resolved !== void 0) return { mapIndex: resolved, offset: 0 };
4298
+ return anchors.get(id) ?? { mapIndex: Number.POSITIVE_INFINITY, offset: 0 };
3883
4299
  };
3884
4300
  return userLayerIds.map((id, insertionIndex) => ({
3885
4301
  id,
3886
4302
  insertionIndex,
3887
- mapIndex: mapIndexOf(id)
4303
+ ...stackKeyOf(id)
3888
4304
  })).sort((a, b) => {
3889
4305
  if (a.mapIndex !== b.mapIndex) return a.mapIndex > b.mapIndex ? -1 : 1;
4306
+ if (a.offset !== b.offset) return b.offset - a.offset;
3890
4307
  return a.insertionIndex - b.insertionIndex;
3891
4308
  }).map((entry) => entry.id);
3892
4309
  }
@@ -3916,6 +4333,10 @@ class LayerControl {
3916
4333
  */
3917
4334
  moveLayerUp(layerId) {
3918
4335
  var _a, _b;
4336
+ if (this.groupStates.size > 0) {
4337
+ this.moveLayerWithinGroup(layerId, "up");
4338
+ return;
4339
+ }
3919
4340
  const layerIds = this.getUserLayerIdsInMapOrder();
3920
4341
  const index = layerIds.indexOf(layerId);
3921
4342
  if (index <= 0) return;
@@ -3950,6 +4371,10 @@ class LayerControl {
3950
4371
  */
3951
4372
  moveLayerToTop(layerId) {
3952
4373
  var _a, _b;
4374
+ if (this.groupStates.size > 0) {
4375
+ this.moveLayerWithinGroup(layerId, "top");
4376
+ return;
4377
+ }
3953
4378
  const layerIds = this.getUserLayerIdsInMapOrder();
3954
4379
  const index = layerIds.indexOf(layerId);
3955
4380
  if (index <= 0) return;
@@ -3981,6 +4406,10 @@ class LayerControl {
3981
4406
  */
3982
4407
  moveLayerDown(layerId) {
3983
4408
  var _a, _b;
4409
+ if (this.groupStates.size > 0) {
4410
+ this.moveLayerWithinGroup(layerId, "down");
4411
+ return;
4412
+ }
3984
4413
  const layerIds = this.getUserLayerIdsInMapOrder();
3985
4414
  const index = layerIds.indexOf(layerId);
3986
4415
  if (index < 0 || index >= layerIds.length - 1) return;
@@ -4017,6 +4446,10 @@ class LayerControl {
4017
4446
  */
4018
4447
  moveLayerToBottom(layerId) {
4019
4448
  var _a, _b;
4449
+ if (this.groupStates.size > 0) {
4450
+ this.moveLayerWithinGroup(layerId, "bottom");
4451
+ return;
4452
+ }
4020
4453
  const layerIds = this.getUserLayerIdsInMapOrder();
4021
4454
  if (layerIds.length <= 1) return;
4022
4455
  const index = layerIds.indexOf(layerId);
@@ -4051,6 +4484,82 @@ class LayerControl {
4051
4484
  this.buildLayerItems();
4052
4485
  (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
4053
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
+ }
4054
4563
  /**
4055
4564
  * Show inline remove confirmation instead of browser confirm() dialog.
4056
4565
  * Works in Jupyter notebooks where native dialogs may not appear.
@@ -4135,6 +4644,7 @@ class LayerControl {
4135
4644
  if (itemEl) {
4136
4645
  itemEl.remove();
4137
4646
  }
4647
+ this.pruneEmptyGroups();
4138
4648
  (_b = this.onLayerRemove) == null ? void 0 : _b.call(this, layerId);
4139
4649
  setTimeout(() => {
4140
4650
  this.state.isStyleOperationInProgress = false;
@@ -4237,10 +4747,9 @@ class LayerControl {
4237
4747
  clone.style.top = `${currentTop + deltaY}px`;
4238
4748
  this.state.drag.startY = e.clientY;
4239
4749
  this.state.drag.currentY = e.clientY;
4240
- const items = Array.from(
4241
- this.panel.querySelectorAll(".layer-control-item:not(.dragging)")
4242
- ).filter(
4243
- (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"
4244
4753
  );
4245
4754
  for (const item of items) {
4246
4755
  const itemRect = item.getBoundingClientRect();
@@ -4304,8 +4813,7 @@ class LayerControl {
4304
4813
  * Apply UI order to map layers
4305
4814
  */
4306
4815
  applyUIOrderToMap() {
4307
- var _a, _b;
4308
- this.state.isStyleOperationInProgress = true;
4816
+ var _a;
4309
4817
  const items = this.panel.querySelectorAll(".layer-control-item");
4310
4818
  const uiLayerIds = [];
4311
4819
  items.forEach((item) => {
@@ -4314,6 +4822,16 @@ class LayerControl {
4314
4822
  uiLayerIds.push(layerId);
4315
4823
  }
4316
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;
4317
4835
  const style = this.map.getStyle();
4318
4836
  const mapLibreLayerIds = new Set(((_a = style == null ? void 0 : style.layers) == null ? void 0 : _a.map((l) => l.id)) || []);
4319
4837
  for (let i = 0; i < uiLayerIds.length; i++) {
@@ -4333,7 +4851,6 @@ class LayerControl {
4333
4851
  } catch (e) {
4334
4852
  }
4335
4853
  }
4336
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, uiLayerIds);
4337
4854
  setTimeout(() => {
4338
4855
  this.state.isStyleOperationInProgress = false;
4339
4856
  }, 200);