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

233 lines
6.6 KiB
JavaScript
Raw Normal View History

2015-10-28 10:09:55 -07:00
(function ($) {
})(jQuery);
/**
* jQuery Unveil
* A very lightweight jQuery plugin to lazy load images
* http://luis-almeida.github.com/unveil
*
* Licensed under the MIT license.
* Copyright 2013 Luís Almeida
* https://github.com/luis-almeida
2015-06-28 07:45:21 -07:00
*/
2015-10-28 10:09:55 -07:00
(function ($) {
/**
* Copyright 2012, Digital Fusion
* Licensed under the MIT license.
* http://teamdf.com/jquery-plugins/license/
*
* @author Sam Sehnert
* @desc A small plugin that checks whether elements are within
* the user visible viewport of a web browser.
* only accounts for vertical position, not horizontal.
*/
var $w = $(window);
var thresholdX = Math.max(screen.availWidth);
var thresholdY = Math.max(screen.availHeight);
function visibleInViewport(elem, partial, hidden, direction) {
var t = elem,
vpWidth = $w.width(),
vpHeight = $w.height(),
direction = (direction) ? direction : 'both',
clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true;
if (typeof t.getBoundingClientRect === 'function') {
// Use this native browser method, if available.
var rec = t.getBoundingClientRect(),
tViz = rec.top >= 0 && rec.top < vpHeight + thresholdY,
bViz = rec.bottom > 0 && rec.bottom <= vpHeight + thresholdY,
lViz = rec.left >= 0 && rec.left < vpWidth + thresholdX,
rViz = rec.right > 0 && rec.right <= vpWidth + thresholdX,
vVisible = partial ? tViz || bViz : tViz && bViz,
hVisible = partial ? lViz || rViz : lViz && rViz;
if (direction === 'both')
return clientSize && vVisible && hVisible;
else if (direction === 'vertical')
return clientSize && vVisible;
else if (direction === 'horizontal')
return clientSize && hVisible;
} else {
2015-10-28 10:09:55 -07:00
var $t = $(elem);
var viewTop = $w.scrollTop(),
viewBottom = viewTop + vpHeight,
viewLeft = $w.scrollLeft(),
viewRight = viewLeft + vpWidth,
offset = $t.offset(),
_top = offset.top,
_bottom = _top + $t.height(),
_left = offset.left,
_right = _left + $t.width(),
compareTop = partial === true ? _bottom : _top,
compareBottom = partial === true ? _top : _bottom,
compareLeft = partial === true ? _right : _left,
compareRight = partial === true ? _left : _right;
if (direction === 'both')
return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop)) && ((compareRight <= viewRight) && (compareLeft >= viewLeft));
else if (direction === 'vertical')
return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop));
else if (direction === 'horizontal')
return !!clientSize && ((compareRight <= viewRight) && (compareLeft >= viewLeft));
}
2015-10-28 10:09:55 -07:00
}
2015-10-28 10:09:55 -07:00
var unveilId = 0;
2015-10-28 10:09:55 -07:00
function isVisible(elem) {
return visibleInViewport(elem, true, false, 'both');
}
2015-10-28 10:09:55 -07:00
function fillImage(elem) {
var source = elem.getAttribute('data-src');
if (source) {
ImageStore.setImageInto(elem, source);
elem.setAttribute("data-src", '');
}
}
2015-10-28 10:09:55 -07:00
function unveilElements(elems, parent) {
2015-10-28 10:09:55 -07:00
if (!elems.length) {
return;
}
2015-10-28 10:09:55 -07:00
var images = elems;
2015-10-28 10:09:55 -07:00
unveilId++;
2015-10-28 10:09:55 -07:00
var parents = [];
if (parent) {
parents = parent.querySelectorAll('.itemsContainer');
if (!parents.length) {
parents = [parent];
2015-06-28 07:45:21 -07:00
}
2015-10-28 10:09:55 -07:00
}
2015-01-22 23:15:15 -07:00
2015-10-28 10:09:55 -07:00
function unveil() {
2015-10-28 10:09:55 -07:00
var remaining = [];
2015-10-28 10:09:55 -07:00
for (var i = 0, length = images.length; i < length; i++) {
var img = images[i];
if (isVisible(img)) {
fillImage(img);
} else {
remaining.push(img);
}
}
2015-10-28 10:09:55 -07:00
images = remaining;
2015-10-28 10:09:55 -07:00
if (!images.length) {
2015-11-27 12:54:01 -07:00
document.removeEventListener('scroll', unveil);
window.removeEventListener('resize', unveil);
bindEvent(parents, 'removeEventListener', 'scroll', unveil);
}
}
2015-11-27 12:54:01 -07:00
document.addEventListener('scroll', unveil);
window.addEventListener('resize', unveil);
2015-05-09 21:29:04 -07:00
2015-10-28 10:09:55 -07:00
if (parents.length) {
2015-11-27 12:54:01 -07:00
bindEvent(parents, 'addEventListener', 'scroll', unveil);
}
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-11-27 12:54:01 -07:00
function bindEvent(elems, method, name, fn) {
for (var i = 0, length = elems.length; i < length; i++) {
elems[i][method](name, fn);
}
}
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];
var source = elem.getAttribute('data-src');
if (source) {
ImageStore.setImageInto(elem, source);
elem.setAttribute("data-src", '');
}
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
$.fn.lazyChildren = function () {
2015-10-12 12:09:56 -07:00
2015-10-28 10:09:55 -07:00
if (this.length == 1) {
lazyChildren(this[0]);
} else {
unveilElements($('.lazy', this));
}
2015-10-28 10:09:55 -07:00
return this;
};
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]);
}
2015-10-28 10:09:55 -07:00
window.ImageLoader = {
fillImages: fillImages,
lazyImage: lazyImage,
lazyChildren: lazyChildren
};
2015-10-28 10:09:55 -07:00
})(window.jQuery || window.Zepto);
2015-10-28 10:09:55 -07:00
(function () {
2015-10-28 10:09:55 -07:00
function setImageIntoElement(elem, url) {
2015-10-28 10:09:55 -07:00
if (elem.tagName !== "IMG") {
2015-10-28 10:09:55 -07:00
elem.style.backgroundImage = "url('" + url + "')";
2015-10-28 10:09:55 -07:00
} else {
elem.setAttribute("src", url);
}
2015-11-28 13:48:35 -07:00
if (browserInfo.chrome && !browserInfo.mobile) {
2015-10-28 10:09:55 -07:00
if (!elem.classList.contains('noFade')) {
fadeIn(elem, 1);
}
2015-10-28 10:09:55 -07:00
}
}
2015-10-28 10:09:55 -07:00
function fadeIn(elem, iterations) {
2015-10-12 12:09:56 -07:00
2015-10-28 10:09:55 -07:00
var keyframes = [
{ opacity: '0', offset: 0 },
{ opacity: '1', offset: 1 }];
var timing = { duration: 200, iterations: iterations };
return elem.animate(keyframes, timing);
}
2015-05-11 09:32:15 -07:00
2015-10-28 10:09:55 -07:00
function simpleImageStore() {
2015-05-09 21:29:04 -07:00
2015-10-28 10:09:55 -07:00
var self = this;
2015-05-09 21:29:04 -07:00
2015-10-28 10:09:55 -07:00
self.setImageInto = setImageIntoElement;
}
2015-05-09 21:29:04 -07:00
2015-10-28 10:09:55 -07:00
console.log('creating simpleImageStore');
window.ImageStore = new simpleImageStore();
2015-05-12 21:55:19 -07:00
2015-10-28 10:09:55 -07:00
})();