Skip to content

Commit 5dd5dfa

Browse files
committed
feat(tree): hook up expanded option view + minor populate handler tweak
1 parent e8da1ce commit 5dd5dfa

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/tree/__tests__/tree.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ describe('SchemaTree', () => {
11231123
tree.populate();
11241124

11251125
expect(onPopulate).toBeCalledTimes(1);
1126-
expect(onPopulate).toBeCalledWith(tree.root);
1126+
expect(onPopulate).toBeCalledWith(tree, tree.root);
11271127
});
11281128

11291129
test('given $ref resolution, should be called', () => {
@@ -1144,8 +1144,8 @@ describe('SchemaTree', () => {
11441144
tree.unwrap(tree.itemAt(1) as TreeListParentNode);
11451145

11461146
expect(onPopulate).toBeCalledTimes(2);
1147-
expect(onPopulate).nthCalledWith(1, tree.root);
1148-
expect(onPopulate).nthCalledWith(2, tree.itemAt(1));
1147+
expect(onPopulate).nthCalledWith(1, tree, tree.root);
1148+
expect(onPopulate).nthCalledWith(2, tree, tree.itemAt(1));
11491149
});
11501150

11511151
test('given expanding, should be called', () => {
@@ -1166,8 +1166,8 @@ describe('SchemaTree', () => {
11661166
tree.unwrap(tree.itemAt(0) as TreeListParentNode);
11671167

11681168
expect(onPopulate).toBeCalledTimes(2);
1169-
expect(onPopulate).nthCalledWith(1, tree.root);
1170-
expect(onPopulate).nthCalledWith(2, tree.itemAt(0));
1169+
expect(onPopulate).nthCalledWith(1, tree, tree.root);
1170+
expect(onPopulate).nthCalledWith(2, tree, tree.itemAt(0));
11711171
});
11721172
});
11731173
});

src/tree/tree.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type SchemaTreeRefDereferenceFn = (
2121
schema: JSONSchema4,
2222
) => Optional<JSONSchema4>;
2323

24-
export type SchemaTreePopulateHandler = (node: TreeListParentNode) => void;
24+
export type SchemaTreePopulateHandler = (tree: SchemaTree, node: TreeListParentNode) => void;
2525

2626
export type SchemaTreeOptions = {
2727
expandedDepth: number;
@@ -37,7 +37,11 @@ export class SchemaTree extends Tree {
3737
public treeOptions: SchemaTreeOptions;
3838

3939
constructor(public schema: JSONSchema4, public state: TreeState, opts: SchemaTreeOptions) {
40-
super();
40+
super({
41+
expanded: node =>
42+
(!(node.id in state.expanded) && SchemaTree.getLevel(node) <= opts.expandedDepth) ||
43+
state.expanded[node.id] === true,
44+
});
4145

4246
this.treeOptions = opts;
4347
}
@@ -66,7 +70,7 @@ export class SchemaTree extends Tree {
6670
});
6771
this.state.expanded = expanded;
6872
this.invalidate();
69-
this.treeOptions.onPopulate?.(this.root);
73+
this.treeOptions.onPopulate?.(this, this.root);
7074
}
7175

7276
public populateTreeFragment(parent: TreeListParentNode, schema: JSONSchema4, path: JsonPath, stepIn: boolean) {
@@ -88,7 +92,7 @@ export class SchemaTree extends Tree {
8892

8993
this.insertTreeFragment(stepIn ? this.stepIn(artificialRoot, parent) : artificialRoot.children, parent);
9094

91-
this.treeOptions.onPopulate?.(parent);
95+
this.treeOptions.onPopulate?.(this, parent);
9296
}
9397

9498
protected insertErrorNode(parent: TreeListParentNode, error: string) {
@@ -121,7 +125,7 @@ export class SchemaTree extends Tree {
121125
} else if (hasRefItems(schemaNode)) {
122126
this.populateRefFragment(node, [...path, 'items'], schemaNode.items.$ref);
123127
} else {
124-
throw new Error(`I do know not how not expand node ${path.join('.')}`);
128+
throw new Error(`I do know not how to expand this node ${path.join('.')}`);
125129
}
126130
} catch (ex) {
127131
this.insertErrorNode(node, ex.message);

0 commit comments

Comments
 (0)