[6a3a178] | 1 | /**
|
---|
| 2 | * @license
|
---|
| 3 | * Copyright Google LLC All Rights Reserved.
|
---|
| 4 | *
|
---|
| 5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 6 | * found in the LICENSE file at https://angular.io/license
|
---|
| 7 | */
|
---|
| 8 | import { BaseTreeControl } from './base-tree-control';
|
---|
| 9 | /** Flat tree control. Able to expand/collapse a subtree recursively for flattened tree. */
|
---|
| 10 | export class FlatTreeControl extends BaseTreeControl {
|
---|
| 11 | /** Construct with flat tree data node functions getLevel and isExpandable. */
|
---|
| 12 | constructor(getLevel, isExpandable, options) {
|
---|
| 13 | super();
|
---|
| 14 | this.getLevel = getLevel;
|
---|
| 15 | this.isExpandable = isExpandable;
|
---|
| 16 | this.options = options;
|
---|
| 17 | if (this.options) {
|
---|
| 18 | this.trackBy = this.options.trackBy;
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
| 21 | /**
|
---|
| 22 | * Gets a list of the data node's subtree of descendent data nodes.
|
---|
| 23 | *
|
---|
| 24 | * To make this working, the `dataNodes` of the TreeControl must be flattened tree nodes
|
---|
| 25 | * with correct levels.
|
---|
| 26 | */
|
---|
| 27 | getDescendants(dataNode) {
|
---|
| 28 | const startIndex = this.dataNodes.indexOf(dataNode);
|
---|
| 29 | const results = [];
|
---|
| 30 | // Goes through flattened tree nodes in the `dataNodes` array, and get all descendants.
|
---|
| 31 | // The level of descendants of a tree node must be greater than the level of the given
|
---|
| 32 | // tree node.
|
---|
| 33 | // If we reach a node whose level is equal to the level of the tree node, we hit a sibling.
|
---|
| 34 | // If we reach a node whose level is greater than the level of the tree node, we hit a
|
---|
| 35 | // sibling of an ancestor.
|
---|
| 36 | for (let i = startIndex + 1; i < this.dataNodes.length && this.getLevel(dataNode) < this.getLevel(this.dataNodes[i]); i++) {
|
---|
| 37 | results.push(this.dataNodes[i]);
|
---|
| 38 | }
|
---|
| 39 | return results;
|
---|
| 40 | }
|
---|
| 41 | /**
|
---|
| 42 | * Expands all data nodes in the tree.
|
---|
| 43 | *
|
---|
| 44 | * To make this working, the `dataNodes` variable of the TreeControl must be set to all flattened
|
---|
| 45 | * data nodes of the tree.
|
---|
| 46 | */
|
---|
| 47 | expandAll() {
|
---|
| 48 | this.expansionModel.select(...this.dataNodes.map(node => this._trackByValue(node)));
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmxhdC10cmVlLWNvbnRyb2wuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3RyZWUvY29udHJvbC9mbGF0LXRyZWUtY29udHJvbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxPQUFPLEVBQUMsZUFBZSxFQUFDLE1BQU0scUJBQXFCLENBQUM7QUFPcEQsMkZBQTJGO0FBQzNGLE1BQU0sT0FBTyxlQUEwQixTQUFRLGVBQXFCO0lBRWxFLDhFQUE4RTtJQUM5RSxZQUNvQixRQUFpQyxFQUNqQyxZQUFzQyxFQUMvQyxPQUFzQztRQUMvQyxLQUFLLEVBQUUsQ0FBQztRQUhVLGFBQVEsR0FBUixRQUFRLENBQXlCO1FBQ2pDLGlCQUFZLEdBQVosWUFBWSxDQUEwQjtRQUMvQyxZQUFPLEdBQVAsT0FBTyxDQUErQjtRQUcvQyxJQUFJLElBQUksQ0FBQyxPQUFPLEVBQUU7WUFDaEIsSUFBSSxDQUFDLE9BQU8sR0FBRyxJQUFJLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQztTQUNyQztJQUNILENBQUM7SUFFRDs7Ozs7T0FLRztJQUNILGNBQWMsQ0FBQyxRQUFXO1FBQ3hCLE1BQU0sVUFBVSxHQUFHLElBQUksQ0FBQyxTQUFTLENBQUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO1FBQ3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