Remove duplicated logic from PointerInteraction

This commit is contained in:
Andreas Hocevar
2022-06-16 11:26:28 +02:00
parent f99dc1e9ec
commit 134ec9c8d0
3 changed files with 49 additions and 52 deletions
+32 -16
View File
@@ -9,6 +9,7 @@ import PointerEventType from './pointer/EventType.js';
import Target from './events/Target.js';
import {PASSIVE_EVENT_LISTENERS} from './has.js';
import {VOID} from './functions.js';
import {getValues} from './obj.js';
import {listen, unlistenByKey} from './events.js';
class MapBrowserEventHandler extends Target {
@@ -67,10 +68,10 @@ class MapBrowserEventHandler extends Target {
const element = this.map_.getViewport();
/**
* @type {number}
* @type {Array<PointerEvent>}
* @private
*/
this.activePointers_ = 0;
this.activePointers_ = [];
/**
* @type {!Object<number, Event>}
@@ -104,7 +105,7 @@ class MapBrowserEventHandler extends Target {
this.relayedListenerKey_ = listen(
element,
PointerEventType.POINTERMOVE,
this.relayEvent_,
this.relayMoveEvent_,
this
);
@@ -176,9 +177,6 @@ class MapBrowserEventHandler extends Target {
event.type == MapBrowserEventType.POINTERCANCEL
) {
delete this.trackedTouches_[id];
} else if (event.type == MapBrowserEventType.POINTERDOWN) {
this.trackedTouches_[id] = event;
} else if (event.type == MapBrowserEventType.POINTERMOVE) {
for (const pointerId in this.trackedTouches_) {
if (this.trackedTouches_[pointerId].target !== event.target) {
// Some platforms assign a new pointerId when the target changes.
@@ -189,8 +187,13 @@ class MapBrowserEventHandler extends Target {
break;
}
}
} else if (
event.type == MapBrowserEventType.POINTERDOWN ||
event.type == MapBrowserEventType.POINTERMOVE
) {
this.trackedTouches_[id] = event;
}
this.activePointers_ = Object.keys(this.trackedTouches_).length;
this.activePointers_ = getValues(this.trackedTouches_);
}
/**
@@ -203,7 +206,10 @@ class MapBrowserEventHandler extends Target {
const newEvent = new MapBrowserEvent(
MapBrowserEventType.POINTERUP,
this.map_,
pointerEvent
pointerEvent,
undefined,
undefined,
this.activePointers_
);
this.dispatchEvent(newEvent);
@@ -222,7 +228,7 @@ class MapBrowserEventHandler extends Target {
this.emulateClick_(this.down_);
}
if (this.activePointers_ === 0) {
if (this.activePointers_.length === 0) {
this.dragListenerKeys_.forEach(unlistenByKey);
this.dragListenerKeys_.length = 0;
this.dragging_ = false;
@@ -246,12 +252,15 @@ class MapBrowserEventHandler extends Target {
* @private
*/
handlePointerDown_(pointerEvent) {
this.emulateClicks_ = this.activePointers_ === 0;
this.emulateClicks_ = this.activePointers_.length === 0;
this.updateActivePointers_(pointerEvent);
const newEvent = new MapBrowserEvent(
MapBrowserEventType.POINTERDOWN,
this.map_,
pointerEvent
pointerEvent,
undefined,
undefined,
this.activePointers_
);
this.dispatchEvent(newEvent);
@@ -315,29 +324,36 @@ class MapBrowserEventHandler extends Target {
// To avoid a 'false' touchmove event to be dispatched, we test if the pointer
// moved a significant distance.
if (this.isMoving_(pointerEvent)) {
this.updateActivePointers_(pointerEvent);
this.dragging_ = true;
const newEvent = new MapBrowserEvent(
MapBrowserEventType.POINTERDRAG,
this.map_,
pointerEvent,
this.dragging_
this.dragging_,
undefined,
this.activePointers_
);
this.dispatchEvent(newEvent);
}
}
/**
* Wrap and relay a pointer event. Note that this requires that the type
* string for the MapBrowserEvent matches the PointerEvent type.
* Wrap and relay a pointermove event.
* @param {PointerEvent} pointerEvent Pointer
* event.
* @private
*/
relayEvent_(pointerEvent) {
relayMoveEvent_(pointerEvent) {
this.originalPointerMoveEvent_ = pointerEvent;
const dragging = !!(this.down_ && this.isMoving_(pointerEvent));
this.dispatchEvent(
new MapBrowserEvent(pointerEvent.type, this.map_, pointerEvent, dragging)
new MapBrowserEvent(
MapBrowserEventType.POINTERMOVE,
this.map_,
pointerEvent,
dragging
)
);
}