UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

215 lines (214 loc) • 6.79 kB
/** * DevExtreme (esm/__internal/events/gesture/emitter.gesture.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { createEvent, eventData, eventDelta, isDxMouseWheelEvent, isMouseEvent, isTouchEvent, needSkipEvent } from "../../../common/core/events/utils/index"; import $ from "../../../core/renderer"; import { noop } from "../../../core/utils/common"; import { sign } from "../../../core/utils/math"; import readyCallbacks from "../../../core/utils/ready_callbacks"; import { styleProp } from "../../../core/utils/style"; import { isDefined } from "../../../core/utils/type"; import devices from "../../core/devices"; import { callOnce } from "../../core/utils/call_once"; import domUtils from "../../core/utils/m_dom"; import Emitter from "../../events/core/emitter"; import eventsEngine from "../../events/core/events_engine"; const ready = readyCallbacks.add; const { abs: abs } = Math; const SLEEP = 0; const INITED = 1; const STARTED = 2; let TOUCH_BOUNDARY = 10; const IMMEDIATE_TOUCH_BOUNDARY = 0; const IMMEDIATE_TIMEOUT = 180; const supportPointerEvents = function() { const prop = styleProp("pointer-events"); return prop }; const setGestureCover = callOnce(() => { const isDesktop = "desktop" === devices.real().deviceType; if (!supportPointerEvents() || !isDesktop) { return noop } const $cover = $("<div>").addClass("dx-gesture-cover").css("pointerEvents", "none"); eventsEngine.subscribeGlobal($cover, "dxmousewheel", e => { e.preventDefault() }); ready(() => { $cover.appendTo("body") }); return function(toggle, cursor) { $cover.css("pointerEvents", toggle ? "all" : "none"); if (toggle) { $cover.css("cursor", cursor) } } }); const gestureCover = function(toggle, cursor) { const gestureCoverStrategy = setGestureCover(); gestureCoverStrategy(toggle, cursor) }; class GestureEmitter extends Emitter { constructor() { super(...arguments); this.gesture = true } static touchBoundary(newBoundary) { if (isDefined(newBoundary)) { TOUCH_BOUNDARY = newBoundary; return } return TOUCH_BOUNDARY } configure(data, eventName) { this.getElement().css("msTouchAction", data.immediate ? "pinch-zoom" : ""); super.configure(data, eventName) } allowInterruptionByMouseWheel() { return 2 !== this._stage } getDirection(e) { return this.direction } _cancel(e) { super._cancel(e); this._toggleGestureCover(false); this._stage = 0 } start(e) { const isMacContextMenuClick = isMouseEvent(e) && Boolean(e.ctrlKey) && devices.real().mac; if (e._needSkipEvent || needSkipEvent(e) || isMacContextMenuClick) { this._cancel(e); return } this._startEvent = createEvent(e); this._startEventData = eventData(e); this._stage = 1; this._init(e); this._setupImmediateTimer() } _setupImmediateTimer() { clearTimeout(this._immediateTimer); this._immediateAccepted = false; if (!this.immediate) { return } if (0 === this.immediateTimeout) { this._immediateAccepted = true; return } this._immediateTimer = setTimeout(() => { this._immediateAccepted = true }, this.immediateTimeout ?? 180) } move(e) { if (1 === this._stage && this._directionConfirmed(e)) { this._stage = 2; this._resetActiveElement(); this._toggleGestureCover(true); this._clearSelection(e); this._adjustStartEvent(e); this._start(this._startEvent); if (0 === this._stage) { return } this._requestAccept(e); this._move(e); this._forgetAccept() } else if (2 === this._stage) { this._clearSelection(e); this._move(e) } } _directionConfirmed(e) { const touchBoundary = this._getTouchBoundary(e); const delta = eventDelta(this._startEventData, eventData(e)); const deltaX = abs(delta.x); const deltaY = abs(delta.y); const horizontalMove = this._validateMove(touchBoundary, deltaX, deltaY); const verticalMove = this._validateMove(touchBoundary, deltaY, deltaX); const direction = this.getDirection(e); const bothAccepted = "both" === direction && (horizontalMove || verticalMove); const horizontalAccepted = "horizontal" === direction && horizontalMove; const verticalAccepted = "vertical" === direction && verticalMove; return Boolean(bothAccepted || horizontalAccepted || verticalAccepted || this._immediateAccepted) } _validateMove(touchBoundary, mainAxis, crossAxis) { return Boolean(mainAxis) && mainAxis >= touchBoundary && (this.immediate ? mainAxis >= crossAxis : true) } _getTouchBoundary(e) { return this.immediate || isDxMouseWheelEvent(e) ? 0 : TOUCH_BOUNDARY } _adjustStartEvent(e) { const touchBoundary = this._getTouchBoundary(e); const delta = eventDelta(this._startEventData, eventData(e)); this._startEvent.pageX += sign(delta.x) * touchBoundary; this._startEvent.pageY += sign(delta.y) * touchBoundary } _resetActiveElement() { if ("ios" === devices.real().platform && this.getElement().find(":focus").length) { domUtils.resetActiveElement() } } _toggleGestureCover(toggle) { this._toggleGestureCoverImpl(toggle) } _toggleGestureCoverImpl(toggle) { const isStarted = 2 === this._stage; if (isStarted) { gestureCover(toggle, this.getElement().css("cursor")) } } _clearSelection(e) { if (isDxMouseWheelEvent(e) || isTouchEvent(e)) { return } domUtils.clearSelection() } end(e) { this._toggleGestureCover(false); if (2 === this._stage) { this._end(e) } else if (1 === this._stage) { this._stop(e) } this._stage = 0 } dispose() { clearTimeout(this._immediateTimer); super.dispose(); this._toggleGestureCover(false) } _init(e) {} _start(e) {} _move(e) {} _stop(e) {} _end(e) {} } GestureEmitter.initialTouchBoundary = TOUCH_BOUNDARY; export default GestureEmitter;