maplibre-gl-layer-control 0.13.0 → 0.14.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.
package/dist/index.cjs CHANGED
@@ -686,6 +686,7 @@ class LayerControl {
686
686
  __publicField(this, "styleEditors");
687
687
  __publicField(this, "initialSourceIds", null);
688
688
  __publicField(this, "initialLayerIds", null);
689
+ __publicField(this, "initialLayerStates");
689
690
  // Panel width management
690
691
  __publicField(this, "minPanelWidth");
691
692
  __publicField(this, "maxPanelWidth");
@@ -729,11 +730,12 @@ class LayerControl {
729
730
  this.onLayerRename = options.onLayerRename;
730
731
  this.onLayerReorder = options.onLayerReorder;
731
732
  this.onLayerRemove = options.onLayerRemove;
733
+ this.initialLayerStates = options.layerStates || {};
732
734
  this.state = {
733
735
  collapsed: options.collapsed !== false,
734
736
  panelWidth: options.panelWidth || 350,
735
737
  activeStyleEditor: null,
736
- layerStates: options.layerStates || {},
738
+ layerStates: {},
737
739
  originalStyles: /* @__PURE__ */ new Map(),
738
740
  userInteractingWithSlider: false,
739
741
  backgroundLegendOpen: false,
@@ -757,7 +759,7 @@ class LayerControl {
757
759
  },
758
760
  isStyleOperationInProgress: false
759
761
  };
760
- this.targetLayers = options.layers || Object.keys(this.state.layerStates);
762
+ this.targetLayers = options.layers || Object.keys(this.initialLayerStates);
761
763
  this.styleEditors = /* @__PURE__ */ new Map();
762
764
  if (options.customLayerAdapters && options.customLayerAdapters.length > 0) {
763
765
  this.customLayerRegistry = new CustomLayerRegistry();
@@ -929,11 +931,11 @@ class LayerControl {
929
931
  const layerType = layer.type;
930
932
  const opacity = getLayerOpacity(this.map, layerId, layerType);
931
933
  const friendlyName = this.generateFriendlyName(layerId);
932
- this.state.layerStates[layerId] = {
934
+ this.state.layerStates[layerId] = this.mergeWithUserState(layerId, {
933
935
  visible: isVisible,
934
936
  opacity,
935
937
  name: friendlyName
936
- };
938
+ });
937
939
  });
938
940
  } else {
939
941
  const userLayers = [];
@@ -964,11 +966,11 @@ class LayerControl {
964
966
  const layerType = layer.type;
965
967
  const opacity = getLayerOpacity(this.map, layerId, layerType);
966
968
  const friendlyName = this.generateFriendlyName(layerId);
967
- this.state.layerStates[layerId] = {
969
+ this.state.layerStates[layerId] = this.mergeWithUserState(layerId, {
968
970
  visible: isVisible,
969
971
  opacity,
970
972
  name: friendlyName
971
- };
973
+ });
972
974
  });
973
975
  }
974
976
  if (this.customLayerRegistry) {
@@ -1089,6 +1091,21 @@ class LayerControl {
1089
1091
  name = name.replace(/\b\w/g, (char) => char.toUpperCase());
1090
1092
  return name || layerId;
1091
1093
  }
1094
+ /**
1095
+ * Merge auto-detected layer state with user-provided initial state.
1096
+ * User-provided values take precedence over detected values.
1097
+ */
1098
+ mergeWithUserState(layerId, detected) {
1099
+ const userState = this.initialLayerStates[layerId];
1100
+ if (!userState) return detected;
1101
+ return {
1102
+ visible: userState.visible ?? detected.visible,
1103
+ opacity: userState.opacity ?? detected.opacity,
1104
+ name: userState.name ?? detected.name,
1105
+ ...userState.isCustomLayer !== void 0 && { isCustomLayer: userState.isCustomLayer },
1106
+ ...userState.customLayerType !== void 0 && { customLayerType: userState.customLayerType }
1107
+ };
1108
+ }
1092
1109
  /**
1093
1110
  * Check if a layer ID belongs to a drawing library (Geoman, Mapbox GL Draw, etc.)
1094
1111
  * @param layerId The layer ID to check
@@ -2200,10 +2217,10 @@ class LayerControl {
2200
2217
  removeBtn.title = "Remove layer from map";
2201
2218
  removeBtn.addEventListener("click", (e) => {
2202
2219
  e.stopPropagation();
2203
- if (confirm("Are you sure you want to remove this layer?")) {
2220
+ this.showRemoveConfirmation(editor, () => {
2204
2221
  this.closeStyleEditor(layerId);
2205
2222
  this.removeLayer(layerId);
2206
- }
2223
+ });
2207
2224
  });
2208
2225
  const closeActionBtn = document.createElement("button");
2209
2226
  closeActionBtn.className = "style-editor-button style-editor-button-close";
@@ -2292,10 +2309,10 @@ class LayerControl {
2292
2309
  removeBtn.title = "Remove layer from map";
2293
2310
  removeBtn.addEventListener("click", (e) => {
2294
2311
  e.stopPropagation();
2295
- if (confirm("Are you sure you want to remove this layer?")) {
2312
+ this.showRemoveConfirmation(editor, () => {
2296
2313
  this.closeStyleEditor(layerId);
2297
2314
  this.removeLayer(layerId);
2298
- }
2315
+ });
2299
2316
  });
2300
2317
  const closeActionBtn = document.createElement("button");
2301
2318
  closeActionBtn.className = "style-editor-button style-editor-button-close";
@@ -2375,10 +2392,10 @@ class LayerControl {
2375
2392
  removeBtn.title = "Remove layer from map";
2376
2393
  removeBtn.addEventListener("click", (e) => {
2377
2394
  e.stopPropagation();
2378
- if (confirm("Are you sure you want to remove this layer?")) {
2395
+ this.showRemoveConfirmation(editor, () => {
2379
2396
  this.closeStyleEditor(layerId);
2380
2397
  this.removeLayer(layerId);
2381
- }
2398
+ });
2382
2399
  });
2383
2400
  const closeActionBtn = document.createElement("button");
2384
2401
  closeActionBtn.className = "style-editor-button style-editor-button-close";
@@ -3247,6 +3264,51 @@ class LayerControl {
3247
3264
  this.buildLayerItems();
3248
3265
  (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
3249
3266
  }
3267
+ /**
3268
+ * Show inline remove confirmation instead of browser confirm() dialog.
3269
+ * Works in Jupyter notebooks where native dialogs may not appear.
3270
+ */
3271
+ showRemoveConfirmation(container, onConfirm) {
3272
+ const existing = container.querySelector(".layer-control-remove-confirm");
3273
+ if (existing) {
3274
+ existing.remove();
3275
+ }
3276
+ const confirmEl = document.createElement("div");
3277
+ confirmEl.className = "layer-control-remove-confirm";
3278
+ const message = document.createElement("div");
3279
+ message.className = "layer-control-remove-confirm-message";
3280
+ message.innerHTML = `
3281
+ <svg class="layer-control-remove-confirm-icon" viewBox="0 0 20 20" fill="currentColor">
3282
+ <path fill-rule="evenodd" d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495zM10 5a.75.75 0 01.75.75v3.5a.75.75 0 01-1.5 0v-3.5A.75.75 0 0110 5zm0 9a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd"/>
3283
+ </svg>
3284
+ <span>Remove this layer?</span>
3285
+ `;
3286
+ const buttons = document.createElement("div");
3287
+ buttons.className = "layer-control-remove-confirm-buttons";
3288
+ const cancelBtn = document.createElement("button");
3289
+ cancelBtn.className = "layer-control-remove-confirm-btn layer-control-remove-confirm-btn-cancel";
3290
+ cancelBtn.textContent = "Cancel";
3291
+ cancelBtn.addEventListener("click", (e) => {
3292
+ e.stopPropagation();
3293
+ confirmEl.remove();
3294
+ });
3295
+ const confirmBtn = document.createElement("button");
3296
+ confirmBtn.className = "layer-control-remove-confirm-btn layer-control-remove-confirm-btn-confirm";
3297
+ confirmBtn.textContent = "Remove";
3298
+ confirmBtn.addEventListener("click", (e) => {
3299
+ e.stopPropagation();
3300
+ confirmEl.remove();
3301
+ onConfirm();
3302
+ });
3303
+ buttons.appendChild(cancelBtn);
3304
+ buttons.appendChild(confirmBtn);
3305
+ confirmEl.appendChild(message);
3306
+ confirmEl.appendChild(buttons);
3307
+ container.appendChild(confirmEl);
3308
+ requestAnimationFrame(() => {
3309
+ confirmEl.scrollIntoView({ behavior: "smooth", block: "nearest" });
3310
+ });
3311
+ }
3250
3312
  /**
3251
3313
  * Remove a layer from the map
3252
3314
  */