123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- var __defProp = Object.defineProperty;
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
- var __getOwnPropNames = Object.getOwnPropertyNames;
- var __hasOwnProp = Object.prototype.hasOwnProperty;
- var __export = (target, all) => {
- for (var name in all)
- __defProp(target, name, { get: all[name], enumerable: true });
- };
- var __copyProps = (to, from, except, desc) => {
- if (from && typeof from === "object" || typeof from === "function") {
- for (let key of __getOwnPropNames(from))
- if (!__hasOwnProp.call(to, key) && key !== except)
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
- }
- return to;
- };
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
- var stdin_exports = {};
- __export(stdin_exports, {
- default: () => stdin_default
- });
- module.exports = __toCommonJS(stdin_exports);
- var import_use = require("@vant/use");
- var import_util = require("./util");
- var import_utils = require("../../utils");
- var import_vue = require("vue");
- var stdin_default = (lazyManager) => ({
- props: {
- src: [String, Object],
- tag: {
- type: String,
- default: "img"
- }
- },
- render() {
- var _a, _b;
- return (0, import_vue.h)(
- this.tag,
- {
- src: this.renderSrc
- },
- (_b = (_a = this.$slots).default) == null ? void 0 : _b.call(_a)
- );
- },
- data() {
- return {
- el: null,
- options: {
- src: "",
- error: "",
- loading: "",
- attempt: lazyManager.options.attempt
- },
- state: {
- loaded: false,
- error: false,
- attempt: 0
- },
- renderSrc: ""
- };
- },
- watch: {
- src() {
- this.init();
- lazyManager.addLazyBox(this);
- lazyManager.lazyLoadHandler();
- }
- },
- created() {
- this.init();
- this.renderSrc = this.options.loading;
- },
- mounted() {
- this.el = this.$el;
- lazyManager.addLazyBox(this);
- lazyManager.lazyLoadHandler();
- },
- beforeUnmount() {
- lazyManager.removeComponent(this);
- },
- methods: {
- init() {
- const { src, loading, error } = lazyManager.valueFormatter(this.src);
- this.state.loaded = false;
- this.options.src = src;
- this.options.error = error;
- this.options.loading = loading;
- this.renderSrc = this.options.loading;
- },
- checkInView() {
- const rect = (0, import_use.useRect)(this.$el);
- return rect.top < window.innerHeight * lazyManager.options.preLoad && rect.bottom > 0 && rect.left < window.innerWidth * lazyManager.options.preLoad && rect.right > 0;
- },
- load(onFinish = import_utils.noop) {
- if (this.state.attempt > this.options.attempt - 1 && this.state.error) {
- if (process.env.NODE_ENV !== "production" && !lazyManager.options.silent) {
- console.log(
- `[@vant/lazyload] ${this.options.src} tried too more than ${this.options.attempt} times`
- );
- }
- onFinish();
- return;
- }
- const { src } = this.options;
- (0, import_util.loadImageAsync)(
- { src },
- ({ src: src2 }) => {
- this.renderSrc = src2;
- this.state.loaded = true;
- },
- () => {
- this.state.attempt++;
- this.renderSrc = this.options.error;
- this.state.error = true;
- }
- );
- }
- }
- });
|