List.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_vue2 = require("vue");
  25. var import_utils = require("../utils");
  26. var import_use = require("@vant/use");
  27. var import_use_expose = require("../composables/use-expose");
  28. var import_use_tab_status = require("../composables/use-tab-status");
  29. var import_loading = require("../loading");
  30. const [name, bem, t] = (0, import_utils.createNamespace)("list");
  31. const listProps = {
  32. error: Boolean,
  33. offset: (0, import_utils.makeNumericProp)(300),
  34. loading: Boolean,
  35. finished: Boolean,
  36. errorText: String,
  37. direction: (0, import_utils.makeStringProp)("down"),
  38. loadingText: String,
  39. finishedText: String,
  40. immediateCheck: import_utils.truthProp
  41. };
  42. var stdin_default = (0, import_vue2.defineComponent)({
  43. name,
  44. props: listProps,
  45. emits: ["load", "update:error", "update:loading"],
  46. setup(props, {
  47. emit,
  48. slots
  49. }) {
  50. const loading = (0, import_vue2.ref)(props.loading);
  51. const root = (0, import_vue2.ref)();
  52. const placeholder = (0, import_vue2.ref)();
  53. const tabStatus = (0, import_use_tab_status.useTabStatus)();
  54. const scrollParent = (0, import_use.useScrollParent)(root);
  55. const check = () => {
  56. (0, import_vue2.nextTick)(() => {
  57. if (loading.value || props.finished || props.error || (tabStatus == null ? void 0 : tabStatus.value) === false) {
  58. return;
  59. }
  60. const {
  61. offset,
  62. direction
  63. } = props;
  64. const scrollParentRect = (0, import_use.useRect)(scrollParent);
  65. if (!scrollParentRect.height || (0, import_utils.isHidden)(root)) {
  66. return;
  67. }
  68. let isReachEdge = false;
  69. const placeholderRect = (0, import_use.useRect)(placeholder);
  70. if (direction === "up") {
  71. isReachEdge = scrollParentRect.top - placeholderRect.top <= offset;
  72. } else {
  73. isReachEdge = placeholderRect.bottom - scrollParentRect.bottom <= offset;
  74. }
  75. if (isReachEdge) {
  76. loading.value = true;
  77. emit("update:loading", true);
  78. emit("load");
  79. }
  80. });
  81. };
  82. const renderFinishedText = () => {
  83. if (props.finished) {
  84. const text = slots.finished ? slots.finished() : props.finishedText;
  85. if (text) {
  86. return (0, import_vue.createVNode)("div", {
  87. "class": bem("finished-text")
  88. }, [text]);
  89. }
  90. }
  91. };
  92. const clickErrorText = () => {
  93. emit("update:error", false);
  94. check();
  95. };
  96. const renderErrorText = () => {
  97. if (props.error) {
  98. const text = slots.error ? slots.error() : props.errorText;
  99. if (text) {
  100. return (0, import_vue.createVNode)("div", {
  101. "role": "button",
  102. "class": bem("error-text"),
  103. "tabindex": 0,
  104. "onClick": clickErrorText
  105. }, [text]);
  106. }
  107. }
  108. };
  109. const renderLoading = () => {
  110. if (loading.value && !props.finished) {
  111. return (0, import_vue.createVNode)("div", {
  112. "class": bem("loading")
  113. }, [slots.loading ? slots.loading() : (0, import_vue.createVNode)(import_loading.Loading, {
  114. "class": bem("loading-icon")
  115. }, {
  116. default: () => [props.loadingText || t("loading")]
  117. })]);
  118. }
  119. };
  120. (0, import_vue2.watch)(() => [props.loading, props.finished, props.error], check);
  121. if (tabStatus) {
  122. (0, import_vue2.watch)(tabStatus, (tabActive) => {
  123. if (tabActive) {
  124. check();
  125. }
  126. });
  127. }
  128. (0, import_vue2.onUpdated)(() => {
  129. loading.value = props.loading;
  130. });
  131. (0, import_vue2.onMounted)(() => {
  132. if (props.immediateCheck) {
  133. check();
  134. }
  135. });
  136. (0, import_use_expose.useExpose)({
  137. check
  138. });
  139. (0, import_use.useEventListener)("scroll", check, {
  140. target: scrollParent,
  141. passive: true
  142. });
  143. return () => {
  144. var _a;
  145. const Content = (_a = slots.default) == null ? void 0 : _a.call(slots);
  146. const Placeholder = (0, import_vue.createVNode)("div", {
  147. "ref": placeholder,
  148. "class": bem("placeholder")
  149. }, null);
  150. return (0, import_vue.createVNode)("div", {
  151. "ref": root,
  152. "role": "feed",
  153. "class": bem(),
  154. "aria-busy": loading.value
  155. }, [props.direction === "down" ? Content : Placeholder, renderLoading(), renderFinishedText(), renderErrorText(), props.direction === "up" ? Content : Placeholder]);
  156. };
  157. }
  158. });