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
|
// lazyload
( function( window, factory ) {
// universal module definition
/* jshint strict: false */
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'./flickity',
'fizzy-ui-utils/utils'
], function( Flickity, utils ) {
return factory( window, Flickity, utils );
});
} else if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
window,
require('./flickity'),
require('fizzy-ui-utils')
);
} else {
// browser global
factory(
window,
window.Flickity,
window.fizzyUIUtils
);
}
}( window, function factory( window, Flickity, utils ) {
'use strict';
Flickity.createMethods.push('_createLazyload');
var proto = Flickity.prototype;
proto._createLazyload = function() {
this.on( 'select', this.lazyLoad );
};
proto.lazyLoad = function() {
var lazyLoad = this.options.lazyLoad;
if ( !lazyLoad ) {
return;
}
// get adjacent cells, use lazyLoad option for adjacent count
var adjCount = typeof lazyLoad == 'number' ? lazyLoad : 0;
var cellElems = this.getAdjacentCellElements( adjCount );
// get lazy images in those cells
var lazyImages = [];
cellElems.forEach( function( cellElem ) {
var lazyCellImages = getCellLazyImages( cellElem );
lazyImages = lazyImages.concat( lazyCellImages );
});
// load lazy images
lazyImages.forEach( function( img ) {
new LazyLoader( img, this );
}, this );
};
function getCellLazyImages( cellElem ) {
// check if cell element is lazy image
if ( cellElem.nodeName == 'IMG' &&
cellElem.getAttribute('data-flickity-lazyload') ) {
return [ cellElem ];
}
// select lazy images in cell
var imgs = cellElem.querySelectorAll('img[data-flickity-lazyload]');
return utils.makeArray( imgs );
}
// -------------------------- LazyLoader -------------------------- //
/**
* class to handle loading images
*/
function LazyLoader( img, flickity ) {
this.img = img;
this.flickity = flickity;
this.load();
}
LazyLoader.prototype.handleEvent = utils.handleEvent;
LazyLoader.prototype.load = function() {
this.img.addEventListener( 'load', this );
this.img.addEventListener( 'error', this );
// load image
this.img.src = this.img.getAttribute('data-flickity-lazyload');
// remove attr
this.img.removeAttribute('data-flickity-lazyload');
};
LazyLoader.prototype.onload = function( event ) {
this.complete( event, 'flickity-lazyloaded' );
};
LazyLoader.prototype.onerror = function( event ) {
this.complete( event, 'flickity-lazyerror' );
};
LazyLoader.prototype.complete = function( event, className ) {
// unbind events
this.img.removeEventListener( 'load', this );
this.img.removeEventListener( 'error', this );
var cell = this.flickity.getParentCell( this.img );
var cellElem = cell && cell.element;
this.flickity.cellSizeChange( cellElem );
this.img.classList.add( className );
this.flickity.dispatchEvent( 'lazyLoad', event, cellElem );
};
// ----- ----- //
Flickity.LazyLoader = LazyLoader;
return Flickity;
}));
|