Skip to content

Commit

Permalink
fix(geo): layer clone options to prevent states from sticking
Browse files Browse the repository at this point in the history
(cherry picked from commit d99c31335632cb7a0913da16acb0543b77f12f73)
  • Loading branch information
aziz rabhi authored and alecarn committed Nov 26, 2024
1 parent e1ee741 commit d0b998b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
37 changes: 24 additions & 13 deletions packages/geo/src/lib/layer/shared/layer-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ export class LayerController extends LayerSelectionModel {

remove(...layers: AnyLayer[]): void {
const list = layers.reduce((list, layer) => {
if (this.isSelected(layer)) {
this.deselect(layer);
}
const result = this._remove(layer);
Array.isArray(result) ? list.push(...result) : list.push(result);
return list;
Expand Down Expand Up @@ -309,7 +312,7 @@ export class LayerController extends LayerSelectionModel {
this.selectBaseLayer(layer);
}
this._baseLayers.push(layer);
} else if (isLayerLinked(layer) && !layer.showInLayerList) {
} else if (this.isOtherLayer(layer)) {
this._otherLayers.push(layer);
} else if (this.isSystemLayer(layer) && !layer.showInLayerList) {
this._systemLayers.push(layer);
Expand All @@ -328,38 +331,46 @@ export class LayerController extends LayerSelectionModel {
}

private _remove(layer: AnyLayer): AnyLayer | AnyLayer[] {
if (this.isSystemLayer(layer)) {
this.removeSystemLayer(layer);
} else if (isBaseLayer(layer)) {
if (isBaseLayer(layer)) {
this.removeBaselayer(layer);
} else if (this.isOtherLayer(layer)) {
this.removeOtherLayer(layer);
} else if (this.isSystemLayer(layer)) {
this.removeSystemLayer(layer);
}

layer.remove(false);
return layer;
}

private removeBaselayer(layer: AnyLayer) {
const index = this._baseLayers.findIndex((lyr) => lyr.id === layer.id);
if (index === -1) {
return;
}
this._baseLayers.splice(index, 1);
this.removeIn(layer, this._baseLayers);
}

private removeSystemLayer(layer: AnyLayer) {
const index = this._systemLayers.findIndex(
(sysLayer) => sysLayer.id === layer.id
);
this.removeIn(layer, this._systemLayers);
}

private removeOtherLayer(layer: AnyLayer) {
this.removeIn(layer, this._otherLayers);
}

private removeIn(layer: AnyLayer, store: AnyLayer[]) {
const index = store.findIndex((sysLayer) => sysLayer.id === layer.id);
if (index === -1) {
return;
}
this._systemLayers.splice(index, 1);
store.splice(index, 1);
}

private isSystemLayer(layer: AnyLayer): boolean {
return isLayerItem(layer) && layer.isIgoInternalLayer;
}

private isOtherLayer(layer: AnyLayer): boolean {
return isLayerLinked(layer) && !layer.showInLayerList;
}

private clearBaselayers(): void {
this.baseLayers.forEach((layer) => this._remove(layer));
this._baseLayers = [];
Expand Down
16 changes: 9 additions & 7 deletions packages/geo/src/lib/layer/shared/layer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,28 @@ export class LayerService {
options: AnyLayerItemOptions,
detailedContextUri?: string
): Observable<Layer | undefined> {
const layerOptions = computeMVTOptionsOnHover(options);
if (layerOptions.source) {
return new Observable((d) => d.next(this.createLayer(layerOptions)));
let optionsCloned = { ...options };

computeMVTOptionsOnHover(optionsCloned);
if (optionsCloned.source) {
return new Observable((d) => d.next(this.createLayer(optionsCloned)));
}

return this.dataSourceService
.createAsyncDataSource(layerOptions.sourceOptions, detailedContextUri)
.createAsyncDataSource(optionsCloned.sourceOptions, detailedContextUri)
.pipe(
map((source) => {
if (source === undefined) {
const found = this.unavailableLayers.some(
(el) => el === layerOptions
(el) => el === optionsCloned
);
if (!found) {
this.unavailableLayers.push(layerOptions);
this.unavailableLayers.push(optionsCloned);
}

return undefined;
}
return this.createLayer(Object.assign(layerOptions, { source }));
return this.createLayer(Object.assign(optionsCloned, { source }));
})
);
}
Expand Down

0 comments on commit d0b998b

Please sign in to comment.