lazy-image.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 name in all)
  7. __defProp(target, name, { get: all[name], 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_use = require("@vant/use");
  24. var import_util = require("./util");
  25. var import_utils = require("../../utils");
  26. var import_vue = require("vue");
  27. var stdin_default = (lazyManager) => ({
  28. props: {
  29. src: [String, Object],
  30. tag: {
  31. type: String,
  32. default: "img"
  33. }
  34. },
  35. render() {
  36. var _a, _b;
  37. return (0, import_vue.h)(
  38. this.tag,
  39. {
  40. src: this.renderSrc
  41. },
  42. (_b = (_a = this.$slots).default) == null ? void 0 : _b.call(_a)
  43. );
  44. },
  45. data() {
  46. return {
  47. el: null,
  48. options: {
  49. src: "",
  50. error: "",
  51. loading: "",
  52. attempt: lazyManager.options.attempt
  53. },
  54. state: {
  55. loaded: false,
  56. error: false,
  57. attempt: 0
  58. },
  59. renderSrc: ""
  60. };
  61. },
  62. watch: {
  63. src() {
  64. this.init();
  65. lazyManager.addLazyBox(this);
  66. lazyManager.lazyLoadHandler();
  67. }
  68. },
  69. created() {
  70. this.init();
  71. },
  72. mounted() {
  73. this.el = this.$el;
  74. lazyManager.addLazyBox(this);
  75. lazyManager.lazyLoadHandler();
  76. },
  77. beforeUnmount() {
  78. lazyManager.removeComponent(this);
  79. },
  80. methods: {
  81. init() {
  82. const { src, loading, error } = lazyManager.valueFormatter(this.src);
  83. this.state.loaded = false;
  84. this.options.src = src;
  85. this.options.error = error;
  86. this.options.loading = loading;
  87. this.renderSrc = this.options.loading;
  88. },
  89. checkInView() {
  90. const rect = (0, import_use.useRect)(this.$el);
  91. return rect.top < window.innerHeight * lazyManager.options.preLoad && rect.bottom > 0 && rect.left < window.innerWidth * lazyManager.options.preLoad && rect.right > 0;
  92. },
  93. load(onFinish = import_utils.noop) {
  94. if (this.state.attempt > this.options.attempt - 1 && this.state.error) {
  95. if (process.env.NODE_ENV !== "production" && !lazyManager.options.silent) {
  96. console.log(
  97. `[@vant/lazyload] ${this.options.src} tried too more than ${this.options.attempt} times`
  98. );
  99. }
  100. onFinish();
  101. return;
  102. }
  103. const { src } = this.options;
  104. (0, import_util.loadImageAsync)(
  105. { src },
  106. ({ src: src2 }) => {
  107. this.renderSrc = src2;
  108. this.state.loaded = true;
  109. },
  110. () => {
  111. this.state.attempt++;
  112. this.renderSrc = this.options.error;
  113. this.state.error = true;
  114. }
  115. );
  116. }
  117. }
  118. });