1 import {Component, Input, Output, EventEmitter, TemplateRef} from '@angular/core';
2 import {Tree, TreeNode} from './tree';
9 (nodeClicked)="nodeClicked($event)">
16 const rootNode = new TreeNode({
20 new TreeNode({id: 2, label: 'Child'}),
21 new TreeNode({id: 3, label: 'Child2'})
25 this.myTree = new Tree(rootNode);
28 nodeClicked(node: TreeNode) {
29 console.log('someone clicked on ' + node.label);
35 templateUrl: 'tree.component.html',
36 styleUrls: ['tree.component.css']
38 export class TreeComponent {
41 @Input() set tree(t: Tree) {
44 this._tree.nodeList(); // reindex nodes
52 @Input() showSelectors = false; // the checkboxes, etc.
53 @Input() disableRootSelector = false; // checkbox at the top of the tree
54 @Input() toggleOnClick = false; // selectNode vs toggleNodeSelection
55 @Input() rowTrailingTemplate: TemplateRef<any>;
57 @Output() nodeClicked: EventEmitter<TreeNode>;
58 @Output() nodeChecked: EventEmitter<TreeNode>;
61 this.nodeClicked = new EventEmitter<TreeNode>();
62 this.nodeChecked = new EventEmitter<TreeNode>();
65 displayNodes(): TreeNode[] {
66 if (!this.tree) { return []; }
67 return this.tree.nodeList(true);
70 handleNodeClick(node: TreeNode) {
71 if (this.disableRootSelector && node === this.tree.rootNode) {
74 if (this.toggleOnClick) {
75 this.tree.toggleNodeSelection(node);
77 this.tree.selectNode(node);
79 this.nodeClicked.emit(node);
82 handleNodeCheck(node: TreeNode) {
83 // If needed, add logic here to handle the case where
84 // a node's checkbox was clicked.
85 // since ngModel is node.selected, we don't need to set it ourselves
86 // this.handleNodeClick(node);
87 this.nodeClicked.emit(node);
92 this.tree.expandAll();
98 this.tree.collapseAll();
102 toggleSelections(ev: any) {
103 if (ev.target.checked) {
112 this.tree.nodeList().forEach(node => {
113 if (!(this.disableRootSelector && (node === this.tree.rootNode))) {
114 node.selected = true;
122 this.tree.nodeList().forEach(node => {
123 if (!(this.disableRootSelector && (node === this.tree.rootNode))) {
124 node.selected = false;