|
| 1 | +import {Component, Injectable} from '@angular/core'; |
| 2 | +import {SelectionModel} from '@angular/cdk/collections'; |
| 3 | +import {FlatTreeControl} from '@angular/cdk/tree'; |
| 4 | +import {MatTreeFlattener, MatTreeFlatDataSource} from '@angular/material/tree'; |
| 5 | +import {of as ofObservable} from 'rxjs/observable/of'; |
| 6 | +import {Observable} from 'rxjs/Observable'; |
| 7 | +import {BehaviorSubject} from 'rxjs/BehaviorSubject'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Node for to-do item |
| 11 | + */ |
| 12 | +export class TodoItemNode { |
| 13 | + children: TodoItemNode[]; |
| 14 | + item: string; |
| 15 | +} |
| 16 | + |
| 17 | +/** Flat to-do item node with expandable and level information */ |
| 18 | +export class TodoItemFlatNode { |
| 19 | + item: string; |
| 20 | + level: number; |
| 21 | + expandable: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * The Json object for to-do list data. |
| 26 | + */ |
| 27 | +const TREE_DATA = { |
| 28 | + 'Reminders': [ |
| 29 | + 'Cook dinner', |
| 30 | + 'Read the Material Design spec', |
| 31 | + 'Upgrade Application to Angular' |
| 32 | + ], |
| 33 | + 'Groceries': { |
| 34 | + 'Organic eggs': null, |
| 35 | + 'Protein Powder': null, |
| 36 | + 'Almond Meal flour': null, |
| 37 | + 'Fruits': { |
| 38 | + 'Apple': null, |
| 39 | + 'Orange': null, |
| 40 | + 'Berries': ['Blueberry', 'Raspberry'] |
| 41 | + } |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * Checklist database, it can build a tree structured Json object. |
| 47 | + * Each node in Json object represents a to-do item or a category. |
| 48 | + * If a node is a category, it has children items and new items can be added under the category. |
| 49 | + */ |
| 50 | +@Injectable() |
| 51 | +export class ChecklistDatabase { |
| 52 | + dataChange: BehaviorSubject<TodoItemNode[]> = new BehaviorSubject<TodoItemNode[]>([]); |
| 53 | + |
| 54 | + get data(): TodoItemNode[] { return this.dataChange.value; } |
| 55 | + |
| 56 | + constructor() { |
| 57 | + this.initialize(); |
| 58 | + } |
| 59 | + |
| 60 | + initialize() { |
| 61 | + // Build the tree nodes from Json object. The result is a list of `TodoItemNode` with nested |
| 62 | + // file node as children. |
| 63 | + const data = this.buildFileTree(TREE_DATA, 0); |
| 64 | + |
| 65 | + // Notify the change. |
| 66 | + this.dataChange.next(data); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Build the file structure tree. The `value` is the Json object, or a sub-tree of a Json object. |
| 71 | + * The return value is the list of `TodoItemNode`. |
| 72 | + */ |
| 73 | + buildFileTree(value: any, level: number) { |
| 74 | + let data: any[] = []; |
| 75 | + for (let k in value) { |
| 76 | + let v = value[k]; |
| 77 | + let node = new TodoItemNode(); |
| 78 | + node.item = `${k}`; |
| 79 | + if (v === null || v === undefined) { |
| 80 | + // no action |
| 81 | + } else if (typeof v === 'object') { |
| 82 | + node.children = this.buildFileTree(v, level + 1); |
| 83 | + } else { |
| 84 | + node.item = v; |
| 85 | + } |
| 86 | + data.push(node); |
| 87 | + } |
| 88 | + return data; |
| 89 | + } |
| 90 | + |
| 91 | + /** Add an item to to-do list */ |
| 92 | + insertItem(parent: TodoItemNode, name: string) { |
| 93 | + const child = <TodoItemNode>{item: name}; |
| 94 | + if (parent.children) { |
| 95 | + parent.children.push(child); |
| 96 | + this.dataChange.next(this.data); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * @title Tree with checkboxes |
| 103 | + */ |
| 104 | +@Component({ |
| 105 | + moduleId: module.id, |
| 106 | + selector: 'tree-checklist-example', |
| 107 | + templateUrl: 'tree-checklist-example.html', |
| 108 | + styleUrls: ['tree-checklist-example.css'], |
| 109 | + providers: [ChecklistDatabase] |
| 110 | +}) |
| 111 | +export class TreeChecklistExample { |
| 112 | + /** Map from flat node to nested node. This helps us finding the nested node to be modified */ |
| 113 | + flatNodeMap: Map<TodoItemFlatNode, TodoItemNode> = new Map<TodoItemFlatNode, TodoItemNode>(); |
| 114 | + |
| 115 | + /** Map from nested node to flattened node. This helps us to keep the same object for selection */ |
| 116 | + nestedNodeMap: Map<TodoItemNode, TodoItemFlatNode> = new Map<TodoItemNode, TodoItemFlatNode>(); |
| 117 | + |
| 118 | + /** A selected parent node to be inserted */ |
| 119 | + selectedParent: TodoItemFlatNode | null = null; |
| 120 | + |
| 121 | + /** The new item's name */ |
| 122 | + newItemName: string = ''; |
| 123 | + |
| 124 | + treeControl: FlatTreeControl<TodoItemFlatNode>; |
| 125 | + |
| 126 | + treeFlattener: MatTreeFlattener<TodoItemNode, TodoItemFlatNode>; |
| 127 | + |
| 128 | + dataSource: MatTreeFlatDataSource<TodoItemNode, TodoItemFlatNode>; |
| 129 | + |
| 130 | + /** The selection for checklist */ |
| 131 | + checklistSelection = new SelectionModel<TodoItemFlatNode>(true /* multiple */); |
| 132 | + |
| 133 | + constructor(private database: ChecklistDatabase) { |
| 134 | + this.treeFlattener = new MatTreeFlattener(this.transformer, this.getLevel, |
| 135 | + this.isExpandable, this.getChildren); |
| 136 | + this.treeControl = new FlatTreeControl<TodoItemFlatNode>(this.getLevel, this.isExpandable); |
| 137 | + this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); |
| 138 | + |
| 139 | + database.dataChange.subscribe(data => { |
| 140 | + this.dataSource.data = data; |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + getLevel = (node: TodoItemFlatNode) => { return node.level; }; |
| 145 | + |
| 146 | + isExpandable = (node: TodoItemFlatNode) => { return node.expandable; }; |
| 147 | + |
| 148 | + getChildren = (node: TodoItemNode): Observable<TodoItemNode[]> => { |
| 149 | + return ofObservable(node.children); |
| 150 | + } |
| 151 | + |
| 152 | + hasChild = (_: number, _nodeData: TodoItemFlatNode) => { return _nodeData.expandable; }; |
| 153 | + |
| 154 | + /** |
| 155 | + * Transformer to convert nested node to flat node. Record the nodes in maps for later use. |
| 156 | + */ |
| 157 | + transformer = (node: TodoItemNode, level: number) => { |
| 158 | + let flatNode = this.nestedNodeMap.has(node) |
| 159 | + ? this.nestedNodeMap.get(node)! |
| 160 | + : new TodoItemFlatNode(); |
| 161 | + flatNode.item = node.item; |
| 162 | + flatNode.level = level; |
| 163 | + flatNode.expandable = !!node.children; |
| 164 | + this.flatNodeMap.set(flatNode, node); |
| 165 | + this.nestedNodeMap.set(node, flatNode); |
| 166 | + return flatNode; |
| 167 | + } |
| 168 | + |
| 169 | + /** Whether all the descendants of the node are selected */ |
| 170 | + descendantsAllSelected(node: TodoItemFlatNode): boolean { |
| 171 | + const descendants = this.treeControl.getDescendants(node); |
| 172 | + return descendants.every(child => this.checklistSelection.isSelected(child)); |
| 173 | + } |
| 174 | + |
| 175 | + /** Whether part of the descendants are selected */ |
| 176 | + descendantsPartiallySelected(node: TodoItemFlatNode): boolean { |
| 177 | + const descendants = this.treeControl.getDescendants(node); |
| 178 | + const result = descendants.some(child => this.checklistSelection.isSelected(child)); |
| 179 | + return result && !this.descendantsAllSelected(node); |
| 180 | + } |
| 181 | + |
| 182 | + /** Toggle the to-do item selection. Select/deselect all the descendants node */ |
| 183 | + todoItemSelectionToggle(node: TodoItemFlatNode): void { |
| 184 | + this.checklistSelection.toggle(node); |
| 185 | + const descendants = this.treeControl.getDescendants(node); |
| 186 | + this.checklistSelection.isSelected(node) |
| 187 | + ? this.checklistSelection.select(...descendants) |
| 188 | + : this.checklistSelection.deselect(...descendants); |
| 189 | + } |
| 190 | + |
| 191 | + /** Insert a new item with name `newItemName` to be under category `selectedParent`. */ |
| 192 | + addNode() { |
| 193 | + if (this.selectedParent) { |
| 194 | + const parentNode = this.flatNodeMap.get(this.selectedParent); |
| 195 | + this.database.insertItem(parentNode!, this.newItemName); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /** Select the category so we can insert the new item. */ |
| 200 | + setParent(node: TodoItemFlatNode) { |
| 201 | + this.selectedParent = node; |
| 202 | + } |
| 203 | +} |
0 commit comments