mirror of
https://github.com/umap-project/umap.git
synced 2025-04-29 11:52:38 +02:00
chore: move computePosition to Positioned base class
This commit is contained in:
parent
a101309e03
commit
7ce5646c54
2 changed files with 20 additions and 15 deletions
|
@ -63,6 +63,22 @@ export class Positioned {
|
||||||
else this.container.style.bottom = 'initial'
|
else this.container.style.bottom = 'initial'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
computePosition([x, y]) {
|
||||||
|
let left
|
||||||
|
let top
|
||||||
|
if (x < window.innerWidth / 2) {
|
||||||
|
left = x
|
||||||
|
} else {
|
||||||
|
left = x - this.container.offsetWidth
|
||||||
|
}
|
||||||
|
if (y < window.innerHeight / 2) {
|
||||||
|
top = y
|
||||||
|
} else {
|
||||||
|
top = y - this.container.offsetHeight
|
||||||
|
}
|
||||||
|
this.setPosition({ left, top })
|
||||||
|
}
|
||||||
|
|
||||||
getDocHeight() {
|
getDocHeight() {
|
||||||
const D = document
|
const D = document
|
||||||
return Math.max(
|
return Math.max(
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { loadTemplate } from '../utils.js'
|
import { loadTemplate } from '../utils.js'
|
||||||
|
import { Positioned } from './base.js'
|
||||||
|
|
||||||
export default class ContextMenu {
|
export default class ContextMenu extends Positioned {
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
|
super()
|
||||||
this.options = options
|
this.options = options
|
||||||
this.container = document.createElement('ul')
|
this.container = document.createElement('ul')
|
||||||
this.container.className = 'umap-contextmenu'
|
this.container.className = 'umap-contextmenu'
|
||||||
|
@ -26,7 +28,7 @@ export default class ContextMenu {
|
||||||
this.container.appendChild(li)
|
this.container.appendChild(li)
|
||||||
}
|
}
|
||||||
document.body.appendChild(this.container)
|
document.body.appendChild(this.container)
|
||||||
this.setPosition([x, y], this.container)
|
this.computePosition([x, y])
|
||||||
this.container.querySelector('button').focus()
|
this.container.querySelector('button').focus()
|
||||||
this.container.addEventListener('keydown', (event) => {
|
this.container.addEventListener('keydown', (event) => {
|
||||||
if (event.key === 'Escape') {
|
if (event.key === 'Escape') {
|
||||||
|
@ -36,19 +38,6 @@ 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() {
|
close() {
|
||||||
try {
|
try {
|
||||||
this.container.remove()
|
this.container.remove()
|
||||||
|
|
Loading…
Reference in a new issue