From ef5a1c223c7f423881ecf01c00b783b5966b0e9c Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 22 Aug 2018 01:27:50 +0300 Subject: [PATCH] Get position from passed event|object --- client/src/ui/components/BdContextMenu.vue | 8 ++------ client/src/ui/contextmenus.js | 7 +++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/client/src/ui/components/BdContextMenu.vue b/client/src/ui/components/BdContextMenu.vue index be46ee56..cd2c41cc 100644 --- a/client/src/ui/components/BdContextMenu.vue +++ b/client/src/ui/components/BdContextMenu.vue @@ -33,18 +33,14 @@ methods: { calculatePosition() { if (!this.activeMenu.menu.groups.length) return {}; + this.mouseX = this.activeMenu.menu.x; + this.mouseY = this.activeMenu.menu.y; const height = this.activeMenu.menu.groups.reduce((total, group) => total + group.items.length, 0) * 28; this.top = window.innerHeight - this.mouseY - height < 0 ? this.mouseY - height : this.mouseY; this.left = window.innerWidth - this.mouseX - 170 < 0 ? this.mouseX - 170 : this.mouseX; this.renderLeft = (this.left + 170 * 2) > window.innerWidth; return { top: `${this.top}px`, left: `${this.left}px` }; } - }, - mounted() { - window.addEventListener('contextmenu', e => { - this.mouseX = e.clientX; - this.mouseY = e.clientY; - }); } } diff --git a/client/src/ui/contextmenus.js b/client/src/ui/contextmenus.js index cd95b070..6c0c1f81 100644 --- a/client/src/ui/contextmenus.js +++ b/client/src/ui/contextmenus.js @@ -12,10 +12,13 @@ export class BdContextMenu { /** * Show a context menu + * @param {MouseEvent|Object} e MouseEvent or Object { x: 0, y: 0 } * @param {Object[]} grops Groups of items to show in context menu */ - static show(groups) { - this.activeMenu.menu = { groups }; + static show(e, groups) { + const x = e.x || e.clientX; + const y = e.y || e.clientY; + this.activeMenu.menu = { x, y, groups }; } static get activeMenu() {