tdesign-vue
Version:
368 lines (364 loc) • 14.8 kB
JavaScript
/**
* tdesign v1.14.1
* (c) 2025 tdesign
* @license MIT
*/
import { h as helper } from '../_chunks/dep-e77071c7.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import { ChevronLeftIcon, ChevronRightIcon } from 'tdesign-icons-vue';
import { kebabCase } from 'lodash-es';
import props from './props.js';
import _SwiperItem from './swiper-item.js';
import { isVNode } from '../hooks/render-tnode.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { emitEvent } from '../utils/event.js';
import { getClassPrefixMixins, getGlobalIconMixins } from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import '@vue/composition-api';
import '@babel/runtime/helpers/readOnlyError';
import '@babel/runtime/helpers/typeof';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../config-provider/context.js';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-d2f11e9e.js';
import '../_chunks/dep-05dc6583.js';
import 'dayjs';
import '../_chunks/dep-62386ed0.js';
import '../config-provider/type.js';
import '../_common/js/global-config/t.js';
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(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 classPrefixMixins = getClassPrefixMixins("swiper");
var defaultNavigation = {
placement: "inside",
showSlideBtn: "always",
size: "medium",
type: "bars"
};
var _Swiper = mixins(Vue, classPrefixMixins, getGlobalIconMixins()).extend(_objectSpread(_objectSpread({
name: "TSwiper",
components: {
TSwiperItem: _SwiperItem
}
}, {
swiperTimer: 0,
swiperSwitchingTimer: 0
}), {}, {
props: _objectSpread({}, props),
data: function data() {
return {
currentIndex: 0,
isHovering: false,
isSwitching: false,
swiperItemList: [],
showArrow: false
};
},
computed: {
swiperItemLength: function swiperItemLength() {
return this.swiperItemList.length;
},
navigationConfig: function navigationConfig() {
return _objectSpread(_objectSpread({}, defaultNavigation), isVNode(this.navigation) ? {} : this.navigation);
},
isEnd: function isEnd() {
if (this.type === "card") {
return !this.loop && this.currentIndex + 1 >= this.swiperItemLength;
}
return !this.loop && this.currentIndex + 2 >= this.swiperItemLength;
},
propsToUpdateSetTimer: function propsToUpdateSetTimer() {
return [this.autoplay, this.currentIndex, this.duration, this.interval];
},
swiperWrapClass: function swiperWrapClass() {
return _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(this.componentName, "__wrap"), true), "".concat(this.componentName, "--inside"), this.navigationConfig.placement === "inside"), "".concat(this.componentName, "--outside"), this.navigationConfig.placement === "outside"), "".concat(this.componentName, "--vertical"), this.direction === "vertical"), "".concat(this.componentName, "--large"), this.navigationConfig.size === "large"), "".concat(this.componentName, "--small"), this.navigationConfig.size === "small");
},
containerStyle: function containerStyle() {
var offsetHeight = this.height ? "".concat(this.height, "px") : "".concat(this.getWrapAttribute("offsetHeight"), "px");
if (this.type === "card" || this.animation === "fade") {
return {
height: offsetHeight
};
}
if (this.animation === "slide") {
if (this.direction === "vertical") {
return {
height: offsetHeight,
transform: "translate3d(0, -".concat(this.currentIndex * 100, "%, 0px)"),
transition: this.isSwitching ? "transform ".concat(this.duration / 1e3, "s ease") : ""
};
}
return {
msTransform: "translate3d(-".concat(this.currentIndex * 100, "%, 0px, 0px)"),
WebkitTransform: "translate3d(-".concat(this.currentIndex * 100, "%, 0px, 0px)"),
transform: "translate3d(-".concat(this.currentIndex * 100, "%, 0px, 0px)"),
transition: this.isSwitching ? "transform ".concat(this.duration / 1e3, "s ease") : ""
};
}
return {};
},
swiperItems: function swiperItems() {
var _this = this;
var h = this.$createElement;
return this.swiperItemList.map(function (swiperItem, index) {
return h(_SwiperItem, helper([{
"attrs": {
"index": index,
"currentIndex": _this.currentIndex,
"isSwitching": _this.isSwitching,
"getWrapAttribute": _this.getWrapAttribute,
"swiperItemLength": _this.swiperItemLength
}
}, {
"props": _objectSpread(_objectSpread({}, _this.$props), swiperItem.propsData)
}]), [swiperItem.children]);
});
}
},
watch: {
propsToUpdateSetTimer: function propsToUpdateSetTimer() {
this.setTimer();
},
isSwitching: function isSwitching() {
var _this2 = this;
if (this.isSwitching) {
if (this.swiperSwitchingTimer) clearTimeout(this.swiperSwitchingTimer);
this.swiperSwitchingTimer = setTimeout(function () {
_this2.isSwitching = false;
_this2.swiperSwitchingTimer = 0;
if (_this2.isEnd) {
_this2.clearTimer();
}
}, this.duration + 50);
}
},
current: function current() {
this.swiperTo(this.current, {
source: "autoplay"
});
}
},
mounted: function mounted() {
this.updateSwiperItems();
this.setTimer();
this.showArrow = this.navigationConfig.showSlideBtn === "always";
},
updated: function updated() {
var _this3 = this;
this.$nextTick(function () {
_this3.updateSwiperItems();
});
},
methods: {
updateSwiperItems: function updateSwiperItems() {
var _this$$scopedSlots$de,
_this$$scopedSlots,
_this4 = this;
var originalChildren = ((_this$$scopedSlots$de = (_this$$scopedSlots = this.$scopedSlots)["default"]) === null || _this$$scopedSlots$de === void 0 ? void 0 : _this$$scopedSlots$de.call(_this$$scopedSlots, {})) || [];
var swiperItemList = originalChildren.map(function (swiper) {
return swiper.componentOptions;
}).filter(function (swiper) {
return kebabCase(swiper === null || swiper === void 0 ? void 0 : swiper.tag).endsWith("".concat(_this4.componentName, "-item"));
});
var isUnchange = swiperItemList.length === this.swiperItemList.length && this.swiperItemList.every(function (swiperItem, index) {
return swiperItem === swiperItemList[index];
});
if (isUnchange) return;
this.swiperItemList = swiperItemList;
},
setTimer: function setTimer() {
var _this5 = this;
if (this.autoplay && this.interval > 0) {
this.clearTimer();
this.swiperTimer = setTimeout(function () {
_this5.swiperTo(_this5.currentIndex + 1, {
source: "autoplay"
});
}, this.currentIndex === 0 ? this.interval - (this.duration + 50) : this.interval);
}
},
clearTimer: function clearTimer() {
if (this.swiperTimer) {
clearTimeout(this.swiperTimer);
this.swiperTimer = 0;
}
},
onMouseEnter: function onMouseEnter() {
this.isHovering = true;
if (this.stopOnHover) {
this.clearTimer();
}
if (this.navigationConfig.showSlideBtn === "hover") {
this.showArrow = true;
}
},
onMouseLeave: function onMouseLeave() {
this.isHovering = false;
if (!this.isEnd) {
this.setTimer();
}
if (this.navigationConfig.showSlideBtn === "hover") {
this.showArrow = false;
}
},
onMouseEnterNavigationItem: function onMouseEnterNavigationItem(i) {
if (this.trigger === "hover") {
this.swiperTo(i, {
source: "hover"
});
}
},
onClickNavigationItem: function onClickNavigationItem(i) {
if (this.trigger === "click") {
this.swiperTo(i, {
source: "click"
});
}
},
swiperTo: function swiperTo(index, context) {
var targetIndex = index % this.swiperItemLength;
emitEvent(this, "change", targetIndex, context);
this.isSwitching = true;
this.currentIndex = targetIndex;
},
goNext: function goNext(context) {
if (this.isSwitching) return;
if (this.type === "card") {
return this.swiperTo(this.currentIndex + 1 >= this.swiperItemLength ? 0 : this.currentIndex + 1, context);
}
return this.swiperTo(this.currentIndex + 1, context);
},
goPrevious: function goPrevious(context) {
if (this.isSwitching) return;
if (this.currentIndex - 1 < 0) {
return this.swiperTo(this.swiperItemLength - 1, context);
}
return this.swiperTo(this.currentIndex - 1, context);
},
getWrapAttribute: function getWrapAttribute(attr) {
var _this$$refs$swiperWra;
return (_this$$refs$swiperWra = this.$refs.swiperWrap) === null || _this$$refs$swiperWra === void 0 || (_this$$refs$swiperWra = _this$$refs$swiperWra.parentNode) === null || _this$$refs$swiperWra === void 0 ? void 0 : _this$$refs$swiperWra[attr];
},
renderPagination: function renderPagination() {
var _this6 = this;
var h = this.$createElement;
var fractionIndex = this.currentIndex + 1 > this.swiperItemLength ? 1 : this.currentIndex + 1;
var _this$useGlobalIcon = this.useGlobalIcon({
ChevronLeftIcon: ChevronLeftIcon,
ChevronRightIcon: ChevronRightIcon
}),
ChevronLeftIcon$1 = _this$useGlobalIcon.ChevronLeftIcon,
ChevronRightIcon$1 = _this$useGlobalIcon.ChevronRightIcon;
return h("div", {
"class": "".concat(this.componentName, "__arrow")
}, [h("div", {
"class": "".concat(this.componentName, "__arrow-left"),
"on": {
"click": function click() {
return _this6.goPrevious({
source: "click"
});
}
}
}, [h(ChevronLeftIcon$1)]), h("div", {
"class": "".concat(this.componentName, "__navigation-text-fraction")
}, [fractionIndex, "/", this.swiperItemLength]), h("div", {
"class": "".concat(this.componentName, "__arrow-right"),
"on": {
"click": function click() {
return _this6.goNext({
source: "click"
});
}
}
}, [h(ChevronRightIcon$1)])]);
},
renderArrow: function renderArrow() {
var _this7 = this;
var h = this.$createElement;
if (!this.showArrow) return null;
var _this$useGlobalIcon2 = this.useGlobalIcon({
ChevronLeftIcon: ChevronLeftIcon,
ChevronRightIcon: ChevronRightIcon
}),
ChevronLeftIcon$1 = _this$useGlobalIcon2.ChevronLeftIcon,
ChevronRightIcon$1 = _this$useGlobalIcon2.ChevronRightIcon;
return h("div", {
"class": ["".concat(this.componentName, "__arrow"), "".concat(this.componentName, "__arrow--default")]
}, [h("div", {
"class": "".concat(this.componentName, "__arrow-left"),
"on": {
"click": function click() {
return _this7.goPrevious({
source: "click"
});
}
}
}, [h(ChevronLeftIcon$1)]), h("div", {
"class": "".concat(this.componentName, "__arrow-right"),
"on": {
"click": function click() {
return _this7.goNext({
source: "click"
});
}
}
}, [h(ChevronRightIcon$1)])]);
},
renderNavigation: function renderNavigation() {
var _this8 = this;
var h = this.$createElement;
var navigationSlot = renderTNodeJSX(this, "navigation");
if (isVNode(this.navigation)) return this.navigation;
if (navigationSlot && isVNode(navigationSlot === null || navigationSlot === void 0 ? void 0 : navigationSlot[0])) return navigationSlot;
if (this.navigationConfig.type === "fraction") {
return h("div", {
"class": ["".concat(this.componentName, "__navigation"), "".concat(this.componentName, "__navigation--fraction")]
}, [this.renderPagination()]);
}
return h("ul", {
"class": ["".concat(this.componentName, "__navigation"), _defineProperty({}, "".concat(this.componentName, "__navigation-bars"), this.navigationConfig.type === "bars")]
}, [this.swiperItemList.map(function (_, i) {
return h("li", {
"key": i,
"class": ["".concat(_this8.componentName, "__navigation-item"), _defineProperty({}, "".concat(_this8.classPrefix, "-is-active"), i === _this8.currentIndex)],
"on": {
"mouseenter": function mouseenter() {
return _this8.onMouseEnterNavigationItem(i);
},
"click": function click() {
return _this8.onClickNavigationItem(i);
}
}
}, [h("span")]);
})]);
},
renderSwiperItems: function renderSwiperItems() {
return this.swiperItems;
}
},
render: function render() {
var h = arguments[0];
return h("div", {
"class": this.componentName,
"on": {
"mouseenter": this.onMouseEnter,
"mouseleave": this.onMouseLeave
},
"ref": "swiperWrap"
}, [h("div", {
"class": this.swiperWrapClass
}, [h("div", {
"class": ["".concat(this.componentName, "__content"), _defineProperty(_defineProperty({}, "".concat(this.componentName, "-fade"), this.animation === "fade"), "".concat(this.componentName, "-card"), this.type === "card")]
}, [h("div", {
"class": "".concat(this.componentName, "__container"),
"style": this.containerStyle
}, [this.renderSwiperItems()])]), this.renderNavigation(), this.renderArrow()])]);
}
}));
export { _Swiper as default };
//# sourceMappingURL=swiper.js.map