|
1 | | -import {Input} from "angular2/core"; |
2 | | -import {Component} from "angular2/core"; |
| 1 | +import {Input, Component} from "angular2/core"; |
3 | 2 | import {CORE_DIRECTIVES} from "angular2/common"; |
4 | 3 |
|
5 | 4 | @Component({ |
6 | | - selector: 'ng2-tree', |
7 | | - template: ` |
| 5 | + selector: 'ng2-tree', |
| 6 | + template: ` |
8 | 7 | <ul class="tree"> |
9 | 8 | <li> |
10 | | - <span class="folding" (click)="switchFolding($event, tree)" [ngClass]="foldingType(tree)"></span><span class="node-value">{{tree.value}}</span> |
| 9 | + <span class="folding" (click)="switchFolding($event, tree)" [ngClass]="foldingType(tree)"></span> |
| 10 | + <span class="node-value">{{tree.value}}</span> |
11 | 11 | <ng2-tree *ngFor="#child of tree.children" [tree]="child"></ng2-tree> |
12 | 12 | </li> |
13 | 13 | </ul> |
14 | | - `, |
15 | | - directives: [Ng2Tree, CORE_DIRECTIVES] |
| 14 | + `, |
| 15 | + directives: [Ng2Tree, CORE_DIRECTIVES] |
16 | 16 | }) |
17 | 17 | export class Ng2Tree { |
18 | | - @Input() |
19 | | - private tree:any; |
| 18 | + private static COMPONENT_TAG_NAME: string = 'NG2-TREE'; |
| 19 | + private static FOLDING_NODE_EXPANDED: string = 'node-expanded'; |
| 20 | + private static FOLDING_NODE_COLLAPSED: string = 'node-collapsed'; |
| 21 | + private static FOLDING_NODE_LEAF: string = 'node-leaf'; |
20 | 22 |
|
21 | | - private switchFolding($event: any, tree: any): void { |
22 | | - this.handleFoldingType($event.target.parentNode, tree); |
23 | | - } |
24 | | - |
25 | | - private foldingType(node: any): any { |
26 | | - if (node.foldingType) { |
27 | | - return node.foldingType; |
28 | | - } |
| 23 | + @Input() |
| 24 | + private tree: any; |
29 | 25 |
|
30 | | - if (node.children) { |
31 | | - node.foldingType = 'node-expanded'; |
32 | | - return 'node-expanded'; |
| 26 | + private switchFolding($event: any, tree: any): void { |
| 27 | + this.handleFoldingType($event.target.parentNode, tree); |
33 | 28 | } |
34 | 29 |
|
35 | | - node.foldingType = 'leaf'; |
36 | | - return 'leaf'; |
37 | | - } |
| 30 | + private foldingType(node: any): any { |
| 31 | + if (node.foldingType) { |
| 32 | + return node.foldingType; |
| 33 | + } |
| 34 | + |
| 35 | + if (node.children) { |
| 36 | + node.foldingType = Ng2Tree.FOLDING_NODE_EXPANDED; |
| 37 | + } else { |
| 38 | + node.foldingType = Ng2Tree.FOLDING_NODE_LEAF; |
| 39 | + } |
38 | 40 |
|
39 | | - private nextFoldingType(node: any): string { |
40 | | - if (node.foldingType === 'node-expanded') { |
41 | | - return 'node-collapsed'; |
| 41 | + return node.foldingType; |
42 | 42 | } |
43 | 43 |
|
44 | | - return 'node-expanded'; |
45 | | - } |
| 44 | + private nextFoldingType(node: any): string { |
| 45 | + if (node.foldingType === Ng2Tree.FOLDING_NODE_EXPANDED) { |
| 46 | + return Ng2Tree.FOLDING_NODE_COLLAPSED; |
| 47 | + } |
46 | 48 |
|
47 | | - private handleFoldingType(parent: any, node: any) { |
48 | | - if (node.foldingType === 'leaf') { |
49 | | - return; |
50 | | - } |
51 | | - |
52 | | - let display = 'block'; |
53 | | - if (node.foldingType === 'node-expanded') { |
54 | | - display = 'none'; |
| 49 | + return Ng2Tree.FOLDING_NODE_EXPANDED; |
55 | 50 | } |
56 | 51 |
|
57 | | - node.foldingType = this.nextFoldingType(node); |
58 | | - for (let element of parent.childNodes) { |
59 | | - if (element.nodeName === 'NG2-TREE') { |
60 | | - element.style.display = display; |
61 | | - } |
| 52 | + private handleFoldingType(parent: any, node: any) { |
| 53 | + if (node.foldingType === Ng2Tree.FOLDING_NODE_LEAF) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + let display = 'block'; |
| 58 | + if (node.foldingType === Ng2Tree.FOLDING_NODE_EXPANDED) { |
| 59 | + display = 'none'; |
| 60 | + } |
| 61 | + |
| 62 | + node.foldingType = this.nextFoldingType(node); |
| 63 | + for (let element of parent.childNodes) { |
| 64 | + if (element.nodeName === Ng2Tree.COMPONENT_TAG_NAME) { |
| 65 | + element.style.display = display; |
| 66 | + } |
| 67 | + } |
62 | 68 | } |
63 | | - } |
64 | 69 | } |
0 commit comments