forked from verlok/vanilla-lazyload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazyload.transpiled.js
329 lines (288 loc) · 13.1 KB
/
lazyload.transpiled.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
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; };
(function (global, factory) {
(typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : global.LazyLoad = factory();
})(this, function () {
'use strict';
var defaultSettings = {
elements_selector: "img",
container: window,
threshold: 300,
throttle: 150,
data_src: "original",
data_srcset: "originalSet",
class_loading: "loading",
class_loaded: "loaded",
class_error: "error",
class_initial: "initial",
skip_invisible: true,
callback_load: null,
callback_error: null,
callback_set: null,
callback_processed: null
};
var isBot = !("onscroll" in window) || /glebot/.test(navigator.userAgent);
var callCallback = function callCallback(callback, argument) {
if (callback) {
callback(argument);
}
};
var getTopOffset = function getTopOffset(element) {
return element.getBoundingClientRect().top + window.pageYOffset - element.ownerDocument.documentElement.clientTop;
};
var isBelowViewport = function isBelowViewport(element, container, threshold) {
var fold = container === window ? window.innerHeight + window.pageYOffset : getTopOffset(container) + container.offsetHeight;
return fold <= getTopOffset(element) - threshold;
};
var getLeftOffset = function getLeftOffset(element) {
return element.getBoundingClientRect().left + window.pageXOffset - element.ownerDocument.documentElement.clientLeft;
};
var isAtRightOfViewport = function isAtRightOfViewport(element, container, threshold) {
var documentWidth = window.innerWidth;
var fold = container === window ? documentWidth + window.pageXOffset : getLeftOffset(container) + documentWidth;
return fold <= getLeftOffset(element) - threshold;
};
var isAboveViewport = function isAboveViewport(element, container, threshold) {
var fold = container === window ? window.pageYOffset : getTopOffset(container);
return fold >= getTopOffset(element) + threshold + element.offsetHeight;
};
var isAtLeftOfViewport = function isAtLeftOfViewport(element, container, threshold) {
var fold = container === window ? window.pageXOffset : getLeftOffset(container);
return fold >= getLeftOffset(element) + threshold + element.offsetWidth;
};
var isInsideViewport = function isInsideViewport(element, container, threshold) {
return !isBelowViewport(element, container, threshold) && !isAboveViewport(element, container, threshold) && !isAtRightOfViewport(element, container, threshold) && !isAtLeftOfViewport(element, container, threshold);
};
/* Creates instance and notifies it through the window element */
var createInstance = function createInstance(classObj, options) {
var instance = new classObj(options);
var event = new CustomEvent("LazyLoad::Initialized", { detail: { instance: instance } });
window.dispatchEvent(event);
};
/* Auto initialization of one or more instances of lazyload, depending on the
options passed in (plain object or an array) */
var autoInitialize = function autoInitialize(classObj, options) {
var optsLength = options.length;
if (!optsLength) {
// Plain object
createInstance(classObj, options);
} else {
// Array of objects
for (var i = 0; i < optsLength; i++) {
createInstance(classObj, options[i]);
}
}
};
var setSourcesForPicture = function setSourcesForPicture(element, srcsetDataAttribute) {
var parent = element.parentElement;
if (parent.tagName !== "PICTURE") {
return;
}
for (var i = 0; i < parent.children.length; i++) {
var pictureChild = parent.children[i];
if (pictureChild.tagName === "SOURCE") {
var sourceSrcset = pictureChild.dataset[srcsetDataAttribute];
if (sourceSrcset) {
pictureChild.setAttribute("srcset", sourceSrcset);
}
}
}
};
var setSources = function setSources(element, srcsetDataAttribute, srcDataAttribute) {
var tagName = element.tagName;
var elementSrc = element.dataset[srcDataAttribute];
if (tagName === "IMG") {
setSourcesForPicture(element, srcsetDataAttribute);
var imgSrcset = element.dataset[srcsetDataAttribute];
if (imgSrcset) {
element.setAttribute("srcset", imgSrcset);
}
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
return;
}
if (tagName === "IFRAME") {
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
return;
}
if (elementSrc) {
element.style.backgroundImage = "url(" + elementSrc + ")";
}
};
/*
* Constructor
*/
var LazyLoad = function LazyLoad(instanceSettings) {
this._settings = _extends({}, defaultSettings, instanceSettings);
this._queryOriginNode = this._settings.container === window ? document : this._settings.container;
this._previousLoopTime = 0;
this._loopTimeout = null;
this._boundHandleScroll = this.handleScroll.bind(this);
this._isFirstLoop = true;
window.addEventListener("resize", this._boundHandleScroll);
this.update();
};
LazyLoad.prototype = {
/*
* Private methods
*/
_reveal: function _reveal(element) {
var settings = this._settings;
var errorCallback = function errorCallback() {
/* As this method is asynchronous, it must be protected against external destroy() calls */
if (!settings) {
return;
}
element.removeEventListener("load", loadCallback);
element.removeEventListener("error", errorCallback);
element.classList.remove(settings.class_loading);
element.classList.add(settings.class_error);
callCallback(settings.callback_error, element);
};
var loadCallback = function loadCallback() {
/* As this method is asynchronous, it must be protected against external destroy() calls */
if (!settings) {
return;
}
element.classList.remove(settings.class_loading);
element.classList.add(settings.class_loaded);
element.removeEventListener("load", loadCallback);
element.removeEventListener("error", errorCallback);
/* Calling LOAD callback */
callCallback(settings.callback_load, element);
};
if (element.tagName === "IMG" || element.tagName === "IFRAME") {
element.addEventListener("load", loadCallback);
element.addEventListener("error", errorCallback);
element.classList.add(settings.class_loading);
}
setSources(element, settings.data_srcset, settings.data_src);
/* Calling SET callback */
callCallback(settings.callback_set, element);
},
_loopThroughElements: function _loopThroughElements() {
var settings = this._settings,
elements = this._elements,
elementsLength = !elements ? 0 : elements.length;
var i = void 0,
processedIndexes = [],
firstLoop = this._isFirstLoop;
for (i = 0; i < elementsLength; i++) {
var element = elements[i];
/* If must skip_invisible and element is invisible, skip it */
if (settings.skip_invisible && element.offsetParent === null) {
continue;
}
if (isBot || isInsideViewport(element, settings.container, settings.threshold)) {
if (firstLoop) {
element.classList.add(settings.class_initial);
}
/* Start loading the image */
this._reveal(element);
/* Marking the element as processed. */
processedIndexes.push(i);
element.dataset.wasProcessed = true;
}
}
/* Removing processed elements from this._elements. */
while (processedIndexes.length > 0) {
elements.splice(processedIndexes.pop(), 1);
/* Calling the end loop callback */
callCallback(settings.callback_processed, elements.length);
}
/* Stop listening to scroll event when 0 elements remains */
if (elementsLength === 0) {
this._stopScrollHandler();
}
/* Sets isFirstLoop to false */
if (firstLoop) {
this._isFirstLoop = false;
}
},
_purgeElements: function _purgeElements() {
var elements = this._elements,
elementsLength = elements.length;
var i = void 0,
elementsToPurge = [];
for (i = 0; i < elementsLength; i++) {
var element = elements[i];
/* If the element has already been processed, skip it */
if (element.dataset.wasProcessed) {
elementsToPurge.push(i);
}
}
/* Removing elements to purge from this._elements. */
while (elementsToPurge.length > 0) {
elements.splice(elementsToPurge.pop(), 1);
}
},
_startScrollHandler: function _startScrollHandler() {
if (!this._isHandlingScroll) {
this._isHandlingScroll = true;
this._settings.container.addEventListener("scroll", this._boundHandleScroll);
}
},
_stopScrollHandler: function _stopScrollHandler() {
if (this._isHandlingScroll) {
this._isHandlingScroll = false;
this._settings.container.removeEventListener("scroll", this._boundHandleScroll);
}
},
/*
* Public methods
*/
handleScroll: function handleScroll() {
var throttle = this._settings.throttle;
if (throttle !== 0) {
var getTime = function getTime() {
new Date().getTime();
};
var now = getTime();
var remainingTime = throttle - (now - this._previousLoopTime);
if (remainingTime <= 0 || remainingTime > throttle) {
if (this._loopTimeout) {
clearTimeout(this._loopTimeout);
this._loopTimeout = null;
}
this._previousLoopTime = now;
this._loopThroughElements();
} else if (!this._loopTimeout) {
this._loopTimeout = setTimeout(function () {
this._previousLoopTime = getTime();
this._loopTimeout = null;
this._loopThroughElements();
}.bind(this), remainingTime);
}
} else {
this._loopThroughElements();
}
},
update: function update() {
// Converts to array the nodeset obtained querying the DOM from _queryOriginNode with elements_selector
this._elements = Array.prototype.slice.call(this._queryOriginNode.querySelectorAll(this._settings.elements_selector));
this._purgeElements();
this._loopThroughElements();
this._startScrollHandler();
},
destroy: function destroy() {
window.removeEventListener("resize", this._boundHandleScroll);
if (this._loopTimeout) {
clearTimeout(this._loopTimeout);
this._loopTimeout = null;
}
this._stopScrollHandler();
this._elements = null;
this._queryOriginNode = null;
this._settings = null;
}
};
/* Automatic instances creation if required (useful for async script loading!) */
var autoInitOptions = window.lazyLoadOptions;
if (autoInitOptions) {
autoInitialize(LazyLoad, autoInitOptions);
}
return LazyLoad;
});