All files / views/trees/definition-tree/nodes group-container-node.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 6/6
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                    65x               22x               248x       19x 20x   20x 9x       10x      
import { DefinitionTreeNode } from "../definition-tree-node";
 
/**
 * Base class for nodes that list group nodes, such as 'Classes' or 'Concepts', and only show
 * the groups that have children.
 */
export abstract class GroupContainerNode extends DefinitionTreeNode {
	/**
	 * The group nodes that may be listed below this node, created once per node so that
	 * {@link hasChildren}, {@link getChildren} and the tree items of the listed group nodes share
	 * the memoized results of {@link DefinitionTreeNode.hasChildrenCached}. Safe to memoize
	 * because the provider recreates all nodes on refresh.
	 */
	private _groupNodes: DefinitionTreeNode[] | undefined;
 
	/**
	 * Create the group nodes that may be listed below this node, whether they have children or not.
	 * @returns The group nodes in the order they are displayed.
	 */
	protected abstract createGroupNodes(): DefinitionTreeNode[];
 
	/**
	 * Get the group nodes that may be listed below this node, whether they have children or not.
	 * @returns The group nodes in the order they are displayed.
	 */
	private _getGroupNodes(): DefinitionTreeNode[] {
		return this._groupNodes ??= this.createGroupNodes();
	}
 
	/**
	 * Indicates whether any group node has children, stopping at the first one that has.
	 * @returns `true` if the node has children, `false` otherwise.
	 */
	override hasChildren(): boolean {
		return this._getGroupNodes().some(node => node.hasChildrenCached());
	}
 
	/**
	 * Get the group nodes that have children.
	 * @returns The group nodes in the order they are displayed.
	 */
	override getChildren(): DefinitionTreeNode[] {
		return this._getGroupNodes().filter(node => node.hasChildrenCached());
	}
 
	override resolveNodeForUri(iri: string): DefinitionTreeNode | undefined {
		for (const child of this.getChildren()) {
			const found = child.resolveNodeForUri(iri);
 
			if (found) {
				return found;
			}
		}
 
		return undefined;
	}
}