UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

77 lines (76 loc) • 2.48 kB
import { Point } from "../../geometry/Point.js"; import Browser_default from "../../core/Browser.js"; import { getPointers } from "../../dom/DomEvent.PointerEvents.js"; import { off, on, preventDefault } from "../../dom/DomEvent.js"; import { Handler } from "../../core/Handler.js"; //#region src/map/handler/TapHoldHandler.js const tapHoldDelay = 600; var TapHoldHandler = class TapHoldHandler extends Handler { constructor(...args) { super(...args); this.callInitHooks(TapHoldHandler); } addHooks() { on(this._map._container, "pointerdown", this._onDown, this); } removeHooks() { off(this._map._container, "pointerdown", this._onDown, this); this._cancel(); this._cancelClickPrevent(); } _onDown(e) { clearTimeout(this._holdTimeout); if (getPointers().length !== 1 || e.pointerType === "mouse") return; const el = this._map._container; this._startPos = this._newPos = new Point(e.clientX, e.clientY); this._holdTimeout = setTimeout((() => { this._cancel(); if (!this._isTapValid()) return; on(el, "pointerup", preventDefault); on(el, "pointerup pointercancel", this._cancelClickPrevent, this); this._simulateEvent("contextmenu", e); }), tapHoldDelay); on(el, "pointerup pointercancel contextmenu", this._cancel, this); on(el, "pointermove", this._onMove, this); } _cancelClickPrevent() { const el = this._map._container; off(el, "pointerup", preventDefault); off(el, "pointerup pointercancel", this._cancelClickPrevent, this); } _cancel() { clearTimeout(this._holdTimeout); const el = this._map._container; off(el, "pointerup pointercancel contextmenu", this._cancel, this); off(el, "pointermove", this._onMove, this); } _onMove(e) { this._newPos = new Point(e.clientX, e.clientY); } _isTapValid() { return this._newPos.distanceTo(this._startPos) <= this._map.options.tapTolerance; } _simulateEvent(type, e) { const simulatedEvent = new MouseEvent(type, { bubbles: true, cancelable: true, view: window, screenX: e.screenX, screenY: e.screenY, clientX: e.clientX, clientY: e.clientY }); simulatedEvent._simulated = true; e.target.dispatchEvent(simulatedEvent); } static register(MaplairMap) { MaplairMap.mergeOptions({ tapHold: Browser_default.safari && Browser_default.mobile, tapTolerance: 15 }); MaplairMap.addInitHook("addHandler", "tapHold", TapHoldHandler); } }; //#endregion export { TapHoldHandler }; //# sourceMappingURL=TapHoldHandler.js.map