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.cjs CHANGED
@@ -45,6 +45,13 @@ class CustomLayerRegistry {
45
45
  });
46
46
  return ids;
47
47
  }
48
+ /**
49
+ * Get each adapter's layer IDs as a separate list, in adapter order.
50
+ * @returns One array of layer IDs per registered adapter
51
+ */
52
+ getLayerIdGroups() {
53
+ return Array.from(this.adapters.values(), (adapter) => adapter.getLayerIds());
54
+ }
48
55
  /**
49
56
  * Check if a layer ID is managed by any adapter.
50
57
  * @param layerId The layer ID to check
@@ -157,6 +164,90 @@ class CustomLayerRegistry {
157
164
  }
158
165
  return false;
159
166
  }
167
+ /**
168
+ * Get the layer groups of every adapter. When two adapters define the same
169
+ * group ID, the first registered adapter's group wins.
170
+ * @returns The groups, in adapter order
171
+ */
172
+ getGroups() {
173
+ const groups = [];
174
+ const seen = /* @__PURE__ */ new Set();
175
+ this.adapters.forEach((adapter) => {
176
+ var _a;
177
+ for (const group of ((_a = adapter.getGroups) == null ? void 0 : _a.call(adapter)) ?? []) {
178
+ if (seen.has(group.id)) continue;
179
+ seen.add(group.id);
180
+ groups.push(group);
181
+ }
182
+ });
183
+ return groups;
184
+ }
185
+ /**
186
+ * Get the ID of the group a custom layer belongs to.
187
+ * @param layerId The layer ID
188
+ * @returns The group ID, or undefined when the layer is ungrouped
189
+ */
190
+ getLayerGroupId(layerId) {
191
+ var _a;
192
+ const adapter = this.getAdapterForLayer(layerId);
193
+ return (_a = adapter == null ? void 0 : adapter.getLayerGroupId) == null ? void 0 : _a.call(adapter, layerId);
194
+ }
195
+ /**
196
+ * Get the adapter that defines a group.
197
+ * @param groupId The group ID
198
+ * @returns The adapter or null if no adapter defines the group
199
+ */
200
+ getAdapterForGroup(groupId) {
201
+ var _a;
202
+ for (const adapter of this.adapters.values()) {
203
+ if ((_a = adapter.getGroups) == null ? void 0 : _a.call(adapter).some((group) => group.id === groupId)) {
204
+ return adapter;
205
+ }
206
+ }
207
+ return null;
208
+ }
209
+ /**
210
+ * Set visibility of a group.
211
+ * @param groupId The group ID
212
+ * @param visible Whether the group should be visible
213
+ * @returns true if the operation was handled by an adapter
214
+ */
215
+ setGroupVisibility(groupId, visible) {
216
+ const adapter = this.getAdapterForGroup(groupId);
217
+ if (adapter == null ? void 0 : adapter.setGroupVisibility) {
218
+ adapter.setGroupVisibility(groupId, visible);
219
+ return true;
220
+ }
221
+ return false;
222
+ }
223
+ /**
224
+ * Set opacity of a group.
225
+ * @param groupId The group ID
226
+ * @param opacity The opacity value (0-1)
227
+ * @returns true if the operation was handled by an adapter
228
+ */
229
+ setGroupOpacity(groupId, opacity) {
230
+ const adapter = this.getAdapterForGroup(groupId);
231
+ if (adapter == null ? void 0 : adapter.setGroupOpacity) {
232
+ adapter.setGroupOpacity(groupId, opacity);
233
+ return true;
234
+ }
235
+ return false;
236
+ }
237
+ /**
238
+ * Record that a group was collapsed or expanded.
239
+ * @param groupId The group ID
240
+ * @param collapsed Whether the group is now collapsed
241
+ * @returns true if the operation was handled by an adapter
242
+ */
243
+ setGroupCollapsed(groupId, collapsed) {
244
+ const adapter = this.getAdapterForGroup(groupId);
245
+ if (adapter == null ? void 0 : adapter.setGroupCollapsed) {
246
+ adapter.setGroupCollapsed(groupId, collapsed);
247
+ return true;
248
+ }
249
+ return false;
250
+ }
160
251
  /**
161
252
  * Subscribe to layer changes across all adapters.
162
253
  * @param callback Function called when layers are added or removed
@@ -702,6 +793,10 @@ class LayerControl {
702
793
  __publicField(this, "customLayerUnsubscribe", null);
703
794
  __publicField(this, "removedCustomLayerIds", /* @__PURE__ */ new Set());
704
795
  __publicField(this, "nativeLayerGroups", /* @__PURE__ */ new Map());
796
+ /** Layer groups from the custom layer adapters, as of the last panel build */
797
+ __publicField(this, "groupStates", /* @__PURE__ */ new Map());
798
+ /** Group IDs, parents, and memberships the panel was last built from */
799
+ __publicField(this, "groupStructureKey", "");
705
800
  __publicField(this, "basemapStyleUrl", null);
706
801
  __publicField(this, "basemapLayerIds", null);
707
802
  __publicField(this, "widthFrame", null);
@@ -1428,6 +1523,9 @@ class LayerControl {
1428
1523
  * Set visibility of all layers
1429
1524
  */
1430
1525
  setAllLayersVisibility(visible) {
1526
+ this.groupStates.forEach((_group, groupId) => {
1527
+ this.setGroupVisibility(groupId, visible);
1528
+ });
1431
1529
  Object.keys(this.state.layerStates).forEach((layerId) => {
1432
1530
  this.toggleLayerVisibility(layerId, visible);
1433
1531
  const itemEl = this.panel.querySelector(`[data-layer-id="${layerId}"]`);
@@ -1574,7 +1672,10 @@ class LayerControl {
1574
1672
  * Build layer items (called initially and when layers change)
1575
1673
  */
1576
1674
  buildLayerItems() {
1577
- const existingItems = this.panel.querySelectorAll(".layer-control-item");
1675
+ var _a;
1676
+ const existingItems = this.panel.querySelectorAll(
1677
+ ".layer-control-item, .layer-control-group"
1678
+ );
1578
1679
  existingItems.forEach((item) => item.remove());
1579
1680
  this.styleEditors.clear();
1580
1681
  const orderedLayerIds = this.getUserLayerIdsInMapOrder();
@@ -1587,20 +1688,302 @@ class LayerControl {
1587
1688
  if (this.state.layerStates["Background"]) {
1588
1689
  orderedLayerIds.push("Background");
1589
1690
  }
1691
+ this.groupStates = new Map(
1692
+ (((_a = this.customLayerRegistry) == null ? void 0 : _a.getGroups()) ?? []).map((group) => [
1693
+ group.id,
1694
+ { ...group }
1695
+ ])
1696
+ );
1697
+ this.groupStructureKey = this.computeGroupStructureKey(this.groupStates);
1698
+ const groupContainers = /* @__PURE__ */ new Map();
1590
1699
  orderedLayerIds.forEach((layerId) => {
1591
1700
  const state = this.state.layerStates[layerId];
1592
1701
  if (!state) {
1593
1702
  return;
1594
1703
  }
1595
1704
  if (layerId === "Background" || this.targetLayers.length === 0 || this.targetLayers.includes(layerId)) {
1596
- this.addLayerItem(layerId, state);
1705
+ const container = layerId === "Background" ? this.panel : this.getGroupContainer(
1706
+ this.getLayerGroupIdOf(layerId),
1707
+ groupContainers,
1708
+ /* @__PURE__ */ new Set()
1709
+ );
1710
+ this.addLayerItem(layerId, state, container);
1597
1711
  }
1598
1712
  });
1599
1713
  }
1714
+ /**
1715
+ * Get the ID of the group a layer belongs to, ignoring a group ID that no
1716
+ * adapter defines.
1717
+ * @param layerId The layer ID
1718
+ * @returns The group ID, or undefined when the layer is ungrouped
1719
+ */
1720
+ getLayerGroupIdOf(layerId) {
1721
+ var _a;
1722
+ if (this.groupStates.size === 0) return void 0;
1723
+ const groupId = (_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerGroupId(layerId);
1724
+ return groupId !== void 0 && this.groupStates.has(groupId) ? groupId : void 0;
1725
+ }
1726
+ /**
1727
+ * Get the element a group's rows go into, creating the group (and any
1728
+ * enclosing groups) on first use.
1729
+ * @param groupId The group ID, or undefined for the panel's top level
1730
+ * @param containers Group child containers already created in this build
1731
+ * @param visiting Groups on the current parent chain, to stop on a cycle
1732
+ * @returns The element to append the group's rows to
1733
+ */
1734
+ getGroupContainer(groupId, containers, visiting) {
1735
+ if (groupId === void 0) return this.panel;
1736
+ const existing = containers.get(groupId);
1737
+ if (existing) return existing;
1738
+ const group = this.groupStates.get(groupId);
1739
+ if (!group || visiting.has(groupId)) return this.panel;
1740
+ visiting.add(groupId);
1741
+ const parentId = group.parentId !== void 0 && this.groupStates.has(group.parentId) ? group.parentId : void 0;
1742
+ const parent = this.getGroupContainer(parentId, containers, visiting);
1743
+ const { element, children } = this.createGroupItem(group);
1744
+ parent.appendChild(element);
1745
+ containers.set(groupId, children);
1746
+ return children;
1747
+ }
1748
+ /**
1749
+ * Summarize the group structure the panel is built from: each group's ID
1750
+ * and parent, and each layer's group. A change to any of these needs a
1751
+ * rebuild; a change to a group's name, visibility, opacity, or collapsed
1752
+ * state does not.
1753
+ * @param groups The groups to summarize
1754
+ * @returns A string that changes whenever the structure does
1755
+ */
1756
+ computeGroupStructureKey(groups) {
1757
+ if (groups.size === 0) return "";
1758
+ const memberships = Object.keys(this.state.layerStates).filter((layerId) => layerId !== "Background").map((layerId) => {
1759
+ var _a;
1760
+ return [
1761
+ layerId,
1762
+ ((_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerGroupId(layerId)) ?? null
1763
+ ];
1764
+ });
1765
+ return JSON.stringify({
1766
+ groups: Array.from(groups.values(), (group) => [
1767
+ group.id,
1768
+ group.parentId ?? null
1769
+ ]),
1770
+ memberships
1771
+ });
1772
+ }
1773
+ /**
1774
+ * Create the element for a layer group: a header row with a collapse
1775
+ * toggle, visibility checkbox, name, and opacity slider, followed by a
1776
+ * container for the group's layers and nested groups.
1777
+ * @param group The group to render
1778
+ * @returns The group element and its child container
1779
+ */
1780
+ createGroupItem(group) {
1781
+ const element = document.createElement("div");
1782
+ element.className = "layer-control-group";
1783
+ element.setAttribute("data-group-id", group.id);
1784
+ const row = document.createElement("div");
1785
+ row.className = "layer-control-row layer-control-group-row";
1786
+ if (this.enableDragAndDrop) {
1787
+ row.appendChild(this.createDisabledDragHandle());
1788
+ }
1789
+ const checkbox = document.createElement("input");
1790
+ checkbox.type = "checkbox";
1791
+ checkbox.className = "layer-control-checkbox layer-control-group-checkbox";
1792
+ checkbox.addEventListener("change", () => {
1793
+ this.setGroupVisibility(group.id, checkbox.checked);
1794
+ });
1795
+ row.appendChild(checkbox);
1796
+ if (this.showLayerSymbol) {
1797
+ const symbol = document.createElement("span");
1798
+ symbol.className = "layer-control-symbol layer-control-group-symbol";
1799
+ symbol.innerHTML = `<svg viewBox="0 0 16 16" aria-hidden="true">
1800
+ <path d="M1.5 3.5h4.5l1.5 1.5h7v8.5h-13z" fill="#f2c94c" stroke="#b8902a" stroke-width="1"/>
1801
+ </svg>`;
1802
+ symbol.title = "Layer group";
1803
+ row.appendChild(symbol);
1804
+ }
1805
+ const name = document.createElement("span");
1806
+ name.className = "layer-control-name layer-control-group-name";
1807
+ row.appendChild(name);
1808
+ if (this.showOpacitySlider) {
1809
+ const opacity = document.createElement("input");
1810
+ opacity.type = "range";
1811
+ opacity.className = "layer-control-opacity layer-control-group-opacity";
1812
+ opacity.min = "0";
1813
+ opacity.max = "1";
1814
+ opacity.step = "0.01";
1815
+ opacity.addEventListener("mousedown", () => {
1816
+ this.state.userInteractingWithSlider = true;
1817
+ });
1818
+ opacity.addEventListener("mouseup", () => {
1819
+ this.state.userInteractingWithSlider = false;
1820
+ });
1821
+ opacity.addEventListener("input", () => {
1822
+ this.setGroupOpacity(group.id, parseFloat(opacity.value));
1823
+ });
1824
+ row.appendChild(opacity);
1825
+ }
1826
+ const toggle = document.createElement("button");
1827
+ toggle.type = "button";
1828
+ toggle.className = "layer-control-group-toggle";
1829
+ toggle.innerHTML = `<svg viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
1830
+ <path d="M6 4l4 4-4 4z"/>
1831
+ </svg>`;
1832
+ toggle.addEventListener("click", (e) => {
1833
+ var _a;
1834
+ e.stopPropagation();
1835
+ this.setGroupCollapsed(group.id, !((_a = this.groupStates.get(group.id)) == null ? void 0 : _a.collapsed));
1836
+ });
1837
+ row.appendChild(toggle);
1838
+ const children = document.createElement("div");
1839
+ children.className = "layer-control-group-children";
1840
+ element.appendChild(row);
1841
+ element.appendChild(children);
1842
+ this.updateGroupElement(element, group);
1843
+ return { element, children };
1844
+ }
1845
+ /**
1846
+ * Show a group's state on its element.
1847
+ * @param element The group element
1848
+ * @param group The group state to show
1849
+ */
1850
+ updateGroupElement(element, group) {
1851
+ element.classList.toggle("collapsed", group.collapsed);
1852
+ element.classList.toggle("layer-control-group-hidden", !group.visible);
1853
+ const row = element.querySelector(":scope > .layer-control-group-row");
1854
+ if (!row) return;
1855
+ const toggle = row.querySelector(
1856
+ ".layer-control-group-toggle"
1857
+ );
1858
+ if (toggle) {
1859
+ toggle.setAttribute("aria-expanded", String(!group.collapsed));
1860
+ toggle.title = group.collapsed ? "Expand group" : "Collapse group";
1861
+ toggle.setAttribute(
1862
+ "aria-label",
1863
+ `${group.collapsed ? "Expand" : "Collapse"} ${group.name}`
1864
+ );
1865
+ }
1866
+ const checkbox = row.querySelector(
1867
+ ".layer-control-group-checkbox"
1868
+ );
1869
+ if (checkbox) checkbox.checked = group.visible;
1870
+ const name = row.querySelector(".layer-control-group-name");
1871
+ if (name) {
1872
+ name.textContent = group.name;
1873
+ name.title = group.name;
1874
+ }
1875
+ const opacity = row.querySelector(
1876
+ ".layer-control-group-opacity"
1877
+ );
1878
+ if (opacity) {
1879
+ opacity.value = String(group.opacity);
1880
+ opacity.title = `Opacity: ${Math.round(group.opacity * 100)}%`;
1881
+ }
1882
+ }
1883
+ /**
1884
+ * Get the element of a rendered group.
1885
+ * @param groupId The group ID
1886
+ * @returns The group element, or null when the group is not rendered
1887
+ */
1888
+ getGroupElement(groupId) {
1889
+ const groups = this.panel.querySelectorAll(".layer-control-group");
1890
+ for (const element of Array.from(groups)) {
1891
+ if (element.dataset.groupId === groupId) {
1892
+ return element;
1893
+ }
1894
+ }
1895
+ return null;
1896
+ }
1897
+ /**
1898
+ * Remove group elements left without any layer row, e.g. after their last
1899
+ * layer was removed.
1900
+ */
1901
+ pruneEmptyGroups() {
1902
+ const groups = Array.from(this.panel.querySelectorAll(".layer-control-group"));
1903
+ for (const element of groups.reverse()) {
1904
+ if (!element.querySelector(".layer-control-item")) element.remove();
1905
+ }
1906
+ }
1907
+ /**
1908
+ * Set a group's visibility from the panel and report it to its adapter.
1909
+ * @param groupId The group ID
1910
+ * @param visible Whether the group should be visible
1911
+ */
1912
+ setGroupVisibility(groupId, visible) {
1913
+ var _a;
1914
+ const group = this.groupStates.get(groupId);
1915
+ if (!group) return;
1916
+ group.visible = visible;
1917
+ const element = this.getGroupElement(groupId);
1918
+ if (element) this.updateGroupElement(element, group);
1919
+ this.state.isStyleOperationInProgress = true;
1920
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupVisibility(groupId, visible);
1921
+ setTimeout(() => {
1922
+ this.state.isStyleOperationInProgress = false;
1923
+ }, 200);
1924
+ }
1925
+ /**
1926
+ * Set a group's opacity from the panel and report it to its adapter.
1927
+ * @param groupId The group ID
1928
+ * @param opacity The opacity value (0-1)
1929
+ */
1930
+ setGroupOpacity(groupId, opacity) {
1931
+ var _a;
1932
+ const group = this.groupStates.get(groupId);
1933
+ if (!group) return;
1934
+ group.opacity = opacity;
1935
+ const element = this.getGroupElement(groupId);
1936
+ if (element) this.updateGroupElement(element, group);
1937
+ this.state.isStyleOperationInProgress = true;
1938
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupOpacity(groupId, opacity);
1939
+ setTimeout(() => {
1940
+ this.state.isStyleOperationInProgress = false;
1941
+ }, 200);
1942
+ }
1943
+ /**
1944
+ * Collapse or expand a group in the panel and report it to its adapter.
1945
+ * @param groupId The group ID
1946
+ * @param collapsed Whether the group should be collapsed
1947
+ */
1948
+ setGroupCollapsed(groupId, collapsed) {
1949
+ var _a;
1950
+ const group = this.groupStates.get(groupId);
1951
+ if (!group) return;
1952
+ group.collapsed = collapsed;
1953
+ const element = this.getGroupElement(groupId);
1954
+ if (element) this.updateGroupElement(element, group);
1955
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupCollapsed(groupId, collapsed);
1956
+ }
1957
+ /**
1958
+ * Re-read the layer groups from the custom layer adapters. Call this after
1959
+ * groups change outside the control. The panel is rebuilt when a group was
1960
+ * added, removed, or re-parented, or a layer changed group; otherwise the
1961
+ * group rows' name, visibility, opacity, and collapsed state are updated in
1962
+ * place.
1963
+ */
1964
+ refreshGroups() {
1965
+ var _a;
1966
+ if (!this.panel) return;
1967
+ const next = new Map(
1968
+ (((_a = this.customLayerRegistry) == null ? void 0 : _a.getGroups()) ?? []).map((group) => [
1969
+ group.id,
1970
+ { ...group }
1971
+ ])
1972
+ );
1973
+ if (this.computeGroupStructureKey(next) !== this.groupStructureKey) {
1974
+ this.buildLayerItems();
1975
+ return;
1976
+ }
1977
+ this.groupStates = next;
1978
+ next.forEach((group, groupId) => {
1979
+ const element = this.getGroupElement(groupId);
1980
+ if (element) this.updateGroupElement(element, group);
1981
+ });
1982
+ }
1600
1983
  /**
1601
1984
  * Add a single layer item to the panel
1602
1985
  */
1603
- addLayerItem(layerId, state) {
1986
+ addLayerItem(layerId, state, container = this.panel) {
1604
1987
  const item = document.createElement("div");
1605
1988
  item.className = "layer-control-item";
1606
1989
  item.setAttribute("data-layer-id", layerId);
@@ -1684,7 +2067,7 @@ class LayerControl {
1684
2067
  this.showContextMenu(layerId, e.clientX, e.clientY);
1685
2068
  });
1686
2069
  }
1687
- this.panel.appendChild(item);
2070
+ container.appendChild(item);
1688
2071
  }
1689
2072
  /**
1690
2073
  * Create a symbol element for a layer
@@ -3581,6 +3964,8 @@ class LayerControl {
3581
3964
  }
3582
3965
  if (layersAdded) {
3583
3966
  this.buildLayerItems();
3967
+ } else {
3968
+ this.pruneEmptyGroups();
3584
3969
  }
3585
3970
  } catch (error) {
3586
3971
  console.warn("Failed to check for new layers:", error);
@@ -3861,6 +4246,7 @@ class LayerControl {
3861
4246
  * Includes both MapLibre layers and custom layers managed by adapters.
3862
4247
  */
3863
4248
  getUserLayerIdsInMapOrder() {
4249
+ var _a;
3864
4250
  const style = this.map.getStyle();
3865
4251
  if (!(style == null ? void 0 : style.layers)) return [];
3866
4252
  const mapLayerIds = style.layers.map((l) => l.id);
@@ -3868,11 +4254,11 @@ class LayerControl {
3868
4254
  const userLayerIds = Object.keys(this.state.layerStates).filter(
3869
4255
  (id) => id !== "Background"
3870
4256
  );
3871
- const mapIndexOf = (id) => {
3872
- var _a;
4257
+ const resolvedIndexOf = (id) => {
4258
+ var _a2;
3873
4259
  const direct = indexById.get(id);
3874
4260
  if (direct !== void 0) return direct;
3875
- const nativeIds = (_a = this.customLayerRegistry) == null ? void 0 : _a.getNativeLayerIds(id);
4261
+ const nativeIds = (_a2 = this.customLayerRegistry) == null ? void 0 : _a2.getNativeLayerIds(id);
3876
4262
  if (nativeIds && nativeIds.length > 0) {
3877
4263
  let best = -1;
3878
4264
  for (const nativeId of nativeIds) {
@@ -3881,14 +4267,45 @@ class LayerControl {
3881
4267
  }
3882
4268
  if (best !== -1) return best;
3883
4269
  }
3884
- return Number.POSITIVE_INFINITY;
4270
+ return void 0;
4271
+ };
4272
+ const anchors = /* @__PURE__ */ new Map();
4273
+ for (const group of ((_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerIdGroups()) ?? []) {
4274
+ const resolved = group.map(resolvedIndexOf);
4275
+ const known = resolved.filter((idx) => idx !== void 0);
4276
+ if (known.length === 0) continue;
4277
+ const ids = known[0] > known[known.length - 1] ? [...group].reverse() : group;
4278
+ const indexes = ids === group ? resolved : [...resolved].reverse();
4279
+ ids.forEach((id, position) => {
4280
+ if (indexes[position] !== void 0) return;
4281
+ for (let below = position - 1; below >= 0; below--) {
4282
+ const idx = indexes[below];
4283
+ if (idx !== void 0) {
4284
+ anchors.set(id, { mapIndex: idx, offset: position - below });
4285
+ return;
4286
+ }
4287
+ }
4288
+ for (let above = position + 1; above < ids.length; above++) {
4289
+ const idx = indexes[above];
4290
+ if (idx !== void 0) {
4291
+ anchors.set(id, { mapIndex: idx, offset: position - above });
4292
+ return;
4293
+ }
4294
+ }
4295
+ });
4296
+ }
4297
+ const stackKeyOf = (id) => {
4298
+ const resolved = resolvedIndexOf(id);
4299
+ if (resolved !== void 0) return { mapIndex: resolved, offset: 0 };
4300
+ return anchors.get(id) ?? { mapIndex: Number.POSITIVE_INFINITY, offset: 0 };
3885
4301
  };
3886
4302
  return userLayerIds.map((id, insertionIndex) => ({
3887
4303
  id,
3888
4304
  insertionIndex,
3889
- mapIndex: mapIndexOf(id)
4305
+ ...stackKeyOf(id)
3890
4306
  })).sort((a, b) => {
3891
4307
  if (a.mapIndex !== b.mapIndex) return a.mapIndex > b.mapIndex ? -1 : 1;
4308
+ if (a.offset !== b.offset) return b.offset - a.offset;
3892
4309
  return a.insertionIndex - b.insertionIndex;
3893
4310
  }).map((entry) => entry.id);
3894
4311
  }
@@ -3918,6 +4335,10 @@ class LayerControl {
3918
4335
  */
3919
4336
  moveLayerUp(layerId) {
3920
4337
  var _a, _b;
4338
+ if (this.groupStates.size > 0) {
4339
+ this.moveLayerWithinGroup(layerId, "up");
4340
+ return;
4341
+ }
3921
4342
  const layerIds = this.getUserLayerIdsInMapOrder();
3922
4343
  const index = layerIds.indexOf(layerId);
3923
4344
  if (index <= 0) return;
@@ -3952,6 +4373,10 @@ class LayerControl {
3952
4373
  */
3953
4374
  moveLayerToTop(layerId) {
3954
4375
  var _a, _b;
4376
+ if (this.groupStates.size > 0) {
4377
+ this.moveLayerWithinGroup(layerId, "top");
4378
+ return;
4379
+ }
3955
4380
  const layerIds = this.getUserLayerIdsInMapOrder();
3956
4381
  const index = layerIds.indexOf(layerId);
3957
4382
  if (index <= 0) return;
@@ -3983,6 +4408,10 @@ class LayerControl {
3983
4408
  */
3984
4409
  moveLayerDown(layerId) {
3985
4410
  var _a, _b;
4411
+ if (this.groupStates.size > 0) {
4412
+ this.moveLayerWithinGroup(layerId, "down");
4413
+ return;
4414
+ }
3986
4415
  const layerIds = this.getUserLayerIdsInMapOrder();
3987
4416
  const index = layerIds.indexOf(layerId);
3988
4417
  if (index < 0 || index >= layerIds.length - 1) return;
@@ -4019,6 +4448,10 @@ class LayerControl {
4019
4448
  */
4020
4449
  moveLayerToBottom(layerId) {
4021
4450
  var _a, _b;
4451
+ if (this.groupStates.size > 0) {
4452
+ this.moveLayerWithinGroup(layerId, "bottom");
4453
+ return;
4454
+ }
4022
4455
  const layerIds = this.getUserLayerIdsInMapOrder();
4023
4456
  if (layerIds.length <= 1) return;
4024
4457
  const index = layerIds.indexOf(layerId);
@@ -4053,6 +4486,82 @@ class LayerControl {
4053
4486
  this.buildLayerItems();
4054
4487
  (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
4055
4488
  }
4489
+ /**
4490
+ * Move a layer within its own group (or among the top-level rows), so the
4491
+ * context-menu moves never carry it out of its group. A nested group counts
4492
+ * as one block: moving up or down steps past the whole block.
4493
+ * @param layerId The layer ID
4494
+ * @param direction Where to move the layer within its group
4495
+ */
4496
+ moveLayerWithinGroup(layerId, direction) {
4497
+ var _a;
4498
+ const order = this.getUserLayerIdsInMapOrder();
4499
+ const groupId = this.getLayerGroupIdOf(layerId);
4500
+ const blocks = [];
4501
+ for (const id of order) {
4502
+ const key = this.getChildKeyWithin(id, groupId);
4503
+ if (key === null) continue;
4504
+ const lastBlock = blocks[blocks.length - 1];
4505
+ if (lastBlock && lastBlock.key === key && key !== `layer:${id}`) {
4506
+ lastBlock.ids.push(id);
4507
+ } else {
4508
+ blocks.push({ key, ids: [id] });
4509
+ }
4510
+ }
4511
+ const position = blocks.findIndex((block2) => block2.key === `layer:${layerId}`);
4512
+ const last = blocks.length - 1;
4513
+ if (position < 0) return;
4514
+ if ((direction === "up" || direction === "top") && position === 0) return;
4515
+ if ((direction === "down" || direction === "bottom") && position === last) {
4516
+ return;
4517
+ }
4518
+ const above = direction === "up" || direction === "top";
4519
+ const block = {
4520
+ up: blocks[position - 1],
4521
+ top: blocks[0],
4522
+ down: blocks[position + 1],
4523
+ bottom: blocks[last]
4524
+ }[direction];
4525
+ const anchor = above ? block.ids[0] : block.ids[block.ids.length - 1];
4526
+ const next = order.filter((id) => id !== layerId);
4527
+ const anchorIndex = next.indexOf(anchor);
4528
+ next.splice(above ? anchorIndex : anchorIndex + 1, 0, layerId);
4529
+ const newLayerStates = {};
4530
+ if (this.state.layerStates["Background"]) {
4531
+ newLayerStates["Background"] = this.state.layerStates["Background"];
4532
+ }
4533
+ next.forEach((id) => {
4534
+ if (this.state.layerStates[id]) {
4535
+ newLayerStates[id] = this.state.layerStates[id];
4536
+ }
4537
+ });
4538
+ this.state.layerStates = newLayerStates;
4539
+ this.applyLayerOrderToMap(next);
4540
+ this.buildLayerItems();
4541
+ (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, next);
4542
+ }
4543
+ /**
4544
+ * Identify which row of a group a layer falls under: the layer itself when
4545
+ * it belongs to the group directly, or the nested group that holds it.
4546
+ * @param layerId The layer ID
4547
+ * @param groupId The group, or undefined for the panel's top level
4548
+ * @returns `layer:<id>` or `group:<id>`, or null when the layer is not
4549
+ * inside the group at all
4550
+ */
4551
+ getChildKeyWithin(layerId, groupId) {
4552
+ var _a;
4553
+ let current = this.getLayerGroupIdOf(layerId);
4554
+ if (current === groupId) return `layer:${layerId}`;
4555
+ const visited = /* @__PURE__ */ new Set();
4556
+ while (current !== void 0 && !visited.has(current)) {
4557
+ visited.add(current);
4558
+ const parentId = (_a = this.groupStates.get(current)) == null ? void 0 : _a.parentId;
4559
+ const parent = parentId !== void 0 && this.groupStates.has(parentId) ? parentId : void 0;
4560
+ if (parent === groupId) return `group:${current}`;
4561
+ current = parent;
4562
+ }
4563
+ return null;
4564
+ }
4056
4565
  /**
4057
4566
  * Show inline remove confirmation instead of browser confirm() dialog.
4058
4567
  * Works in Jupyter notebooks where native dialogs may not appear.
@@ -4137,6 +4646,7 @@ class LayerControl {
4137
4646
  if (itemEl) {
4138
4647
  itemEl.remove();
4139
4648
  }
4649
+ this.pruneEmptyGroups();
4140
4650
  (_b = this.onLayerRemove) == null ? void 0 : _b.call(this, layerId);
4141
4651
  setTimeout(() => {
4142
4652
  this.state.isStyleOperationInProgress = false;
@@ -4239,10 +4749,9 @@ class LayerControl {
4239
4749
  clone.style.top = `${currentTop + deltaY}px`;
4240
4750
  this.state.drag.startY = e.clientY;
4241
4751
  this.state.drag.currentY = e.clientY;
4242
- const items = Array.from(
4243
- this.panel.querySelectorAll(".layer-control-item:not(.dragging)")
4244
- ).filter(
4245
- (item) => item.dataset.layerId !== "Background"
4752
+ const scope = placeholder.parentElement;
4753
+ const items = Array.from((scope == null ? void 0 : scope.children) ?? []).filter(
4754
+ (item) => (item.classList.contains("layer-control-item") || item.classList.contains("layer-control-group")) && !item.classList.contains("dragging") && item.dataset.layerId !== "Background"
4246
4755
  );
4247
4756
  for (const item of items) {
4248
4757
  const itemRect = item.getBoundingClientRect();
@@ -4306,8 +4815,7 @@ class LayerControl {
4306
4815
  * Apply UI order to map layers
4307
4816
  */
4308
4817
  applyUIOrderToMap() {
4309
- var _a, _b;
4310
- this.state.isStyleOperationInProgress = true;
4818
+ var _a;
4311
4819
  const items = this.panel.querySelectorAll(".layer-control-item");
4312
4820
  const uiLayerIds = [];
4313
4821
  items.forEach((item) => {
@@ -4316,6 +4824,16 @@ class LayerControl {
4316
4824
  uiLayerIds.push(layerId);
4317
4825
  }
4318
4826
  });
4827
+ this.applyLayerOrderToMap(uiLayerIds);
4828
+ (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, uiLayerIds);
4829
+ }
4830
+ /**
4831
+ * Restack the map's MapLibre layers to match a panel order.
4832
+ * @param uiLayerIds User layer IDs top-to-bottom, as the panel lists them
4833
+ */
4834
+ applyLayerOrderToMap(uiLayerIds) {
4835
+ var _a;
4836
+ this.state.isStyleOperationInProgress = true;
4319
4837
  const style = this.map.getStyle();
4320
4838
  const mapLibreLayerIds = new Set(((_a = style == null ? void 0 : style.layers) == null ? void 0 : _a.map((l) => l.id)) || []);
4321
4839
  for (let i = 0; i < uiLayerIds.length; i++) {
@@ -4335,7 +4853,6 @@ class LayerControl {
4335
4853
  } catch (e) {
4336
4854
  }
4337
4855
  }
4338
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, uiLayerIds);
4339
4856
  setTimeout(() => {
4340
4857
  this.state.isStyleOperationInProgress = false;
4341
4858
  }, 200);