-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathother_trees.txt
251 lines (215 loc) · 8.63 KB
/
other_trees.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
return this.checklistSelection.selected.map(s => s.item).join(",");
<mat-form-field>
<input matInput placeholder="Search" (input)="filterChanged($event.target.value)">
</mat-form-field>
<mat-tree [dataSource]="dataSource" [treeControl]="treeControl">
<mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle matTreeNodePadding>
<button mat-icon-button disabled></button>
<mat-checkbox class="checklist-leaf-node"
[checked]="checklistSelection.isSelected(node)"
(change)="checklistSelection.toggle(node);">{{node.item}}</mat-checkbox>
</mat-tree-node>
<mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding>
<button mat-icon-button matTreeNodeToggle
[attr.aria-label]="'toggle ' + node.filename">
<mat-icon class="mat-icon-rtl-mirror">
{{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
</button>
<mat-checkbox [checked]="descendantsAllSelected(node)"
[indeterminate]="descendantsPartiallySelected(node)"
(change)="todoItemSelectionToggle(node)">{{node.item}}</mat-checkbox>
</mat-tree-node>
</mat-tree>
<!-- Copyright 2018 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license -->
import { SelectionModel } from '@angular/cdk/collections';
import { FlatTreeControl } from '@angular/cdk/tree';
import { Component, Injectable } from '@angular/core';
import { MatTreeFlatDataSource, MatTreeFlattener } from '@angular/material/tree';
import { BehaviorSubject } from 'rxjs';
/**
* Node for to-do item
*/
export class TreeItemNode {
children: TreeItemNode[];
item: string;
code: string;
}
/** Flat to-do item node with expandable and level information */
export class TreeItemFlatNode {
item: string;
level: number;
expandable: boolean;
code: string;
}
/**
* The Json object for to-do list data.
*/
const TREE_DATA = [
{ 'text': 'Turkiye', 'code': '0.1' },
{ 'text': 'İstanbul', 'code': '0.1.1' },
{ 'text': 'Beykoz', 'code': '0.1.1.1' },
{ 'text': 'Fatih', 'code': '0.1.1.1' },
{ 'text': 'Ankara', 'code': '0.1.2' },
{ 'text': 'Cankaya', 'code': '0.1.2.1' },
{ 'text': 'Etimesgut', 'code': '0.1.2.1' },
{ 'text': 'Elazig', 'code': '0.1.3' },
{ 'text': 'Palu', 'code': '0.1.3.1' },
{ 'text': 'Baskil', 'code': '0.1.3.2' },
{ 'text': 'Sivrice', 'code': '0.1.3.3' }
];
/**
* Checklist database, it can build a tree structured Json object.
* Each node in Json object represents a to-do item or a category.
* If a node is a category, it has children items and new items can be added under the category.
*/
@Injectable()
export class ChecklistDatabase {
dataChange = new BehaviorSubject<TreeItemNode[]>([]);
treeData: any[];
get data(): TreeItemNode[] { return this.dataChange.value; }
constructor() {
this.initialize();
}
initialize() {
this.treeData = TREE_DATA;
// Build the tree nodes from Json object. The result is a list of `TodoItemNode` with nested
// file node as children.
const data = this.buildFileTree(TREE_DATA, '0');
// Notify the change.
this.dataChange.next(data);
}
/**
* Build the file structure tree. The `value` is the Json object, or a sub-tree of a Json object.
* The return value is the list of `TodoItemNode`.
*/
buildFileTree(obj: any[], level: string): TreeItemNode[] {
return obj.filter(o =>
(<string>o.code).startsWith(level + '.')
&& (o.code.match(/\./g) || []).length === (level.match(/\./g) || []).length + 1
)
.map(o => {
const node = new TreeItemNode();
node.item = o.text;
node.code = o.code;
const children = obj.filter(so => (<string>so.code).startsWith(level + '.'));
if (children && children.length > 0) {
node.children = this.buildFileTree(children, o.code);
}
return node;
});
}
public filter(filterText: string) {
let filteredTreeData;
if (filterText) {
console.log(this.treeData);
filteredTreeData = this.treeData.filter(d => d.text.toLocaleLowerCase().indexOf(filterText.toLocaleLowerCase()) > -1);
Object.assign([], filteredTreeData).forEach(ftd => {
let str = (<string>ftd.code);
while (str.lastIndexOf('.') > -1) {
const index = str.lastIndexOf('.');
str = str.substring(0, index);
if (filteredTreeData.findIndex(t => t.code === str) === -1) {
const obj = this.treeData.find(d => d.code === str);
if (obj) {
filteredTreeData.push(obj);
}
}
}
});
} else {
filteredTreeData = this.treeData;
}
// Build the tree nodes from Json object. The result is a list of `TodoItemNode` with nested
// file node as children.
const data = this.buildFileTree(filteredTreeData, '0');
// Notify the change.
this.dataChange.next(data);
}
}
/**
* @title Tree with checkboxes
*/
@Component({
selector: 'tree-checklist-example',
templateUrl: 'tree-checklist-example.html',
styleUrls: ['tree-checklist-example.css'],
providers: [ChecklistDatabase]
})
export class TreeChecklistExample {
/** Map from flat node to nested node. This helps us finding the nested node to be modified */
flatNodeMap = new Map<TreeItemFlatNode, TreeItemNode>();
/** Map from nested node to flattened node. This helps us to keep the same object for selection */
nestedNodeMap = new Map<TreeItemNode, TreeItemFlatNode>();
/** A selected parent node to be inserted */
selectedParent: TreeItemFlatNode | null = null;
/** The new item's name */
newItemName = '';
treeControl: FlatTreeControl<TreeItemFlatNode>;
treeFlattener: MatTreeFlattener<TreeItemNode, TreeItemFlatNode>;
dataSource: MatTreeFlatDataSource<TreeItemNode, TreeItemFlatNode>;
/** The selection for checklist */
checklistSelection = new SelectionModel<TreeItemFlatNode>(false /* multiple */);
constructor(private database: ChecklistDatabase) {
this.treeFlattener = new MatTreeFlattener(this.transformer, this.getLevel,
this.isExpandable, this.getChildren);
this.treeControl = new FlatTreeControl<TreeItemFlatNode>(this.getLevel, this.isExpandable);
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
database.dataChange.subscribe(data => {
this.dataSource.data = data;
});
}
getLevel = (node: TreeItemFlatNode) => node.level;
isExpandable = (node: TreeItemFlatNode) => node.expandable;
getChildren = (node: TreeItemNode): TreeItemNode[] => node.children;
hasChild = (_: number, _nodeData: TreeItemFlatNode) => _nodeData.expandable;
/**
* Transformer to convert nested node to flat node. Record the nodes in maps for later use.
*/
transformer = (node: TreeItemNode, level: number) => {
const existingNode = this.nestedNodeMap.get(node);
const flatNode = existingNode && existingNode.item === node.item
? existingNode
: new TreeItemFlatNode();
flatNode.item = node.item;
flatNode.level = level;
flatNode.code = node.code;
flatNode.expandable = node.children && node.children.length > 0;
this.flatNodeMap.set(flatNode, node);
this.nestedNodeMap.set(node, flatNode);
return flatNode;
}
/** Whether all the descendants of the node are selected */
descendantsAllSelected(node: TreeItemFlatNode): boolean {
const descendants = this.treeControl.getDescendants(node);
return descendants.every(child => this.checklistSelection.isSelected(child));
}
/** Whether part of the descendants are selected */
descendantsPartiallySelected(node: TreeItemFlatNode): boolean {
const descendants = this.treeControl.getDescendants(node);
const result = descendants.some(child => this.checklistSelection.isSelected(child));
return result && !this.descendantsAllSelected(node);
}
/** Toggle the to-do item selection. Select/deselect all the descendants node */
todoItemSelectionToggle(node: TreeItemFlatNode): void {
this.checklistSelection.toggle(node);
const descendants = this.treeControl.getDescendants(node);
this.checklistSelection.isSelected(node)
? this.checklistSelection.select(...descendants)
: this.checklistSelection.deselect(...descendants);
}
filterChanged(filterText: string) {
this.database.filter(filterText);
if(filterText)
{
this.treeControl.expandAll();
} else {
this.treeControl.collapseAll();
}
}
}
/** Copyright 2018 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */