vant 3.2.8 → 3.3.3-beta.nuxt3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/address-edit/AddressEdit.js +154 -180
- package/es/address-edit/AddressEditDetail.d.ts +10 -9
- package/es/address-edit/AddressEditDetail.js +6 -6
- package/es/button/Button.js +2 -2
- package/es/calendar/Calendar.js +8 -14
- package/es/calendar/CalendarHeader.js +1 -3
- package/es/calendar/CalendarMonth.js +11 -10
- package/es/calendar/types.d.ts +1 -1
- package/es/cascader/Cascader.js +10 -2
- package/es/checkbox/Checker.js +1 -1
- package/es/checkbox/types.d.ts +2 -6
- package/es/composables/use-id.d.ts +1 -0
- package/es/composables/use-id.js +14 -0
- package/es/composables/use-route.d.ts +1 -1
- package/es/composables/use-route.js +4 -4
- package/es/count-down/CountDown.js +1 -0
- package/es/coupon-list/CouponList.js +40 -24
- package/es/coupon-list/index.css +1 -1
- package/es/coupon-list/index.less +0 -5
- package/es/dropdown-item/DropdownItem.js +7 -2
- package/es/dropdown-item/types.d.ts +2 -6
- package/es/dropdown-menu/DropdownMenu.js +7 -3
- package/es/dropdown-menu/types.d.ts +1 -0
- package/es/empty/Network.js +22 -34
- package/es/field/Field.d.ts +1 -1
- package/es/field/Field.js +13 -5
- package/es/field/index.d.ts +2 -2
- package/es/field/types.d.ts +7 -6
- package/es/form/Form.js +2 -2
- package/es/image/Image.js +4 -9
- package/es/image-preview/ImagePreview.js +3 -4
- package/es/index.d.ts +1 -1
- package/es/index.js +1 -1
- package/es/lazyload/index.d.ts +1 -1
- package/es/lazyload/index.js +1 -1
- package/es/lazyload/vue-lazyload/index.d.ts +55 -0
- package/es/lazyload/vue-lazyload/index.js +46 -0
- package/es/lazyload/vue-lazyload/lazy-component.js +53 -0
- package/es/lazyload/vue-lazyload/lazy-container.js +98 -0
- package/es/lazyload/vue-lazyload/lazy-image.js +115 -0
- package/es/lazyload/vue-lazyload/lazy.js +507 -0
- package/es/lazyload/vue-lazyload/listener.js +241 -0
- package/es/lazyload/vue-lazyload/util.js +183 -0
- package/es/list/List.js +3 -1
- package/es/locale/lang/pt-BR.d.ts +73 -0
- package/es/locale/lang/pt-BR.js +72 -0
- package/es/number-keyboard/NumberKeyboardKey.js +4 -4
- package/es/pagination/Pagination.js +67 -49
- package/es/pagination/index.css +1 -1
- package/es/pagination/index.less +32 -31
- package/es/password-input/PasswordInput.d.ts +1 -1
- package/es/password-input/index.d.ts +1 -1
- package/es/popover/Popover.js +2 -0
- package/es/popup/Popup.d.ts +1 -2
- package/es/popup/Popup.js +2 -5
- package/es/popup/index.d.ts +1 -2
- package/es/rate/Rate.js +5 -3
- package/es/search/Search.js +8 -2
- package/es/sidebar/Sidebar.js +1 -0
- package/es/sidebar-item/SidebarItem.js +4 -1
- package/es/slider/Slider.js +5 -3
- package/es/stepper/Stepper.js +11 -8
- package/es/swipe/Swipe.js +11 -6
- package/es/swipe/index.css +1 -1
- package/es/swipe/index.less +2 -0
- package/es/swipe/types.d.ts +1 -3
- package/es/switch/Switch.js +1 -0
- package/es/switch/index.css +1 -1
- package/es/switch/index.less +2 -0
- package/es/tab/Tab.js +18 -5
- package/es/tab/index.css +1 -1
- package/es/tab/index.less +1 -1
- package/es/tabbar/Tabbar.d.ts +1 -1
- package/es/tabbar/Tabbar.js +10 -10
- package/es/tabbar-item/TabbarItem.js +16 -7
- package/es/tabs/Tabs.js +10 -5
- package/es/tabs/TabsTitle.d.ts +6 -0
- package/es/tabs/TabsTitle.js +7 -1
- package/es/tabs/types.d.ts +1 -0
- package/es/uploader/UploaderPreviewItem.js +4 -1
- package/es/uploader/utils.d.ts +2 -2
- package/es/uploader/utils.js +2 -2
- package/es/utils/dom.d.ts +1 -0
- package/es/utils/dom.js +6 -2
- package/es/utils/format.js +6 -5
- package/es/vue-tsx-shim.d.ts +1 -0
- package/lib/address-edit/AddressEdit.js +155 -180
- package/lib/address-edit/AddressEditDetail.d.ts +10 -9
- package/lib/address-edit/AddressEditDetail.js +6 -6
- package/lib/button/Button.js +1 -1
- package/lib/calendar/Calendar.js +8 -14
- package/lib/calendar/CalendarHeader.js +1 -3
- package/lib/calendar/CalendarMonth.js +11 -10
- package/lib/calendar/types.d.ts +1 -1
- package/lib/cascader/Cascader.js +10 -2
- package/lib/checkbox/Checker.js +1 -1
- package/lib/checkbox/types.d.ts +2 -6
- package/lib/composables/use-id.d.ts +1 -0
- package/lib/composables/use-id.js +21 -0
- package/lib/composables/use-route.d.ts +1 -1
- package/lib/composables/use-route.js +4 -4
- package/lib/count-down/CountDown.js +1 -0
- package/lib/coupon-list/CouponList.js +39 -22
- package/lib/coupon-list/index.css +1 -1
- package/lib/coupon-list/index.less +0 -5
- package/lib/dropdown-item/DropdownItem.js +7 -2
- package/lib/dropdown-item/types.d.ts +2 -6
- package/lib/dropdown-menu/DropdownMenu.js +7 -2
- package/lib/dropdown-menu/types.d.ts +1 -0
- package/lib/empty/Network.js +22 -34
- package/lib/field/Field.d.ts +1 -1
- package/lib/field/Field.js +14 -5
- package/lib/field/index.d.ts +2 -2
- package/lib/field/types.d.ts +7 -6
- package/lib/form/Form.js +1 -1
- package/lib/image/Image.js +4 -9
- package/lib/image-preview/ImagePreview.js +1 -2
- package/lib/index.css +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/lazyload/index.d.ts +1 -1
- package/lib/lazyload/index.js +3 -3
- package/lib/lazyload/vue-lazyload/index.d.ts +55 -0
- package/lib/lazyload/vue-lazyload/index.js +58 -0
- package/lib/lazyload/vue-lazyload/lazy-component.js +62 -0
- package/lib/lazyload/vue-lazyload/lazy-container.js +106 -0
- package/lib/lazyload/vue-lazyload/lazy-image.js +125 -0
- package/lib/lazyload/vue-lazyload/lazy.js +520 -0
- package/lib/lazyload/vue-lazyload/listener.js +251 -0
- package/lib/lazyload/vue-lazyload/util.js +213 -0
- package/lib/list/List.js +3 -1
- package/lib/locale/lang/pt-BR.d.ts +73 -0
- package/lib/locale/lang/pt-BR.js +77 -0
- package/lib/number-keyboard/NumberKeyboardKey.js +3 -3
- package/lib/pagination/Pagination.js +65 -47
- package/lib/pagination/index.css +1 -1
- package/lib/pagination/index.less +32 -31
- package/lib/password-input/PasswordInput.d.ts +1 -1
- package/lib/password-input/index.d.ts +1 -1
- package/lib/popover/Popover.js +2 -0
- package/lib/popup/Popup.d.ts +1 -2
- package/lib/popup/Popup.js +2 -5
- package/lib/popup/index.d.ts +1 -2
- package/lib/rate/Rate.js +5 -3
- package/lib/search/Search.js +9 -2
- package/lib/sidebar/Sidebar.js +1 -0
- package/lib/sidebar-item/SidebarItem.js +4 -1
- package/lib/slider/Slider.js +5 -3
- package/lib/ssr.js +7 -0
- package/lib/ssr.mjs +1 -0
- package/lib/stepper/Stepper.js +11 -8
- package/lib/swipe/Swipe.js +9 -4
- package/lib/swipe/index.css +1 -1
- package/lib/swipe/index.less +2 -0
- package/lib/swipe/types.d.ts +1 -3
- package/lib/switch/Switch.js +1 -0
- package/lib/switch/index.css +1 -1
- package/lib/switch/index.less +2 -0
- package/lib/tab/Tab.js +20 -5
- package/lib/tab/index.css +1 -1
- package/lib/tab/index.less +1 -1
- package/lib/tabbar/Tabbar.d.ts +1 -1
- package/lib/tabbar/Tabbar.js +10 -10
- package/lib/tabbar-item/TabbarItem.js +16 -7
- package/lib/tabs/Tabs.js +9 -3
- package/lib/tabs/TabsTitle.d.ts +6 -0
- package/lib/tabs/TabsTitle.js +7 -1
- package/lib/tabs/types.d.ts +1 -0
- package/lib/uploader/UploaderPreviewItem.js +3 -0
- package/lib/uploader/utils.d.ts +2 -2
- package/lib/uploader/utils.js +3 -2
- package/lib/utils/dom.d.ts +1 -0
- package/lib/utils/dom.js +9 -2
- package/lib/utils/format.js +7 -5
- package/lib/vant.cjs.js +14407 -0
- package/lib/vant.cjs.min.js +1 -0
- package/lib/vant.es.js +1125 -2186
- package/lib/vant.es.min.js +1125 -2186
- package/lib/vant.js +1109 -1089
- package/lib/vant.min.js +1 -1
- package/lib/vue-tsx-shim.d.ts +1 -0
- package/package.json +26 -11
- package/vetur/attributes.json +557 -557
- package/vetur/tags.json +183 -183
- package/vetur/web-types.json +1577 -1577
- package/es/composables/use-link-field.d.ts +0 -0
- package/es/composables/use-link-field.js +0 -0
- package/lib/composables/use-link-field.d.ts +0 -0
- package/lib/composables/use-link-field.js +0 -1
@@ -0,0 +1,507 @@
|
|
1
|
+
import { nextTick } from 'vue';
|
2
|
+
import { inBrowser, getScrollParent } from '@vant/use';
|
3
|
+
import { remove, on, off, throttle, supportWebp, getDPR, getBestSelectionFromSrcset, hasIntersectionObserver, modeType, ImageCache } from './util';
|
4
|
+
import { isObject } from '../../utils';
|
5
|
+
import ReactiveListener from './listener';
|
6
|
+
var DEFAULT_URL = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
|
7
|
+
var DEFAULT_EVENTS = ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove'];
|
8
|
+
var DEFAULT_OBSERVER_OPTIONS = {
|
9
|
+
rootMargin: '0px',
|
10
|
+
threshold: 0
|
11
|
+
};
|
12
|
+
export default function () {
|
13
|
+
return class Lazy {
|
14
|
+
constructor(_ref) {
|
15
|
+
var {
|
16
|
+
preLoad,
|
17
|
+
error,
|
18
|
+
throttleWait,
|
19
|
+
preLoadTop,
|
20
|
+
dispatchEvent,
|
21
|
+
loading,
|
22
|
+
attempt,
|
23
|
+
silent = true,
|
24
|
+
scale,
|
25
|
+
listenEvents,
|
26
|
+
filter,
|
27
|
+
adapter,
|
28
|
+
observer,
|
29
|
+
observerOptions
|
30
|
+
} = _ref;
|
31
|
+
this.mode = modeType.event;
|
32
|
+
this.listeners = [];
|
33
|
+
this.targetIndex = 0;
|
34
|
+
this.targets = [];
|
35
|
+
this.options = {
|
36
|
+
silent,
|
37
|
+
dispatchEvent: !!dispatchEvent,
|
38
|
+
throttleWait: throttleWait || 200,
|
39
|
+
preLoad: preLoad || 1.3,
|
40
|
+
preLoadTop: preLoadTop || 0,
|
41
|
+
error: error || DEFAULT_URL,
|
42
|
+
loading: loading || DEFAULT_URL,
|
43
|
+
attempt: attempt || 3,
|
44
|
+
scale: scale || getDPR(scale),
|
45
|
+
ListenEvents: listenEvents || DEFAULT_EVENTS,
|
46
|
+
supportWebp: supportWebp(),
|
47
|
+
filter: filter || {},
|
48
|
+
adapter: adapter || {},
|
49
|
+
observer: !!observer,
|
50
|
+
observerOptions: observerOptions || DEFAULT_OBSERVER_OPTIONS
|
51
|
+
};
|
52
|
+
this.initEvent();
|
53
|
+
this.imageCache = new ImageCache({
|
54
|
+
max: 200
|
55
|
+
});
|
56
|
+
this.lazyLoadHandler = throttle(this.lazyLoadHandler.bind(this), this.options.throttleWait);
|
57
|
+
this.setMode(this.options.observer ? modeType.observer : modeType.event);
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* update config
|
61
|
+
* @param {Object} config params
|
62
|
+
* @return
|
63
|
+
*/
|
64
|
+
|
65
|
+
|
66
|
+
config(options) {
|
67
|
+
if (options === void 0) {
|
68
|
+
options = {};
|
69
|
+
}
|
70
|
+
|
71
|
+
Object.assign(this.options, options);
|
72
|
+
}
|
73
|
+
/**
|
74
|
+
* output listener's load performance
|
75
|
+
* @return {Array}
|
76
|
+
*/
|
77
|
+
|
78
|
+
|
79
|
+
performance() {
|
80
|
+
return this.listeners.map(item => item.performance());
|
81
|
+
}
|
82
|
+
/*
|
83
|
+
* add lazy component to queue
|
84
|
+
* @param {Vue} vm lazy component instance
|
85
|
+
* @return
|
86
|
+
*/
|
87
|
+
|
88
|
+
|
89
|
+
addLazyBox(vm) {
|
90
|
+
this.listeners.push(vm);
|
91
|
+
|
92
|
+
if (inBrowser) {
|
93
|
+
this.addListenerTarget(window);
|
94
|
+
this.observer && this.observer.observe(vm.el);
|
95
|
+
|
96
|
+
if (vm.$el && vm.$el.parentNode) {
|
97
|
+
this.addListenerTarget(vm.$el.parentNode);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
101
|
+
/*
|
102
|
+
* add image listener to queue
|
103
|
+
* @param {DOM} el
|
104
|
+
* @param {object} binding vue directive binding
|
105
|
+
* @param {vnode} vnode vue directive vnode
|
106
|
+
* @return
|
107
|
+
*/
|
108
|
+
|
109
|
+
|
110
|
+
add(el, binding, vnode) {
|
111
|
+
if (this.listeners.some(item => item.el === el)) {
|
112
|
+
this.update(el, binding);
|
113
|
+
return nextTick(this.lazyLoadHandler);
|
114
|
+
}
|
115
|
+
|
116
|
+
var value = this.valueFormatter(binding.value);
|
117
|
+
var {
|
118
|
+
src
|
119
|
+
} = value;
|
120
|
+
nextTick(() => {
|
121
|
+
src = getBestSelectionFromSrcset(el, this.options.scale) || src;
|
122
|
+
this.observer && this.observer.observe(el);
|
123
|
+
var container = Object.keys(binding.modifiers)[0];
|
124
|
+
var $parent;
|
125
|
+
|
126
|
+
if (container) {
|
127
|
+
$parent = vnode.context.$refs[container]; // if there is container passed in, try ref first, then fallback to getElementById to support the original usage
|
128
|
+
|
129
|
+
$parent = $parent ? $parent.$el || $parent : document.getElementById(container);
|
130
|
+
}
|
131
|
+
|
132
|
+
if (!$parent) {
|
133
|
+
$parent = getScrollParent(el);
|
134
|
+
}
|
135
|
+
|
136
|
+
var newListener = new ReactiveListener({
|
137
|
+
bindType: binding.arg,
|
138
|
+
$parent,
|
139
|
+
el,
|
140
|
+
src,
|
141
|
+
loading: value.loading,
|
142
|
+
error: value.error,
|
143
|
+
cors: value.cors,
|
144
|
+
elRenderer: this.elRenderer.bind(this),
|
145
|
+
options: this.options,
|
146
|
+
imageCache: this.imageCache
|
147
|
+
});
|
148
|
+
this.listeners.push(newListener);
|
149
|
+
|
150
|
+
if (inBrowser) {
|
151
|
+
this.addListenerTarget(window);
|
152
|
+
this.addListenerTarget($parent);
|
153
|
+
}
|
154
|
+
|
155
|
+
this.lazyLoadHandler();
|
156
|
+
nextTick(() => this.lazyLoadHandler());
|
157
|
+
});
|
158
|
+
}
|
159
|
+
/**
|
160
|
+
* update image src
|
161
|
+
* @param {DOM} el
|
162
|
+
* @param {object} vue directive binding
|
163
|
+
* @return
|
164
|
+
*/
|
165
|
+
|
166
|
+
|
167
|
+
update(el, binding, vnode) {
|
168
|
+
var value = this.valueFormatter(binding.value);
|
169
|
+
var {
|
170
|
+
src
|
171
|
+
} = value;
|
172
|
+
src = getBestSelectionFromSrcset(el, this.options.scale) || src;
|
173
|
+
var exist = this.listeners.find(item => item.el === el);
|
174
|
+
|
175
|
+
if (!exist) {
|
176
|
+
this.add(el, binding, vnode);
|
177
|
+
} else {
|
178
|
+
exist.update({
|
179
|
+
src,
|
180
|
+
error: value.error,
|
181
|
+
loading: value.loading
|
182
|
+
});
|
183
|
+
}
|
184
|
+
|
185
|
+
if (this.observer) {
|
186
|
+
this.observer.unobserve(el);
|
187
|
+
this.observer.observe(el);
|
188
|
+
}
|
189
|
+
|
190
|
+
this.lazyLoadHandler();
|
191
|
+
nextTick(() => this.lazyLoadHandler());
|
192
|
+
}
|
193
|
+
/**
|
194
|
+
* remove listener form list
|
195
|
+
* @param {DOM} el
|
196
|
+
* @return
|
197
|
+
*/
|
198
|
+
|
199
|
+
|
200
|
+
remove(el) {
|
201
|
+
if (!el) return;
|
202
|
+
this.observer && this.observer.unobserve(el);
|
203
|
+
var existItem = this.listeners.find(item => item.el === el);
|
204
|
+
|
205
|
+
if (existItem) {
|
206
|
+
this.removeListenerTarget(existItem.$parent);
|
207
|
+
this.removeListenerTarget(window);
|
208
|
+
remove(this.listeners, existItem);
|
209
|
+
existItem.$destroy();
|
210
|
+
}
|
211
|
+
}
|
212
|
+
/*
|
213
|
+
* remove lazy components form list
|
214
|
+
* @param {Vue} vm Vue instance
|
215
|
+
* @return
|
216
|
+
*/
|
217
|
+
|
218
|
+
|
219
|
+
removeComponent(vm) {
|
220
|
+
if (!vm) return;
|
221
|
+
remove(this.listeners, vm);
|
222
|
+
this.observer && this.observer.unobserve(vm.el);
|
223
|
+
|
224
|
+
if (vm.$parent && vm.$el.parentNode) {
|
225
|
+
this.removeListenerTarget(vm.$el.parentNode);
|
226
|
+
}
|
227
|
+
|
228
|
+
this.removeListenerTarget(window);
|
229
|
+
}
|
230
|
+
|
231
|
+
setMode(mode) {
|
232
|
+
if (!hasIntersectionObserver && mode === modeType.observer) {
|
233
|
+
mode = modeType.event;
|
234
|
+
}
|
235
|
+
|
236
|
+
this.mode = mode; // event or observer
|
237
|
+
|
238
|
+
if (mode === modeType.event) {
|
239
|
+
if (this.observer) {
|
240
|
+
this.listeners.forEach(listener => {
|
241
|
+
this.observer.unobserve(listener.el);
|
242
|
+
});
|
243
|
+
this.observer = null;
|
244
|
+
}
|
245
|
+
|
246
|
+
this.targets.forEach(target => {
|
247
|
+
this.initListen(target.el, true);
|
248
|
+
});
|
249
|
+
} else {
|
250
|
+
this.targets.forEach(target => {
|
251
|
+
this.initListen(target.el, false);
|
252
|
+
});
|
253
|
+
this.initIntersectionObserver();
|
254
|
+
}
|
255
|
+
}
|
256
|
+
/*
|
257
|
+
*** Private functions ***
|
258
|
+
*/
|
259
|
+
|
260
|
+
/*
|
261
|
+
* add listener target
|
262
|
+
* @param {DOM} el listener target
|
263
|
+
* @return
|
264
|
+
*/
|
265
|
+
|
266
|
+
|
267
|
+
addListenerTarget(el) {
|
268
|
+
if (!el) return;
|
269
|
+
var target = this.targets.find(target => target.el === el);
|
270
|
+
|
271
|
+
if (!target) {
|
272
|
+
target = {
|
273
|
+
el,
|
274
|
+
id: ++this.targetIndex,
|
275
|
+
childrenCount: 1,
|
276
|
+
listened: true
|
277
|
+
};
|
278
|
+
this.mode === modeType.event && this.initListen(target.el, true);
|
279
|
+
this.targets.push(target);
|
280
|
+
} else {
|
281
|
+
target.childrenCount++;
|
282
|
+
}
|
283
|
+
|
284
|
+
return this.targetIndex;
|
285
|
+
}
|
286
|
+
/*
|
287
|
+
* remove listener target or reduce target childrenCount
|
288
|
+
* @param {DOM} el or window
|
289
|
+
* @return
|
290
|
+
*/
|
291
|
+
|
292
|
+
|
293
|
+
removeListenerTarget(el) {
|
294
|
+
this.targets.forEach((target, index) => {
|
295
|
+
if (target.el === el) {
|
296
|
+
target.childrenCount--;
|
297
|
+
|
298
|
+
if (!target.childrenCount) {
|
299
|
+
this.initListen(target.el, false);
|
300
|
+
this.targets.splice(index, 1);
|
301
|
+
target = null;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
});
|
305
|
+
}
|
306
|
+
/*
|
307
|
+
* add or remove eventlistener
|
308
|
+
* @param {DOM} el DOM or Window
|
309
|
+
* @param {boolean} start flag
|
310
|
+
* @return
|
311
|
+
*/
|
312
|
+
|
313
|
+
|
314
|
+
initListen(el, start) {
|
315
|
+
this.options.ListenEvents.forEach(evt => (start ? on : off)(el, evt, this.lazyLoadHandler));
|
316
|
+
}
|
317
|
+
|
318
|
+
initEvent() {
|
319
|
+
var _this = this;
|
320
|
+
|
321
|
+
this.Event = {
|
322
|
+
listeners: {
|
323
|
+
loading: [],
|
324
|
+
loaded: [],
|
325
|
+
error: []
|
326
|
+
}
|
327
|
+
};
|
328
|
+
|
329
|
+
this.$on = (event, func) => {
|
330
|
+
if (!this.Event.listeners[event]) this.Event.listeners[event] = [];
|
331
|
+
this.Event.listeners[event].push(func);
|
332
|
+
};
|
333
|
+
|
334
|
+
this.$once = (event, func) => {
|
335
|
+
var on = function () {
|
336
|
+
_this.$off(event, on);
|
337
|
+
|
338
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
339
|
+
args[_key] = arguments[_key];
|
340
|
+
}
|
341
|
+
|
342
|
+
func.apply(_this, args);
|
343
|
+
};
|
344
|
+
|
345
|
+
this.$on(event, on);
|
346
|
+
};
|
347
|
+
|
348
|
+
this.$off = (event, func) => {
|
349
|
+
if (!func) {
|
350
|
+
if (!this.Event.listeners[event]) return;
|
351
|
+
this.Event.listeners[event].length = 0;
|
352
|
+
return;
|
353
|
+
}
|
354
|
+
|
355
|
+
remove(this.Event.listeners[event], func);
|
356
|
+
};
|
357
|
+
|
358
|
+
this.$emit = (event, context, inCache) => {
|
359
|
+
if (!this.Event.listeners[event]) return;
|
360
|
+
this.Event.listeners[event].forEach(func => func(context, inCache));
|
361
|
+
};
|
362
|
+
}
|
363
|
+
/**
|
364
|
+
* find nodes which in viewport and trigger load
|
365
|
+
* @return
|
366
|
+
*/
|
367
|
+
|
368
|
+
|
369
|
+
lazyLoadHandler() {
|
370
|
+
var freeList = [];
|
371
|
+
this.listeners.forEach(listener => {
|
372
|
+
if (!listener.el || !listener.el.parentNode) {
|
373
|
+
freeList.push(listener);
|
374
|
+
}
|
375
|
+
|
376
|
+
var catIn = listener.checkInView();
|
377
|
+
if (!catIn) return;
|
378
|
+
listener.load();
|
379
|
+
});
|
380
|
+
freeList.forEach(item => {
|
381
|
+
remove(this.listeners, item);
|
382
|
+
item.$destroy();
|
383
|
+
});
|
384
|
+
}
|
385
|
+
/**
|
386
|
+
* init IntersectionObserver
|
387
|
+
* set mode to observer
|
388
|
+
* @return
|
389
|
+
*/
|
390
|
+
|
391
|
+
|
392
|
+
initIntersectionObserver() {
|
393
|
+
if (!hasIntersectionObserver) {
|
394
|
+
return;
|
395
|
+
}
|
396
|
+
|
397
|
+
this.observer = new IntersectionObserver(this.observerHandler.bind(this), this.options.observerOptions);
|
398
|
+
|
399
|
+
if (this.listeners.length) {
|
400
|
+
this.listeners.forEach(listener => {
|
401
|
+
this.observer.observe(listener.el);
|
402
|
+
});
|
403
|
+
}
|
404
|
+
}
|
405
|
+
/**
|
406
|
+
* init IntersectionObserver
|
407
|
+
* @return
|
408
|
+
*/
|
409
|
+
|
410
|
+
|
411
|
+
observerHandler(entries) {
|
412
|
+
entries.forEach(entry => {
|
413
|
+
if (entry.isIntersecting) {
|
414
|
+
this.listeners.forEach(listener => {
|
415
|
+
if (listener.el === entry.target) {
|
416
|
+
if (listener.state.loaded) return this.observer.unobserve(listener.el);
|
417
|
+
listener.load();
|
418
|
+
}
|
419
|
+
});
|
420
|
+
}
|
421
|
+
});
|
422
|
+
}
|
423
|
+
/**
|
424
|
+
* set element attribute with image'url and state
|
425
|
+
* @param {object} lazyload listener object
|
426
|
+
* @param {string} state will be rendered
|
427
|
+
* @param {bool} inCache is rendered from cache
|
428
|
+
* @return
|
429
|
+
*/
|
430
|
+
|
431
|
+
|
432
|
+
elRenderer(listener, state, cache) {
|
433
|
+
if (!listener.el) return;
|
434
|
+
var {
|
435
|
+
el,
|
436
|
+
bindType
|
437
|
+
} = listener;
|
438
|
+
var src;
|
439
|
+
|
440
|
+
switch (state) {
|
441
|
+
case 'loading':
|
442
|
+
src = listener.loading;
|
443
|
+
break;
|
444
|
+
|
445
|
+
case 'error':
|
446
|
+
src = listener.error;
|
447
|
+
break;
|
448
|
+
|
449
|
+
default:
|
450
|
+
({
|
451
|
+
src
|
452
|
+
} = listener);
|
453
|
+
break;
|
454
|
+
}
|
455
|
+
|
456
|
+
if (bindType) {
|
457
|
+
el.style[bindType] = 'url("' + src + '")';
|
458
|
+
} else if (el.getAttribute('src') !== src) {
|
459
|
+
el.setAttribute('src', src);
|
460
|
+
}
|
461
|
+
|
462
|
+
el.setAttribute('lazy', state);
|
463
|
+
this.$emit(state, listener, cache);
|
464
|
+
this.options.adapter[state] && this.options.adapter[state](listener, this.options);
|
465
|
+
|
466
|
+
if (this.options.dispatchEvent) {
|
467
|
+
var event = new CustomEvent(state, {
|
468
|
+
detail: listener
|
469
|
+
});
|
470
|
+
el.dispatchEvent(event);
|
471
|
+
}
|
472
|
+
}
|
473
|
+
/**
|
474
|
+
* generate loading loaded error image url
|
475
|
+
* @param {string} image's src
|
476
|
+
* @return {object} image's loading, loaded, error url
|
477
|
+
*/
|
478
|
+
|
479
|
+
|
480
|
+
valueFormatter(value) {
|
481
|
+
var src = value;
|
482
|
+
var {
|
483
|
+
loading,
|
484
|
+
error
|
485
|
+
} = this.options; // value is object
|
486
|
+
|
487
|
+
if (isObject(value)) {
|
488
|
+
if (process.env.NODE_ENV !== 'production' && !value.src && !this.options.silent) {
|
489
|
+
console.error('[@vant/lazyload] miss src with ' + value);
|
490
|
+
}
|
491
|
+
|
492
|
+
({
|
493
|
+
src
|
494
|
+
} = value);
|
495
|
+
loading = value.loading || this.options.loading;
|
496
|
+
error = value.error || this.options.error;
|
497
|
+
}
|
498
|
+
|
499
|
+
return {
|
500
|
+
src,
|
501
|
+
loading,
|
502
|
+
error
|
503
|
+
};
|
504
|
+
}
|
505
|
+
|
506
|
+
};
|
507
|
+
}
|