Skip to content

Commit

Permalink
feat(tree): unselect tree via controller
Browse files Browse the repository at this point in the history
  • Loading branch information
rychkog committed Feb 20, 2018
1 parent 0ffad0d commit 6c43391
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/demo/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ declare const alertify: any;
(nodeRemoved)="onNodeRemoved($event)"
(nodeRenamed)="onNodeRenamed($event)"
(nodeSelected)="onNodeSelected($event)"
(nodeUnselected)="onNodeUnselected($event)"
(nodeMoved)="onNodeMoved($event)"
(nodeCreated)="onNodeFFSCreated($event)"
(nodeExpanded)="onNodeExpanded($event)"
Expand All @@ -66,6 +67,7 @@ declare const alertify: any;
<div class="tree-controlls">
<p class="notice">Tree API exposed via TreeController</p>
<button button (click)="handleActionOnFFS(13, 'select')">Select 'boot' node</button>
<button button (click)="handleActionOnFFS(13, 'unselect')">Unselect 'boot' node</button>
<button button (click)="handleActionOnFFS(13, 'allowSelection')">Allow selection of the 'boot' node</button>
<button button (click)="handleActionOnFFS(13, 'forbidSelection')">Forbid selection of the 'boot' node</button>
<button button (click)="handleActionOnFFS(2, 'collapse')">Collapse 'bin' node</button>
Expand Down Expand Up @@ -312,9 +314,6 @@ export class AppComponent implements OnInit {
{
value: 'boot',
id: 13,
settings: {
selectionAllowed: false
},
children: [
{
value: 'grub',
Expand Down Expand Up @@ -615,6 +614,10 @@ export class AppComponent implements OnInit {
AppComponent.logEvent(e, 'Selected');
}

public onNodeUnselected(e: NodeEvent): void {
AppComponent.logEvent(e, 'Unselected');
}

public onMenuItemSelected(e: MenuItemSelectedEvent) {
AppComponent.logEvent(e, `You selected ${e.selectedItem} menu item`);
}
Expand Down
6 changes: 6 additions & 0 deletions src/tree-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export class TreeController {
}
}

public unselect(): void {
if (this.isSelected()) {
this.component.onNodeUnselected({ button: MouseButtons.Left });
}
}

public isSelected(): boolean {
return this.component.isSelected;
}
Expand Down
11 changes: 11 additions & 0 deletions src/tree-internal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
}
}

public onNodeUnselected(e: { button: number }): void {
if (!this.tree.selectionAllowed) {
return;
}

if (EventUtils.isLeftButtonClicked(e as MouseEvent)) {
this.isSelected = false;
this.treeService.fireNodeUnselected(this.tree);
}
}

public showRightMenu(e: MouseEvent): void {
if (!this.tree.hasRightMenu()) {
return;
Expand Down
8 changes: 8 additions & 0 deletions src/tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class TreeComponent implements OnInit, OnChanges, OnDestroy {

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

@Output() public nodeUnselected: EventEmitter<any> = new EventEmitter();

@Output() public nodeMoved: EventEmitter<any> = new EventEmitter();

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

this.subscriptions.push(
this.treeService.nodeUnselected$.subscribe((e: NodeEvent) => {
this.nodeUnselected.emit(e);
})
);

this.subscriptions.push(
this.treeService.nodeMoved$.subscribe((e: NodeEvent) => {
this.nodeMoved.emit(e);
Expand Down
6 changes: 6 additions & 0 deletions src/tree.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export class NodeSelectedEvent extends NodeEvent {
}
}

export class NodeUnselectedEvent extends NodeEvent {
public constructor(node: Tree) {
super(node);
}
}

export class NodeDestructiveEvent extends NodeEvent {
public constructor(node: Tree) {
super(node);
Expand Down
8 changes: 7 additions & 1 deletion src/tree.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
NodeRemovedEvent,
NodeRenamedEvent,
NodeSelectedEvent,
NodeUncheckedEvent
NodeUncheckedEvent,
NodeUnselectedEvent
} from './tree.events';
import { RenamableNode } from './tree.types';
import { Tree } from './tree';
Expand All @@ -29,6 +30,7 @@ export class TreeService {
public nodeRenamed$: Subject<NodeRenamedEvent> = new Subject<NodeRenamedEvent>();
public nodeCreated$: Subject<NodeCreatedEvent> = new Subject<NodeCreatedEvent>();
public nodeSelected$: Subject<NodeSelectedEvent> = new Subject<NodeSelectedEvent>();
public nodeUnselected$: Subject<NodeUnselectedEvent> = new Subject<NodeUnselectedEvent>();
public nodeExpanded$: Subject<NodeExpandedEvent> = new Subject<NodeExpandedEvent>();
public nodeCollapsed$: Subject<NodeCollapsedEvent> = new Subject<NodeCollapsedEvent>();
public menuItemSelected$: Subject<MenuItemSelectedEvent> = new Subject<MenuItemSelectedEvent>();
Expand Down Expand Up @@ -59,6 +61,10 @@ export class TreeService {
this.nodeSelected$.next(new NodeSelectedEvent(tree));
}

public fireNodeUnselected(tree: Tree): void {
this.nodeUnselected$.next(new NodeUnselectedEvent(tree));
}

public fireNodeRenamed(oldValue: RenamableNode | string, tree: Tree): void {
this.nodeRenamed$.next(new NodeRenamedEvent(tree, oldValue, tree.value));
}
Expand Down

0 comments on commit 6c43391

Please sign in to comment.