tdesign-mobile-vue
Version:
tdesign-mobile-vue
424 lines (416 loc) • 18.4 kB
JavaScript
/**
* tdesign v1.13.2
* (c) 2026 TDesign Group
* @license MIT
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var _typeof = require('@babel/runtime/helpers/typeof');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var swipeCell_useSwipe = require('../swipe-cell/useSwipe.js');
var config = require('../config.js');
var swiper_props = require('./props.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var shared_dom = require('../shared/dom.js');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useClass = require('../hooks/useClass.js');
var isNumber = require('../_chunks/dep-dea6ba04.js');
require('@vueuse/core');
require('../_chunks/dep-c041cd95.js');
require('../shared/util.js');
require('../_chunks/dep-016f6f84.js');
require('../_chunks/dep-ea3a180f.js');
require('../_chunks/dep-bcc78903.js');
require('../_chunks/dep-d16bf091.js');
require('../_chunks/dep-e1cc657b.js');
require('../_chunks/dep-c6554877.js');
require('../_chunks/dep-a58ecafb.js');
require('../_chunks/dep-a8725fec.js');
require('../_chunks/dep-abe9931d.js');
require('../_chunks/dep-db763263.js');
require('../hooks/render-tnode.js');
require('../_chunks/dep-88660f08.js');
require('../_chunks/dep-04cf2308.js');
require('../_chunks/dep-671f1b66.js');
require('../_chunks/dep-a1831458.js');
require('../_chunks/dep-082c7326.js');
require('../_chunks/dep-591548d1.js');
require('../_chunks/dep-a9fd9906.js');
require('../_chunks/dep-4a0acbaa.js');
require('../_common/js/utils/general.js');
require('../_chunks/dep-ede0b0a5.js');
require('../_chunks/dep-dafca6f5.js');
require('../_common/js/global-config/mobile/default-config.js');
require('../_common/js/global-config/mobile/locale/zh_CN.js');
require('../_chunks/dep-88b647f4.js');
require('../_chunks/dep-f339c931.js');
require('dayjs');
require('../_chunks/dep-89205991.js');
require('../_chunks/dep-a837a88d.js');
require('../_chunks/dep-d2416277.js');
require('../_chunks/dep-f5894bc3.js');
require('../_chunks/dep-d404c733.js');
require('../_chunks/dep-bd009758.js');
require('../_chunks/dep-83213f5d.js');
require('../_chunks/dep-412d3c4b.js');
require('../_chunks/dep-644f5d6a.js');
require('../_chunks/dep-933759bc.js');
require('../_chunks/dep-a625ee7d.js');
require('../_chunks/dep-e49294a5.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default["default"](e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var DEFAULT_SWIPER_NAVIGATION = {
paginationPosition: "bottom",
placement: "inside",
showControls: false,
type: "dots"
};
var SWIPE_THRESHOLD = 100;
var prefix = config["default"].prefix;
var _Swiper = Vue.defineComponent({
name: "".concat(prefix, "-swiper"),
props: _objectSpread(_objectSpread({}, swiper_props["default"]), {}, {
disabled: {
type: Boolean,
default: false
}
}),
emits: ["change", "update:current", "update:modelValue", "transitionenter", "transitionleave"],
setup: function setup(props2, _ref) {
var _currentIndex$value;
var emit = _ref.emit,
expose = _ref.expose,
slots = _ref.slots;
var swiperClass = hooks_useClass.usePrefixClass("swiper");
var swiperNavClass = hooks_useClass.usePrefixClass("swiper-nav");
var renderTNodeJSX = hooks_tnode.useTNodeJSX();
var setOffset = function setOffset(offset) {
var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "X";
translateContainer.value = "translate".concat(direction, "(").concat(offset, "px)");
};
var root = Vue.ref();
var items = Vue.ref([]);
var _toRefs = Vue.toRefs(props2),
value = _toRefs.current,
modelValue = _toRefs.modelValue;
var _useVModel = hooks_useVModel["default"](value, modelValue, props2.defaultCurrent),
_useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
currentIndex = _useVModel2[0],
setCurrent = _useVModel2[1];
var swiperContainer = Vue.ref(null);
var previous = Vue.ref((_currentIndex$value = currentIndex.value) !== null && _currentIndex$value !== void 0 ? _currentIndex$value : 0);
var animating = Vue.ref(false);
var disabled = Vue.ref(false);
var isSwiperDisabled = Vue.computed(function () {
return props2.disabled === true;
});
var translateContainer = Vue.ref("");
var isVertical = Vue.computed(function () {
return props2.direction === "vertical";
});
var containerHeight = Vue.ref("auto");
var moveDirection = Vue.ref(0);
var navigationConfig = Vue.computed(function () {
if (props2.navigation === true) {
return DEFAULT_SWIPER_NAVIGATION;
}
if (_typeof__default["default"](props2.navigation) === "object" && props2.navigation !== null) {
return _objectSpread(_objectSpread({}, DEFAULT_SWIPER_NAVIGATION), props2.navigation);
}
return {};
});
var enableBuiltinNavigation = Vue.computed(function () {
if (!Object.keys(navigationConfig.value).length) return false;
var minShowNum = navigationConfig.value.minShowNum;
return minShowNum ? items.value.length >= minShowNum : true;
});
var isBottomPagination = Vue.computed(function () {
if (!enableBuiltinNavigation.value) return false;
var _navigationConfig$val = navigationConfig.value,
paginationPosition = _navigationConfig$val.paginationPosition,
type = _navigationConfig$val.type;
return paginationPosition === "bottom" && (type === "dots" || type === "dots-bar");
});
var rootClass = Vue.computed(function () {
return ["".concat(swiperClass.value), _defineProperty__default["default"]({}, "".concat(swiperClass.value, "--").concat(navigationConfig.value.placement), isBottomPagination.value)];
});
var autoplayTimer = null;
var onItemClick = function onItemClick() {
var _props2$onClick, _currentIndex$value2;
(_props2$onClick = props2.onClick) === null || _props2$onClick === void 0 || _props2$onClick.call(props2, (_currentIndex$value2 = currentIndex.value) !== null && _currentIndex$value2 !== void 0 ? _currentIndex$value2 : 0);
};
var move = function move(step, source) {
var _root$value, _root$value2;
var isReset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var targetValue = arguments.length > 3 ? arguments[3] : undefined;
var nextIndex = currentIndex.value + step;
if (!props2.loop && !(isReset || typeof targetValue === "number")) {
if (nextIndex < 0 || nextIndex >= items.value.length) return;
}
if (!isReset && items.value.length === 2 && props2.loop) {
moveDirection.value = step > 0 ? 1 : -1;
updateItemPosition();
}
animating.value = true;
var innerTargetValue = targetValue !== null && targetValue !== void 0 ? targetValue : isReset ? step : nextIndex;
processIndex(innerTargetValue, source);
var moveDir = !isVertical.value ? "X" : "Y";
var distance = (_root$value = (_root$value2 = root.value) === null || _root$value2 === void 0 ? void 0 : _root$value2[isVertical.value ? "offsetHeight" : "offsetWidth"]) !== null && _root$value !== void 0 ? _root$value : 0;
translateContainer.value = "translate".concat(moveDir, "(").concat(isReset ? 0 : -1 * distance * step, "px)");
};
var handleAnimationEnd = function handleAnimationEnd() {
disabled.value = false;
animating.value = false;
translateContainer.value = "translate".concat(isVertical.value ? "Y" : "X", "(0)");
moveDirection.value = 0;
updateItemPosition();
};
var stopAutoplay = function stopAutoplay() {
if (!autoplayTimer) return;
clearInterval(autoplayTimer);
autoplayTimer = null;
};
var startAutoplay = function startAutoplay() {
if (!props2.autoplay || autoplayTimer !== null) return false;
autoplayTimer = setInterval(function () {
goNext("autoplay");
}, props2.interval);
};
var goPrev = function goPrev(source) {
disabled.value = true;
move(-1, source);
};
var goNext = function goNext(source) {
disabled.value = true;
move(1, source);
};
var innerSetCurrent = function innerSetCurrent(val) {
setCurrent(val);
previous.value = val;
};
var processIndex = function processIndex(index, source) {
var max = items.value.length;
var val = index;
if (index < 0) {
val = props2.loop ? max - 1 : 0;
}
if (index >= max) {
val = props2.loop ? 0 : max - 1;
}
innerSetCurrent(val);
emit("update:current", val);
emit("change", val, {
source: source
});
};
var _useSwipe = swipeCell_useSwipe.useSwipe(swiperContainer, {
onSwipeStart: function onSwipeStart() {
if (disabled.value || isSwiperDisabled.value || !items.value.length) return;
stopAutoplay();
},
onSwipe: function onSwipe(e) {
if (disabled.value || isSwiperDisabled.value || !items.value.length) return;
onTouchMove(e);
},
onSwipeEnd: function onSwipeEnd() {
if (disabled.value || isSwiperDisabled.value || !items.value.length) return;
onTouchEnd();
}
}),
lengthX = _useSwipe.lengthX,
lengthY = _useSwipe.lengthY;
var onTouchMove = function onTouchMove(event) {
shared_dom.preventDefault(event, false);
animating.value = false;
var curIndex = currentIndex.value;
var maxIndex = items.value.length - 1;
var distance = isVertical.value ? lengthY.value : lengthX.value;
var dir = isVertical.value ? "Y" : "X";
if (!props2.loop && (curIndex <= 0 && distance < 0 || curIndex >= maxIndex && distance > 0)) return;
if (items.value.length === 2 && props2.loop) {
moveDirection.value = distance > 0 ? 1 : -1;
updateItemPosition();
}
setOffset(-distance, dir);
};
var onTouchEnd = function onTouchEnd() {
var distance = isVertical.value ? lengthY.value : lengthX.value;
if (distance < -SWIPE_THRESHOLD) {
move(-1, "touch");
} else if (distance > SWIPE_THRESHOLD) {
move(1, "touch");
} else {
move(currentIndex.value, "touch", true);
}
startAutoplay();
};
var onTransitionstart = function onTransitionstart(event) {
emit("transitionenter", event);
};
var _onTransitionend = function onTransitionend(event) {
emit("transitionleave", event);
};
var addChild = function addChild(item) {
items.value.push(item);
};
var removeChild = function removeChild(uid) {
var removedIndex = items.value.findIndex(function (item) {
return item.uid === uid;
});
if (removedIndex === -1) return;
items.value.splice(removedIndex, 1);
if (items.value.length === 0) {
innerSetCurrent(0);
return;
}
if (removedIndex < currentIndex.value) {
innerSetCurrent(currentIndex.value - 1);
} else if (removedIndex === currentIndex.value && currentIndex.value >= items.value.length) {
innerSetCurrent(items.value.length - 1);
}
updateItemPosition();
};
var updateItemPosition = function updateItemPosition() {
if (!items.value.length) return;
items.value.forEach(function (item, index) {
item.calcTranslateStyle(index, currentIndex.value);
});
};
var setContainerHeight = function setContainerHeight(height) {
return containerHeight.value = isNumber.isNumber(height) ? "".concat(height, "px") : height;
};
var updateContainerHeight = function updateContainerHeight() {
var _currentIndex$value3, _target$proxy;
if (props2.height) {
setContainerHeight(props2.height);
return;
}
var target = items.value[(_currentIndex$value3 = currentIndex.value) !== null && _currentIndex$value3 !== void 0 ? _currentIndex$value3 : 0];
var rect = target === null || target === void 0 || (_target$proxy = target.proxy) === null || _target$proxy === void 0 ? void 0 : _target$proxy.$el.getBoundingClientRect();
if (rect) {
setContainerHeight(rect.height);
}
};
Vue.watch(currentIndex, updateContainerHeight);
Vue.watch(function () {
return props2.current;
}, function (val, oldVal) {
if (val === previous.value) return;
stopAutoplay();
move(val - oldVal, "autoplay", false, val);
startAutoplay();
});
Vue.provide("parent", {
loop: props2.loop,
root: root,
items: items,
isVertical: isVertical,
addChild: addChild,
removeChild: removeChild,
setContainerHeight: setContainerHeight,
moveDirection: moveDirection
});
Vue.onMounted(function () {
startAutoplay();
updateItemPosition();
updateContainerHeight();
});
Vue.onUnmounted(function () {
stopAutoplay();
});
var swipeTo = function swipeTo(index, options) {
if (index < 0 || index >= items.value.length) return;
var step = index - currentIndex.value;
if (step === 0) return;
stopAutoplay();
if (options !== null && options !== void 0 && options.immediate) {
processIndex(index, "autoplay");
updateItemPosition();
} else {
move(step, "autoplay", false, index);
}
startAutoplay();
};
expose({
swipeTo: swipeTo
});
var renderControlsNav = function renderControlsNav() {
var _navigationConfig$val2;
if (isVertical.value || !((_navigationConfig$val2 = navigationConfig.value) !== null && _navigationConfig$val2 !== void 0 && _navigationConfig$val2.showControls)) return null;
return Vue.createVNode("span", {
"class": "".concat(swiperNavClass.value, "__btn")
}, [Vue.createVNode("span", {
"class": "".concat(swiperNavClass.value, "__btn--prev"),
"onClick": function onClick() {
return goPrev("nav");
}
}, null), Vue.createVNode("span", {
"class": "".concat(swiperNavClass.value, "__btn--next"),
"onClick": function onClick() {
return goNext("nav");
}
}, null)]);
};
var renderDotsNav = function renderDotsNav() {
var navType = navigationConfig.value.type;
if (!navType || !["dots", "dots-bar"].includes(navType)) return null;
return Vue.createVNode(Vue.Fragment, null, [items.value.map(function (_, index) {
return Vue.createVNode("span", {
"key": "page".concat(index),
"class": ["".concat(swiperNavClass.value, "__").concat(navType, "-item"), index === currentIndex.value && "".concat(swiperNavClass.value, "__").concat(navType, "-item--active"), "".concat(swiperNavClass.value, "__").concat(navType, "-item--").concat(props2.direction)]
}, null);
})]);
};
var renderFractionNav = function renderFractionNav() {
var _navigationConfig$val3, _currentIndex$value4;
if (((_navigationConfig$val3 = navigationConfig.value) === null || _navigationConfig$val3 === void 0 ? void 0 : _navigationConfig$val3.type) !== "fraction") return null;
return Vue.createVNode("span", null, ["".concat(((_currentIndex$value4 = currentIndex.value) !== null && _currentIndex$value4 !== void 0 ? _currentIndex$value4 : 0) + 1, "/").concat(items.value.length)]);
};
var renderTypeNav = function renderTypeNav() {
if (!("type" in navigationConfig.value)) return null;
return Vue.createVNode("span", {
"class": ["".concat(swiperNavClass.value, "--").concat(props2.direction), "".concat(swiperNavClass.value, "__").concat(navigationConfig.value.type || ""), "".concat(swiperNavClass.value, "--").concat(navigationConfig.value.paginationPosition || "bottom"), "".concat(swiperNavClass.value, "--").concat(navigationConfig.value.placement)]
}, [renderDotsNav(), renderFractionNav()]);
};
return function () {
var swiperNav = function swiperNav() {
if (props2.navigation === false) return null;
if (enableBuiltinNavigation.value) {
return Vue.createVNode(Vue.Fragment, null, [renderControlsNav(), renderTypeNav()]);
}
if (typeof props2.navigation === "function") return props2.navigation();
if (slots !== null && slots !== void 0 && slots.navigation) return slots.navigation();
return null;
};
return Vue.createVNode("div", {
"ref": root,
"class": rootClass.value
}, [Vue.createVNode("div", {
"ref": swiperContainer,
"class": "".concat(swiperClass.value, "__container"),
"style": {
flexDirection: isVertical.value ? "column" : "row",
transition: animating.value ? "transform ".concat(props2.duration, "ms") : "none",
transform: translateContainer.value,
height: containerHeight.value
},
"onTransitionstart": onTransitionstart,
"onTransitionend": function onTransitionend(event) {
if (event.target === event.currentTarget) {
_onTransitionend(event);
handleAnimationEnd();
}
},
"onClick": onItemClick
}, [renderTNodeJSX("default")]), swiperNav()]);
};
}
});
exports["default"] = _Swiper;
//# sourceMappingURL=swiper.js.map