/** * @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 { BaseTreeControl } from './base-tree-control'; /** Flat tree control. Able to expand/collapse a subtree recursively for flattened tree. */ export class FlatTreeControl extends BaseTreeControl { /** Construct with flat tree data node functions getLevel and isExpandable. */ constructor(getLevel, isExpandable, options) { super(); this.getLevel = getLevel; this.isExpandable = isExpandable; this.options = options; if (this.options) { this.trackBy = this.options.trackBy; } } /** * Gets a list of the data node's subtree of descendent data nodes. * * To make this working, the `dataNodes` of the TreeControl must be flattened tree nodes * with correct levels. */ getDescendants(dataNode) { const startIndex = this.dataNodes.indexOf(dataNode); const results = []; // Goes through flattened tree nodes in the `dataNodes` array, and get all descendants. // The level of descendants of a tree node must be greater than the level of the given // tree node. // If we reach a node whose level is equal to the level of the tree node, we hit a sibling. // If we reach a node whose level is greater than the level of the tree node, we hit a // sibling of an ancestor. for (let i = startIndex + 1; i < this.dataNodes.length && this.getLevel(dataNode) < this.getLevel(this.dataNodes[i]); i++) { results.push(this.dataNodes[i]); } return results; } /** * Expands all data nodes in the tree. * * To make this working, the `dataNodes` variable of the TreeControl must be set to all flattened * data nodes of the tree. */ expandAll() { this.expansionModel.select(...this.dataNodes.map(node => this._trackByValue(node))); } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmxhdC10cmVlLWNvbnRyb2wuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3RyZWUvY29udHJvbC9mbGF0LXRyZWUtY29udHJvbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxPQUFPLEVBQUMsZUFBZSxFQUFDLE1BQU0scUJBQXFCLENBQUM7QUFPcEQsMkZBQTJGO0FBQzNGLE1BQU0sT0FBTyxlQUEwQixTQUFRLGVBQXFCO0lBQ2x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