/** * @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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmVzdGVkLXRyZWUtY29udHJvbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3NyYy9jZGsvdHJlZS9jb250cm9sL25lc3RlZC10cmVlLWNvbnRyb2wudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBQ0gsT0FB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