1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/thirdparty/jquery.unveil-custom.js

193 lines
5.5 KiB
JavaScript
Raw Normal View History

2015-06-27 23:29:50 -04: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);
$.fn.visibleInViewport = function (partial, hidden, direction, threshold) {
if (this.length < 1)
return;
var $t = this.length > 1 ? this.eq(0) : this,
t = $t.get(0),
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 + threshold,
bViz = rec.bottom > 0 && rec.bottom <= vpHeight + threshold,
lViz = rec.left >= 0 && rec.left < vpWidth + threshold,
rViz = rec.right > 0 && rec.right <= vpWidth + threshold,
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 {
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));
}
};
})(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-05-10 00:29:04 -04:00
(function ($) {
var unveilId = 0;
2015-05-13 00:55:19 -04:00
function getThreshold() {
2015-05-28 01:51:48 -04:00
// If less than 100, the search window ends up not getting images
// If less than 200, this happens on the home page
// Need to fix those before this can be set to 0
2015-06-10 23:01:41 -04:00
2015-06-16 00:52:01 -04:00
if (window.AppInfo && AppInfo.isNativeApp && $.browser.safari) {
2015-06-22 11:43:19 -04:00
return 7000;
2015-06-16 00:52:01 -04:00
}
2015-06-20 00:48:45 -04:00
var screens = $.browser.mobile ? 2.5 : 1;
2015-06-10 23:01:41 -04:00
2015-06-15 00:17:12 -04:00
// This helps eliminate the draw-in effect as you scroll
2015-06-16 00:52:01 -04:00
return Math.max(screen.availHeight * screens, 1000);
2015-05-13 00:55:19 -04:00
}
2015-05-11 12:32:15 -04:00
2015-06-27 23:29:50 -04:00
var threshold = getThreshold();
function isVisible() {
return $(this).visibleInViewport(true, false, 'both', threshold);
}
function fillImage() {
var elem = this;
var source = elem.getAttribute('data-src');
if (source) {
ImageStore.setImageInto(elem, source);
elem.setAttribute("data-src", '');
}
}
2015-05-11 12:32:15 -04:00
$.fn.unveil = function () {
var $w = $(window),
images = this,
loaded;
2015-05-10 00:29:04 -04:00
unveilId++;
var eventNamespace = 'unveil' + unveilId;
2015-06-27 23:29:50 -04:00
this.one("unveil", fillImage);
function unveil() {
2015-06-27 23:29:50 -04:00
var inview = images.filter(isVisible);
loaded = inview.trigger("unveil");
images = images.not(loaded);
2015-05-10 00:29:04 -04:00
if (!images.length) {
$w.off('scroll.' + eventNamespace);
$w.off('resize.' + eventNamespace);
}
}
2015-05-10 00:29:04 -04:00
$w.on('scroll.' + eventNamespace, unveil);
$w.on('resize.' + eventNamespace, unveil);
unveil();
return this;
};
2015-06-27 23:29:50 -04:00
$.fn.fillImages = function () {
return this.each(fillImage);
};
2015-01-23 01:15:15 -05:00
$.fn.lazyChildren = function () {
2015-05-10 00:29:04 -04:00
var lazyItems = $(".lazy", this);
if (lazyItems.length) {
2015-05-11 12:32:15 -04:00
lazyItems.unveil();
2015-05-10 00:29:04 -04:00
}
2015-01-23 01:15:15 -05:00
return this;
};
2015-05-10 00:29:04 -04:00
$.fn.lazyImage = function (url) {
2015-06-27 23:29:50 -04:00
return this.attr('data-src', url).fillImages();
2015-05-10 00:29:04 -04:00
};
})(window.jQuery || window.Zepto);
(function () {
2015-05-11 12:32:15 -04:00
function setImageIntoElement(elem, url) {
2015-06-20 20:49:42 -04:00
if (elem.tagName !== "IMG") {
2015-05-11 12:32:15 -04:00
elem.style.backgroundImage = "url('" + url + "')";
} else {
elem.setAttribute("src", url);
}
}
2015-05-10 00:29:04 -04:00
function simpleImageStore() {
var self = this;
2015-05-11 12:32:15 -04:00
self.setImageInto = setImageIntoElement;
2015-05-10 00:29:04 -04:00
}
2015-05-13 00:55:19 -04:00
console.log('creating simpleImageStore');
window.ImageStore = new simpleImageStore();
2015-05-10 00:29:04 -04:00
})();