UNPKG

tdesign-mobile-vue

Version:
216 lines (212 loc) 6.72 kB
/** * tdesign v1.9.3 * (c) 2025 TDesign Group * @license MIT */ import { ref, defineComponent, reactive, nextTick, computed, watch, createVNode } from 'vue'; import { preventDefault } from '../shared/dom.mjs'; import config from '../config.mjs'; import { Loading } from '../loading/index.mjs'; import { useConfig } from '../config-provider/useConfig.mjs'; import 'lodash-es'; import '../loading/loading.mjs'; import '../_chunks/dep-900db0e1.mjs'; import '../_chunks/dep-4915223e.mjs'; import '../loading/icon/gradient.mjs'; import '../_common/js/loading/circle-adapter.mjs'; import '../_common/js/utils/setStyle.mjs'; import '../_common/js/utils/helper.mjs'; import '../_chunks/dep-fb4113ef.mjs'; import '../_chunks/dep-a953013f.mjs'; import '../_chunks/dep-da32ae58.mjs'; import '../_chunks/dep-26b97ced.mjs'; import '../hooks/useClass.mjs'; import '../config-provider/context.mjs'; import '../_common/js/global-config/mobile/default-config.mjs'; import '../_common/js/global-config/mobile/locale/zh_CN.mjs'; import '../_chunks/dep-bca0f578.mjs'; import '../_chunks/dep-6b2b02fd.mjs'; import '../_chunks/dep-c6f44a15.mjs'; import '../_chunks/dep-c984d53e.mjs'; import '../config-provider/type.mjs'; import '../loading/icon/spinner.mjs'; import '../loading/props.mjs'; import '../hooks/tnode.mjs'; import '../hooks/render-tnode.mjs'; import '../_common/js/utils/general.mjs'; import '../shared/index.mjs'; import '../shared/functions.mjs'; import '../shared/util.mjs'; import '../shared/component.mjs'; import '../shared/render.mjs'; import '../shared/render-tnode.mjs'; import '../shared/useCountDown/index.mjs'; import '../_chunks/dep-99e43b7b.mjs'; import '@vueuse/core'; import '../shared/useCountDown/utils.mjs'; import '../shared/useTouch/index.mjs'; import '../shared/useScrollParent/index.mjs'; import '../shared/useTest/index.mjs'; import '../shared/useClickAway/index.mjs'; import '../shared/hover.mjs'; import './style/css.mjs'; import '../loading/type.mjs'; import '../loading/plugin.mjs'; var prefix = config.prefix; var _useConfig = useConfig("list"), globalConfig = _useConfig.globalConfig; var name = "".concat(prefix, "-pull-refresh"); function useTouch() { var startY = ref(0); var deltaY = ref(0); var start = function start(event) { startY.value = event.touches[0].clientY; deltaY.value = 0; }; var move = function move(event) { var touch = event.touches[0]; deltaY.value = touch.clientY - startY.value; }; return { startY: startY, deltaY: deltaY, start: start, move: move }; } var PULL_DISTANCE = 50; var ANIMATION_DURATION = 300; var TEXT_MAP = { loading: globalConfig.value.loading, pulling: globalConfig.value.pulling, loosing: globalConfig.value.loosing, success: globalConfig.value.success }; var SHOW_TEXT_LIST = ["pulling", "loosing", "success"]; var PullRefreshProps = { modelValue: Boolean }; var easeDistance = function easeDistance(distance, pullDistance) { if (distance > pullDistance) { if (distance < pullDistance * 2) { distance = pullDistance + (distance - pullDistance) / 2; } else { distance = pullDistance * 1.5 + (distance - pullDistance * 2) / 4; } } return Math.round(distance); }; function isElement(node) { var ELEMENT_NODE_TYPE = 1; return node.tagName !== "HTML" && node.tagName !== "BODY" && node.nodeType === ELEMENT_NODE_TYPE; } var getScrollParent = function getScrollParent(node) { var res = node; while (res && isElement(res)) { if (/auto|scroll/i.test(window.getComputedStyle(res).overflowY)) { return res; } res = res.parentNode; } }; var pullRefresh = defineComponent({ name: name, components: { TLoading: Loading }, props: PullRefreshProps, emits: ["refresh", "update:modelValue"], setup: function setup(props, _ref) { var emit = _ref.emit, slots = _ref.slots; var state = reactive({ status: "normal", distance: 0, duration: 0 }); var setStatus = function setStatus(distance, isLoading) { state.distance = distance; if (isLoading) { state.status = "loading"; } else if (distance === 0) { state.status = "normal"; } else if (distance < PULL_DISTANCE) { state.status = "pulling"; } else { state.status = "loosing"; } }; var touch = useTouch(); var isTouchable = function isTouchable() { return state.status !== "loading" && state.status !== "success"; }; var isReachTop = function isReachTop(e) { var scrollParent = getScrollParent(e.target); return !scrollParent || !scrollParent.scrollTop; }; var onTouchStart = function onTouchStart(e) { if (!isReachTop(e)) return; if (isTouchable()) { state.duration = 0; touch.start(e); } }; var onTouchMove = function onTouchMove(e) { if (!isTouchable()) return; if (!isReachTop(e)) return; var deltaY = touch.deltaY; if (deltaY.value >= 0) { preventDefault(e, false); setStatus(easeDistance(deltaY.value, PULL_DISTANCE)); } touch.move(e); }; var onTouchEnd = function onTouchEnd() { state.duration = ANIMATION_DURATION; if (state.status === "loosing") { emit("update:modelValue", true); nextTick(function () { return emit("refresh"); }); } else { setStatus(0); } }; var trackStyle = computed(function () { return { transitionDuration: "".concat(state.duration, "ms"), transform: state.distance ? "translate3d(0, ".concat(state.distance, "px, 0)") : "" }; }); watch(function () { return props.modelValue; }, function (value) { if (value) { setStatus(PULL_DISTANCE, true); } else { state.status = "success"; setTimeout(function () { setStatus(0, false); }, ANIMATION_DURATION); } }); return function () { return createVNode("div", { "class": name }, [createVNode("div", { "class": "".concat(name, "__track"), "style": trackStyle.value, "onTouchstart": onTouchStart, "onTouchmove": onTouchMove, "onTouchend": onTouchEnd, "onTouchcancel": onTouchEnd }, [createVNode("div", { "class": "".concat(name, "__head") }, [SHOW_TEXT_LIST.includes(state.status) && createVNode("div", null, [TEXT_MAP[state.status]]), state.status === "loading" && createVNode(Loading, { "text": globalConfig.value.loading }, null)]), slots.default && slots.default()])]); }; } }); export { pullRefresh as default }; //# sourceMappingURL=pull-refresh.mjs.map