-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
responsively-lazy.js
478 lines (438 loc) · 19.8 KB
/
responsively-lazy.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
/*
* Responsively Lazy
* https://ivopetkov.com/responsively-lazy/
* Copyright (c) Ivo Petkov
* Free to use under the MIT license.
*
* Debug:
* document.cookie = "ivopetkov-responsively-lazy=debug";
*/
var responsivelyLazy = typeof responsivelyLazy !== 'undefined' ? responsivelyLazy : (function () {
if (typeof window.addEventListener !== 'undefined' && typeof document.querySelectorAll !== 'undefined') { // Check for old browsers
var debug = document.cookie.indexOf('ivopetkov-responsively-lazy=debug') !== -1;
var hasWebPSupport = null;
var hasAVIFSupport = null;
var hasSrcSetSupport = 'srcset' in document.createElement('img');
var windowWidth = null;
var windowHeight = null;
var hasIntersectionObserverSupport = typeof IntersectionObserver !== 'undefined';
var mutationObserverIsDisabled = false;
var getVisibilityPriority = function (element) {
var thresholdHorizontal = 0;
var thresholdVertical = 0;
var thresholdValue = element.getAttribute('data-responsively-lazy-threshold');
if (thresholdValue !== null) {
if (thresholdValue.substr(-2) === 'px') {
thresholdHorizontal = thresholdVertical = parseInt(thresholdValue.substr(0, thresholdValue.length - 2), 10);
} else if (thresholdValue.substr(-1) === '%') {
var percent = parseInt(thresholdValue.substr(0, thresholdValue.length - 1), 10) / 100;
thresholdHorizontal = Math.floor(windowWidth * percent);
thresholdVertical = Math.floor(windowHeight * percent);
}
}
var rect = element.getBoundingClientRect();
var elementTop = rect.top;
var elementLeft = rect.left;
var elementWidth = rect.width;
var elementHeight = rect.height;
if (elementTop === 0 && elementLeft === 0 && elementWidth === 0 && elementHeight === 0) {
return 0;
}
if (elementWidth === 0) {
elementWidth = 1;
}
if (elementHeight === 0) {
elementHeight = 1;
}
var getVisibleAreaSize = function (elementPoint, elementSize, windowSize) {
return elementPoint < windowSize && elementPoint + elementSize > 0 ? Math.min(windowSize, elementPoint + elementSize) - Math.max(0, elementPoint) : 0;
};
return (getVisibleAreaSize(elementLeft - thresholdHorizontal, elementWidth + 2 * thresholdHorizontal, windowWidth) * getVisibleAreaSize(elementTop - thresholdVertical, elementHeight + 2 * thresholdVertical, windowHeight)) / ((elementWidth + 2 * thresholdHorizontal) * (elementHeight + 2 * thresholdVertical)) * 100;
}
var evalScripts = function (scripts, startIndex) {
var scriptsCount = scripts.length;
for (var i = startIndex; i < scriptsCount; i++) {
var breakAfterThisScript = false;
var script = scripts[i];
var newScript = document.createElement('script');
var type = script.getAttribute('type');
if (type !== null) {
newScript.setAttribute("type", type);
}
var src = script.getAttribute('src');
if (src !== null) {
newScript.setAttribute("src", src);
if ((typeof script.async === 'undefined' || script.async === false) && i + 1 < scriptsCount) {
breakAfterThisScript = true;
newScript.addEventListener('load', function () {
evalScripts(scripts, i + 1);
});
}
}
newScript.innerHTML = script.innerHTML;
script.parentNode.insertBefore(newScript, script);
script.parentNode.removeChild(script);
if (breakAfterThisScript) {
break;
}
}
};
var loadImageQueue = [];
var processLoadImageQueueLock = false;
var processLoadImageQueue = function () {
if (processLoadImageQueueLock) {
return;
}
processLoadImageQueueLock = true;
var maxConcurrentImages = 3;
loadImageQueue = loadImageQueue.filter(function (item) { return item[2] !== 2 }); // Remove completed
for (var i = 0; i < loadImageQueue.length; i++) { // Update visibility priority
loadImageQueue[i][4] = getVisibilityPriority(loadImageQueue[i][1]);
}
loadImageQueue.sort(function (item1, item2) { // Sort by visibility priority
return item2[4] - item1[4];
});
var currentlyLoadingCount = loadImageQueue.filter(function (item) { return item[3] === 1 }).length;
for (var i = 0; i < loadImageQueue.length; i++) {
if (currentlyLoadingCount >= maxConcurrentImages) {
break;
}
var item = loadImageQueue[i];
if (item[3] === 0) {
item[3] = 1; // Status: loading
item[2](); // Call load()
currentlyLoadingCount++;
}
}
processLoadImageQueueLock = false;
};
var loadImageCounter = 0;
var loadImage = function (contextElement, url, callback) {
loadImageCounter++;
var key = 'i' + loadImageCounter;
var timeout = null;
var onDone = function () {
clearTimeout(timeout);
for (var i = 0; i < loadImageQueue.length; i++) {
var item = loadImageQueue[i];
if (item[0] === key) {
item[3] = 2; // Status: completed
break;
}
}
processLoadImageQueue();
};
var image = new Image();
image.onload = function () {
onDone();
callback(true);
};
image.onerror = function () {
onDone();
callback(false);
};
var load = function () {
image.src = url;
timeout = setTimeout(onDone, 60000);
};
loadImageQueue.push([key, contextElement, load, 0, 0]); // key, element, on load function, status, visiblity priority
processLoadImageQueue();
};
var updateImage = function (type, element) {
var options = [];
var value = element.getAttribute('data-responsively-lazy');
var maxOptionWidth = null;
if (value !== null) {
value = value.trim();
if (value.length > 0) {
value = value.split(',');
for (var j = 0; j < value.length; j++) {
var optionImage = null;
var optionWidth = 999998;
var skipOption = false;
var optionParts = value[j].trim().split(' ');
for (var k = 0; k < optionParts.length; k++) {
var optionPart = optionParts[k];
var optionPartLength = optionPart.length;
if (optionPartLength === 0) {
continue;
}
if (optionImage === null) {
optionImage = optionPart;
} else {
if (optionPart[optionPartLength - 1] === 'w') {
optionWidth = parseInt(optionPart.substr(0, optionPartLength - 1), 10);
} else if (optionPart === 'webp' && !hasWebPSupport) {
skipOption = true;
} else if (optionPart === 'avif' && !hasAVIFSupport) {
skipOption = true;
}
}
}
if (skipOption) {
continue;
}
if ((optionImage.indexOf('%2F') !== -1 || optionImage.indexOf('%3F') !== -1) && optionImage.indexOf('/') === -1 && optionImage.indexOf('?') === -1) {// path is encoded
optionImage = decodeURIComponent(optionImage);
}
options.push([optionImage, optionWidth]);
if (maxOptionWidth < optionWidth) {
maxOptionWidth = optionWidth;
}
}
options.sort(function (a, b) {
return a[1] - b[1];
});
var temp = [];
for (var j = 0; j < options.length; j++) {
var option = options[j];
if (j > 0) {
if (option[1] === temp[temp.length - 1][1]) {
continue;
}
}
temp.push([option[0], option[1]]);
}
options = temp;
}
}
var elementWidth = element.getBoundingClientRect().width * (typeof window.devicePixelRatio !== 'undefined' ? window.devicePixelRatio : 1);
var bestSelectedOption = null;
for (var j = 0; j < options.length; j++) {
var option = options[j];
if (option[1] >= elementWidth || option[1] === maxOptionWidth) { // Show the largest available option even if the element width is larger (can be webp or avif)
bestSelectedOption = option;
break;
}
}
if (bestSelectedOption === null) {
if (type === 'img') {
bestSelectedOption = [element.getAttribute('src'), 999999]; // No options found
} else { // background
bestSelectedOption = [null, 999999];
}
}
if (typeof element.responsivelyLazyOption === 'undefined') {
element.responsivelyLazyOption = ['', 0];
}
if (element.responsivelyLazyOption[1] < bestSelectedOption[1]) {
element.responsivelyLazyOption = bestSelectedOption;
var url = bestSelectedOption[0];
if (url === null) {
return;
}
loadImage(element, url, function (result) {
if (result && element.responsivelyLazyOption[0] === url) {
if (type === 'img') {
if (url === element.getAttribute('src')) {
element.removeAttribute('srcset');
} else {
element.setAttribute('srcset', url);
}
} else { // background
element.style.backgroundImage = 'url(' + url + ')';
}
if (typeof element.responsivelyLazyLoadDispached === 'undefined') {
element.responsivelyLazyLoadDispached = true;
var handler = element.getAttribute('data-on-responsively-lazy-load');
if (handler !== null) {
(new Function(handler).bind(element))();
}
if (typeof Event !== 'undefined') {
var event = new Event('responsively-lazy-load');
element.dispatchEvent(event);
}
}
} else {
element.responsivelyLazyOption = ['', 0];
}
});
}
};
var updateWindowSize = function () {
windowWidth = window.innerWidth;
windowHeight = window.innerHeight;
};
var updateElement = function (element, options) {
if (typeof element.responsivelyLazyDone !== 'undefined') {
return;
}
var ignoreThreshold = typeof options.ignoreThreshold !== 'undefined' ? options.ignoreThreshold : false;
if (!ignoreThreshold && getVisibilityPriority(element) === 0) {
return;
}
var type = element.getAttribute('data-responsively-lazy-type');
if (type !== 'background' && type !== 'html') {
type = 'img';
}
if (type === 'html') {
element.responsivelyLazyDone = true;
mutationObserverIsDisabled = true;
element.innerHTML = element.getAttribute('data-responsively-lazy');
var scripts = element.querySelectorAll('script');
if (scripts.length > 0) {
evalScripts(scripts, 0);
}
mutationObserverIsDisabled = false;
} else if (type === 'img') {
if (hasSrcSetSupport) {
updateImage(type, element);
}
} else { // background
updateImage(type, element);
}
};
var run = function (element, options) {
if (hasWebPSupport === null) {
return;
}
if (hasAVIFSupport === null) {
return;
}
if (typeof options === 'undefined') {
options = {};
}
if (debug) {
var timerLabel = 'responsivelyLazy::run';
console.time(timerLabel);
}
if (typeof element !== 'undefined' && element !== null) {
if (element.getAttribute('data-responsively-lazy') !== null) {
updateElement(element, options);
}
} else {
var elements = document.querySelectorAll('[data-responsively-lazy]');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
updateElement(elements[i], options);
}
}
if (debug) {
console.timeEnd(timerLabel);
}
};
var testImageSupport = function (base64, callback) {
var image = new Image();
image.onload = image.onerror = function () {
callback(image);
};
image.src = 'data:image/webp;base64,' + base64;
};
var runIfAllTestsDone = function () {
if (hasWebPSupport !== null && hasAVIFSupport !== null) {
run();
}
};
testImageSupport('UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAD8D+JaQAA3AA/ua1AAA=', function (image) {
hasWebPSupport = image.width === 1;
runIfAllTestsDone();
});
testImageSupport('AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUEAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAACAAAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAEAAAABAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgSAAAAAAABNjb2xybmNseAABAA0AAIAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAAChtZGF0EgAKBzgABpAQ0AIyExAAAAAP+j9adAx6kYPdyoRe9BA=', function (image) {
hasAVIFSupport = image.width === 1;
runIfAllTestsDone();
});
updateWindowSize();
var requestAnimationFrameFunction = window.requestAnimationFrame || function (callback) {
window.setTimeout(callback, 20);
};
var hasChange = false;
var process = function () {
if (hasChange) {
hasChange = false;
run();
}
requestAnimationFrameFunction.call(null, process);
};
process();
if (hasIntersectionObserverSupport) {
var intersectionObserver = new IntersectionObserver(function (entries) {
for (var i in entries) {
var entry = entries[i];
if (entry.intersectionRatio > 0) {
updateElement(entry.target, {});
}
}
});
var updateIntersectionObservers = function () {
var elements = document.querySelectorAll('[data-responsively-lazy]');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (typeof element.responsivelyLazyObserver === 'undefined') {
element.responsivelyLazyObserver = true;
intersectionObserver.observe(element);
}
}
};
var changeTimeout = null;
}
var setChanged = function () {
if (hasIntersectionObserverSupport) {
window.clearTimeout(changeTimeout);
changeTimeout = window.setTimeout(function () {
hasChange = true;
}, 50);
} else {
hasChange = true;
}
};
var updateParentNodesScrollListeners = function () {
var elements = document.querySelectorAll('[data-responsively-lazy]');
for (var i = 0; i < elements.length; i++) {
var parentNode = elements[i].parentNode;
while (parentNode && parentNode.tagName.toLowerCase() !== 'html') {
if (typeof parentNode.responsivelyLazyScroll === 'undefined') {
parentNode.responsivelyLazyScroll = true;
parentNode.addEventListener('scroll', setChanged);
}
parentNode = parentNode.parentNode;
}
}
};
var initialized = false;
var initialize = function () {
if (initialized) {
return;
}
initialized = true;
window.addEventListener('resize', function () {
updateWindowSize();
setChanged();
});
window.addEventListener('scroll', setChanged);
window.addEventListener('load', setChanged);
window.addEventListener('orientationchange', function () {
updateWindowSize();
setChanged();
});
if (hasIntersectionObserverSupport) {
updateIntersectionObservers();
}
updateParentNodesScrollListeners();
if (typeof MutationObserver !== 'undefined') {
var observer = new MutationObserver(function () {
if (!mutationObserverIsDisabled) {
if (hasIntersectionObserverSupport) {
updateIntersectionObservers();
}
updateParentNodesScrollListeners();
setChanged();
}
});
observer.observe(document.querySelector('body'), { childList: true, subtree: true });
}
};
document.addEventListener('readystatechange', () => { // interactive or complete
initialize();
run();
});
if (document.readyState === 'complete') {
initialize();
run();
}
} else {
var run = function () { };
}
return {
'run': run
};
}());