|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { ChangeDetectionStrategy } from "@angular/core"; |
| 4 | +import { Component } from "@angular/core"; |
| 5 | +import { EventEmitter } from "@angular/core"; |
| 6 | + |
| 7 | +// Import the application components and services. |
| 8 | +import { TreeNode } from "./tree.component"; |
| 9 | + |
| 10 | +// ----------------------------------------------------------------------------------- // |
| 11 | +// ----------------------------------------------------------------------------------- // |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: "my-tree-node", |
| 15 | + inputs: [ "node", "selectedNode" ], |
| 16 | + outputs: [ "selectEvents: select" ], |
| 17 | + host: { |
| 18 | + "[class.selected]": "( node === selectedNode )" |
| 19 | + }, |
| 20 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 21 | + styleUrls: [ "./tree-node.component.less" ], |
| 22 | + template: |
| 23 | + ` |
| 24 | + <a (click)="selectEvents.emit( node )" class="label"> |
| 25 | + {{ node.label }} |
| 26 | + </a> |
| 27 | +
|
| 28 | + <div *ngIf="node.children.length" class="children"> |
| 29 | +
|
| 30 | + <ng-template ngFor let-child [ngForOf]="node.children"> |
| 31 | +
|
| 32 | + <my-tree-node |
| 33 | + [node]="child" |
| 34 | + [selectedNode]="selectedNode" |
| 35 | + (select)="selectEvents.emit( $event )"> |
| 36 | + </my-tree-node> |
| 37 | +
|
| 38 | + </ng-template> |
| 39 | +
|
| 40 | + </div> |
| 41 | + ` |
| 42 | +}) |
| 43 | +export class TreeNodeComponent { |
| 44 | + |
| 45 | + public node: TreeNode | null; |
| 46 | + public selectedNode: TreeNode | null; |
| 47 | + public selectEvents: EventEmitter<TreeNode>; |
| 48 | + |
| 49 | + // I initialize the tree node component. |
| 50 | + constructor() { |
| 51 | + |
| 52 | + this.node = null; |
| 53 | + this.selectedNode = null; |
| 54 | + this.selectEvents = new EventEmitter(); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments