Skip to content

Commit 2214eca

Browse files
committed
refactor context menu position calculation with pageX and pageY
1 parent 6b24698 commit 2214eca

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/components/DgTable.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { TweenMax, Linear } from 'gsap'
3535
import { records } from '../data.json'
3636
import settings from '../tableSettings'
3737
import { toCurrency, toMMMMYYYY, capitalize, toGMapQuery, toUpperMagnitude } from 'utils/filters'
38-
import { offsets } from 'utils/mouse'
38+
import { normPagePosInEvent } from 'utils/mouse'
3939
import DgMenu from 'components/DgMenu'
4040
import DgCellMenu from 'components/DgCellMenu'
4141
import DgCellDetail from 'components/DgCellDetail'
@@ -213,9 +213,12 @@ export default {
213213
this.expandCol(attribute, event)
214214
},
215215
openMenu (event) {
216-
const {x, y} = offsets(event)
217-
this.menuPos.x = x + event.target.offsetLeft
218-
this.menuPos.y = y + event.target.offsetTop
216+
const rect = this.$el.getBoundingClientRect()
217+
const OFFSET = 1 // show menu with 1px offset to mouse
218+
event = normPagePosInEvent(event)
219+
220+
this.menuPos.x = event.pageX - rect.left + OFFSET
221+
this.menuPos.y = event.pageY - rect.top + OFFSET
219222
this.clearFocusCell()
220223
this.closeFilterMenu()
221224
this.showMenu = true

src/utils/mouse.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
function offsets (e) {
2-
e = e || window.event
1+
/* Mouse position calc */
32

4-
const target = e.target || e.srcElement
5-
const style = target.currentStyle || window.getComputedStyle(target, null)
6-
const borderLeftWidth = parseInt(style['borderLeftWidth'], 10)
7-
const borderTopWidth = parseInt(style['borderTopWidth'], 10)
8-
const rect = target.getBoundingClientRect()
9-
const offsetX = e.clientX - borderLeftWidth - rect.left
10-
const offsetY = e.clientY - borderTopWidth - rect.top
3+
// normalize pageX and pageY in mouse event
4+
function normPagePosInEvent (e) {
5+
e = e || window.event
116

12-
return {
13-
x: offsetX,
14-
y: offsetY
7+
if (e.pageX === undefined) {
8+
e.pageX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft
9+
e.pageY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop
1510
}
11+
12+
return e
1613
}
17-
export {offsets}
14+
15+
export { normPagePosInEvent }

0 commit comments

Comments
 (0)