2022-03-11 23:42:26 +01:00
|
|
|
/**
|
|
|
|
* Add an event listener bound to the context of the superClass.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node Element to attach the event listener to.
|
|
|
|
* @param {String} eventName Name of the event.
|
|
|
|
* @param {String} cbName Name of the handler.
|
|
|
|
*/
|
|
|
|
export const EventHelpers = function(superClass) {
|
|
|
|
return class extends superClass {
|
2024-07-30 11:27:32 +02:00
|
|
|
listen(node, eventName, cbName, options) {
|
|
|
|
const boundListener = this.__registerListener(cbName, node, eventName);
|
2022-03-11 23:42:26 +01:00
|
|
|
|
2024-07-30 11:27:32 +02:00
|
|
|
if (eventName === 'track') {
|
|
|
|
this._track(node, boundListener);
|
|
|
|
return;
|
2022-03-11 23:42:26 +01:00
|
|
|
}
|
|
|
|
|
2024-07-30 11:27:32 +02:00
|
|
|
node.addEventListener(eventName, boundListener, options);
|
2022-03-11 23:42:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an event listener bound to the context of the superClass.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node Element to attach the event listener to.
|
|
|
|
* @param {String} eventName Name of the event.
|
|
|
|
* @param {String} cbName Name of the handler.
|
|
|
|
*/
|
2024-07-30 11:27:32 +02:00
|
|
|
unlisten(node, eventName, cbName, options) {
|
2022-03-11 23:42:26 +01:00
|
|
|
this.__boundEventListeners = this.__boundEventListeners || new WeakMap();
|
|
|
|
const listeners = this.__boundEventListeners.get(node);
|
|
|
|
const eventKey = `${eventName}_${cbName}`;
|
|
|
|
|
|
|
|
if (listeners && typeof listeners[eventKey]) {
|
2024-07-30 11:27:32 +02:00
|
|
|
node.removeEventListener(eventName, listeners[eventKey], options);
|
2022-03-11 23:42:26 +01:00
|
|
|
listeners[eventKey] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 11:27:32 +02:00
|
|
|
once(node, eventName, cbName, options) {
|
2022-03-11 23:42:26 +01:00
|
|
|
const wrappedCbName = `__onceCb__${cbName}`;
|
|
|
|
|
|
|
|
this[wrappedCbName] = (...args) => {
|
|
|
|
this[cbName](...args);
|
2024-07-30 11:27:32 +02:00
|
|
|
this.unlisten(node, eventName, wrappedCbName, options);
|
2022-03-11 23:42:26 +01:00
|
|
|
};
|
|
|
|
|
2024-07-30 11:27:32 +02:00
|
|
|
this.listen(node, eventName, wrappedCbName, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
__registerListener(cbName, node, eventName) {
|
|
|
|
this.__boundEventListeners = this.__boundEventListeners || new WeakMap();
|
|
|
|
const boundListener = this[cbName].bind(this);
|
|
|
|
const eventKey = `${eventName}_${cbName}`;
|
|
|
|
let listeners = this.__boundEventListeners.get(node);
|
|
|
|
|
|
|
|
// If there is already a handler for the event assigned we stop here.
|
|
|
|
if (listeners && typeof listeners[eventKey] === 'function') return;
|
|
|
|
|
|
|
|
if (!listeners) {
|
|
|
|
listeners = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners[eventKey] = boundListener;
|
|
|
|
this.__boundEventListeners.set(node, listeners);
|
|
|
|
|
|
|
|
return boundListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
_track(node, boundListener) {
|
|
|
|
this._boundTrackingListener = boundListener;
|
|
|
|
this.listen(node, 'mousedown', '_trackMouseDown');
|
|
|
|
}
|
|
|
|
|
|
|
|
_trackMouseDown(e) {
|
|
|
|
this._trackInfo = {
|
|
|
|
originX: e.clientX,
|
|
|
|
originY: e.clientY,
|
|
|
|
started: false
|
|
|
|
};
|
|
|
|
|
|
|
|
this.listen(window, 'mousemove', '_trackMouseMove', true);
|
|
|
|
this.once(window, 'mouseup', '_trackMouseUp');
|
|
|
|
}
|
|
|
|
|
|
|
|
_trackMouseMove(e) {
|
|
|
|
if (!this._trackInfo.started && trackHasMovedEnough(this._trackInfo, e.clientX, e.clientY)) {
|
|
|
|
this._trackInfo.started = true;
|
|
|
|
this._boundTrackingListener(new CustomEvent('track', { detail: { state: 'start', dx: e.clientX - this._trackInfo.originX, dy: e.clientY - this._trackInfo.originY }, bubbles: true, composed: true }));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._trackInfo.started) {
|
|
|
|
this._boundTrackingListener(new CustomEvent('track', { detail: { state: 'track', dx: e.clientX - this._trackInfo.originX, dy: e.clientY - this._trackInfo.originY }, bubbles: true, composed: true }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_trackMouseUp(e) {
|
|
|
|
this._boundTrackingListener(new CustomEvent('track', { detail: { state: 'end', dx: e.clientX - this._trackInfo.originX, dy: e.clientY - this._trackInfo.originY }, bubbles: true, composed: true }));
|
|
|
|
this.unlisten(window, 'mousemove', '_trackMouseMove', true);
|
|
|
|
this._trackInfo = null;
|
2022-03-11 23:42:26 +01:00
|
|
|
}
|
|
|
|
}
|
2024-07-30 11:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const TRACK_DISTANCE = 5;
|
|
|
|
|
|
|
|
function trackHasMovedEnough(info, x, y) {
|
|
|
|
let dx = Math.abs(info.originX - x);
|
|
|
|
let dy = Math.abs(info.originY - y);
|
|
|
|
return (dx >= TRACK_DISTANCE || dy >= TRACK_DISTANCE);
|
|
|
|
}
|