license import { Observable } 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 < T > extends BaseTreeControl < T > { /** Construct with nested tree function getChildren. */ constructor ( public getChildren ) { super ( ) ; } /**
     * 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 ) ; } /** Gets a list of descendant dataNodes of a subtree rooted at given data node recursively. */ getDescendants ( dataNode ) { const descendants = [ ] ; this . _getDescendants ( descendants , dataNode ) ; return descendants . splice ( 1 ) ; } /** A helper function to get descendants recursively. */ protected _getDescendants ( descendants , dataNode ) { descendants . push ( dataNode ) ; const childrenNodes = this . getChildren ( dataNode ) ; if ( Array . isArray ( childrenNodes ) ) { childrenNodes . forEach ( ( child ) => this . _getDescendants ( descendants , child ) ) ; } else if ( childrenNodes instanceof Observable ) { childrenNodes . pipe ( take ( 1 ) , filter ( Boolean ) ) . subscribe ( children => { children . forEach ( ( child ) => this . _getDescendants ( descendants , child ) ) ; } ) ; } } }