Skip to content

Commit 6c43391

Browse files
committed
feat(tree): unselect tree via controller
1 parent 0ffad0d commit 6c43391

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

src/demo/app/app.component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ declare const alertify: any;
5555
(nodeRemoved)="onNodeRemoved($event)"
5656
(nodeRenamed)="onNodeRenamed($event)"
5757
(nodeSelected)="onNodeSelected($event)"
58+
(nodeUnselected)="onNodeUnselected($event)"
5859
(nodeMoved)="onNodeMoved($event)"
5960
(nodeCreated)="onNodeFFSCreated($event)"
6061
(nodeExpanded)="onNodeExpanded($event)"
@@ -66,6 +67,7 @@ declare const alertify: any;
6667
<div class="tree-controlls">
6768
<p class="notice">Tree API exposed via TreeController</p>
6869
<button button (click)="handleActionOnFFS(13, 'select')">Select 'boot' node</button>
70+
<button button (click)="handleActionOnFFS(13, 'unselect')">Unselect 'boot' node</button>
6971
<button button (click)="handleActionOnFFS(13, 'allowSelection')">Allow selection of the 'boot' node</button>
7072
<button button (click)="handleActionOnFFS(13, 'forbidSelection')">Forbid selection of the 'boot' node</button>
7173
<button button (click)="handleActionOnFFS(2, 'collapse')">Collapse 'bin' node</button>
@@ -312,9 +314,6 @@ export class AppComponent implements OnInit {
312314
{
313315
value: 'boot',
314316
id: 13,
315-
settings: {
316-
selectionAllowed: false
317-
},
318317
children: [
319318
{
320319
value: 'grub',
@@ -615,6 +614,10 @@ export class AppComponent implements OnInit {
615614
AppComponent.logEvent(e, 'Selected');
616615
}
617616

617+
public onNodeUnselected(e: NodeEvent): void {
618+
AppComponent.logEvent(e, 'Unselected');
619+
}
620+
618621
public onMenuItemSelected(e: MenuItemSelectedEvent) {
619622
AppComponent.logEvent(e, `You selected ${e.selectedItem} menu item`);
620623
}

src/tree-controller.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export class TreeController {
2121
}
2222
}
2323

24+
public unselect(): void {
25+
if (this.isSelected()) {
26+
this.component.onNodeUnselected({ button: MouseButtons.Left });
27+
}
28+
}
29+
2430
public isSelected(): boolean {
2531
return this.component.isSelected;
2632
}

src/tree-internal.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
193193
}
194194
}
195195

196+
public onNodeUnselected(e: { button: number }): void {
197+
if (!this.tree.selectionAllowed) {
198+
return;
199+
}
200+
201+
if (EventUtils.isLeftButtonClicked(e as MouseEvent)) {
202+
this.isSelected = false;
203+
this.treeService.fireNodeUnselected(this.tree);
204+
}
205+
}
206+
196207
public showRightMenu(e: MouseEvent): void {
197208
if (!this.tree.hasRightMenu()) {
198209
return;

src/tree.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export class TreeComponent implements OnInit, OnChanges, OnDestroy {
4343

4444
@Output() public nodeSelected: EventEmitter<any> = new EventEmitter();
4545

46+
@Output() public nodeUnselected: EventEmitter<any> = new EventEmitter();
47+
4648
@Output() public nodeMoved: EventEmitter<any> = new EventEmitter();
4749

4850
@Output() public nodeExpanded: EventEmitter<any> = new EventEmitter();
@@ -100,6 +102,12 @@ export class TreeComponent implements OnInit, OnChanges, OnDestroy {
100102
})
101103
);
102104

105+
this.subscriptions.push(
106+
this.treeService.nodeUnselected$.subscribe((e: NodeEvent) => {
107+
this.nodeUnselected.emit(e);
108+
})
109+
);
110+
103111
this.subscriptions.push(
104112
this.treeService.nodeMoved$.subscribe((e: NodeEvent) => {
105113
this.nodeMoved.emit(e);

src/tree.events.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export class NodeSelectedEvent extends NodeEvent {
1111
}
1212
}
1313

14+
export class NodeUnselectedEvent extends NodeEvent {
15+
public constructor(node: Tree) {
16+
super(node);
17+
}
18+
}
19+
1420
export class NodeDestructiveEvent extends NodeEvent {
1521
public constructor(node: Tree) {
1622
super(node);

src/tree.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
NodeRemovedEvent,
1111
NodeRenamedEvent,
1212
NodeSelectedEvent,
13-
NodeUncheckedEvent
13+
NodeUncheckedEvent,
14+
NodeUnselectedEvent
1415
} from './tree.events';
1516
import { RenamableNode } from './tree.types';
1617
import { Tree } from './tree';
@@ -29,6 +30,7 @@ export class TreeService {
2930
public nodeRenamed$: Subject<NodeRenamedEvent> = new Subject<NodeRenamedEvent>();
3031
public nodeCreated$: Subject<NodeCreatedEvent> = new Subject<NodeCreatedEvent>();
3132
public nodeSelected$: Subject<NodeSelectedEvent> = new Subject<NodeSelectedEvent>();
33+
public nodeUnselected$: Subject<NodeUnselectedEvent> = new Subject<NodeUnselectedEvent>();
3234
public nodeExpanded$: Subject<NodeExpandedEvent> = new Subject<NodeExpandedEvent>();
3335
public nodeCollapsed$: Subject<NodeCollapsedEvent> = new Subject<NodeCollapsedEvent>();
3436
public menuItemSelected$: Subject<MenuItemSelectedEvent> = new Subject<MenuItemSelectedEvent>();
@@ -59,6 +61,10 @@ export class TreeService {
5961
this.nodeSelected$.next(new NodeSelectedEvent(tree));
6062
}
6163

64+
public fireNodeUnselected(tree: Tree): void {
65+
this.nodeUnselected$.next(new NodeUnselectedEvent(tree));
66+
}
67+
6268
public fireNodeRenamed(oldValue: RenamableNode | string, tree: Tree): void {
6369
this.nodeRenamed$.next(new NodeRenamedEvent(tree, oldValue, tree.value));
6470
}

0 commit comments

Comments
 (0)