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 { SelectionModel } from '@angular/cdk/collections';
|
---|
9 | /** Base tree control. It has basic toggle/expand/collapse operations on a single data node. */
|
---|
10 | export class BaseTreeControl {
|
---|
11 | constructor() {
|
---|
12 | /** A selection model with multi-selection to track expansion status. */
|
---|
13 | this.expansionModel = new SelectionModel(true);
|
---|
14 | }
|
---|
15 | /** Toggles one single data node's expanded/collapsed state. */
|
---|
16 | toggle(dataNode) {
|
---|
17 | this.expansionModel.toggle(this._trackByValue(dataNode));
|
---|
18 | }
|
---|
19 | /** Expands one single data node. */
|
---|
20 | expand(dataNode) {
|
---|
21 | this.expansionModel.select(this._trackByValue(dataNode));
|
---|
22 | }
|
---|
23 | /** Collapses one single data node. */
|
---|
24 | collapse(dataNode) {
|
---|
25 | this.expansionModel.deselect(this._trackByValue(dataNode));
|
---|
26 | }
|
---|
27 | /** Whether a given data node is expanded or not. Returns true if the data node is expanded. */
|
---|
28 | isExpanded(dataNode) {
|
---|
29 | return this.expansionModel.isSelected(this._trackByValue(dataNode));
|
---|
30 | }
|
---|
31 | /** Toggles a subtree rooted at `node` recursively. */
|
---|
32 | toggleDescendants(dataNode) {
|
---|
33 | this.expansionModel.isSelected(this._trackByValue(dataNode)) ?
|
---|
34 | this.collapseDescendants(dataNode) :
|
---|
35 | this.expandDescendants(dataNode);
|
---|
36 | }
|
---|
37 | /** Collapse all dataNodes in the tree. */
|
---|
38 | collapseAll() {
|
---|
39 | this.expansionModel.clear();
|
---|
40 | }
|
---|
41 | /** Expands a subtree rooted at given data node recursively. */
|
---|
42 | expandDescendants(dataNode) {
|
---|
43 | let toBeProcessed = [dataNode];
|
---|
44 | toBeProcessed.push(...this.getDescendants(dataNode));
|
---|
45 | this.expansionModel.select(...toBeProcessed.map(value => this._trackByValue(value)));
|
---|
46 | }
|
---|
47 | /** Collapses a subtree rooted at given data node recursively. */
|
---|
48 | collapseDescendants(dataNode) {
|
---|
49 | let toBeProcessed = [dataNode];
|
---|
50 | toBeProcessed.push(...this.getDescendants(dataNode));
|
---|
51 | this.expansionModel.deselect(...toBeProcessed.map(value => this._trackByValue(value)));
|
---|
52 | }
|
---|
53 | _trackByValue(value) {
|
---|
54 | return this.trackBy ? this.trackBy(value) : value;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZS10cmVlLWNvbnRyb2wuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3RyZWUvY29udHJvbC9iYXNlLXRyZWUtY29udHJvbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFDSCxPQUFPLEVBQUMsY0FBYyxFQUFDLE1BQU0sMEJBQTBCLENBQUM7QUFJeEQsK0ZBQStGO0FBQy9GLE1BQU0sT0FBZ0IsZUFBZTtJQUFyQztRQVdFLHdFQUF3RTtRQUN4RSxtQkFBYyxHQUFzQixJQUFJLGNBQWMsQ0FBSSxJQUFJLENBQUMsQ0FBQztJQXVFbEUsQ0FBQztJQWpEQywrREFBK0Q7SUFDL0QsTUFBTSxDQUFDLFFBQVc7UUFDaEIsSUFBSSxDQUFDLGNBQWMsQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLGFBQWEsQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFDO0lBQzNELENBQUM7SUFFRCxvQ0FBb0M7SUFDcEMsTUFBTSxDQUFDLFFBQVc7UUFDaEIsSUFBSSxDQUFDLGNBQWMsQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLGFBQWEsQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFDO0lBQzNELENB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