PullRefresh.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { createVNode as _createVNode } from "vue";
  2. import { ref, watch, reactive, nextTick, defineComponent } from "vue";
  3. import { numericProp, getScrollTop, preventDefault, createNamespace, makeNumericProp } from "../utils/index.mjs";
  4. import { useEventListener, useScrollParent } from "@vant/use";
  5. import { useTouch } from "../composables/use-touch.mjs";
  6. import { Loading } from "../loading/index.mjs";
  7. const [name, bem, t] = createNamespace("pull-refresh");
  8. const DEFAULT_HEAD_HEIGHT = 50;
  9. const TEXT_STATUS = ["pulling", "loosing", "success"];
  10. const pullRefreshProps = {
  11. disabled: Boolean,
  12. modelValue: Boolean,
  13. headHeight: makeNumericProp(DEFAULT_HEAD_HEIGHT),
  14. successText: String,
  15. pullingText: String,
  16. loosingText: String,
  17. loadingText: String,
  18. pullDistance: numericProp,
  19. successDuration: makeNumericProp(500),
  20. animationDuration: makeNumericProp(300)
  21. };
  22. var stdin_default = defineComponent({
  23. name,
  24. props: pullRefreshProps,
  25. emits: ["change", "refresh", "update:modelValue"],
  26. setup(props, {
  27. emit,
  28. slots
  29. }) {
  30. let reachTop;
  31. const root = ref();
  32. const track = ref();
  33. const scrollParent = useScrollParent(root);
  34. const state = reactive({
  35. status: "normal",
  36. distance: 0,
  37. duration: 0
  38. });
  39. const touch = useTouch();
  40. const getHeadStyle = () => {
  41. if (props.headHeight !== DEFAULT_HEAD_HEIGHT) {
  42. return {
  43. height: `${props.headHeight}px`
  44. };
  45. }
  46. };
  47. const isTouchable = () => state.status !== "loading" && state.status !== "success" && !props.disabled;
  48. const ease = (distance) => {
  49. const pullDistance = +(props.pullDistance || props.headHeight);
  50. if (distance > pullDistance) {
  51. if (distance < pullDistance * 2) {
  52. distance = pullDistance + (distance - pullDistance) / 2;
  53. } else {
  54. distance = pullDistance * 1.5 + (distance - pullDistance * 2) / 4;
  55. }
  56. }
  57. return Math.round(distance);
  58. };
  59. const setStatus = (distance, isLoading) => {
  60. const pullDistance = +(props.pullDistance || props.headHeight);
  61. state.distance = distance;
  62. if (isLoading) {
  63. state.status = "loading";
  64. } else if (distance === 0) {
  65. state.status = "normal";
  66. } else if (distance < pullDistance) {
  67. state.status = "pulling";
  68. } else {
  69. state.status = "loosing";
  70. }
  71. emit("change", {
  72. status: state.status,
  73. distance
  74. });
  75. };
  76. const getStatusText = () => {
  77. const {
  78. status
  79. } = state;
  80. if (status === "normal") {
  81. return "";
  82. }
  83. return props[`${status}Text`] || t(status);
  84. };
  85. const renderStatus = () => {
  86. const {
  87. status,
  88. distance
  89. } = state;
  90. if (slots[status]) {
  91. return slots[status]({
  92. distance
  93. });
  94. }
  95. const nodes = [];
  96. if (TEXT_STATUS.includes(status)) {
  97. nodes.push(_createVNode("div", {
  98. "class": bem("text")
  99. }, [getStatusText()]));
  100. }
  101. if (status === "loading") {
  102. nodes.push(_createVNode(Loading, {
  103. "class": bem("loading")
  104. }, {
  105. default: getStatusText
  106. }));
  107. }
  108. return nodes;
  109. };
  110. const showSuccessTip = () => {
  111. state.status = "success";
  112. setTimeout(() => {
  113. setStatus(0);
  114. }, +props.successDuration);
  115. };
  116. const checkPosition = (event) => {
  117. reachTop = getScrollTop(scrollParent.value) === 0;
  118. if (reachTop) {
  119. state.duration = 0;
  120. touch.start(event);
  121. }
  122. };
  123. const onTouchStart = (event) => {
  124. if (isTouchable()) {
  125. checkPosition(event);
  126. }
  127. };
  128. const onTouchMove = (event) => {
  129. if (isTouchable()) {
  130. if (!reachTop) {
  131. checkPosition(event);
  132. }
  133. const {
  134. deltaY
  135. } = touch;
  136. touch.move(event);
  137. if (reachTop && deltaY.value >= 0 && touch.isVertical()) {
  138. preventDefault(event);
  139. setStatus(ease(deltaY.value));
  140. }
  141. }
  142. };
  143. const onTouchEnd = () => {
  144. if (reachTop && touch.deltaY.value && isTouchable()) {
  145. state.duration = +props.animationDuration;
  146. if (state.status === "loosing") {
  147. setStatus(+props.headHeight, true);
  148. emit("update:modelValue", true);
  149. nextTick(() => emit("refresh"));
  150. } else {
  151. setStatus(0);
  152. }
  153. }
  154. };
  155. watch(() => props.modelValue, (value) => {
  156. state.duration = +props.animationDuration;
  157. if (value) {
  158. setStatus(+props.headHeight, true);
  159. } else if (slots.success || props.successText) {
  160. showSuccessTip();
  161. } else {
  162. setStatus(0, false);
  163. }
  164. });
  165. useEventListener("touchmove", onTouchMove, {
  166. target: track
  167. });
  168. return () => {
  169. var _a;
  170. const trackStyle = {
  171. transitionDuration: `${state.duration}ms`,
  172. transform: state.distance ? `translate3d(0,${state.distance}px, 0)` : ""
  173. };
  174. return _createVNode("div", {
  175. "ref": root,
  176. "class": bem()
  177. }, [_createVNode("div", {
  178. "ref": track,
  179. "class": bem("track"),
  180. "style": trackStyle,
  181. "onTouchstartPassive": onTouchStart,
  182. "onTouchend": onTouchEnd,
  183. "onTouchcancel": onTouchEnd
  184. }, [_createVNode("div", {
  185. "class": bem("head"),
  186. "style": getHeadStyle()
  187. }, [renderStatus()]), (_a = slots.default) == null ? void 0 : _a.call(slots)])]);
  188. };
  189. }
  190. });
  191. export {
  192. stdin_default as default
  193. };