Skip to content

Commit c7a0106

Browse files
committed
fix(tree): fix tree lint error, and remove circular dependencies (#9877)
1 parent 2fd50bb commit c7a0106

File tree

9 files changed

+111
-125
lines changed

9 files changed

+111
-125
lines changed

src/cdk/tree/nested-node.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,29 @@ import {
1515
} from '@angular/core';
1616
import {takeUntil} from 'rxjs/operators/takeUntil';
1717

18-
import {CdkTree} from './tree';
18+
import {CdkTree, CdkTreeNode} from './tree';
1919
import {CdkTreeNodeOutlet} from './outlet';
20-
import {CdkTreeNode} from './node';
2120

2221
/**
2322
* Nested node is a child of `<cdk-tree>`. It works with nested tree.
2423
* By using `cdk-nested-tree-node` component in tree node template, children of the parent node will
2524
* be added in the `cdkTreeNodeOutlet` in tree node template.
2625
* For example:
26+
* ```html
2727
* <cdk-mested-tree-node>
2828
* {{node.name}}
2929
* <ng-template cdkTreeNodeOutlet></ng-template>
3030
* </cdk-tree-node>
31+
* ```
3132
* The children of node will be automatically added to `cdkTreeNodeOutlet`, the result dom will be
3233
* like this:
34+
* ```html
3335
* <cdk-nested-tree-node>
3436
* {{node.name}}
35-
* <cdk-nested-tree-node>{{child1.name}}</cdk-tree-node>
36-
* <cdk-nested-tree-node>{{child2.name}}</cdk-tree-node>
37+
* <cdk-nested-tree-node>{{child1.name}}</cdk-tree-node>
38+
* <cdk-nested-tree-node>{{child2.name}}</cdk-tree-node>
3739
* </cdk-tree-node>
40+
* ```
3841
*/
3942
@Directive({
4043
selector: 'cdk-nested-tree-node',

src/cdk/tree/node.ts

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {FocusableOption} from '@angular/cdk/a11y';
9-
import {
10-
Directive,
11-
ElementRef,
12-
Input,
13-
OnDestroy,
14-
TemplateRef
15-
} from '@angular/core';
16-
import {takeUntil} from 'rxjs/operators/takeUntil';
17-
import {Subject} from 'rxjs/Subject';
18-
import {CdkTree} from './tree';
19-
import {getTreeControlFunctionsMissingError} from './tree-errors';
8+
9+
import {Directive, TemplateRef} from '@angular/core';
2010

2111

2212
/** Context provided to the tree node component. */
@@ -58,79 +48,3 @@ export class CdkTreeNodeDef<T> {
5848
/** @docs-private */
5949
constructor(public template: TemplateRef<any>) {}
6050
}
61-
62-
63-
/**
64-
* Tree node for CdkTree. It contains the data in the tree node.
65-
*/
66-
@Directive({
67-
selector: 'cdk-tree-node',
68-
exportAs: 'cdkTreeNode',
69-
host: {
70-
'[attr.aria-expanded]': 'isExpanded',
71-
'[attr.aria-level]': 'level',
72-
'[attr.role]': 'role',
73-
'class': 'cdk-tree-node',
74-
},
75-
})
76-
export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
77-
/**
78-
* The most recently created `CdkTreeNode`. We save it in static variable so we can retrieve it
79-
* in `CdkTree` and set the data to it.
80-
*/
81-
static mostRecentTreeNode: CdkTreeNode<any> | null = null;
82-
83-
/** Subject that emits when the component has been destroyed. */
84-
protected _destroyed = new Subject<void>();
85-
86-
/** The tree node's data. */
87-
get data(): T { return this._data; }
88-
set data(value: T) {
89-
this._data = value;
90-
this._setRoleFromData();
91-
}
92-
protected _data: T;
93-
94-
get isExpanded(): boolean {
95-
return this._tree.treeControl.isExpanded(this._data);
96-
}
97-
98-
get level(): number {
99-
return this._tree.treeControl.getLevel ? this._tree.treeControl.getLevel(this._data) : 0;
100-
}
101-
102-
/**
103-
* The role of the node should be 'group' if it's an internal node,
104-
* and 'treeitem' if it's a leaf node.
105-
*/
106-
@Input() role: 'treeitem' | 'group' = 'treeitem';
107-
108-
constructor(protected _elementRef: ElementRef,
109-
protected _tree: CdkTree<T>) {
110-
CdkTreeNode.mostRecentTreeNode = this;
111-
}
112-
113-
ngOnDestroy() {
114-
this._destroyed.next();
115-
this._destroyed.complete();
116-
}
117-
118-
/** Focuses the menu item. Implements for FocusableOption. */
119-
focus(): void {
120-
this._elementRef.nativeElement.focus();
121-
}
122-
123-
private _setRoleFromData(): void {
124-
if (this._tree.treeControl.isExpandable) {
125-
this.role = this._tree.treeControl.isExpandable(this._data) ? 'group' : 'treeitem';
126-
} else {
127-
if (!this._tree.treeControl.getChildren) {
128-
throw getTreeControlFunctionsMissingError();
129-
}
130-
this._tree.treeControl.getChildren(this._data).pipe(takeUntil(this._destroyed))
131-
.subscribe(children => {
132-
this.role = children && children.length ? 'group' : 'treeitem';
133-
});
134-
}
135-
}
136-
}

src/cdk/tree/padding.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {coerceNumberProperty} from '@angular/cdk/coercion';
1111
import {Directive, ElementRef, Input, OnDestroy, Optional, Renderer2} from '@angular/core';
1212
import {takeUntil} from 'rxjs/operators/takeUntil';
1313
import {Subject} from 'rxjs/Subject';
14-
import {CdkTreeNode} from './node';
15-
import {CdkTree} from './tree';
14+
import {CdkTree, CdkTreeNode} from './tree';
1615

1716
/**
1817
* Indent for the children tree dataNodes.

src/cdk/tree/toggle.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
Directive,
1212
Input,
1313
} from '@angular/core';
14-
import {CdkTree} from './tree';
15-
import {CdkTreeNode} from './node';
14+
import {CdkTree, CdkTreeNode} from './tree';
1615

1716
/**
1817
* Node toggle to expand/collapse the node.

src/cdk/tree/tree-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {NgModule} from '@angular/core';
1212
import {CdkTreeNodeOutlet} from './outlet';
1313
import {CdkTreeNodePadding} from './padding';
1414
import {CdkTreeNodeToggle} from './toggle';
15-
import {CdkTree} from './tree';
16-
import {CdkTreeNodeDef, CdkTreeNode} from './node';
15+
import {CdkTree, CdkTreeNode} from './tree';
16+
import {CdkTreeNodeDef} from './node';
1717
import {CdkNestedTreeNode} from './nested-node';
1818

1919
const EXPORTED_DECLARATIONS = [

src/cdk/tree/tree.ts

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import {FocusableOption} from '@angular/cdk/a11y';
89
import {CollectionViewer, DataSource} from '@angular/cdk/collections';
910
import {
1011
ChangeDetectionStrategy,
1112
ChangeDetectorRef,
1213
Component,
1314
ContentChildren,
15+
Directive,
16+
ElementRef,
1417
Input,
1518
IterableDiffers,
1619
IterableDiffer,
@@ -26,15 +29,91 @@ import {BehaviorSubject} from 'rxjs/BehaviorSubject';
2629
import {takeUntil} from 'rxjs/operators/takeUntil';
2730
import {Subject} from 'rxjs/Subject';
2831
import {Subscription} from 'rxjs/Subscription';
29-
import {CdkTreeNodeDef, CdkTreeNode, CdkTreeNodeOutletContext} from './node';
32+
import {CdkTreeNodeDef, CdkTreeNodeOutletContext} from './node';
3033
import {CdkTreeNodeOutlet} from './outlet';
3134
import {TreeControl} from './control/tree-control';
3235
import {
3336
getTreeControlMissingError,
3437
getTreeMissingMatchingNodeDefError,
35-
getTreeMultipleDefaultNodeDefsError
38+
getTreeMultipleDefaultNodeDefsError,
39+
getTreeControlFunctionsMissingError
3640
} from './tree-errors';
3741

42+
/**
43+
* Tree node for CdkTree. It contains the data in the tree node.
44+
*/
45+
@Directive({
46+
selector: 'cdk-tree-node',
47+
exportAs: 'cdkTreeNode',
48+
host: {
49+
'[attr.aria-expanded]': 'isExpanded',
50+
'[attr.aria-level]': 'level',
51+
'[attr.role]': 'role',
52+
'class': 'cdk-tree-node',
53+
},
54+
})
55+
export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
56+
/**
57+
* The most recently created `CdkTreeNode`. We save it in static variable so we can retrieve it
58+
* in `CdkTree` and set the data to it.
59+
*/
60+
static mostRecentTreeNode: CdkTreeNode<any> | null = null;
61+
62+
/** Subject that emits when the component has been destroyed. */
63+
protected _destroyed = new Subject<void>();
64+
65+
/** The tree node's data. */
66+
get data(): T { return this._data; }
67+
set data(value: T) {
68+
this._data = value;
69+
this._setRoleFromData();
70+
}
71+
protected _data: T;
72+
73+
get isExpanded(): boolean {
74+
return this._tree.treeControl.isExpanded(this._data);
75+
}
76+
77+
get level(): number {
78+
return this._tree.treeControl.getLevel ? this._tree.treeControl.getLevel(this._data) : 0;
79+
}
80+
81+
/**
82+
* The role of the node should be 'group' if it's an internal node,
83+
* and 'treeitem' if it's a leaf node.
84+
*/
85+
@Input() role: 'treeitem' | 'group' = 'treeitem';
86+
87+
constructor(protected _elementRef: ElementRef,
88+
protected _tree: CdkTree<T>) {
89+
CdkTreeNode.mostRecentTreeNode = this;
90+
}
91+
92+
ngOnDestroy() {
93+
this._destroyed.next();
94+
this._destroyed.complete();
95+
}
96+
97+
/** Focuses the menu item. Implements for FocusableOption. */
98+
focus(): void {
99+
this._elementRef.nativeElement.focus();
100+
}
101+
102+
private _setRoleFromData(): void {
103+
if (this._tree.treeControl.isExpandable) {
104+
this.role = this._tree.treeControl.isExpandable(this._data) ? 'group' : 'treeitem';
105+
} else {
106+
if (!this._tree.treeControl.getChildren) {
107+
throw getTreeControlFunctionsMissingError();
108+
}
109+
this._tree.treeControl.getChildren(this._data).pipe(takeUntil(this._destroyed))
110+
.subscribe(children => {
111+
this.role = children && children.length ? 'group' : 'treeitem';
112+
});
113+
}
114+
}
115+
}
116+
38117

39118
/**
40119
* CDK tree component that connects with a data source to retrieve data of type `T` and renders
@@ -67,7 +146,7 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
67146
private _defaultNodeDef: CdkTreeNodeDef<T> | null;
68147

69148
/** Data subscription */
70-
private _dataSubscription : Subscription | null;
149+
private _dataSubscription: Subscription | null;
71150

72151
/**
73152
* Provides a stream containing the latest data array to render. Influenced by the tree's
@@ -91,9 +170,6 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
91170
/** The tree node template for the tree */
92171
@ContentChildren(CdkTreeNodeDef) _nodeDefs: QueryList<CdkTreeNodeDef<T>>;
93172

94-
/** The tree node inside the tree */
95-
@ContentChildren(CdkTreeNode, {descendants: true}) items: QueryList<CdkTreeNode<T>>;
96-
97173
// TODO(tinayuangao): Setup a listener for scrolling, emit the calculated view to viewChange.
98174
// Remove the MAX_VALUE in viewChange
99175
/**

src/demo-app/tree/tree-demo.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
import {Component} from '@angular/core';
1010
import {FlatTreeControl, NestedTreeControl} from '@angular/cdk/tree';
11-
import {MatTreeFlattener, MatTreeFlatDataSource, MatTreeNestedDataSource} from '@angular/material/tree';
11+
import {
12+
MatTreeFlattener,
13+
MatTreeFlatDataSource,
14+
MatTreeNestedDataSource
15+
} from '@angular/material/tree';
1216
import {of as ofObservable} from 'rxjs/observable/of';
1317

1418
import {JsonNode, JsonDatabase} from './json-database';
@@ -48,10 +52,9 @@ export class TreeDemo {
4852
this.nestedDataSource = new MatTreeNestedDataSource();
4953

5054
database.dataChange.subscribe(data => {
51-
console.log(`datachanges in demo`)
5255
this.dataSource.data = data;
5356
this.nestedDataSource.data = data;
54-
})
57+
});
5558
}
5659

5760
transformer = (node: JsonNode, level: number) => {
@@ -61,7 +64,7 @@ export class TreeDemo {
6164
flatNode.level = level;
6265
flatNode.expandable = !!node.children;
6366
return flatNode;
64-
};
67+
}
6568

6669
getLevel = (node: JsonFlatNode) => { return node.level; };
6770

src/lib/tree/data-source/flat-data-source.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import {BehaviorSubject} from 'rxjs/BehaviorSubject';
4242
* level: 2
4343
* },
4444
* NodeTwo: {
45-
* key: 'Pear',
46-
* expandable: false,
47-
* level: 2
48-
* }
45+
* key: 'Pear',
46+
* expandable: false,
47+
* level: 2
48+
* }
4949
* and the output flattened type is `F` with additional information.
5050
*/
5151
export class MatTreeFlattener<T, F> {

0 commit comments

Comments
 (0)