From 96c9bf941370ad9dcd0e4ec1359a30c81030835e Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 12 Jul 2024 12:21:30 +0200 Subject: [PATCH] wip: set contextmenu x/y according to position in window --- umap/static/umap/js/modules/ui/contextmenu.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/umap/static/umap/js/modules/ui/contextmenu.js b/umap/static/umap/js/modules/ui/contextmenu.js index 89db7940..65516055 100644 --- a/umap/static/umap/js/modules/ui/contextmenu.js +++ b/umap/static/umap/js/modules/ui/contextmenu.js @@ -26,8 +26,7 @@ export default class ContextMenu { this.container.appendChild(li) } document.body.appendChild(this.container) - this.container.style.top = `${y}px` - this.container.style.left = `${x}px` + this.setPosition([x, y], this.container) this.container.querySelector('button').focus() this.container.addEventListener('keydown', (event) => { if (event.key === 'Escape') { @@ -37,6 +36,19 @@ export default class ContextMenu { }) } + setPosition([x, y], element) { + if (x < window.innerWidth / 2) { + this.container.style.left = `${x}px` + } else { + this.container.style.left = `${x - element.offsetWidth}px` + } + if (y < window.innerHeight / 2) { + this.container.style.top = `${y}px` + } else { + this.container.style.top = `${y - element.offsetHeight}px` + } + } + close() { try { this.container.remove()