jellyfin-web/dashboard-ui/thirdparty/jquery.unveil-custom.js

130 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-01-16 11:29:08 -07:00
define(['visibleinviewport', 'imageloader'], function (visibleInViewport, imageLoader) {
2016-01-12 12:55:45 -07:00
var wheelEvent = (document.implementation.hasFeature('Event.wheel', '3.0') ? 'wheel' : 'mousewheel');
var thresholdX = screen.availWidth;
var thresholdY = screen.availHeight;
2015-10-28 10:09:55 -07:00
function isVisible(elem) {
2016-01-12 12:55:45 -07:00
return visibleInViewport(elem, true, thresholdX, thresholdY);
2015-10-28 10:09:55 -07:00
}
2015-10-28 10:09:55 -07:00
function fillImage(elem) {
var source = elem.getAttribute('data-src');
if (source) {
2016-01-16 11:29:08 -07:00
imageLoader.loadImage(elem, source).then(fadeIn);
2015-10-28 10:09:55 -07:00
elem.setAttribute("data-src", '');
}
}
2016-01-16 11:29:08 -07:00
function fadeIn(elem) {
if (!browserInfo.animate || browserInfo.mobile) {
return;
}
if (elem.classList.contains('noFade')) {
return;
}
var keyframes = [
{ opacity: '0', offset: 0 },
{ opacity: '1', offset: 1 }];
var timing = { duration: 300, iterations: 1 };
elem.animate(keyframes, timing);
}
2016-01-13 14:22:46 -07:00
function cancelAll(tokens) {
for (var i = 0, length = tokens.length; i < length; i++) {
2016-01-13 14:22:46 -07:00
tokens[i] = true;
2015-10-28 10:09:55 -07:00
}
2016-01-13 14:22:46 -07:00
}
2016-01-13 14:22:46 -07:00
function unveilElements(images) {
2016-01-13 14:22:46 -07:00
if (!images.length) {
return;
}
var cancellationTokens = [];
function unveilInternal(tokenIndex) {
2015-10-28 10:09:55 -07:00
var remaining = [];
2016-01-13 14:22:46 -07:00
var anyFound = false;
var out = false;
// TODO: This out construct assumes left to right, top to bottom
2015-10-28 10:09:55 -07:00
for (var i = 0, length = images.length; i < length; i++) {
2016-01-13 14:22:46 -07:00
if (cancellationTokens[tokenIndex]) {
return;
}
2015-10-28 10:09:55 -07:00
var img = images[i];
2016-01-13 14:22:46 -07:00
if (!out && isVisible(img)) {
anyFound = true;
2015-10-28 10:09:55 -07:00
fillImage(img);
} else {
2016-01-13 14:22:46 -07:00
if (anyFound) {
out = true;
}
2015-10-28 10:09:55 -07:00
remaining.push(img);
}
}
2015-10-28 10:09:55 -07:00
images = remaining;
2015-10-28 10:09:55 -07:00
if (!images.length) {
2016-01-13 14:22:46 -07:00
document.removeEventListener('focus', unveil, true);
2016-01-10 10:37:02 -07:00
document.removeEventListener('scroll', unveil, true);
document.removeEventListener(wheelEvent, unveil, true);
window.removeEventListener('resize', unveil, true);
}
}
2016-01-13 14:22:46 -07:00
function unveil() {
cancelAll(cancellationTokens);
var index = cancellationTokens.length;
cancellationTokens.length++;
setTimeout(function () {
unveilInternal(index);
}, 1);
}
2015-12-14 08:43:03 -07:00
document.addEventListener('scroll', unveil, true);
2016-01-13 14:22:46 -07:00
document.addEventListener('focus', unveil, true);
2015-12-14 08:43:03 -07:00
document.addEventListener(wheelEvent, unveil, true);
window.addEventListener('resize', unveil, true);
2015-05-09 21:29:04 -07:00
2015-10-28 10:09:55 -07:00
unveil();
}
2015-05-11 09:32:15 -07:00
2015-10-28 10:09:55 -07:00
function fillImages(elems) {
2015-05-11 09:32:15 -07:00
2015-10-28 10:09:55 -07:00
for (var i = 0, length = elems.length; i < length; i++) {
var elem = elems[0];
2016-01-16 11:29:08 -07:00
fillImage(elem);
2015-05-11 09:32:15 -07:00
}
2015-10-28 10:09:55 -07:00
}
2015-10-12 12:09:56 -07:00
2015-10-28 10:09:55 -07:00
function lazyChildren(elem) {
2015-10-28 10:09:55 -07:00
unveilElements(elem.getElementsByClassName('lazy'), elem);
}
2015-10-28 10:09:55 -07:00
function lazyImage(elem, url) {
2015-10-28 10:09:55 -07:00
elem.setAttribute('data-src', url);
fillImages([elem]);
}
2016-01-12 12:55:45 -07:00
window.ImageLoader = {
fillImages: fillImages,
lazyImage: lazyImage,
lazyChildren: lazyChildren
};
});