lazy-image.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. this.renderSrc = this.options.loading;
  72. },
  73. mounted() {
  74. this.el = this.$el;
  75. lazyManager.addLazyBox(this);
  76. lazyManager.lazyLoadHandler();
  77. },
  78. beforeUnmount() {
  79. lazyManager.removeComponent(this);
  80. },
  81. methods: {
  82. init() {
  83. const { src, loading, error } = lazyManager.valueFormatter(this.src);
  84. this.state.loaded = false;
  85. this.options.src = src;
  86. this.options.error = error;
  87. this.options.loading = loading;
  88. this.renderSrc = this.options.loading;
  89. },
  90. checkInView() {
  91. const rect = (0, import_use.useRect)(this.$el);
  92. return rect.top < window.innerHeight * lazyManager.options.preLoad && rect.bottom > 0 && rect.left < window.innerWidth * lazyManager.options.preLoad && rect.right > 0;
  93. },
  94. load(onFinish = import_utils.noop) {
  95. if (this.state.attempt > this.options.attempt - 1 && this.state.error) {
  96. if (process.env.NODE_ENV !== "production" && !lazyManager.options.silent) {
  97. console.log(
  98. `[@vant/lazyload] ${this.options.src} tried too more than ${this.options.attempt} times`
  99. );
  100. }
  101. onFinish();
  102. return;
  103. }
  104. const { src } = this.options;
  105. (0, import_util.loadImageAsync)(
  106. { src },
  107. ({ src: src2 }) => {
  108. this.renderSrc = src2;
  109. this.state.loaded = true;
  110. },
  111. () => {
  112. this.state.attempt++;
  113. this.renderSrc = this.options.error;
  114. this.state.error = true;
  115. }
  116. );
  117. }
  118. }
  119. });