/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { isObservable } from 'rxjs'; import { take, filter } from 'rxjs/operators'; import { BaseTreeControl } from './base-tree-control'; /** Nested tree control. Able to expand/collapse a subtree recursively for NestedNode type. */ export class NestedTreeControl extends BaseTreeControl { /** Construct with nested tree function getChildren. */ constructor(getChildren, options) { super(); this.getChildren = getChildren; this.options = options; if (this.options) { this.trackBy = this.options.trackBy; } } /** * Expands all dataNodes in the tree. * * To make this working, the `dataNodes` variable of the TreeControl must be set to all root level * data nodes of the tree. */ expandAll() { this.expansionModel.clear(); const allNodes = this.dataNodes.reduce((accumulator, dataNode) => [...accumulator, ...this.getDescendants(dataNode), dataNode], []); this.expansionModel.select(...allNodes.map(node => this._trackByValue(node))); } /** Gets a list of descendant dataNodes of a subtree rooted at given data node recursively. */ getDescendants(dataNode) { const descendants = []; this._getDescendants(descendants, dataNode); // Remove the node itself return descendants.splice(1); } /** A helper function to get descendants recursively. */ _getDescendants(descendants, dataNode) { descendants.push(dataNode); const childrenNodes = this.getChildren(dataNode); if (Array.isArray(childrenNodes)) { childrenNodes.forEach((child) => this._getDescendants(descendants, child)); } else if (isObservable(childrenNodes)) { // TypeScript as of version 3.5 doesn't seem to treat `Boolean` like a function that // returns a `boolean` specifically in the context of `filter`, so we manually clarify that. childrenNodes.pipe(take(1), filter(Boolean)) .subscribe(children => { for (const child of children) { this._getDescendants(descendants, child); } }); } } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmVzdGVkLXRyZWUtY29udHJvbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3NyYy9jZGsvdHJlZS9jb250cm9sL25lc3RlZC10cmVlLWNvbnRyb2wudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBQ0gsT0F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