first commit
This commit is contained in:
3
node_modules/vant/lib/lazyload/index.d.ts
generated
vendored
Normal file
3
node_modules/vant/lib/lazyload/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { Lazyload } from './vue-lazyload';
|
||||
export default Lazyload;
|
||||
export { Lazyload };
|
||||
25
node_modules/vant/lib/lazyload/index.js
generated
vendored
Normal file
25
node_modules/vant/lib/lazyload/index.js
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
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, {
|
||||
Lazyload: () => import_vue_lazyload.Lazyload,
|
||||
default: () => stdin_default
|
||||
});
|
||||
module.exports = __toCommonJS(stdin_exports);
|
||||
var import_vue_lazyload = require("./vue-lazyload");
|
||||
var stdin_default = import_vue_lazyload.Lazyload;
|
||||
1
node_modules/vant/lib/lazyload/style/index.d.ts
generated
vendored
Normal file
1
node_modules/vant/lib/lazyload/style/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
1
node_modules/vant/lib/lazyload/style/index.js
generated
vendored
Normal file
1
node_modules/vant/lib/lazyload/style/index.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
require("../../style/base.css");
|
||||
55
node_modules/vant/lib/lazyload/vue-lazyload/index.d.ts
generated
vendored
Normal file
55
node_modules/vant/lib/lazyload/vue-lazyload/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
import type { App } from 'vue';
|
||||
|
||||
declare type ListenEvent =
|
||||
| 'scroll'
|
||||
| 'wheel'
|
||||
| 'mousewheel'
|
||||
| 'resize'
|
||||
| 'animationend'
|
||||
| 'transitionend'
|
||||
| 'touchmove';
|
||||
|
||||
// eslint-disable-next-line
|
||||
declare type Callback = (listener: any, options: LazyloadOptions) => void;
|
||||
|
||||
declare type Filter = {
|
||||
webp?: Callback;
|
||||
progressive?: Callback;
|
||||
};
|
||||
|
||||
declare type Adapter = {
|
||||
error?: Callback;
|
||||
loaded?: Callback;
|
||||
loading?: Callback;
|
||||
};
|
||||
|
||||
export declare type LazyloadOptions = {
|
||||
error?: string;
|
||||
filter?: Filter;
|
||||
silent?: boolean;
|
||||
adapter?: Adapter;
|
||||
loading?: string;
|
||||
attempt?: number;
|
||||
preLoad?: number;
|
||||
observer?: boolean;
|
||||
lazyImage?: boolean;
|
||||
throttleWait?: number;
|
||||
listenEvents?: ListenEvent[];
|
||||
dispatchEvent?: boolean;
|
||||
lazyComponent?: boolean;
|
||||
observerOptions?: IntersectionObserverInit;
|
||||
};
|
||||
|
||||
export declare const Lazyload: {
|
||||
install(app: App, options?: LazyloadOptions): void;
|
||||
};
|
||||
|
||||
declare module 'vue' {
|
||||
interface ComponentCustomProperties {
|
||||
$Lazyload: {
|
||||
$on: (event: string, handler: Callback) => void;
|
||||
$off: (event: string, handler?: Callback) => void;
|
||||
$once: (event: string, handler: Callback) => void;
|
||||
};
|
||||
}
|
||||
}
|
||||
65
node_modules/vant/lib/lazyload/vue-lazyload/index.js
generated
vendored
Normal file
65
node_modules/vant/lib/lazyload/vue-lazyload/index.js
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
var __create = Object.create;
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __getProtoOf = Object.getPrototypeOf;
|
||||
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
||||
// If the importer is in node compatibility mode or this is not an ESM
|
||||
// file that has been converted to a CommonJS file using a Babel-
|
||||
// compatible transform (i.e. "__esModule" has not been set), then set
|
||||
// "default" to the CommonJS "module.exports" for node compatibility.
|
||||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
||||
mod
|
||||
));
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
var stdin_exports = {};
|
||||
__export(stdin_exports, {
|
||||
Lazyload: () => Lazyload
|
||||
});
|
||||
module.exports = __toCommonJS(stdin_exports);
|
||||
var import_lazy = __toESM(require("./lazy"));
|
||||
var import_lazy_component = __toESM(require("./lazy-component"));
|
||||
var import_lazy_container = __toESM(require("./lazy-container"));
|
||||
var import_lazy_image = __toESM(require("./lazy-image"));
|
||||
const Lazyload = {
|
||||
/*
|
||||
* install function
|
||||
* @param {App} app
|
||||
* @param {object} options lazyload options
|
||||
*/
|
||||
install(app, options = {}) {
|
||||
const LazyClass = (0, import_lazy.default)();
|
||||
const lazy = new LazyClass(options);
|
||||
const lazyContainer = new import_lazy_container.default({ lazy });
|
||||
app.config.globalProperties.$Lazyload = lazy;
|
||||
if (options.lazyComponent) {
|
||||
app.component("LazyComponent", (0, import_lazy_component.default)(lazy));
|
||||
}
|
||||
if (options.lazyImage) {
|
||||
app.component("LazyImage", (0, import_lazy_image.default)(lazy));
|
||||
}
|
||||
app.directive("lazy", {
|
||||
beforeMount: lazy.add.bind(lazy),
|
||||
updated: lazy.update.bind(lazy),
|
||||
unmounted: lazy.remove.bind(lazy)
|
||||
});
|
||||
app.directive("lazy-container", {
|
||||
beforeMount: lazyContainer.bind.bind(lazyContainer),
|
||||
updated: lazyContainer.update.bind(lazyContainer),
|
||||
unmounted: lazyContainer.unbind.bind(lazyContainer)
|
||||
});
|
||||
}
|
||||
};
|
||||
70
node_modules/vant/lib/lazyload/vue-lazyload/lazy-component.js
generated
vendored
Normal file
70
node_modules/vant/lib/lazyload/vue-lazyload/lazy-component.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
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_vue = require("vue");
|
||||
var import_use = require("@vant/use");
|
||||
var stdin_default = (lazy) => ({
|
||||
props: {
|
||||
tag: {
|
||||
type: String,
|
||||
default: "div"
|
||||
}
|
||||
},
|
||||
emits: ["show"],
|
||||
render() {
|
||||
return (0, import_vue.h)(
|
||||
this.tag,
|
||||
this.show && this.$slots.default ? this.$slots.default() : null
|
||||
);
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
el: null,
|
||||
state: {
|
||||
loaded: false
|
||||
},
|
||||
show: false
|
||||
};
|
||||
},
|
||||
mounted() {
|
||||
this.el = this.$el;
|
||||
lazy.addLazyBox(this);
|
||||
lazy.lazyLoadHandler();
|
||||
},
|
||||
beforeUnmount() {
|
||||
lazy.removeComponent(this);
|
||||
},
|
||||
methods: {
|
||||
checkInView() {
|
||||
const rect = (0, import_use.useRect)(this.$el);
|
||||
return import_use.inBrowser && rect.top < window.innerHeight * lazy.options.preLoad && rect.bottom > 0 && rect.left < window.innerWidth * lazy.options.preLoad && rect.right > 0;
|
||||
},
|
||||
load() {
|
||||
this.show = true;
|
||||
this.state.loaded = true;
|
||||
this.$emit("show", this);
|
||||
},
|
||||
destroy() {
|
||||
return this.$destroy;
|
||||
}
|
||||
}
|
||||
});
|
||||
91
node_modules/vant/lib/lazyload/vue-lazyload/lazy-container.js
generated
vendored
Normal file
91
node_modules/vant/lib/lazyload/vue-lazyload/lazy-container.js
generated
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
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: () => LazyContainerManager
|
||||
});
|
||||
module.exports = __toCommonJS(stdin_exports);
|
||||
var import_util = require("./util");
|
||||
const defaultOptions = {
|
||||
selector: "img"
|
||||
};
|
||||
class LazyContainer {
|
||||
constructor({ el, binding, vnode, lazy }) {
|
||||
this.el = null;
|
||||
this.vnode = vnode;
|
||||
this.binding = binding;
|
||||
this.options = {};
|
||||
this.lazy = lazy;
|
||||
this.queue = [];
|
||||
this.update({ el, binding });
|
||||
}
|
||||
update({ el, binding }) {
|
||||
this.el = el;
|
||||
this.options = Object.assign({}, defaultOptions, binding.value);
|
||||
const imgs = this.getImgs();
|
||||
imgs.forEach((el2) => {
|
||||
this.lazy.add(
|
||||
el2,
|
||||
Object.assign({}, this.binding, {
|
||||
value: {
|
||||
src: "dataset" in el2 ? el2.dataset.src : el2.getAttribute("data-src"),
|
||||
error: ("dataset" in el2 ? el2.dataset.error : el2.getAttribute("data-error")) || this.options.error,
|
||||
loading: ("dataset" in el2 ? el2.dataset.loading : el2.getAttribute("data-loading")) || this.options.loading
|
||||
}
|
||||
}),
|
||||
this.vnode
|
||||
);
|
||||
});
|
||||
}
|
||||
getImgs() {
|
||||
return Array.from(this.el.querySelectorAll(this.options.selector));
|
||||
}
|
||||
clear() {
|
||||
const imgs = this.getImgs();
|
||||
imgs.forEach((el) => this.lazy.remove(el));
|
||||
this.vnode = null;
|
||||
this.binding = null;
|
||||
this.lazy = null;
|
||||
}
|
||||
}
|
||||
class LazyContainerManager {
|
||||
constructor({ lazy }) {
|
||||
this.lazy = lazy;
|
||||
this.queue = [];
|
||||
}
|
||||
bind(el, binding, vnode) {
|
||||
const container = new LazyContainer({
|
||||
el,
|
||||
binding,
|
||||
vnode,
|
||||
lazy: this.lazy
|
||||
});
|
||||
this.queue.push(container);
|
||||
}
|
||||
update(el, binding, vnode) {
|
||||
const container = this.queue.find((item) => item.el === el);
|
||||
if (!container) return;
|
||||
container.update({ el, binding, vnode });
|
||||
}
|
||||
unbind(el) {
|
||||
const container = this.queue.find((item) => item.el === el);
|
||||
if (!container) return;
|
||||
container.clear();
|
||||
(0, import_util.remove)(this.queue, container);
|
||||
}
|
||||
}
|
||||
118
node_modules/vant/lib/lazyload/vue-lazyload/lazy-image.js
generated
vendored
Normal file
118
node_modules/vant/lib/lazyload/vue-lazyload/lazy-image.js
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
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();
|
||||
},
|
||||
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;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
455
node_modules/vant/lib/lazyload/vue-lazyload/lazy.js
generated
vendored
Normal file
455
node_modules/vant/lib/lazyload/vue-lazyload/lazy.js
generated
vendored
Normal file
@@ -0,0 +1,455 @@
|
||||
var __create = Object.create;
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __getProtoOf = Object.getPrototypeOf;
|
||||
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
||||
// If the importer is in node compatibility mode or this is not an ESM
|
||||
// file that has been converted to a CommonJS file using a Babel-
|
||||
// compatible transform (i.e. "__esModule" has not been set), then set
|
||||
// "default" to the CommonJS "module.exports" for node compatibility.
|
||||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
||||
mod
|
||||
));
|
||||
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_vue = require("vue");
|
||||
var import_use = require("@vant/use");
|
||||
var import_util = require("./util");
|
||||
var import_utils = require("../../utils");
|
||||
var import_listener = __toESM(require("./listener"));
|
||||
const DEFAULT_URL = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
|
||||
const DEFAULT_EVENTS = [
|
||||
"scroll",
|
||||
"wheel",
|
||||
"mousewheel",
|
||||
"resize",
|
||||
"animationend",
|
||||
"transitionend",
|
||||
"touchmove"
|
||||
];
|
||||
const DEFAULT_OBSERVER_OPTIONS = {
|
||||
rootMargin: "0px",
|
||||
threshold: 0
|
||||
};
|
||||
function stdin_default() {
|
||||
return class Lazy {
|
||||
constructor({
|
||||
preLoad,
|
||||
error,
|
||||
throttleWait,
|
||||
preLoadTop,
|
||||
dispatchEvent,
|
||||
loading,
|
||||
attempt,
|
||||
silent = true,
|
||||
scale,
|
||||
listenEvents,
|
||||
filter,
|
||||
adapter,
|
||||
observer,
|
||||
observerOptions
|
||||
}) {
|
||||
this.mode = import_util.modeType.event;
|
||||
this.listeners = [];
|
||||
this.targetIndex = 0;
|
||||
this.targets = [];
|
||||
this.options = {
|
||||
silent,
|
||||
dispatchEvent: !!dispatchEvent,
|
||||
throttleWait: throttleWait || 200,
|
||||
preLoad: preLoad || 1.3,
|
||||
preLoadTop: preLoadTop || 0,
|
||||
error: error || DEFAULT_URL,
|
||||
loading: loading || DEFAULT_URL,
|
||||
attempt: attempt || 3,
|
||||
scale: scale || (0, import_util.getDPR)(scale),
|
||||
ListenEvents: listenEvents || DEFAULT_EVENTS,
|
||||
supportWebp: (0, import_util.supportWebp)(),
|
||||
filter: filter || {},
|
||||
adapter: adapter || {},
|
||||
observer: !!observer,
|
||||
observerOptions: observerOptions || DEFAULT_OBSERVER_OPTIONS
|
||||
};
|
||||
this.initEvent();
|
||||
this.imageCache = new import_util.ImageCache({ max: 200 });
|
||||
this.lazyLoadHandler = (0, import_util.throttle)(
|
||||
this.lazyLoadHandler.bind(this),
|
||||
this.options.throttleWait
|
||||
);
|
||||
this.setMode(this.options.observer ? import_util.modeType.observer : import_util.modeType.event);
|
||||
}
|
||||
/**
|
||||
* update config
|
||||
* @param {Object} config params
|
||||
* @return
|
||||
*/
|
||||
config(options = {}) {
|
||||
Object.assign(this.options, options);
|
||||
}
|
||||
/**
|
||||
* output listener's load performance
|
||||
* @return {Array}
|
||||
*/
|
||||
performance() {
|
||||
return this.listeners.map((item) => item.performance());
|
||||
}
|
||||
/*
|
||||
* add lazy component to queue
|
||||
* @param {Vue} vm lazy component instance
|
||||
* @return
|
||||
*/
|
||||
addLazyBox(vm) {
|
||||
this.listeners.push(vm);
|
||||
if (import_use.inBrowser) {
|
||||
this.addListenerTarget(window);
|
||||
this.observer && this.observer.observe(vm.el);
|
||||
if (vm.$el && vm.$el.parentNode) {
|
||||
this.addListenerTarget(vm.$el.parentNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* add image listener to queue
|
||||
* @param {DOM} el
|
||||
* @param {object} binding vue directive binding
|
||||
* @param {vnode} vnode vue directive vnode
|
||||
* @return
|
||||
*/
|
||||
add(el, binding, vnode) {
|
||||
if (this.listeners.some((item) => item.el === el)) {
|
||||
this.update(el, binding);
|
||||
return (0, import_vue.nextTick)(this.lazyLoadHandler);
|
||||
}
|
||||
const value = this.valueFormatter(binding.value);
|
||||
let { src } = value;
|
||||
(0, import_vue.nextTick)(() => {
|
||||
src = (0, import_util.getBestSelectionFromSrcset)(el, this.options.scale) || src;
|
||||
this.observer && this.observer.observe(el);
|
||||
const container = Object.keys(binding.modifiers)[0];
|
||||
let $parent;
|
||||
if (container) {
|
||||
$parent = vnode.context.$refs[container];
|
||||
$parent = $parent ? $parent.$el || $parent : document.getElementById(container);
|
||||
}
|
||||
if (!$parent) {
|
||||
$parent = (0, import_use.getScrollParent)(el);
|
||||
}
|
||||
const newListener = new import_listener.default({
|
||||
bindType: binding.arg,
|
||||
$parent,
|
||||
el,
|
||||
src,
|
||||
loading: value.loading,
|
||||
error: value.error,
|
||||
cors: value.cors,
|
||||
elRenderer: this.elRenderer.bind(this),
|
||||
options: this.options,
|
||||
imageCache: this.imageCache
|
||||
});
|
||||
this.listeners.push(newListener);
|
||||
if (import_use.inBrowser) {
|
||||
this.addListenerTarget(window);
|
||||
this.addListenerTarget($parent);
|
||||
}
|
||||
this.lazyLoadHandler();
|
||||
(0, import_vue.nextTick)(() => this.lazyLoadHandler());
|
||||
});
|
||||
}
|
||||
/**
|
||||
* update image src
|
||||
* @param {DOM} el
|
||||
* @param {object} vue directive binding
|
||||
* @return
|
||||
*/
|
||||
update(el, binding, vnode) {
|
||||
const value = this.valueFormatter(binding.value);
|
||||
let { src } = value;
|
||||
src = (0, import_util.getBestSelectionFromSrcset)(el, this.options.scale) || src;
|
||||
const exist = this.listeners.find((item) => item.el === el);
|
||||
if (!exist) {
|
||||
this.add(el, binding, vnode);
|
||||
} else {
|
||||
exist.update({
|
||||
src,
|
||||
error: value.error,
|
||||
loading: value.loading
|
||||
});
|
||||
}
|
||||
if (this.observer) {
|
||||
this.observer.unobserve(el);
|
||||
this.observer.observe(el);
|
||||
}
|
||||
this.lazyLoadHandler();
|
||||
(0, import_vue.nextTick)(() => this.lazyLoadHandler());
|
||||
}
|
||||
/**
|
||||
* remove listener form list
|
||||
* @param {DOM} el
|
||||
* @return
|
||||
*/
|
||||
remove(el) {
|
||||
if (!el) return;
|
||||
this.observer && this.observer.unobserve(el);
|
||||
const existItem = this.listeners.find((item) => item.el === el);
|
||||
if (existItem) {
|
||||
this.removeListenerTarget(existItem.$parent);
|
||||
this.removeListenerTarget(window);
|
||||
(0, import_util.remove)(this.listeners, existItem);
|
||||
existItem.$destroy();
|
||||
}
|
||||
}
|
||||
/*
|
||||
* remove lazy components form list
|
||||
* @param {Vue} vm Vue instance
|
||||
* @return
|
||||
*/
|
||||
removeComponent(vm) {
|
||||
if (!vm) return;
|
||||
(0, import_util.remove)(this.listeners, vm);
|
||||
this.observer && this.observer.unobserve(vm.el);
|
||||
if (vm.$parent && vm.$el.parentNode) {
|
||||
this.removeListenerTarget(vm.$el.parentNode);
|
||||
}
|
||||
this.removeListenerTarget(window);
|
||||
}
|
||||
setMode(mode) {
|
||||
if (!import_util.hasIntersectionObserver && mode === import_util.modeType.observer) {
|
||||
mode = import_util.modeType.event;
|
||||
}
|
||||
this.mode = mode;
|
||||
if (mode === import_util.modeType.event) {
|
||||
if (this.observer) {
|
||||
this.listeners.forEach((listener) => {
|
||||
this.observer.unobserve(listener.el);
|
||||
});
|
||||
this.observer = null;
|
||||
}
|
||||
this.targets.forEach((target) => {
|
||||
this.initListen(target.el, true);
|
||||
});
|
||||
} else {
|
||||
this.targets.forEach((target) => {
|
||||
this.initListen(target.el, false);
|
||||
});
|
||||
this.initIntersectionObserver();
|
||||
}
|
||||
}
|
||||
/*
|
||||
*** Private functions ***
|
||||
*/
|
||||
/*
|
||||
* add listener target
|
||||
* @param {DOM} el listener target
|
||||
* @return
|
||||
*/
|
||||
addListenerTarget(el) {
|
||||
if (!el) return;
|
||||
let target = this.targets.find((target2) => target2.el === el);
|
||||
if (!target) {
|
||||
target = {
|
||||
el,
|
||||
id: ++this.targetIndex,
|
||||
childrenCount: 1,
|
||||
listened: true
|
||||
};
|
||||
this.mode === import_util.modeType.event && this.initListen(target.el, true);
|
||||
this.targets.push(target);
|
||||
} else {
|
||||
target.childrenCount++;
|
||||
}
|
||||
return this.targetIndex;
|
||||
}
|
||||
/*
|
||||
* remove listener target or reduce target childrenCount
|
||||
* @param {DOM} el or window
|
||||
* @return
|
||||
*/
|
||||
removeListenerTarget(el) {
|
||||
this.targets.forEach((target, index) => {
|
||||
if (target.el === el) {
|
||||
target.childrenCount--;
|
||||
if (!target.childrenCount) {
|
||||
this.initListen(target.el, false);
|
||||
this.targets.splice(index, 1);
|
||||
target = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
/*
|
||||
* add or remove eventlistener
|
||||
* @param {DOM} el DOM or Window
|
||||
* @param {boolean} start flag
|
||||
* @return
|
||||
*/
|
||||
initListen(el, start) {
|
||||
this.options.ListenEvents.forEach(
|
||||
(evt) => (start ? import_util.on : import_util.off)(el, evt, this.lazyLoadHandler)
|
||||
);
|
||||
}
|
||||
initEvent() {
|
||||
this.Event = {
|
||||
listeners: {
|
||||
loading: [],
|
||||
loaded: [],
|
||||
error: []
|
||||
}
|
||||
};
|
||||
this.$on = (event, func) => {
|
||||
if (!this.Event.listeners[event]) this.Event.listeners[event] = [];
|
||||
this.Event.listeners[event].push(func);
|
||||
};
|
||||
this.$once = (event, func) => {
|
||||
const on2 = (...args) => {
|
||||
this.$off(event, on2);
|
||||
func.apply(this, args);
|
||||
};
|
||||
this.$on(event, on2);
|
||||
};
|
||||
this.$off = (event, func) => {
|
||||
if (!func) {
|
||||
if (!this.Event.listeners[event]) return;
|
||||
this.Event.listeners[event].length = 0;
|
||||
return;
|
||||
}
|
||||
(0, import_util.remove)(this.Event.listeners[event], func);
|
||||
};
|
||||
this.$emit = (event, context, inCache) => {
|
||||
if (!this.Event.listeners[event]) return;
|
||||
this.Event.listeners[event].forEach((func) => func(context, inCache));
|
||||
};
|
||||
}
|
||||
/**
|
||||
* find nodes which in viewport and trigger load
|
||||
* @return
|
||||
*/
|
||||
lazyLoadHandler() {
|
||||
const freeList = [];
|
||||
this.listeners.forEach((listener) => {
|
||||
if (!listener.el || !listener.el.parentNode) {
|
||||
freeList.push(listener);
|
||||
}
|
||||
const catIn = listener.checkInView();
|
||||
if (!catIn) return;
|
||||
listener.load();
|
||||
});
|
||||
freeList.forEach((item) => {
|
||||
(0, import_util.remove)(this.listeners, item);
|
||||
item.$destroy();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* init IntersectionObserver
|
||||
* set mode to observer
|
||||
* @return
|
||||
*/
|
||||
initIntersectionObserver() {
|
||||
if (!import_util.hasIntersectionObserver) {
|
||||
return;
|
||||
}
|
||||
this.observer = new IntersectionObserver(
|
||||
this.observerHandler.bind(this),
|
||||
this.options.observerOptions
|
||||
);
|
||||
if (this.listeners.length) {
|
||||
this.listeners.forEach((listener) => {
|
||||
this.observer.observe(listener.el);
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* init IntersectionObserver
|
||||
* @return
|
||||
*/
|
||||
observerHandler(entries) {
|
||||
entries.forEach((entry) => {
|
||||
if (entry.isIntersecting) {
|
||||
this.listeners.forEach((listener) => {
|
||||
if (listener.el === entry.target) {
|
||||
if (listener.state.loaded)
|
||||
return this.observer.unobserve(listener.el);
|
||||
listener.load();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* set element attribute with image'url and state
|
||||
* @param {object} lazyload listener object
|
||||
* @param {string} state will be rendered
|
||||
* @param {bool} inCache is rendered from cache
|
||||
* @return
|
||||
*/
|
||||
elRenderer(listener, state, cache) {
|
||||
if (!listener.el) return;
|
||||
const { el, bindType } = listener;
|
||||
let src;
|
||||
switch (state) {
|
||||
case "loading":
|
||||
src = listener.loading;
|
||||
break;
|
||||
case "error":
|
||||
src = listener.error;
|
||||
break;
|
||||
default:
|
||||
({ src } = listener);
|
||||
break;
|
||||
}
|
||||
if (bindType) {
|
||||
el.style[bindType] = 'url("' + src + '")';
|
||||
} else if (el.getAttribute("src") !== src) {
|
||||
el.setAttribute("src", src);
|
||||
}
|
||||
el.setAttribute("lazy", state);
|
||||
this.$emit(state, listener, cache);
|
||||
this.options.adapter[state] && this.options.adapter[state](listener, this.options);
|
||||
if (this.options.dispatchEvent) {
|
||||
const event = new CustomEvent(state, {
|
||||
detail: listener
|
||||
});
|
||||
el.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* generate loading loaded error image url
|
||||
* @param {string} image's src
|
||||
* @return {object} image's loading, loaded, error url
|
||||
*/
|
||||
valueFormatter(value) {
|
||||
let src = value;
|
||||
let { loading, error } = this.options;
|
||||
if ((0, import_utils.isObject)(value)) {
|
||||
if (process.env.NODE_ENV !== "production" && !value.src && !this.options.silent) {
|
||||
console.error("[@vant/lazyload] miss src with " + value);
|
||||
}
|
||||
({ src } = value);
|
||||
loading = value.loading || this.options.loading;
|
||||
error = value.error || this.options.error;
|
||||
}
|
||||
return {
|
||||
src,
|
||||
loading,
|
||||
error
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
235
node_modules/vant/lib/lazyload/vue-lazyload/listener.js
generated
vendored
Normal file
235
node_modules/vant/lib/lazyload/vue-lazyload/listener.js
generated
vendored
Normal file
@@ -0,0 +1,235 @@
|
||||
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: () => ReactiveListener
|
||||
});
|
||||
module.exports = __toCommonJS(stdin_exports);
|
||||
var import_use = require("@vant/use");
|
||||
var import_util = require("./util");
|
||||
var import_utils = require("../../utils");
|
||||
class ReactiveListener {
|
||||
constructor({
|
||||
el,
|
||||
src,
|
||||
error,
|
||||
loading,
|
||||
bindType,
|
||||
$parent,
|
||||
options,
|
||||
cors,
|
||||
elRenderer,
|
||||
imageCache
|
||||
}) {
|
||||
this.el = el;
|
||||
this.src = src;
|
||||
this.error = error;
|
||||
this.loading = loading;
|
||||
this.bindType = bindType;
|
||||
this.attempt = 0;
|
||||
this.cors = cors;
|
||||
this.naturalHeight = 0;
|
||||
this.naturalWidth = 0;
|
||||
this.options = options;
|
||||
this.$parent = $parent;
|
||||
this.elRenderer = elRenderer;
|
||||
this.imageCache = imageCache;
|
||||
this.performanceData = {
|
||||
loadStart: 0,
|
||||
loadEnd: 0
|
||||
};
|
||||
this.filter();
|
||||
this.initState();
|
||||
this.render("loading", false);
|
||||
}
|
||||
/*
|
||||
* init listener state
|
||||
* @return
|
||||
*/
|
||||
initState() {
|
||||
if ("dataset" in this.el) {
|
||||
this.el.dataset.src = this.src;
|
||||
} else {
|
||||
this.el.setAttribute("data-src", this.src);
|
||||
}
|
||||
this.state = {
|
||||
loading: false,
|
||||
error: false,
|
||||
loaded: false,
|
||||
rendered: false
|
||||
};
|
||||
}
|
||||
/*
|
||||
* record performance
|
||||
* @return
|
||||
*/
|
||||
record(event) {
|
||||
this.performanceData[event] = Date.now();
|
||||
}
|
||||
/*
|
||||
* update image listener data
|
||||
* @param {String} image uri
|
||||
* @param {String} loading image uri
|
||||
* @param {String} error image uri
|
||||
* @return
|
||||
*/
|
||||
update({ src, loading, error }) {
|
||||
const oldSrc = this.src;
|
||||
this.src = src;
|
||||
this.loading = loading;
|
||||
this.error = error;
|
||||
this.filter();
|
||||
if (oldSrc !== this.src) {
|
||||
this.attempt = 0;
|
||||
this.initState();
|
||||
}
|
||||
}
|
||||
/*
|
||||
* check el is in view
|
||||
* @return {Boolean} el is in view
|
||||
*/
|
||||
checkInView() {
|
||||
const rect = (0, import_use.useRect)(this.el);
|
||||
return rect.top < window.innerHeight * this.options.preLoad && rect.bottom > this.options.preLoadTop && rect.left < window.innerWidth * this.options.preLoad && rect.right > 0;
|
||||
}
|
||||
/*
|
||||
* listener filter
|
||||
*/
|
||||
filter() {
|
||||
Object.keys(this.options.filter).forEach((key) => {
|
||||
this.options.filter[key](this, this.options);
|
||||
});
|
||||
}
|
||||
/*
|
||||
* render loading first
|
||||
* @params cb:Function
|
||||
* @return
|
||||
*/
|
||||
renderLoading(cb) {
|
||||
this.state.loading = true;
|
||||
(0, import_util.loadImageAsync)(
|
||||
{
|
||||
src: this.loading,
|
||||
cors: this.cors
|
||||
},
|
||||
() => {
|
||||
this.render("loading", false);
|
||||
this.state.loading = false;
|
||||
cb();
|
||||
},
|
||||
() => {
|
||||
cb();
|
||||
this.state.loading = false;
|
||||
if (process.env.NODE_ENV !== "production" && !this.options.silent)
|
||||
console.warn(
|
||||
`[@vant/lazyload] load failed with loading image(${this.loading})`
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
/*
|
||||
* try load image and render it
|
||||
* @return
|
||||
*/
|
||||
load(onFinish = import_utils.noop) {
|
||||
if (this.attempt > this.options.attempt - 1 && this.state.error) {
|
||||
if (process.env.NODE_ENV !== "production" && !this.options.silent) {
|
||||
console.log(
|
||||
`[@vant/lazyload] ${this.src} tried too more than ${this.options.attempt} times`
|
||||
);
|
||||
}
|
||||
onFinish();
|
||||
return;
|
||||
}
|
||||
if (this.state.rendered && this.state.loaded) return;
|
||||
if (this.imageCache.has(this.src)) {
|
||||
this.state.loaded = true;
|
||||
this.render("loaded", true);
|
||||
this.state.rendered = true;
|
||||
return onFinish();
|
||||
}
|
||||
this.renderLoading(() => {
|
||||
var _a, _b;
|
||||
this.attempt++;
|
||||
(_b = (_a = this.options.adapter).beforeLoad) == null ? void 0 : _b.call(_a, this, this.options);
|
||||
this.record("loadStart");
|
||||
(0, import_util.loadImageAsync)(
|
||||
{
|
||||
src: this.src,
|
||||
cors: this.cors
|
||||
},
|
||||
(data) => {
|
||||
this.naturalHeight = data.naturalHeight;
|
||||
this.naturalWidth = data.naturalWidth;
|
||||
this.state.loaded = true;
|
||||
this.state.error = false;
|
||||
this.record("loadEnd");
|
||||
this.render("loaded", false);
|
||||
this.state.rendered = true;
|
||||
this.imageCache.add(this.src);
|
||||
onFinish();
|
||||
},
|
||||
(err) => {
|
||||
!this.options.silent && console.error(err);
|
||||
this.state.error = true;
|
||||
this.state.loaded = false;
|
||||
this.render("error", false);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
/*
|
||||
* render image
|
||||
* @param {String} state to render // ['loading', 'src', 'error']
|
||||
* @param {String} is form cache
|
||||
* @return
|
||||
*/
|
||||
render(state, cache) {
|
||||
this.elRenderer(this, state, cache);
|
||||
}
|
||||
/*
|
||||
* output performance data
|
||||
* @return {Object} performance data
|
||||
*/
|
||||
performance() {
|
||||
let state = "loading";
|
||||
let time = 0;
|
||||
if (this.state.loaded) {
|
||||
state = "loaded";
|
||||
time = (this.performanceData.loadEnd - this.performanceData.loadStart) / 1e3;
|
||||
}
|
||||
if (this.state.error) state = "error";
|
||||
return {
|
||||
src: this.src,
|
||||
state,
|
||||
time
|
||||
};
|
||||
}
|
||||
/*
|
||||
* $destroy
|
||||
* @return
|
||||
*/
|
||||
$destroy() {
|
||||
this.el = null;
|
||||
this.src = null;
|
||||
this.error = null;
|
||||
this.loading = null;
|
||||
this.bindType = null;
|
||||
this.attempt = 0;
|
||||
}
|
||||
}
|
||||
25
node_modules/vant/lib/lazyload/vue-lazyload/util.d.ts
generated
vendored
Normal file
25
node_modules/vant/lib/lazyload/vue-lazyload/util.d.ts
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
export function remove(arr: any, item: any): any;
|
||||
export function getBestSelectionFromSrcset(el: any, scale: any): string | undefined;
|
||||
export function supportWebp(): boolean;
|
||||
export function throttle(action: any, delay: any): (...args: any[]) => void;
|
||||
export function on(el: any, type: any, func: any): void;
|
||||
export function off(el: any, type: any, func: any): void;
|
||||
export const hasIntersectionObserver: boolean;
|
||||
export namespace modeType {
|
||||
let event: string;
|
||||
let observer: string;
|
||||
}
|
||||
export function getDPR(scale?: number): number;
|
||||
export function loadImageAsync(item: any, resolve: any, reject: any): any;
|
||||
export class ImageCache {
|
||||
constructor({ max }: {
|
||||
max: any;
|
||||
});
|
||||
options: {
|
||||
max: any;
|
||||
};
|
||||
caches: any[];
|
||||
has(key: any): boolean;
|
||||
add(key: any): void;
|
||||
free(): void;
|
||||
}
|
||||
180
node_modules/vant/lib/lazyload/vue-lazyload/util.js
generated
vendored
Normal file
180
node_modules/vant/lib/lazyload/vue-lazyload/util.js
generated
vendored
Normal file
@@ -0,0 +1,180 @@
|
||||
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, {
|
||||
ImageCache: () => ImageCache,
|
||||
getBestSelectionFromSrcset: () => getBestSelectionFromSrcset,
|
||||
getDPR: () => getDPR,
|
||||
hasIntersectionObserver: () => hasIntersectionObserver,
|
||||
loadImageAsync: () => loadImageAsync,
|
||||
modeType: () => modeType,
|
||||
off: () => off,
|
||||
on: () => on,
|
||||
remove: () => remove,
|
||||
supportWebp: () => supportWebp,
|
||||
throttle: () => throttle
|
||||
});
|
||||
module.exports = __toCommonJS(stdin_exports);
|
||||
var import_use = require("@vant/use");
|
||||
const hasIntersectionObserver = import_use.inBrowser && "IntersectionObserver" in window && "IntersectionObserverEntry" in window && "intersectionRatio" in window.IntersectionObserverEntry.prototype;
|
||||
const modeType = {
|
||||
event: "event",
|
||||
observer: "observer"
|
||||
};
|
||||
function remove(arr, item) {
|
||||
if (!arr.length) return;
|
||||
const index = arr.indexOf(item);
|
||||
if (index > -1) return arr.splice(index, 1);
|
||||
}
|
||||
function getBestSelectionFromSrcset(el, scale) {
|
||||
if (el.tagName !== "IMG" || !el.getAttribute("data-srcset")) return;
|
||||
let options = el.getAttribute("data-srcset");
|
||||
const container = el.parentNode;
|
||||
const containerWidth = container.offsetWidth * scale;
|
||||
let spaceIndex;
|
||||
let tmpSrc;
|
||||
let tmpWidth;
|
||||
options = options.trim().split(",");
|
||||
const result = options.map((item) => {
|
||||
item = item.trim();
|
||||
spaceIndex = item.lastIndexOf(" ");
|
||||
if (spaceIndex === -1) {
|
||||
tmpSrc = item;
|
||||
tmpWidth = 999998;
|
||||
} else {
|
||||
tmpSrc = item.substr(0, spaceIndex);
|
||||
tmpWidth = parseInt(
|
||||
item.substr(spaceIndex + 1, item.length - spaceIndex - 2),
|
||||
10
|
||||
);
|
||||
}
|
||||
return [tmpWidth, tmpSrc];
|
||||
});
|
||||
result.sort((a, b) => {
|
||||
if (a[0] < b[0]) {
|
||||
return 1;
|
||||
}
|
||||
if (a[0] > b[0]) {
|
||||
return -1;
|
||||
}
|
||||
if (a[0] === b[0]) {
|
||||
if (b[1].indexOf(".webp", b[1].length - 5) !== -1) {
|
||||
return 1;
|
||||
}
|
||||
if (a[1].indexOf(".webp", a[1].length - 5) !== -1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
let bestSelectedSrc = "";
|
||||
let tmpOption;
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
tmpOption = result[i];
|
||||
bestSelectedSrc = tmpOption[1];
|
||||
const next = result[i + 1];
|
||||
if (next && next[0] < containerWidth) {
|
||||
bestSelectedSrc = tmpOption[1];
|
||||
break;
|
||||
} else if (!next) {
|
||||
bestSelectedSrc = tmpOption[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bestSelectedSrc;
|
||||
}
|
||||
const getDPR = (scale = 1) => import_use.inBrowser ? window.devicePixelRatio || scale : scale;
|
||||
function supportWebp() {
|
||||
if (!import_use.inBrowser) return false;
|
||||
let support = true;
|
||||
try {
|
||||
const elem = document.createElement("canvas");
|
||||
if (elem.getContext && elem.getContext("2d")) {
|
||||
support = elem.toDataURL("image/webp").indexOf("data:image/webp") === 0;
|
||||
}
|
||||
} catch (err) {
|
||||
support = false;
|
||||
}
|
||||
return support;
|
||||
}
|
||||
function throttle(action, delay) {
|
||||
let timeout = null;
|
||||
let lastRun = 0;
|
||||
return function(...args) {
|
||||
if (timeout) {
|
||||
return;
|
||||
}
|
||||
const elapsed = Date.now() - lastRun;
|
||||
const runCallback = () => {
|
||||
lastRun = Date.now();
|
||||
timeout = false;
|
||||
action.apply(this, args);
|
||||
};
|
||||
if (elapsed >= delay) {
|
||||
runCallback();
|
||||
} else {
|
||||
timeout = setTimeout(runCallback, delay);
|
||||
}
|
||||
};
|
||||
}
|
||||
function on(el, type, func) {
|
||||
el.addEventListener(type, func, {
|
||||
capture: false,
|
||||
passive: true
|
||||
});
|
||||
}
|
||||
function off(el, type, func) {
|
||||
el.removeEventListener(type, func, false);
|
||||
}
|
||||
const loadImageAsync = (item, resolve, reject) => {
|
||||
const image = new Image();
|
||||
if (!item || !item.src) {
|
||||
return reject(new Error("image src is required"));
|
||||
}
|
||||
image.src = item.src;
|
||||
if (item.cors) {
|
||||
image.crossOrigin = item.cors;
|
||||
}
|
||||
image.onload = () => resolve({
|
||||
naturalHeight: image.naturalHeight,
|
||||
naturalWidth: image.naturalWidth,
|
||||
src: image.src
|
||||
});
|
||||
image.onerror = (e) => reject(e);
|
||||
};
|
||||
class ImageCache {
|
||||
constructor({ max }) {
|
||||
this.options = {
|
||||
max: max || 100
|
||||
};
|
||||
this.caches = [];
|
||||
}
|
||||
has(key) {
|
||||
return this.caches.indexOf(key) > -1;
|
||||
}
|
||||
add(key) {
|
||||
if (this.has(key)) return;
|
||||
this.caches.push(key);
|
||||
if (this.caches.length > this.options.max) {
|
||||
this.free();
|
||||
}
|
||||
}
|
||||
free() {
|
||||
this.caches.shift();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user