maplibre-gl-layer-control 0.18.3 → 0.19.1

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,14 @@ 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
+ /** setLayerState calls made before the layers were listed, applied once they are. */
1383
+ __publicField(this, "pendingLayerStates", /* @__PURE__ */ new Map());
1384
+ /** Panel order set through setLayerOrder, used to order layers with nothing on the map. */
1385
+ __publicField(this, "layerOrderHint", null);
1378
1386
  __publicField(this, "widthFrame", null);
1379
1387
  // Exact-opacity input popup
1380
1388
  __publicField(this, "opacityInputEl", null);
@@ -1464,6 +1472,19 @@ class LayerControl {
1464
1472
  });
1465
1473
  }
1466
1474
  this.basemapStyleUrl = options.basemapStyleUrl || null;
1475
+ if (options.basemapLayerIds) {
1476
+ this.basemapLayerIds = new Set(options.basemapLayerIds);
1477
+ this.basemapLayerIdsProvided = true;
1478
+ }
1479
+ }
1480
+ /**
1481
+ * Whether basemap membership is decided by a known list of basemap layer IDs
1482
+ * (the `basemapLayerIds` option, or the layers of `basemapStyleUrl`) rather
1483
+ * than by heuristics. A fetched style only counts when it had layers; an
1484
+ * explicit list counts even when empty.
1485
+ */
1486
+ hasBasemapLayerIds() {
1487
+ return this.basemapLayerIds !== null && (this.basemapLayerIdsProvided || this.basemapLayerIds.size > 0);
1467
1488
  }
1468
1489
  /**
1469
1490
  * Called when the control is added to the map
@@ -1471,6 +1492,8 @@ class LayerControl {
1471
1492
  onAdd(map) {
1472
1493
  this.map = map;
1473
1494
  this.mapContainer = map.getContainer();
1495
+ this.layersListed = false;
1496
+ this.pendingLayerStates.clear();
1474
1497
  const style = this.map.getStyle();
1475
1498
  if (style && style.sources) {
1476
1499
  this.initialSourceIds = new Set(Object.keys(style.sources));
@@ -1498,6 +1521,7 @@ class LayerControl {
1498
1521
  this.autoDetectLayers();
1499
1522
  }
1500
1523
  this.buildLayerItems();
1524
+ this.finishListing();
1501
1525
  }).catch((error) => {
1502
1526
  console.warn(
1503
1527
  "Failed to fetch basemap style, falling back to heuristic detection:",
@@ -1507,12 +1531,14 @@ class LayerControl {
1507
1531
  this.autoDetectLayers();
1508
1532
  }
1509
1533
  this.buildLayerItems();
1534
+ this.finishListing();
1510
1535
  });
1511
1536
  } else {
1512
1537
  if (Object.keys(this.state.layerStates).length === 0) {
1513
1538
  this.autoDetectLayers();
1514
1539
  }
1515
1540
  this.buildLayerItems();
1541
+ this.finishListing();
1516
1542
  }
1517
1543
  if (!this.state.collapsed) {
1518
1544
  requestAnimationFrame(() => {
@@ -1595,7 +1621,7 @@ class LayerControl {
1595
1621
  if (this.targetLayers.length === 0) {
1596
1622
  const userAddedLayers = [];
1597
1623
  const backgroundLayerIds = [];
1598
- const useBasemapStyleDetection = this.basemapLayerIds !== null && this.basemapLayerIds.size > 0;
1624
+ const useBasemapStyleDetection = this.hasBasemapLayerIds();
1599
1625
  const userAddedSourceIds = useBasemapStyleDetection ? /* @__PURE__ */ new Set() : this.detectUserAddedSources();
1600
1626
  allLayerIds.forEach((layerId) => {
1601
1627
  const layer = this.map.getLayer(layerId);
@@ -2246,19 +2272,143 @@ class LayerControl {
2246
2272
  }
2247
2273
  }
2248
2274
  /**
2249
- * Expand the panel
2275
+ * Whether the panel is collapsed (closed). Before the control is added to a
2276
+ * map this reflects the `collapsed` option.
2277
+ * @returns True while the panel is closed
2278
+ */
2279
+ isCollapsed() {
2280
+ return this.state.collapsed;
2281
+ }
2282
+ /**
2283
+ * Expand (open) the panel. Before the control is added to a map this only
2284
+ * records the state, and the panel opens when it is built.
2250
2285
  */
2251
2286
  expand() {
2252
2287
  this.state.collapsed = false;
2288
+ if (!this.panel) return;
2253
2289
  this.panel.classList.add("expanded");
2254
2290
  this.updatePanelPosition();
2255
2291
  }
2256
2292
  /**
2257
- * Collapse the panel
2293
+ * Collapse (close) the panel.
2258
2294
  */
2259
2295
  collapse() {
2296
+ var _a;
2260
2297
  this.state.collapsed = true;
2261
- this.panel.classList.remove("expanded");
2298
+ (_a = this.panel) == null ? void 0 : _a.classList.remove("expanded");
2299
+ }
2300
+ /**
2301
+ * Update a layer row's visibility, opacity and name in place, without
2302
+ * touching the map or firing any callback. Use it to mirror changes made
2303
+ * outside the control (for example by an application store that owns layer
2304
+ * state) into an already-built panel without rebuilding it.
2305
+ *
2306
+ * Only layers the control already lists are updated, with one exception:
2307
+ * `"Background"` adds the Background row when the control has none (a style
2308
+ * whose basemap lives in style imports has no root basemap layers to detect,
2309
+ * so the control would otherwise show no Background row).
2310
+ *
2311
+ * Until the control has listed the map's layers (during `onAdd`, and while a
2312
+ * `basemapStyleUrl` fetch is in flight) the update is queued and applied
2313
+ * once they are listed, since seeding state earlier would stop the control
2314
+ * from detecting layers on its own.
2315
+ *
2316
+ * @param layerId The layer ID, or `"Background"` for the basemap row
2317
+ * @param state The fields to change; omitted fields keep their value
2318
+ * @returns True if a layer state was updated, created or queued; false
2319
+ * before the control is added to a map or for a layer it does not list
2320
+ */
2321
+ setLayerState(layerId, state) {
2322
+ if (!this.panel) return false;
2323
+ if (!this.layersListed) {
2324
+ const queued = { ...this.pendingLayerStates.get(layerId) };
2325
+ if (state.visible !== void 0) queued.visible = state.visible;
2326
+ if (state.opacity !== void 0) queued.opacity = state.opacity;
2327
+ if (state.name !== void 0) queued.name = state.name;
2328
+ this.pendingLayerStates.set(layerId, queued);
2329
+ return true;
2330
+ }
2331
+ let current = this.state.layerStates[layerId];
2332
+ if (!current) {
2333
+ if (layerId !== "Background") return false;
2334
+ current = this.state.layerStates.Background = {
2335
+ visible: state.visible ?? true,
2336
+ opacity: state.opacity ?? 1,
2337
+ name: state.name ?? "Background"
2338
+ };
2339
+ this.buildLayerItems();
2340
+ }
2341
+ if (state.visible !== void 0) current.visible = state.visible;
2342
+ if (state.opacity !== void 0) current.opacity = state.opacity;
2343
+ if (state.name !== void 0) {
2344
+ current.name = state.name;
2345
+ if (this.state.customLayerNames.has(layerId)) {
2346
+ this.state.customLayerNames.set(layerId, state.name);
2347
+ }
2348
+ }
2349
+ for (const item of this.rowsByLayerId().get(layerId) ?? []) {
2350
+ const checkbox = item.querySelector(
2351
+ ":scope > .layer-control-row > .layer-control-checkbox"
2352
+ );
2353
+ if (checkbox) checkbox.checked = current.visible;
2354
+ const slider = item.querySelector(
2355
+ ":scope > .layer-control-row > .layer-control-opacity"
2356
+ );
2357
+ if (slider) {
2358
+ slider.value = String(current.opacity);
2359
+ slider.title = `Opacity: ${Math.round(current.opacity * 100)}%`;
2360
+ }
2361
+ const name = item.querySelector(
2362
+ ":scope > .layer-control-row > .layer-control-name"
2363
+ );
2364
+ if (name) {
2365
+ name.textContent = current.name;
2366
+ name.title = current.name;
2367
+ }
2368
+ }
2369
+ return true;
2370
+ }
2371
+ /**
2372
+ * Mark the map's layers as listed and apply setLayerState calls queued
2373
+ * before then.
2374
+ */
2375
+ finishListing() {
2376
+ this.layersListed = true;
2377
+ const pending = [...this.pendingLayerStates];
2378
+ this.pendingLayerStates.clear();
2379
+ for (const [layerId, state] of pending) this.setLayerState(layerId, state);
2380
+ }
2381
+ /**
2382
+ * Set the order the panel lists layers in, then redraw the rows.
2383
+ *
2384
+ * Layers whose native MapLibre layers are on the map are always listed in
2385
+ * map stacking order; this order applies to the rest (custom-adapter layers
2386
+ * with nothing on the map yet, such as hidden ones). Use it to put the panel
2387
+ * back after an application refused a drag or context-menu move, since the
2388
+ * control has already moved the row by then. Layers not listed keep their
2389
+ * relative order after the listed ones, and Background stays at the bottom.
2390
+ * A hidden custom layer whose adapter has other layers on the map keeps its
2391
+ * slot beside them in that adapter's list, since the adapter is the source of
2392
+ * truth for where it sits; this order breaks the remaining ties.
2393
+ *
2394
+ * @param layerIds Layer IDs, top of the panel first
2395
+ */
2396
+ setLayerOrder(layerIds) {
2397
+ const states = this.state.layerStates;
2398
+ const order = [];
2399
+ const seen = /* @__PURE__ */ new Set(["Background"]);
2400
+ for (const id of [...layerIds, ...Object.keys(states)]) {
2401
+ if (id in states && !seen.has(id)) {
2402
+ seen.add(id);
2403
+ order.push(id);
2404
+ }
2405
+ }
2406
+ this.layerOrderHint = order;
2407
+ const ordered = {};
2408
+ if ("Background" in states) ordered.Background = states.Background;
2409
+ for (const id of order) ordered[id] = states[id];
2410
+ this.state.layerStates = ordered;
2411
+ if (this.panel) this.buildLayerItems();
2262
2412
  }
2263
2413
  /**
2264
2414
  * Build layer items (called initially and when layers change)
@@ -3018,7 +3168,7 @@ class LayerControl {
3018
3168
  if (this.state.layerStates[layerId] !== void 0 && layerId !== "Background") {
3019
3169
  return true;
3020
3170
  }
3021
- if (this.basemapLayerIds !== null && this.basemapLayerIds.size > 0) {
3171
+ if (this.hasBasemapLayerIds()) {
3022
3172
  return !this.basemapLayerIds.has(layerId);
3023
3173
  }
3024
3174
  if (this.initialLayerIds !== null && !this.initialLayerIds.has(layerId)) {
@@ -5075,7 +5225,7 @@ class LayerControl {
5075
5225
  );
5076
5226
  let layersAdded = false;
5077
5227
  const newLayers = [];
5078
- const useBasemapStyleDetection = this.basemapLayerIds !== null && this.basemapLayerIds.size > 0;
5228
+ const useBasemapStyleDetection = this.hasBasemapLayerIds();
5079
5229
  const useInitialLayerDetection = !useBasemapStyleDetection && this.initialLayerIds !== null && this.initialLayerIds.size > 0;
5080
5230
  currentMapLayerIds.forEach((layerId) => {
5081
5231
  if (layerId !== "Background" && !this.state.layerStates[layerId]) {
@@ -5488,6 +5638,14 @@ class LayerControl {
5488
5638
  const userLayerIds = Object.keys(this.state.layerStates).filter(
5489
5639
  (id) => id !== "Background"
5490
5640
  );
5641
+ const hint = this.layerOrderHint;
5642
+ if (hint) {
5643
+ const rank = new Map(hint.map((id, index) => [id, index]));
5644
+ const keyIndex = new Map(userLayerIds.map((id, index) => [id, index]));
5645
+ userLayerIds.sort(
5646
+ (a, b) => (rank.get(a) ?? hint.length + keyIndex.get(a)) - (rank.get(b) ?? hint.length + keyIndex.get(b))
5647
+ );
5648
+ }
5491
5649
  const resolvedIndexOf = (id) => {
5492
5650
  var _a2;
5493
5651
  const direct = indexById.get(id);
@@ -6115,4 +6273,4 @@ export {
6115
6273
  rgbToHex as r,
6116
6274
  setLayerOpacity as s
6117
6275
  };
6118
- //# sourceMappingURL=LayerControl-dS3dARlw.js.map
6276
+ //# sourceMappingURL=LayerControl-Ccyj0y-D.js.map