From c0322d9ed97f7d769207762ddd4e87d47fe0c590 Mon Sep 17 00:00:00 2001 From: Matthew Ross Date: Tue, 2 May 2017 06:43:50 -0400 Subject: [PATCH] Add initial context menu service - WIP --- .../context-menu/context-menu.component.ts | 7 ++++- .../context-menu/context-menu.service.ts | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/app/shared/context-menu/context-menu.service.ts diff --git a/src/app/shared/context-menu/context-menu.component.ts b/src/app/shared/context-menu/context-menu.component.ts index 193728e..b0b48e8 100644 --- a/src/app/shared/context-menu/context-menu.component.ts +++ b/src/app/shared/context-menu/context-menu.component.ts @@ -35,6 +35,11 @@ export class ContextMenu { let edgeBuffer = 10; let target = this.el.nativeElement.firstElementChild; + // Set to the event position by default + target.style.left = event.pageX + 'px'; + target.style.top = event.pageY + 'px'; + + // Adjust position if near an edge setTimeout(() => { let rect = target.getBoundingClientRect(); @@ -43,7 +48,7 @@ export class ContextMenu { target.style.left = event.pageX - (offsetX ? rect.width : 0) + 'px'; target.style.top = event.pageY - (offsetY ? rect.height : 0) + 'px'; - }, 10); + }, 0); } } diff --git a/src/app/shared/context-menu/context-menu.service.ts b/src/app/shared/context-menu/context-menu.service.ts new file mode 100644 index 0000000..4e5c524 --- /dev/null +++ b/src/app/shared/context-menu/context-menu.service.ts @@ -0,0 +1,27 @@ +import { Injectable } from '@angular/core'; + +import { ContextMenu } from './context-menu.component'; + +@Injectable() +export class ContextMenuService { + private menus: Array; + + constructor() { + this.menus = []; + + document.addEventListener('click', () => { + this.closeAllMenus(); + }); + } + + registerMenu(newMenu: ContextMenu) { + this.menus.push(newMenu); + } + + private closeAllMenus() { + this.menus.forEach(menu => { + menu.isOpen = false; + }); + } +} +