maplibre-gl-layer-control 0.18.3 → 0.19.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.
@@ -1375,6 +1375,12 @@ class LayerControl {
1375
1375
  __publicField(this, "groupStructureKey", "");
1376
1376
  __publicField(this, "basemapStyleUrl", null);
1377
1377
  __publicField(this, "basemapLayerIds", null);
1378
+ /** True when `basemapLayerIds` came from the option, which is authoritative even when empty. */
1379
+ __publicField(this, "basemapLayerIdsProvided", false);
1380
+ /** Set once onAdd has listed the map's layers (after any basemapStyleUrl fetch). */
1381
+ __publicField(this, "layersListed", false);
1382
+ /** Panel order set through setLayerOrder, used to order layers with nothing on the map. */
1383
+ __publicField(this, "layerOrderHint", null);
1378
1384
  __publicField(this, "widthFrame", null);
1379
1385
  // Exact-opacity input popup
1380
1386
  __publicField(this, "opacityInputEl", null);
@@ -1464,6 +1470,19 @@ class LayerControl {
1464
1470
  });
1465
1471
  }
1466
1472
  this.basemapStyleUrl = options.basemapStyleUrl || null;
1473
+ if (options.basemapLayerIds) {
1474
+ this.basemapLayerIds = new Set(options.basemapLayerIds);
1475
+ this.basemapLayerIdsProvided = true;
1476
+ }
1477
+ }
1478
+ /**
1479
+ * Whether basemap membership is decided by a known list of basemap layer IDs
1480
+ * (the `basemapLayerIds` option, or the layers of `basemapStyleUrl`) rather
1481
+ * than by heuristics. A fetched style only counts when it had layers; an
1482
+ * explicit list counts even when empty.
1483
+ */
1484
+ hasBasemapLayerIds() {
1485
+ return this.basemapLayerIds !== null && (this.basemapLayerIdsProvided || this.basemapLayerIds.size > 0);
1467
1486
  }
1468
1487
  /**
1469
1488
  * Called when the control is added to the map
@@ -1471,6 +1490,7 @@ class LayerControl {
1471
1490
  onAdd(map) {
1472
1491
  this.map = map;
1473
1492
  this.mapContainer = map.getContainer();
1493
+ this.layersListed = false;
1474
1494
  const style = this.map.getStyle();
1475
1495
  if (style && style.sources) {
1476
1496
  this.initialSourceIds = new Set(Object.keys(style.sources));
@@ -1498,6 +1518,7 @@ class LayerControl {
1498
1518
  this.autoDetectLayers();
1499
1519
  }
1500
1520
  this.buildLayerItems();
1521
+ this.layersListed = true;
1501
1522
  }).catch((error) => {
1502
1523
  console.warn(
1503
1524
  "Failed to fetch basemap style, falling back to heuristic detection:",
@@ -1507,12 +1528,14 @@ class LayerControl {
1507
1528
  this.autoDetectLayers();
1508
1529
  }
1509
1530
  this.buildLayerItems();
1531
+ this.layersListed = true;
1510
1532
  });
1511
1533
  } else {
1512
1534
  if (Object.keys(this.state.layerStates).length === 0) {
1513
1535
  this.autoDetectLayers();
1514
1536
  }
1515
1537
  this.buildLayerItems();
1538
+ this.layersListed = true;
1516
1539
  }
1517
1540
  if (!this.state.collapsed) {
1518
1541
  requestAnimationFrame(() => {
@@ -1595,7 +1618,7 @@ class LayerControl {
1595
1618
  if (this.targetLayers.length === 0) {
1596
1619
  const userAddedLayers = [];
1597
1620
  const backgroundLayerIds = [];
1598
- const useBasemapStyleDetection = this.basemapLayerIds !== null && this.basemapLayerIds.size > 0;
1621
+ const useBasemapStyleDetection = this.hasBasemapLayerIds();
1599
1622
  const userAddedSourceIds = useBasemapStyleDetection ? /* @__PURE__ */ new Set() : this.detectUserAddedSources();
1600
1623
  allLayerIds.forEach((layerId) => {
1601
1624
  const layer = this.map.getLayer(layerId);
@@ -2246,19 +2269,122 @@ class LayerControl {
2246
2269
  }
2247
2270
  }
2248
2271
  /**
2249
- * Expand the panel
2272
+ * Whether the panel is collapsed (closed). Before the control is added to a
2273
+ * map this reflects the `collapsed` option.
2274
+ * @returns True while the panel is closed
2275
+ */
2276
+ isCollapsed() {
2277
+ return this.state.collapsed;
2278
+ }
2279
+ /**
2280
+ * Expand (open) the panel. Before the control is added to a map this only
2281
+ * records the state, and the panel opens when it is built.
2250
2282
  */
2251
2283
  expand() {
2252
2284
  this.state.collapsed = false;
2285
+ if (!this.panel) return;
2253
2286
  this.panel.classList.add("expanded");
2254
2287
  this.updatePanelPosition();
2255
2288
  }
2256
2289
  /**
2257
- * Collapse the panel
2290
+ * Collapse (close) the panel.
2258
2291
  */
2259
2292
  collapse() {
2293
+ var _a;
2260
2294
  this.state.collapsed = true;
2261
- this.panel.classList.remove("expanded");
2295
+ (_a = this.panel) == null ? void 0 : _a.classList.remove("expanded");
2296
+ }
2297
+ /**
2298
+ * Update a layer row's visibility, opacity and name in place, without
2299
+ * touching the map or firing any callback. Use it to mirror changes made
2300
+ * outside the control (for example by an application store that owns layer
2301
+ * state) into an already-built panel without rebuilding it.
2302
+ *
2303
+ * Only layers the control already lists are updated, with one exception:
2304
+ * `"Background"` adds the Background row when the control has none (a style
2305
+ * whose basemap lives in style imports has no root basemap layers to detect,
2306
+ * so the control would otherwise show no Background row). It returns false
2307
+ * until the control has listed the map's layers (after `onAdd`, and after a
2308
+ * `basemapStyleUrl` fetch), since seeding state earlier would stop the
2309
+ * control from detecting layers on its own.
2310
+ *
2311
+ * @param layerId The layer ID, or `"Background"` for the basemap row
2312
+ * @param state The fields to change; omitted fields keep their value
2313
+ * @returns True if a layer state was updated or created
2314
+ */
2315
+ setLayerState(layerId, state) {
2316
+ if (!this.panel || !this.layersListed) return false;
2317
+ let current = this.state.layerStates[layerId];
2318
+ if (!current) {
2319
+ if (layerId !== "Background") return false;
2320
+ current = this.state.layerStates.Background = {
2321
+ visible: state.visible ?? true,
2322
+ opacity: state.opacity ?? 1,
2323
+ name: state.name ?? "Background"
2324
+ };
2325
+ this.buildLayerItems();
2326
+ }
2327
+ if (state.visible !== void 0) current.visible = state.visible;
2328
+ if (state.opacity !== void 0) current.opacity = state.opacity;
2329
+ if (state.name !== void 0) {
2330
+ current.name = state.name;
2331
+ if (this.state.customLayerNames.has(layerId)) {
2332
+ this.state.customLayerNames.set(layerId, state.name);
2333
+ }
2334
+ }
2335
+ for (const item of this.rowsByLayerId().get(layerId) ?? []) {
2336
+ const checkbox = item.querySelector(
2337
+ ":scope > .layer-control-row > .layer-control-checkbox"
2338
+ );
2339
+ if (checkbox) checkbox.checked = current.visible;
2340
+ const slider = item.querySelector(
2341
+ ":scope > .layer-control-row > .layer-control-opacity"
2342
+ );
2343
+ if (slider) {
2344
+ slider.value = String(current.opacity);
2345
+ slider.title = `Opacity: ${Math.round(current.opacity * 100)}%`;
2346
+ }
2347
+ const name = item.querySelector(
2348
+ ":scope > .layer-control-row > .layer-control-name"
2349
+ );
2350
+ if (name) {
2351
+ name.textContent = current.name;
2352
+ name.title = current.name;
2353
+ }
2354
+ }
2355
+ return true;
2356
+ }
2357
+ /**
2358
+ * Set the order the panel lists layers in, then redraw the rows.
2359
+ *
2360
+ * Layers whose native MapLibre layers are on the map are always listed in
2361
+ * map stacking order; this order applies to the rest (custom-adapter layers
2362
+ * with nothing on the map yet, such as hidden ones). Use it to put the panel
2363
+ * back after an application refused a drag or context-menu move, since the
2364
+ * control has already moved the row by then. Layers not listed keep their
2365
+ * relative order after the listed ones, and Background stays at the bottom.
2366
+ * A hidden custom layer whose adapter has other layers on the map keeps its
2367
+ * slot beside them in that adapter's list, since the adapter is the source of
2368
+ * truth for where it sits; this order breaks the remaining ties.
2369
+ *
2370
+ * @param layerIds Layer IDs, top of the panel first
2371
+ */
2372
+ setLayerOrder(layerIds) {
2373
+ const states = this.state.layerStates;
2374
+ const order = [];
2375
+ const seen = /* @__PURE__ */ new Set(["Background"]);
2376
+ for (const id of [...layerIds, ...Object.keys(states)]) {
2377
+ if (id in states && !seen.has(id)) {
2378
+ seen.add(id);
2379
+ order.push(id);
2380
+ }
2381
+ }
2382
+ this.layerOrderHint = order;
2383
+ const ordered = {};
2384
+ if ("Background" in states) ordered.Background = states.Background;
2385
+ for (const id of order) ordered[id] = states[id];
2386
+ this.state.layerStates = ordered;
2387
+ if (this.panel) this.buildLayerItems();
2262
2388
  }
2263
2389
  /**
2264
2390
  * Build layer items (called initially and when layers change)
@@ -3018,7 +3144,7 @@ class LayerControl {
3018
3144
  if (this.state.layerStates[layerId] !== void 0 && layerId !== "Background") {
3019
3145
  return true;
3020
3146
  }
3021
- if (this.basemapLayerIds !== null && this.basemapLayerIds.size > 0) {
3147
+ if (this.hasBasemapLayerIds()) {
3022
3148
  return !this.basemapLayerIds.has(layerId);
3023
3149
  }
3024
3150
  if (this.initialLayerIds !== null && !this.initialLayerIds.has(layerId)) {
@@ -5075,7 +5201,7 @@ class LayerControl {
5075
5201
  );
5076
5202
  let layersAdded = false;
5077
5203
  const newLayers = [];
5078
- const useBasemapStyleDetection = this.basemapLayerIds !== null && this.basemapLayerIds.size > 0;
5204
+ const useBasemapStyleDetection = this.hasBasemapLayerIds();
5079
5205
  const useInitialLayerDetection = !useBasemapStyleDetection && this.initialLayerIds !== null && this.initialLayerIds.size > 0;
5080
5206
  currentMapLayerIds.forEach((layerId) => {
5081
5207
  if (layerId !== "Background" && !this.state.layerStates[layerId]) {
@@ -5488,6 +5614,14 @@ class LayerControl {
5488
5614
  const userLayerIds = Object.keys(this.state.layerStates).filter(
5489
5615
  (id) => id !== "Background"
5490
5616
  );
5617
+ const hint = this.layerOrderHint;
5618
+ if (hint) {
5619
+ const rank = new Map(hint.map((id, index) => [id, index]));
5620
+ const keyIndex = new Map(userLayerIds.map((id, index) => [id, index]));
5621
+ userLayerIds.sort(
5622
+ (a, b) => (rank.get(a) ?? hint.length + keyIndex.get(a)) - (rank.get(b) ?? hint.length + keyIndex.get(b))
5623
+ );
5624
+ }
5491
5625
  const resolvedIndexOf = (id) => {
5492
5626
  var _a2;
5493
5627
  const direct = indexById.get(id);
@@ -6115,4 +6249,4 @@ export {
6115
6249
  rgbToHex as r,
6116
6250
  setLayerOpacity as s
6117
6251
  };
6118
- //# sourceMappingURL=LayerControl-dS3dARlw.js.map
6252
+ //# sourceMappingURL=LayerControl-CvH3r0T2.js.map