Skip to content

Commit 7c14570

Browse files
committed
update: make providers global only within one tree - meaning each tree will have its own set of providers global across this tree
To accomplish this new component was introduced - BranchyComponent - that is also new name for a component
1 parent d1cc411 commit 7c14570

12 files changed

Lines changed: 61 additions & 48 deletions

demo/app.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import {bootstrap} from '@angular/platform-browser-dynamic';
22
import {Component} from '@angular/core';
3-
import {TreeComponent, TreeService} from '../ng2-tree';
4-
import {NodeDraggableService} from '../src/node-draggable.service';
5-
import {NodeMenuService} from '../src/node-menu.service';
3+
import {BranchyComponent} from 'ng2-branchy.ts';
64

75
@Component({
86
selector: 'app',
97
template: `
10-
<tree [model]="tree"></tree>
11-
<tree [model]="tree2"></tree>
8+
<branchy [model]="tree"></branchy>
9+
<branchy [model]="tree2"></branchy>
1210
`,
13-
directives: [TreeComponent]
11+
directives: [BranchyComponent]
1412
})
1513
class App {
1614
private tree: any = {
@@ -115,4 +113,4 @@ class App {
115113

116114
}
117115

118-
bootstrap(App, [TreeService, NodeDraggableService, NodeMenuService]);
116+
bootstrap(App);

ng2-branchy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src/branchy.component';

ng2-tree.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "ng2-tree",
2+
"name": "ng2-branchy",
33
"version": "1.0.0",
4-
"description": "angular2 component for representing ui tree structures",
5-
"main": "ng2-tree.js",
4+
"description": "angular2 component for visualizing a tree",
5+
"main": "ng2-branchy.js",
66
"scripts": {
77
"preinstall": "rm -rf ./typings && typings install",
88
"dev": "webpack-dev-server --inline --hot",
Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@ import {
1111
NodeMenuEvent,
1212
TreeModel,
1313
FoldingType,
14-
TreeEvent
14+
TreeEvent, NodeDraggableEventAction
1515
} from './types';
1616
import {NodeEditableDirective} from './node-editable.directive';
1717
import {NodeMenuComponent} from './node-menu.component';
18-
import Draggable from './node-draggable.directive';
1918
import {NodeDraggableService} from './node-draggable.service';
2019
import {NodeMenuService} from './node-menu.service';
20+
import {NodeDraggableDirective} from './node-draggable.directive';
2121

2222
@Component({
2323
selector: 'tree',
24-
styles: [require('./tree.component.styl')],
25-
template: require('./tree.component.html'),
26-
directives: [NodeEditableDirective, TreeComponent, NodeMenuComponent, Draggable, CORE_DIRECTIVES],
24+
styles: [require('./branchy.component.styl')],
25+
template: require('./branchy.component.html'),
26+
directives: [
27+
NodeEditableDirective,
28+
TreeComponent,
29+
NodeMenuComponent,
30+
NodeDraggableDirective,
31+
CORE_DIRECTIVES
32+
],
2733
})
28-
export class TreeComponent implements OnInit {
34+
class TreeComponent implements OnInit {
2935
@Input('model')
3036
private tree: TreeModel;
3137

@@ -70,12 +76,12 @@ export class TreeComponent implements OnInit {
7076

7177
private setUpDraggableEventHandler() {
7278
this.nodeDraggableService.draggableNodeEvents$
73-
.filter(event => event.action === 'remove')
79+
.filter(event => event.action === NodeDraggableEventAction.Remove)
7480
.filter(event => event.captured.element === this.element)
7581
.subscribe(event => this.onChildRemoved({node: event.captured.tree}, this.parentTree));
7682

7783
this.nodeDraggableService.draggableNodeEvents$
78-
.filter(event => event.action !== 'remove')
84+
.filter(event => event.action !== NodeDraggableEventAction.Remove)
7985
.filter(event => event.target === this.element)
8086
.filter(event => !this.hasChild(event.captured.tree))
8187
.subscribe(event => {
@@ -93,12 +99,12 @@ export class TreeComponent implements OnInit {
9399

94100
private moveNodeToThisTreeAndRemoveFromPreviousOne(event: NodeDraggableEvent): void {
95101
this.tree.children.push(event.captured.tree);
96-
this.nodeDraggableService.draggableNodeEvents$.next(_.merge(event, {action: 'remove'}));
102+
this.nodeDraggableService.draggableNodeEvents$.next(_.merge(event, {action: NodeDraggableEventAction.Remove}));
97103
}
98104

99105
private moveNodeToParentTreeAndRemoveFromPreviousOne(event: NodeDraggableEvent): void {
100106
this.parentTree.children.splice(this.indexInParent, 0, event.captured.tree);
101-
this.nodeDraggableService.draggableNodeEvents$.next(_.merge(event, {action: 'remove'}));
107+
this.nodeDraggableService.draggableNodeEvents$.next(_.merge(event, {action: NodeDraggableEventAction.Remove}));
102108
}
103109

104110
private isEditInProgress() {
@@ -114,7 +120,7 @@ export class TreeComponent implements OnInit {
114120
}
115121

116122
private isSiblingOf(child: TreeModel) {
117-
return _.includes(this.parentTree.children, child);
123+
return this.parentTree && _.includes(this.parentTree.children, child);
118124
}
119125

120126
private swapWithSibling(sibling: TreeModel): void {
@@ -132,7 +138,7 @@ export class TreeComponent implements OnInit {
132138
}
133139

134140
private switchFolding($event: any, tree: TreeModel): void {
135-
this.handleFoldingType($event.target.parentTree.parentNode, tree);
141+
this.handleFoldingType($event.target.parentNode.parentNode, tree);
136142
}
137143

138144
private foldingType(node: TreeModel): FoldingTypeCssClass {
@@ -257,3 +263,14 @@ export class TreeComponent implements OnInit {
257263
}
258264

259265
type FoldingTypeCssClass = 'node-expanded' | 'node-collapsed' | 'node-leaf';
266+
267+
@Component({
268+
selector: 'branchy',
269+
providers: [NodeMenuService, NodeDraggableService],
270+
template: `<tree [model]="tree"></tree>`,
271+
directives: [TreeComponent]
272+
})
273+
export class BranchyComponent {
274+
@Input('model')
275+
private tree: TreeModel
276+
}

src/node-draggable.directive.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {NodeDraggableService} from './node-draggable.service';
55
@Directive({
66
selector: '[nodeDraggable]'
77
})
8-
export default class NodeDraggableDirective implements OnDestroy {
8+
export class NodeDraggableDirective implements OnDestroy {
99
private static DATA_TRANSFER_STUB_DATA: string = 'some browsers enable drag-n-drop only when dataTransfer has data';
10-
10+
1111
@Input()
1212
private nodeDraggable: ElementRef;
1313

@@ -67,9 +67,7 @@ export default class NodeDraggableDirective implements OnDestroy {
6767

6868
this.removeClass('over-drop-target');
6969

70-
const capturedNode = this.nodeDraggableService.getCapturedNode();
71-
if (!this.containsElementAt(e.pageX, e.pageY)
72-
|| !capturedNode.canBeDroppedAt(this.nodeDraggable)) {
70+
if (!this.isDropPossible(e)) {
7371
return false;
7472
}
7573

@@ -78,6 +76,11 @@ export default class NodeDraggableDirective implements OnDestroy {
7876
}
7977
}
8078

79+
private isDropPossible(e: DragEvent) {
80+
const capturedNode = this.nodeDraggableService.getCapturedNode();
81+
return capturedNode && capturedNode.canBeDroppedAt(this.nodeDraggable) && this.containsElementAt(e.pageX, e.pageY);
82+
}
83+
8184
private handleDragEnd(e: DragEvent): any {
8285
this.removeClass('over-drop-target');
8386
this.nodeDraggableService.releaseCapturedNode();

src/node-menu.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component, EventEmitter, Output, Renderer, Inject, OnDestroy, OnInit} from '@angular/core';
22
import {CORE_DIRECTIVES} from '@angular/common';
3-
import {MouseButtons, NodeMenuItemAction, NodeMenuItemSelectedEvent, NodeMenuAction} from './types';
3+
import {MouseButtons, NodeMenuItemAction, NodeMenuItemSelectedEvent, NodeMenuAction, NodeMenuEvent} from './types';
44
import {NodeMenuService} from './node-menu.service';
55

66
@Component({
@@ -59,7 +59,12 @@ export class NodeMenuComponent implements OnInit, OnDestroy {
5959
const escapePressed = event instanceof KeyboardEvent && event.key === 'Escape';
6060

6161
if (escapePressed || mouseClicked) {
62-
this.nodeMenuService.nodeMenuEvents$.next({sender: event.target, action: NodeMenuAction.Close});
62+
const nodeMenuEvent: NodeMenuEvent = {
63+
sender: (event.target as HTMLElement),
64+
action: NodeMenuAction.Close
65+
};
66+
67+
this.nodeMenuService.nodeMenuEvents$.next(nodeMenuEvent);
6368
}
6469
}
6570

src/tree.service.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)