Fix input consume

This commit is contained in:
barsdeveloper
2022-04-19 21:13:47 +02:00
parent be20984b5a
commit 377320181f
2 changed files with 30 additions and 30 deletions

View File

@@ -31,24 +31,24 @@ export default class IMouseClickDrag extends IPointing {
started = false
constructor(target, blueprint, options = {}) {
options.clickButton ??= 0
options.consumeEvent ??= true
options.exitAnyButton ??= true
options.looseTarget ??= false
options.moveEverywhere ??= false
super(target, blueprint, options)
this.clickButton = options?.clickButton ?? 0
this.exitAnyButton = options?.exitAnyButton ?? true
this.moveEverywhere = options?.moveEverywhere ?? false
this.looseTarget = options?.looseTarget ?? false
this.consumeEvent = options?.consumeEvent ?? true
this.clickedPosition = [0, 0]
const movementListenedElement = this.moveEverywhere ? document.documentElement : this.movementSpace
const movementListenedElement = this.options.moveEverywhere ? document.documentElement : this.movementSpace
let self = this
this.#mouseDownHandler = e => {
this.blueprint.setFocused(true)
switch (e.button) {
case self.clickButton:
case self.options.clickButton:
// Either doesn't matter or consider the click only when clicking on the parent, not descandants
if (self.looseTarget || e.target == e.currentTarget) {
if (this.consumeEvent) {
if (self.options.looseTarget || e.target == e.currentTarget) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
// Attach the listeners
@@ -59,7 +59,7 @@ export default class IMouseClickDrag extends IPointing {
}
break
default:
if (!self.exitAnyButton) {
if (!self.options.exitAnyButton) {
self.#mouseUpHandler(e)
}
break
@@ -67,7 +67,7 @@ export default class IMouseClickDrag extends IPointing {
}
this.#mouseStartedMovingHandler = e => {
if (this.consumeEvent) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
// Delegate from now on to self.#mouseMoveHandler
@@ -82,7 +82,7 @@ export default class IMouseClickDrag extends IPointing {
}
this.#mouseMoveHandler = e => {
if (this.consumeEvent) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
const location = self.locationFromEvent(e)
@@ -94,8 +94,8 @@ export default class IMouseClickDrag extends IPointing {
}
this.#mouseUpHandler = e => {
if (!self.exitAnyButton || e.button == self.clickButton) {
if (this.consumeEvent) {
if (!self.options.exitAnyButton || e.button == self.options.clickButton) {
if (this.options.consumeEvent) {
e.stopImmediatePropagation() // Captured, don't call anyone else
}
// Remove the handlers of "mousemove" and "mouseup"
@@ -120,7 +120,7 @@ export default class IMouseClickDrag extends IPointing {
listenEvents() {
this.target.addEventListener("mousedown", this.#mouseDownHandler)
if (this.clickButton == 2) {
if (this.options.clickButton == 2) {
this.target.addEventListener("contextmenu", e => e.preventDefault())
}
}