devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
154 lines (153 loc) • 5.36 kB
JavaScript
/**
* DevExtreme (esm/__internal/events/swipe.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 {
eventData
} from "../../common/core/events/utils";
import {
getHeight,
getWidth
} from "../../core/utils/size";
import registerEmitter from "../events/core/emitter_registrator";
import GestureEmitter from "../events/gesture/emitter.gesture";
const SWIPE_START_EVENT = "dxswipestart";
const SWIPE_EVENT = "dxswipe";
const SWIPE_END_EVENT = "dxswipeend";
const HorizontalStrategy = {
defaultItemSizeFunc() {
const width = getWidth(this.getElement());
return width
},
getBounds() {
return [this._maxLeftOffset, this._maxRightOffset]
},
calcOffsetRatio(e) {
var _this$_savedEventData;
const endEventData = eventData(e);
const savedX = (null === (_this$_savedEventData = this._savedEventData) || void 0 === _this$_savedEventData ? void 0 : _this$_savedEventData.x) || 0;
return (endEventData.x - savedX) / this._itemSizeFunc().call(this, e)
},
isFastSwipe(e) {
const endEventData = eventData(e);
return this.FAST_SWIPE_SPEED_LIMIT * Math.abs(endEventData.x - this._tickData.x) >= endEventData.time - this._tickData.time
}
};
const VerticalStrategy = {
defaultItemSizeFunc() {
const height = getHeight(this.getElement());
return height
},
getBounds() {
return [this._maxTopOffset, this._maxBottomOffset]
},
calcOffsetRatio(e) {
var _this$_savedEventData2;
const endEventData = eventData(e);
const savedY = (null === (_this$_savedEventData2 = this._savedEventData) || void 0 === _this$_savedEventData2 ? void 0 : _this$_savedEventData2.y) || 0;
return (endEventData.y - savedY) / this._itemSizeFunc().call(this, e)
},
isFastSwipe(e) {
const endEventData = eventData(e);
return this.FAST_SWIPE_SPEED_LIMIT * Math.abs(endEventData.y - this._tickData.y) >= endEventData.time - this._tickData.time
}
};
const STRATEGIES = {
horizontal: HorizontalStrategy,
vertical: VerticalStrategy
};
class SwipeEmitter extends GestureEmitter {
constructor(element) {
super(element);
this.TICK_INTERVAL = 300;
this.FAST_SWIPE_SPEED_LIMIT = 10;
this.direction = "horizontal";
this.elastic = true
}
_getStrategy() {
return STRATEGIES[this.direction]
}
_defaultItemSizeFunc() {
return this._getStrategy().defaultItemSizeFunc.call(this)
}
_itemSizeFunc() {
return this.itemSizeFunc ?? this._defaultItemSizeFunc
}
_init(e) {
this._tickData = eventData(e)
}
_start(e) {
this._savedEventData = eventData(e);
const startEvent = this._fireEvent("dxswipestart", e);
if (!startEvent.cancel) {
this._maxLeftOffset = startEvent.maxLeftOffset;
this._maxRightOffset = startEvent.maxRightOffset;
this._maxTopOffset = startEvent.maxTopOffset;
this._maxBottomOffset = startEvent.maxBottomOffset
}
}
_move(e) {
const strategy = this._getStrategy();
const moveEventData = eventData(e);
let offset = strategy.calcOffsetRatio.call(this, e);
offset = this._fitOffset(offset, this.elastic);
if (moveEventData.time - this._tickData.time > this.TICK_INTERVAL) {
this._tickData = moveEventData
}
this._fireEvent("dxswipe", e, {
offset: offset
});
if (false !== e.cancelable) {
e.preventDefault()
}
}
_end(e) {
const strategy = this._getStrategy();
const offsetRatio = strategy.calcOffsetRatio.call(this, e);
const isFast = strategy.isFastSwipe.call(this, e);
let startOffset = offsetRatio;
let targetOffset = this._calcTargetOffset(offsetRatio, isFast);
startOffset = this._fitOffset(startOffset, this.elastic);
targetOffset = this._fitOffset(targetOffset, false);
this._fireEvent("dxswipeend", e, {
offset: startOffset,
targetOffset: targetOffset
})
}
_fitOffset(offset, elastic) {
const strategy = this._getStrategy();
const bounds = strategy.getBounds.call(this);
const minOffset = bounds[0];
const maxOffset = bounds[1];
if (void 0 !== minOffset && offset < -minOffset) {
return elastic ? (-2 * minOffset + offset) / 3 : -minOffset
}
if (void 0 !== maxOffset && offset > maxOffset) {
return elastic ? (2 * maxOffset + offset) / 3 : maxOffset
}
return offset
}
_calcTargetOffset(offsetRatio, isFast) {
let result = 0;
if (isFast) {
result = Math.ceil(Math.abs(offsetRatio));
if (offsetRatio < 0) {
result = -result
}
} else {
result = Math.round(offsetRatio)
}
return result
}
}
registerEmitter({
emitter: SwipeEmitter,
events: ["dxswipestart", "dxswipe", "dxswipeend"]
});
export {
SWIPE_END_EVENT as end, SWIPE_START_EVENT as start, SWIPE_EVENT as swipe
};