From 16f9a8d57f8c4a9d1b95d609142429cc3e1a6551 Mon Sep 17 00:00:00 2001 From: Isaac Mann Date: Wed, 28 Jun 2017 15:25:53 -0400 Subject: [PATCH] feat(hotkey): Arrow key navigation --- src/lib/contextMenu.component.ts | 9 ++- src/lib/contextMenu.service.ts | 17 +++++- src/lib/contextMenuContent.component.ts | 76 ++++++++++++++++--------- 3 files changed, 70 insertions(+), 32 deletions(-) diff --git a/src/lib/contextMenu.component.ts b/src/lib/contextMenu.component.ts index 9642a02..1f9510e 100644 --- a/src/lib/contextMenu.component.ts +++ b/src/lib/contextMenu.component.ts @@ -102,9 +102,7 @@ export class ContextMenuComponent implements OnDestroy { this.setVisibleMenuItems(); this.contextMenuContent = this.contextMenuInjector.create({ menuItems: this.visibleMenuItems, - item: this.item, - event: this.event, - parentContextMenu: menuEvent.parentContextMenu, + ...menuEvent, }); this.open.next(menuEvent); }); @@ -124,6 +122,11 @@ export class ContextMenuComponent implements OnDestroy { this._contextMenuService.destroyLeafMenu(); } + @HostListener('window:keydown.ArrowLeft') + public destroyLeafSubMenu(): void { + this._contextMenuService.destroyLeafMenu({ exceptRootMenu: true }); + } + public isMenuItemVisible(menuItem: ContextMenuItemDirective): boolean { return this.evaluateIfFunction(menuItem.visible); } diff --git a/src/lib/contextMenu.service.ts b/src/lib/contextMenu.service.ts index 7aa34a1..c0bd36c 100644 --- a/src/lib/contextMenu.service.ts +++ b/src/lib/contextMenu.service.ts @@ -9,6 +9,7 @@ export interface IContextMenuClickEvent { event: MouseEvent; parentContextMenu?: ContextMenuContentComponent; item: any; + activeMenuItemIndex?: number; } @Injectable() @@ -21,7 +22,7 @@ export class ContextMenuService { constructor(private contextMenuInjector: ContextMenuInjectorService) {} - public destroyLeafMenu(): void { + public destroyLeafMenu({exceptRootMenu}: {exceptRootMenu?: boolean} = {}): void { if (this.isDestroyingLeafMenu) { return; } @@ -31,10 +32,22 @@ export class ContextMenuService { if (cmContents && cmContents.length > 1) { cmContents[cmContents.length - 2].instance.focus(); } - if (cmContents && cmContents.length > 0) { + if (cmContents && cmContents.length > (exceptRootMenu ? 1 : 0)) { this.contextMenuInjector.destroy(cmContents[cmContents.length - 1]); } this.isDestroyingLeafMenu = false; }); } + + public getLeafMenu(): ContextMenuContentComponent { + const cmContents: ComponentRef[] = this.contextMenuInjector.getByType(this.contextMenuInjector.type); + if (cmContents && cmContents.length > 0) { + return cmContents[cmContents.length - 1].instance; + } + return undefined; + } + + public isLeafMenu(cmContent: ContextMenuContentComponent): boolean { + return this.getLeafMenu() === cmContent; + } } diff --git a/src/lib/contextMenuContent.component.ts b/src/lib/contextMenuContent.component.ts index 674f3db..c4e86f4 100644 --- a/src/lib/contextMenuContent.component.ts +++ b/src/lib/contextMenuContent.component.ts @@ -11,9 +11,10 @@ import { Input, Optional, Renderer, - ViewChild + ViewChild, + ViewChildren, } from '@angular/core'; -import { OnDestroy, OnInit } from '@angular/core'; +import { OnDestroy, OnInit, QueryList } from '@angular/core'; import { Subscription } from 'rxjs/Subscription'; export interface ILinkConfig { @@ -46,11 +47,11 @@ export interface MouseLocation { ], template: `