|
| 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 | + |
| 9 | +import {CollectionViewer, DataSource} from '@angular/cdk/collections'; |
| 10 | +import {FlatTreeControl, TreeControl} from '@angular/cdk/tree'; |
| 11 | +import {Observable} from 'rxjs/Observable'; |
| 12 | +import {merge} from 'rxjs/observable/merge'; |
| 13 | +import {map} from 'rxjs/operators/map'; |
| 14 | +import {take} from 'rxjs/operators/take'; |
| 15 | +import {BehaviorSubject} from 'rxjs/BehaviorSubject'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tree flattener to convert a normal type of node to node with children & level information. |
| 19 | + * |
| 20 | + * For example, the input data is nested: |
| 21 | + * SomeNode: { |
| 22 | + * key: 'Fruits', |
| 23 | + * children: [ |
| 24 | + * NodeOne: { |
| 25 | + * key: 'Apple', |
| 26 | + * }, |
| 27 | + * NodeTwo: { |
| 28 | + * key: 'Pear', |
| 29 | + * } |
| 30 | + * ] |
| 31 | + * } |
| 32 | + * After flattener flatten the tree, the structure will become |
| 33 | + * SomeNode: { |
| 34 | + * key: 'Fruits', |
| 35 | + * expandable: true, |
| 36 | + * level: 1 |
| 37 | + * }, |
| 38 | + * NodeOne: { |
| 39 | + * key: 'Apple', |
| 40 | + * expandable: false, |
| 41 | + * level: 2 |
| 42 | + * }, |
| 43 | + * NodeTwo: { |
| 44 | + * key: 'Pear', |
| 45 | + * expandable: false, |
| 46 | + * level: 2 |
| 47 | + * } |
| 48 | + */ |
| 49 | +export class MatTreeFlattener<T, F> { |
| 50 | + |
| 51 | + constructor(public transformFunction: (node: T, level: number) => F, |
| 52 | + public getLevel: (node: F) => number, |
| 53 | + public isExpandable: (node: F) => boolean, |
| 54 | + public getChildren: (node: T) => Observable<T[]>) {} |
| 55 | + |
| 56 | + _flattenNode(node: T, level: number, |
| 57 | + resultNodes: F[], parentMap: boolean[]): F[] { |
| 58 | + const flatNode = this.transformFunction(node, level); |
| 59 | + resultNodes.push(flatNode); |
| 60 | + |
| 61 | + if (this.isExpandable(flatNode)) { |
| 62 | + this.getChildren(node).pipe(take(1)).subscribe(children => { |
| 63 | + children.forEach((child, index) => { |
| 64 | + let childParentMap: boolean[] = parentMap.slice(); |
| 65 | + childParentMap.push(index != children.length - 1); |
| 66 | + this._flattenNode(child, level + 1, resultNodes, childParentMap); |
| 67 | + }); |
| 68 | + }); |
| 69 | + } |
| 70 | + return resultNodes; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Flatten a list of node type T to flattened version of node F. |
| 75 | + * Please note that type T may be nested, and the length of `structuredData` may be different |
| 76 | + * from that of returned list `F[]`. |
| 77 | + */ |
| 78 | + flattenNodes(structuredData: T[]): F[] { |
| 79 | + let resultNodes: F[] = []; |
| 80 | + structuredData.forEach(node => this._flattenNode(node, 0, resultNodes, [])); |
| 81 | + return resultNodes; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Expand flattened node with current expansion status. |
| 86 | + * The returned list may have different length. |
| 87 | + */ |
| 88 | + expandFlattenedNodes(nodes: F[], treeControl: TreeControl<F>): F[] { |
| 89 | + let results: F[] = []; |
| 90 | + let currentExpand: boolean[] = []; |
| 91 | + currentExpand[0] = true; |
| 92 | + |
| 93 | + nodes.forEach((node) => { |
| 94 | + let expand = true; |
| 95 | + for (let i = 0; i <= this.getLevel(node); i++) { |
| 96 | + expand = expand && currentExpand[i]; |
| 97 | + } |
| 98 | + if (expand) { |
| 99 | + results.push(node); |
| 100 | + } |
| 101 | + if (this.isExpandable(node)) { |
| 102 | + currentExpand[this.getLevel(node) + 1] = treeControl.isExpanded(node); |
| 103 | + } |
| 104 | + }); |
| 105 | + return results; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +/** |
| 111 | + * Data source for flat tree. |
| 112 | + * The data source need to handle expansion/collapsion of the tree node and change the data feed |
| 113 | + * to `MatTree`. |
| 114 | + * The nested tree nodes of type `T` are flattened through `MatTreeFlattener`, and converted |
| 115 | + * to type `F` for `MatTree` to consume. |
| 116 | + */ |
| 117 | +export class MatTreeFlatDataSource<T, F> implements DataSource<F> { |
| 118 | + _flattenedData = new BehaviorSubject<F[]>([]); |
| 119 | + get flattenedData() { return this._flattenedData.value; } |
| 120 | + |
| 121 | + _expandedData = new BehaviorSubject<F[]>([]); |
| 122 | + get expandedData() { return this._expandedData.value; } |
| 123 | + |
| 124 | + _data: BehaviorSubject<T[]>; |
| 125 | + get data() { return this._data.value; } |
| 126 | + set data(value: T[]) { |
| 127 | + this._data.next(value); |
| 128 | + this._flattenedData.next(this.treeFlattener.flattenNodes(this.data)); |
| 129 | + this.treeControl.dataNodes = this.flattenedData; |
| 130 | + } |
| 131 | + |
| 132 | + constructor(private treeControl: FlatTreeControl<F>, |
| 133 | + private treeFlattener: MatTreeFlattener<T, F>, |
| 134 | + initialData: T[] = []) { |
| 135 | + this._data = new BehaviorSubject<T[]>(initialData); |
| 136 | + } |
| 137 | + |
| 138 | + connect(collectionViewer: CollectionViewer): Observable<F[]> { |
| 139 | + return merge([ |
| 140 | + collectionViewer.viewChange, |
| 141 | + this.treeControl.expansionModel.onChange, |
| 142 | + this._flattenedData]) |
| 143 | + .pipe(map(() => { |
| 144 | + this._expandedData.next( |
| 145 | + this.treeFlattener.expandFlattenedNodes(this.flattenedData, this.treeControl)); |
| 146 | + return this.expandedData; |
| 147 | + })); |
| 148 | + } |
| 149 | + |
| 150 | + disconnect() { |
| 151 | + } |
| 152 | +} |
| 153 | + |
0 commit comments