Skip to content

Commit

Permalink
Refactor layer tree updates (#284)
Browse files Browse the repository at this point in the history
* Refactor layer moving

* Implement suggestions
  • Loading branch information
gjmooney authored Jan 8, 2025
1 parent 3f11f6f commit fbe439d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
3 changes: 2 additions & 1 deletion packages/base/src/panelview/components/layers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ function LayerGroupComponent(props: ILayerGroupProps): JSX.Element {
const groupState = await state.fetch(`jupytergis:${group.name}`);

setOpen(
((groupState as ReadonlyPartialJSONObject).expanded as boolean) ?? false
((groupState as ReadonlyPartialJSONObject)?.expanded as boolean) ??
false
);
};

Expand Down
35 changes: 15 additions & 20 deletions packages/schema/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,46 +523,41 @@ export class JupyterGISModel implements IJupyterGISModel {
layerTree: IJGISLayerItem[],
layerIdToRemove: string
) {
// Iterate over each item in the layerTree
for (let i = 0; i < layerTree.length; i++) {
const currentItem = layerTree[i];

// Check if the current item is a string and matches the target
if (typeof currentItem === 'string' && currentItem === layerIdToRemove) {
// Remove the item from the array
layerTree.splice(i, 1);
// Decrement i to ensure the next iteration processes the remaining items correctly
i--;
} else if (typeof currentItem !== 'string' && 'layers' in currentItem) {
// If the current item is a group, recursively call the function on its layers
this._removeLayerTreeLayer(currentItem.layers, layerIdToRemove);
}
}

this._removeLayerTreeItem(layerTree, layerIdToRemove, true);
this.sharedModel.layerTree = layerTree;
}

private _removeLayerTreeGroup(
layerTree: IJGISLayerItem[],
groupName: string
) {
this._removeLayerTreeItem(layerTree, groupName, false);
this.sharedModel.layerTree = layerTree;
}

private _removeLayerTreeItem(
layerTree: IJGISLayerItem[],
target: string,
isLayer: boolean
) {
// Iterate over each item in the layerTree
for (let i = 0; i < layerTree.length; i++) {
const currentItem = layerTree[i];
const matches = isLayer
? typeof currentItem === 'string' && currentItem === target
: typeof currentItem !== 'string' && currentItem.name === target;

// Check if the current item is a string and matches the target
if (typeof currentItem !== 'string' && currentItem.name === groupName) {
if (matches) {
// Remove the item from the array
layerTree.splice(i, 1);
// Decrement i to ensure the next iteration processes the remaining items correctly
i--;
} else if (typeof currentItem !== 'string' && 'layers' in currentItem) {
// If the current item is a group, recursively call the function on its layers
this._removeLayerTreeGroup(currentItem.layers, groupName);
this._removeLayerTreeItem(currentItem.layers, target, isLayer);
}
}

this.sharedModel.layerTree = layerTree;
}

renameLayerGroup(groupName: string, newName: string): void {
Expand Down

0 comments on commit fbe439d

Please sign in to comment.