Add initial context menu service - WIP

This commit is contained in:
Matthew Ross 2017-05-02 06:43:50 -04:00
parent 2e6c63a5c3
commit c0322d9ed9
2 changed files with 33 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import { ContextMenu } from './context-menu.component';
@Injectable()
export class ContextMenuService {
private menus: Array<ContextMenu>;
constructor() {
this.menus = [];
document.addEventListener('click', () => {
this.closeAllMenus();
});
}
registerMenu(newMenu: ContextMenu) {
this.menus.push(newMenu);
}
private closeAllMenus() {
this.menus.forEach(menu => {
menu.isOpen = false;
});
}
}