|
1 | | -import {Input, Component} from "angular2/core"; |
2 | | -import {CORE_DIRECTIVES} from "angular2/common"; |
| 1 | +import {Input, Component, OnInit} from 'angular2/core'; |
| 2 | +import {CORE_DIRECTIVES} from 'angular2/common'; |
| 3 | +import {Ng2TreeService} from './ng2-tree.service'; |
| 4 | +import {EditableNodeDirective} from './editable-node.directive.ts'; |
3 | 5 |
|
4 | 6 | @Component({ |
5 | | - selector: 'ng2-tree', |
6 | | - template: ` |
7 | | - <ul class="tree"> |
| 7 | + selector: 'ng2-tree', |
| 8 | + template: ` |
| 9 | + <ul class="tree" (keyup)="cancelActions($event)"> |
8 | 10 | <li> |
9 | | - <span class="folding" (click)="switchFolding($event, tree)" [ngClass]="foldingType(tree)"></span> |
10 | | - <span class="node-value">{{tree.value}}</span> |
| 11 | + <div (mouseup)="showMenu($event)"> |
| 12 | + <span class="folding" (click)="switchFolding($event, tree)" [ngClass]="foldingType(tree)"></span> |
| 13 | + <span class="node-value" *ngIf="!edit">{{tree.value}}</span> |
| 14 | + <input type="text" class="node-value" editable [nodeValue]="tree.value" (valueChanged)="applyNewValue($event, tree)" *ngIf="edit"/> |
| 15 | + </div> |
| 16 | + <div class="node-menu" *ngIf="isMenuVisible"> |
| 17 | + <ul class="node-menu-content"> |
| 18 | + <li (click)="rename($event, tree)">Rename node</li> |
| 19 | + <li>Add node</li> |
| 20 | + <li>Remove node</li> |
| 21 | + </ul> |
| 22 | + </div> |
11 | 23 | <ng2-tree *ngFor="#child of tree.children" [tree]="child"></ng2-tree> |
12 | 24 | </li> |
13 | 25 | </ul> |
14 | 26 | `, |
15 | | - directives: [Ng2Tree, CORE_DIRECTIVES] |
| 27 | + directives: [EditableNodeDirective, Ng2Tree, CORE_DIRECTIVES] |
16 | 28 | }) |
17 | | -export class Ng2Tree { |
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'; |
| 29 | +export class Ng2Tree implements OnInit { |
| 30 | + private static COMPONENT_TAG_NAME: string = 'NG2-TREE'; |
| 31 | + private static FOLDING_NODE_EXPANDED: string = 'node-expanded'; |
| 32 | + private static FOLDING_NODE_COLLAPSED: string = 'node-collapsed'; |
| 33 | + private static FOLDING_NODE_LEAF: string = 'node-leaf'; |
22 | 34 |
|
23 | | - @Input() |
24 | | - private tree: any; |
| 35 | + @Input() |
| 36 | + private tree: any; |
25 | 37 |
|
26 | | - private switchFolding($event: any, tree: any): void { |
27 | | - this.handleFoldingType($event.target.parentNode, tree); |
| 38 | + private treeService: Ng2TreeService; |
| 39 | + private isMenuVisible: boolean = false; |
| 40 | + private edit: boolean = false; |
| 41 | + |
| 42 | + constructor(treeService: Ng2TreeService) { |
| 43 | + this.treeService = treeService; |
| 44 | + } |
| 45 | + |
| 46 | + private switchFolding($event: any, tree: any): void { |
| 47 | + console.log('fold'); |
| 48 | + this.handleFoldingType($event.target.parentNode.parentNode, tree); |
| 49 | + } |
| 50 | + |
| 51 | + private foldingType(node: any): any { |
| 52 | + if (node.foldingType) { |
| 53 | + return node.foldingType; |
28 | 54 | } |
29 | 55 |
|
30 | | - private foldingType(node: any): any { |
31 | | - if (node.foldingType) { |
32 | | - return node.foldingType; |
33 | | - } |
| 56 | + if (node.children) { |
| 57 | + node.foldingType = Ng2Tree.FOLDING_NODE_EXPANDED; |
| 58 | + } else { |
| 59 | + node.foldingType = Ng2Tree.FOLDING_NODE_LEAF; |
| 60 | + } |
34 | 61 |
|
35 | | - if (node.children) { |
36 | | - node.foldingType = Ng2Tree.FOLDING_NODE_EXPANDED; |
37 | | - } else { |
38 | | - node.foldingType = Ng2Tree.FOLDING_NODE_LEAF; |
39 | | - } |
| 62 | + return node.foldingType; |
| 63 | + } |
40 | 64 |
|
41 | | - return node.foldingType; |
| 65 | + private nextFoldingType(node: any): string { |
| 66 | + if (node.foldingType === Ng2Tree.FOLDING_NODE_EXPANDED) { |
| 67 | + return Ng2Tree.FOLDING_NODE_COLLAPSED; |
42 | 68 | } |
43 | 69 |
|
44 | | - private nextFoldingType(node: any): string { |
45 | | - if (node.foldingType === Ng2Tree.FOLDING_NODE_EXPANDED) { |
46 | | - return Ng2Tree.FOLDING_NODE_COLLAPSED; |
47 | | - } |
| 70 | + return Ng2Tree.FOLDING_NODE_EXPANDED; |
| 71 | + } |
48 | 72 |
|
49 | | - return Ng2Tree.FOLDING_NODE_EXPANDED; |
| 73 | + private handleFoldingType(parent: any, node: any) { |
| 74 | + if (node.foldingType === Ng2Tree.FOLDING_NODE_LEAF) { |
| 75 | + return; |
50 | 76 | } |
51 | 77 |
|
52 | | - private handleFoldingType(parent: any, node: any) { |
53 | | - if (node.foldingType === Ng2Tree.FOLDING_NODE_LEAF) { |
54 | | - return; |
55 | | - } |
| 78 | + let display = 'block'; |
| 79 | + if (node.foldingType === Ng2Tree.FOLDING_NODE_EXPANDED) { |
| 80 | + display = 'none'; |
| 81 | + } |
56 | 82 |
|
57 | | - let display = 'block'; |
58 | | - if (node.foldingType === Ng2Tree.FOLDING_NODE_EXPANDED) { |
59 | | - display = 'none'; |
60 | | - } |
| 83 | + node.foldingType = this.nextFoldingType(node); |
| 84 | + for (let element of parent.childNodes) { |
| 85 | + if (element.nodeName === Ng2Tree.COMPONENT_TAG_NAME) { |
| 86 | + element.style.display = display; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
61 | 90 |
|
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 | | - } |
| 91 | + private rename($event: any, node: any) { |
| 92 | + if ($event.which === 1) { |
| 93 | + this.edit = true; |
| 94 | + this.isMenuVisible = false; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private showMenu($event: any): void { |
| 99 | + if ($event.which === 3) { |
| 100 | + this.isMenuVisible = !this.isMenuVisible; |
| 101 | + this.treeService.emitMenuEvent({sender: this, action: 'close'}) |
68 | 102 | } |
| 103 | + $event.preventDefault(); |
| 104 | + } |
| 105 | + |
| 106 | + private cancelActions($event: any): void { |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + private applyNewValue($event: any, node: any): void { |
| 111 | + node.value = $event.value; |
| 112 | + this.edit = false; |
| 113 | + } |
| 114 | + |
| 115 | + ngOnInit(): void { |
| 116 | + this.treeService.menuEventStream() |
| 117 | + .subscribe(menuEvent => { |
| 118 | + if (menuEvent.sender !== this && menuEvent.action === 'close') { |
| 119 | + this.isMenuVisible = false; |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
69 | 123 | } |
0 commit comments