devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
201 lines (199 loc) • 7.4 kB
JavaScript
/**
* DevExtreme (cjs/__internal/events/gesture/m_emitter.gesture.js)
* Version: 24.2.6
* Build date: Mon Mar 17 2025
*
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _emitter = _interopRequireDefault(require("../../../common/core/events/core/emitter"));
var _events_engine = _interopRequireDefault(require("../../../common/core/events/core/events_engine"));
var _index = require("../../../common/core/events/utils/index");
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _call_once = _interopRequireDefault(require("../../../core/utils/call_once"));
var _common = require("../../../core/utils/common");
var _math = require("../../../core/utils/math");
var _ready_callbacks = _interopRequireDefault(require("../../../core/utils/ready_callbacks"));
var _style = require("../../../core/utils/style");
var _type = require("../../../core/utils/type");
var _m_devices = _interopRequireDefault(require("../../core/m_devices"));
var _m_dom = _interopRequireDefault(require("../../core/utils/m_dom"));
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
const ready = _ready_callbacks.default.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() {
return (0, _style.styleProp)("pointer-events")
};
const setGestureCover = (0, _call_once.default)((() => {
const isDesktop = "desktop" === _m_devices.default.real().deviceType;
if (!supportPointerEvents() || !isDesktop) {
return _common.noop
}
const $cover = (0, _renderer.default)("<div>").addClass("dx-gesture-cover").css("pointerEvents", "none");
_events_engine.default.subscribeGlobal($cover, "dxmousewheel", (e => {
e.preventDefault()
}));
ready((() => {
$cover.appendTo("body")
}));
return function(toggle, cursor) {
$cover.css("pointerEvents", toggle ? "all" : "none");
toggle && $cover.css("cursor", cursor)
}
}));
const gestureCover = function(toggle, cursor) {
const gestureCoverStrategy = setGestureCover();
gestureCoverStrategy(toggle, cursor)
};
const GestureEmitter = _emitter.default.inherit({
gesture: true,
configure(data) {
this.getElement().css("msTouchAction", data.immediate ? "pinch-zoom" : "");
this.callBase(data)
},
allowInterruptionByMouseWheel() {
return 2 !== this._stage
},
getDirection() {
return this.direction
},
_cancel() {
this.callBase.apply(this, arguments);
this._toggleGestureCover(false);
this._stage = 0
},
start(e) {
if (e._needSkipEvent || (0, _index.needSkipEvent)(e)) {
this._cancel(e);
return
}
this._startEvent = (0, _index.createEvent)(e);
this._startEventData = (0, _index.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 = (0, _index.eventDelta)(this._startEventData, (0, _index.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 bothAccepted || horizontalAccepted || verticalAccepted || this._immediateAccepted
},
_validateMove(touchBoundary, mainAxis, crossAxis) {
return mainAxis && mainAxis >= touchBoundary && (this.immediate ? mainAxis >= crossAxis : true)
},
_getTouchBoundary(e) {
return this.immediate || (0, _index.isDxMouseWheelEvent)(e) ? 0 : TOUCH_BOUNDARY
},
_adjustStartEvent(e) {
const touchBoundary = this._getTouchBoundary(e);
const delta = (0, _index.eventDelta)(this._startEventData, (0, _index.eventData)(e));
this._startEvent.pageX += (0, _math.sign)(delta.x) * touchBoundary;
this._startEvent.pageY += (0, _math.sign)(delta.y) * touchBoundary
},
_resetActiveElement() {
if ("ios" === _m_devices.default.real().platform && this.getElement().find(":focus").length) {
_m_dom.default.resetActiveElement()
}
},
_toggleGestureCover(toggle) {
this._toggleGestureCoverImpl(toggle)
},
_toggleGestureCoverImpl(toggle) {
const isStarted = 2 === this._stage;
if (isStarted) {
gestureCover(toggle, this.getElement().css("cursor"))
}
},
_clearSelection(e) {
if ((0, _index.isDxMouseWheelEvent)(e) || (0, _index.isTouchEvent)(e)) {
return
}
_m_dom.default.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);
this.callBase.apply(this, arguments);
this._toggleGestureCover(false)
},
_init: _common.noop,
_start: _common.noop,
_move: _common.noop,
_stop: _common.noop,
_end: _common.noop
});
GestureEmitter.initialTouchBoundary = TOUCH_BOUNDARY;
GestureEmitter.touchBoundary = function(newBoundary) {
if ((0, _type.isDefined)(newBoundary)) {
TOUCH_BOUNDARY = newBoundary;
return
}
return TOUCH_BOUNDARY
};
var _default = exports.default = GestureEmitter;