tdesign-mobile-vue
Version:
tdesign-mobile-vue
133 lines (125 loc) • 4.84 kB
JavaScript
/**
* tdesign v1.7.0
* (c) 2024 TDesign Group
* @license MIT
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var vue = require('vue');
var core = require('@vueuse/core');
var isObject = require('lodash/isObject');
var shared_dom = require('../shared/dom.js');
var shared_util = require('../shared/util.js');
require('lodash/isFunction');
require('lodash/isString');
require('lodash/isNumber');
require('../config.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
var noop = function noop() {};
function useSwipe(target) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$threshold = options.threshold,
threshold = _options$threshold === void 0 ? 50 : _options$threshold,
onSwipe = options.onSwipe,
onSwipeEnd = options.onSwipeEnd,
onSwipeStart = options.onSwipeStart,
_options$listenerOpti = options.listenerOptions,
listenerOptions = _options$listenerOpti === void 0 ? false : _options$listenerOpti;
var coordsStart = vue.reactive({
x: 0,
y: 0
});
var coordsEnd = vue.reactive({
x: 0,
y: 0
});
var diffX = vue.computed(function () {
return coordsStart.x - coordsEnd.x;
});
var diffY = vue.computed(function () {
return coordsStart.y - coordsEnd.y;
});
var max = Math.max,
abs = Math.abs;
var isThresholdExceeded = vue.computed(function () {
return max(abs(diffX.value), abs(diffY.value)) >= threshold;
});
var isSwiping = vue.ref(false);
var direction = vue.computed(function () {
if (!isThresholdExceeded.value) return "none";
if (abs(diffX.value) > abs(diffY.value)) {
return diffX.value > 0 ? "left" : "right";
}
return diffY.value > 0 ? "up" : "down";
});
var getTouchEventCoords = function getTouchEventCoords(e) {
return [e.touches[0].clientX, e.touches[0].clientY];
};
var updateCoordsStart = function updateCoordsStart(x, y) {
coordsStart.x = x;
coordsStart.y = y;
};
var updateCoordsEnd = function updateCoordsEnd(x, y) {
coordsEnd.x = x;
coordsEnd.y = y;
};
var isPassiveEventSupported = checkPassiveEventSupport();
var onTouchEnd = function onTouchEnd(e) {
if (isSwiping.value) onSwipeEnd === null || onSwipeEnd === void 0 || onSwipeEnd(e, direction.value);
isSwiping.value = false;
};
var stops = [core.useEventListener(target, "touchstart", function (e) {
if (e.touches.length !== 1) return;
if (listenerOptions === true || isObject__default["default"](listenerOptions) && listenerOptions.capture && !listenerOptions.passive) shared_dom.preventDefault(e, false);
var _getTouchEventCoords = getTouchEventCoords(e),
_getTouchEventCoords2 = _slicedToArray__default["default"](_getTouchEventCoords, 2),
x = _getTouchEventCoords2[0],
y = _getTouchEventCoords2[1];
updateCoordsStart(x, y);
updateCoordsEnd(x, y);
onSwipeStart === null || onSwipeStart === void 0 || onSwipeStart(e);
}, listenerOptions), core.useEventListener(target, "touchmove", function (e) {
if (e.touches.length !== 1) return;
var _getTouchEventCoords3 = getTouchEventCoords(e),
_getTouchEventCoords4 = _slicedToArray__default["default"](_getTouchEventCoords3, 2),
x = _getTouchEventCoords4[0],
y = _getTouchEventCoords4[1];
updateCoordsEnd(x, y);
if (!isSwiping.value && isThresholdExceeded.value) isSwiping.value = true;
if (isSwiping.value) onSwipe === null || onSwipe === void 0 || onSwipe(e);
}, listenerOptions), core.useEventListener(target, "touchend", onTouchEnd, listenerOptions), core.useEventListener(target, "touchcancel", onTouchEnd, listenerOptions)];
var stop = function stop() {
return stops.forEach(function (s) {
return s();
});
};
return {
isPassiveEventSupported: isPassiveEventSupported,
isSwiping: isSwiping,
direction: direction,
coordsStart: coordsStart,
coordsEnd: coordsEnd,
lengthX: diffX,
lengthY: diffY,
stop: stop
};
}
function checkPassiveEventSupport() {
var document = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : shared_util.isBrowser ? window.document : void 0;
if (!document) return false;
var supportsPassive = false;
var optionsBlock = {
get passive() {
supportsPassive = true;
return false;
}
};
document.addEventListener("x", noop, optionsBlock);
document.removeEventListener("x", noop);
return supportsPassive;
}
exports.useSwipe = useSwipe;
//# sourceMappingURL=useSwipe.js.map