-
Notifications
You must be signed in to change notification settings - Fork 868
/
vue-lazyload.esm.js
1785 lines (1550 loc) · 45.7 KB
/
vue-lazyload.esm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Vue-Lazyload.js v1.3.5
* (c) 2023 Awe <[email protected]>
* Released under the MIT License.
*/
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var assignSymbols$1 = createCommonjsModule(function (module) {
var toString = Object.prototype.toString;
var isEnumerable = Object.prototype.propertyIsEnumerable;
var getSymbols = Object.getOwnPropertySymbols;
module.exports = function (target) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
if (!isObject(target)) {
throw new TypeError('expected the first argument to be an object');
}
if (args.length === 0 || typeof Symbol !== 'function' || typeof getSymbols !== 'function') {
return target;
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = args[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var arg = _step.value;
var names = getSymbols(arg);
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = names[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var key = _step2.value;
if (isEnumerable.call(arg, key)) {
target[key] = arg[key];
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return target;
};
function isObject(val) {
return typeof val === 'function' || toString.call(val) === '[object Object]' || Array.isArray(val);
}
});
var assignSymbols$2 = /*#__PURE__*/Object.freeze({
__proto__: null,
'default': assignSymbols$1,
__moduleExports: assignSymbols$1
});
var assignSymbols = ( assignSymbols$2 && assignSymbols$1 ) || assignSymbols$2;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var assignDeep = createCommonjsModule(function (module) {
var toString = Object.prototype.toString;
var isValidKey = function isValidKey(key) {
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
};
var assign = module.exports = function (target) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var i = 0;
if (isPrimitive(target)) target = args[i++];
if (!target) target = {};
for (; i < args.length; i++) {
if (isObject(args[i])) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = Object.keys(args[i])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
if (isValidKey(key)) {
if (isObject(target[key]) && isObject(args[i][key])) {
assign(target[key], args[i][key]);
} else {
target[key] = args[i][key];
}
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
assignSymbols(target, args[i]);
}
}
return target;
};
function isObject(val) {
return typeof val === 'function' || toString.call(val) === '[object Object]';
}
function isPrimitive(val) {
return (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' ? val === null : typeof val !== 'function';
}
});
var inBrowser = typeof window !== 'undefined' && window !== null;
var hasIntersectionObserver = checkIntersectionObserver();
function checkIntersectionObserver() {
if (inBrowser && 'IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
// Minimal polyfill for Edge 15's lack of `isIntersecting`
// See: https://github.com/w3c/IntersectionObserver/issues/211
if (!('isIntersecting' in window.IntersectionObserverEntry.prototype)) {
Object.defineProperty(window.IntersectionObserverEntry.prototype, 'isIntersecting', {
get: function get() {
return this.intersectionRatio > 0;
}
});
}
return true;
}
return false;
}
var modeType = {
event: 'event',
observer: 'observer'
// CustomEvent polyfill for IE
};var CustomEvent = function () {
if (!inBrowser) return;
// not IE
if (typeof window.CustomEvent === 'function') return window.CustomEvent;
function CustomEvent(event, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
}
CustomEvent.prototype = window.Event.prototype;
return CustomEvent;
}();
function remove(arr, item) {
if (!arr.length) return;
var index = arr.indexOf(item);
if (index > -1) return arr.splice(index, 1);
}
function some(arr, fn) {
var has = false;
for (var i = 0, len = arr.length; i < len; i++) {
if (fn(arr[i])) {
has = true;
break;
}
}
return has;
}
function getBestSelectionFromSrcset(el, scale) {
if (el.tagName !== 'IMG' || !el.getAttribute('data-srcset')) return;
var options = el.getAttribute('data-srcset');
var result = [];
var container = el.parentNode;
var containerWidth = container.offsetWidth * scale;
var spaceIndex = void 0;
var tmpSrc = void 0;
var tmpWidth = void 0;
options = options.trim().split(',');
options.map(function (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);
}
result.push([tmpWidth, tmpSrc]);
});
result.sort(function (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;
});
var bestSelectedSrc = '';
var tmpOption = void 0;
for (var i = 0; i < result.length; i++) {
tmpOption = result[i];
bestSelectedSrc = tmpOption[1];
var next = result[i + 1];
if (next && next[0] < containerWidth) {
bestSelectedSrc = tmpOption[1];
break;
} else if (!next) {
bestSelectedSrc = tmpOption[1];
break;
}
}
return bestSelectedSrc;
}
function find(arr, fn) {
var item = void 0;
for (var i = 0, len = arr.length; i < len; i++) {
if (fn(arr[i])) {
item = arr[i];
break;
}
}
return item;
}
var getDPR = function getDPR() {
var scale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return inBrowser ? window.devicePixelRatio || scale : scale;
};
function supportWebp() {
if (!inBrowser) return false;
var support = true;
try {
var 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) {
var timeout = null;
var movement = null;
var lastRun = 0;
var needRun = false;
return function () {
needRun = true;
if (timeout) {
return;
}
var elapsed = Date.now() - lastRun;
var context = this;
var args = arguments;
var runCallback = function runCallback() {
lastRun = Date.now();
timeout = false;
action.apply(context, args);
};
if (elapsed >= delay) {
runCallback();
} else {
timeout = setTimeout(runCallback, delay);
}
if (needRun) {
clearTimeout(movement);
movement = setTimeout(runCallback, 2 * delay);
}
};
}
function testSupportsPassive() {
if (!inBrowser) return;
var support = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function get() {
support = true;
}
});
window.addEventListener('test', null, opts);
} catch (e) {}
return support;
}
var supportsPassive = testSupportsPassive();
var _ = {
on: function on(el, type, func) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
if (supportsPassive) {
el.addEventListener(type, func, {
capture: capture,
passive: true
});
} else {
el.addEventListener(type, func, capture);
}
},
off: function off(el, type, func) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
el.removeEventListener(type, func, capture);
}
};
var loadImageAsync = function loadImageAsync(item, resolve, reject) {
var image = new Image();
if (!item || !item.src) {
var err = new Error('image src is required');
return reject(err);
}
image.src = item.src;
if (item.cors) {
image.crossOrigin = item.cors;
}
image.onload = function () {
resolve({
naturalHeight: image.naturalHeight,
naturalWidth: image.naturalWidth,
src: image.src
});
};
image.onerror = function (e) {
reject(e);
};
};
var style = function style(el, prop) {
return typeof getComputedStyle !== 'undefined' ? getComputedStyle(el, null).getPropertyValue(prop) : el.style[prop];
};
var overflow = function overflow(el) {
return style(el, 'overflow') + style(el, 'overflow-y') + style(el, 'overflow-x');
};
var scrollParent = function scrollParent(el) {
if (!inBrowser) return;
if (!(el instanceof HTMLElement)) {
return window;
}
var parent = el;
while (parent) {
if (parent === document.body || parent === document.documentElement) {
break;
}
if (!parent.parentNode) {
break;
}
if (/(scroll|auto)/.test(overflow(parent))) {
return parent;
}
parent = parent.parentNode;
}
return window;
};
function isObject(obj) {
return obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object';
}
function ObjectKeys(obj) {
if (!(obj instanceof Object)) return [];
if (Object.keys) {
return Object.keys(obj);
} else {
var keys = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
}
}
function ArrayFrom(arrLike) {
var len = arrLike.length;
var list = [];
for (var i = 0; i < len; i++) {
list.push(arrLike[i]);
}
return list;
}
function noop() {}
var ImageCache = function () {
function ImageCache(_ref) {
var max = _ref.max;
classCallCheck(this, ImageCache);
this.options = {
max: max || 100
};
this._caches = [];
}
createClass(ImageCache, [{
key: 'has',
value: function has(key) {
return this._caches.indexOf(key) > -1;
}
}, {
key: 'add',
value: function add(key) {
if (this.has(key)) return;
this._caches.push(key);
if (this._caches.length > this.options.max) {
this.free();
}
}
}, {
key: 'free',
value: function free() {
this._caches.shift();
}
}]);
return ImageCache;
}();
// el: {
// state,
// src,
// error,
// loading
// }
var ReactiveListener = function () {
function ReactiveListener(_ref) {
var el = _ref.el,
src = _ref.src,
error = _ref.error,
loading = _ref.loading,
bindType = _ref.bindType,
$parent = _ref.$parent,
options = _ref.options,
cors = _ref.cors,
elRenderer = _ref.elRenderer,
imageCache = _ref.imageCache;
classCallCheck(this, ReactiveListener);
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.rect = null;
this.$parent = $parent;
this.elRenderer = elRenderer;
this._imageCache = imageCache;
this.performanceData = {
init: Date.now(),
loadStart: 0,
loadEnd: 0
};
this.filter();
this.initState();
this.render('loading', false);
}
/*
* init listener state
* @return
*/
createClass(ReactiveListener, [{
key: 'initState',
value: function 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
*/
}, {
key: 'record',
value: function 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
*/
}, {
key: 'update',
value: function update(_ref2) {
var src = _ref2.src,
loading = _ref2.loading,
error = _ref2.error;
var oldSrc = this.src;
this.src = src;
this.loading = loading;
this.error = error;
this.filter();
if (oldSrc !== this.src) {
this.attempt = 0;
this.initState();
}
}
/*
* get el node rect
* @return
*/
}, {
key: 'getRect',
value: function getRect() {
this.rect = this.el.getBoundingClientRect();
}
/*
* check el is in view
* @return {Boolean} el is in view
*/
}, {
key: 'checkInView',
value: function checkInView() {
this.getRect();
return this.rect.top < window.innerHeight * this.options.preLoad && this.rect.bottom > this.options.preLoadTop && this.rect.left < window.innerWidth * this.options.preLoad && this.rect.right > 0;
}
/*
* listener filter
*/
}, {
key: 'filter',
value: function filter() {
var _this = this;
ObjectKeys(this.options.filter).map(function (key) {
_this.options.filter[key](_this, _this.options);
});
}
/*
* render loading first
* @params cb:Function
* @return
*/
}, {
key: 'renderLoading',
value: function renderLoading(cb) {
var _this2 = this;
this.state.loading = true;
loadImageAsync({
src: this.loading,
cors: this.cors
}, function (data) {
_this2.render('loading', false);
_this2.state.loading = false;
cb();
}, function () {
// handler `loading image` load failed
cb();
_this2.state.loading = false;
if (!_this2.options.silent) console.warn('VueLazyload log: load failed with loading image(' + _this2.loading + ')');
});
}
/*
* try load image and render it
* @return
*/
}, {
key: 'load',
value: function load() {
var _this3 = this;
var onFinish = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop;
if (this.attempt > this.options.attempt - 1 && this.state.error) {
if (!this.options.silent) console.log('VueLazyload log: ' + 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(function () {
_this3.attempt++;
_this3.options.adapter['beforeLoad'] && _this3.options.adapter['beforeLoad'](_this3, _this3.options);
_this3.record('loadStart');
loadImageAsync({
src: _this3.src,
cors: _this3.cors
}, function (data) {
_this3.naturalHeight = data.naturalHeight;
_this3.naturalWidth = data.naturalWidth;
_this3.state.loaded = true;
_this3.state.error = false;
_this3.record('loadEnd');
_this3.render('loaded', false);
_this3.state.rendered = true;
_this3._imageCache.add(_this3.src);
onFinish();
}, function (err) {
!_this3.options.silent && console.error(err);
_this3.state.error = true;
_this3.state.loaded = false;
_this3.render('error', false);
});
});
}
/*
* render image
* @param {String} state to render // ['loading', 'src', 'error']
* @param {String} is form cache
* @return
*/
}, {
key: 'render',
value: function render(state, cache) {
this.elRenderer(this, state, cache);
}
/*
* output performance data
* @return {Object} performance data
*/
}, {
key: 'performance',
value: function performance() {
var state = 'loading';
var time = 0;
if (this.state.loaded) {
state = 'loaded';
time = (this.performanceData.loadEnd - this.performanceData.loadStart) / 1000;
}
if (this.state.error) state = 'error';
return {
src: this.src,
state: state,
time: time
};
}
/*
* $destroy
* @return
*/
}, {
key: '$destroy',
value: function $destroy() {
this.el = null;
this.src = null;
this.error = null;
this.loading = null;
this.bindType = null;
this.attempt = 0;
}
}]);
return ReactiveListener;
}();
var DEFAULT_URL = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
var DEFAULT_EVENTS = ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove'];
var DEFAULT_OBSERVER_OPTIONS = {
rootMargin: '0px',
threshold: 0
};
function Lazy(Vue) {
return function () {
function Lazy(_ref) {
var preLoad = _ref.preLoad,
error = _ref.error,
throttleWait = _ref.throttleWait,
preLoadTop = _ref.preLoadTop,
dispatchEvent = _ref.dispatchEvent,
loading = _ref.loading,
attempt = _ref.attempt,
_ref$silent = _ref.silent,
silent = _ref$silent === undefined ? true : _ref$silent,
scale = _ref.scale,
listenEvents = _ref.listenEvents;
_ref.hasbind;
var filter = _ref.filter,
adapter = _ref.adapter,
observer = _ref.observer,
observerOptions = _ref.observerOptions;
classCallCheck(this, Lazy);
this.version = '"1.3.5"';
this.mode = modeType.event;
this.ListenerQueue = [];
this.TargetIndex = 0;
this.TargetQueue = [];
this.options = {
silent: 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 || getDPR(scale),
ListenEvents: listenEvents || DEFAULT_EVENTS,
hasbind: false,
supportWebp: supportWebp(),
filter: filter || {},
adapter: adapter || {},
observer: !!observer,
observerOptions: observerOptions || DEFAULT_OBSERVER_OPTIONS
};
this._initEvent();
this._imageCache = new ImageCache({ max: 200 });
this.lazyLoadHandler = throttle(this._lazyLoadHandler.bind(this), this.options.throttleWait);
this.setMode(this.options.observer ? modeType.observer : modeType.event);
}
/**
* update config
* @param {Object} config params
* @return
*/
createClass(Lazy, [{
key: 'config',
value: function config() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
assignDeep(this.options, options);
}
/**
* output listener's load performance
* @return {Array}
*/
}, {
key: 'performance',
value: function performance() {
var list = [];
this.ListenerQueue.map(function (item) {
list.push(item.performance());
});
return list;
}
/*
* add lazy component to queue
* @param {Vue} vm lazy component instance
* @return
*/
}, {
key: 'addLazyBox',
value: function addLazyBox(vm) {
this.ListenerQueue.push(vm);
if (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
*/
}, {
key: 'add',
value: function add(el, binding, vnode) {
var _this = this;
if (some(this.ListenerQueue, function (item) {
return item.el === el;
})) {
this.update(el, binding);
return Vue.nextTick(this.lazyLoadHandler);
}
var _valueFormatter2 = this._valueFormatter(binding.value),
src = _valueFormatter2.src,
loading = _valueFormatter2.loading,
error = _valueFormatter2.error,
cors = _valueFormatter2.cors;
Vue.nextTick(function () {
src = getBestSelectionFromSrcset(el, _this.options.scale) || src;
_this._observer && _this._observer.observe(el);
var container = Object.keys(binding.modifiers)[0];
var $parent = void 0;
if (container) {
$parent = vnode.context.$refs[container];
// if there is container passed in, try ref first, then fallback to getElementById to support the original usage
$parent = $parent ? $parent.$el || $parent : document.getElementById(container);
}
if (!$parent) {
$parent = scrollParent(el);
}
var newListener = new ReactiveListener({
bindType: binding.arg,
$parent: $parent,
el: el,
loading: loading,
error: error,
src: src,
cors: cors,
elRenderer: _this._elRenderer.bind(_this),
options: _this.options,
imageCache: _this._imageCache
});
_this.ListenerQueue.push(newListener);
if (inBrowser) {
_this._addListenerTarget(window);
_this._addListenerTarget($parent);
}
_this.lazyLoadHandler();
Vue.nextTick(function () {
return _this.lazyLoadHandler();
});
});
}
/**
* update image src
* @param {DOM} el