/** * @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 { SelectionModel } from '@angular/cdk/collections'; /** Base tree control. It has basic toggle/expand/collapse operations on a single data node. */ export class BaseTreeControl { constructor() { /** A selection model with multi-selection to track expansion status. */ this.expansionModel = new SelectionModel(true); } /** Toggles one single data node's expanded/collapsed state. */ toggle(dataNode) { this.expansionModel.toggle(this._trackByValue(dataNode)); } /** Expands one single data node. */ expand(dataNode) { this.expansionModel.select(this._trackByValue(dataNode)); } /** Collapses one single data node. */ collapse(dataNode) { this.expansionModel.deselect(this._trackByValue(dataNode)); } /** Whether a given data node is expanded or not. Returns true if the data node is expanded. */ isExpanded(dataNode) { return this.expansionModel.isSelected(this._trackByValue(dataNode)); } /** Toggles a subtree rooted at `node` recursively. */ toggleDescendants(dataNode) { this.expansionModel.isSelected(this._trackByValue(dataNode)) ? this.collapseDescendants(dataNode) : this.expandDescendants(dataNode); } /** Collapse all dataNodes in the tree. */ collapseAll() { this.expansionModel.clear(); } /** Expands a subtree rooted at given data node recursively. */ expandDescendants(dataNode) { let toBeProcessed = [dataNode]; toBeProcessed.push(...this.getDescendants(dataNode)); this.expansionModel.select(...toBeProcessed.map(value => this._trackByValue(value))); } /** Collapses a subtree rooted at given data node recursively. */ collapseDescendants(dataNode) { let toBeProcessed = [dataNode]; toBeProcessed.push(...this.getDescendants(dataNode)); this.expansionModel.deselect(...toBeProcessed.map(value => this._trackByValue(value))); } _trackByValue(value) { return this.trackBy ? this.trackBy(value) : value; } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZS10cmVlLWNvbnRyb2wuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3RyZWUvY29udHJvbC9iYXNlLXRyZWUtY29udHJvbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUF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