import { ref, watch, reactive, nextTick } from 'vue'; // Utils import { createNamespace } from '../utils'; import { getScrollTop } from '../utils/dom/scroll'; import { preventDefault } from '../utils/dom/event'; // Composition import { useScrollParent } from '@vant/use'; import { useTouch } from '../composition/use-touch'; // Components import Loading from '../loading'; const [createComponent, bem, t] = createNamespace('pull-refresh'); const DEFAULT_HEAD_HEIGHT = 50; const TEXT_STATUS = ['pulling', 'loosing', 'success']; export default createComponent({ props: { disabled: Boolean, successText: String, pullingText: String, loosingText: String, loadingText: String, modelValue: { type: Boolean, required: true, }, successDuration: { type: [Number, String], default: 500, }, animationDuration: { type: [Number, String], default: 300, }, headHeight: { type: [Number, String], default: DEFAULT_HEAD_HEIGHT, }, }, emits: ['refresh', 'update:modelValue'], setup(props, { emit, slots }) { let reachTop; const root = ref(); const scrollParent = useScrollParent(root); const state = reactive({ status: 'normal', distance: 0, duration: 0, }); const touch = useTouch(); const getHeadStyle = () => { if (props.headHeight !== DEFAULT_HEAD_HEIGHT) { return { height: `${props.headHeight}px`, }; } }; const isTouchable = () => state.status !== 'loading' && state.status !== 'success' && !props.disabled; const ease = (distance) => { const headHeight = +props.headHeight; if (distance > headHeight) { if (distance < headHeight * 2) { distance = headHeight + (distance - headHeight) / 2; } else { distance = headHeight * 1.5 + (distance - headHeight * 2) / 4; } } return Math.round(distance); }; const setStatus = (distance, isLoading) => { state.distance = distance; if (isLoading) { state.status = 'loading'; } else if (distance === 0) { state.status = 'normal'; } else if (distance < props.headHeight) { state.status = 'pulling'; } else { state.status = 'loosing'; } }; const renderStatus = () => { const { status, distance } = state; if (slots[status]) { return slots[status]({ distance }); } const nodes = []; const text = (status !== 'normal' && props[`${status}Text`]) || t(status); if (TEXT_STATUS.indexOf(status) !== -1) { nodes.push(