jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/images/imagehelper.js

279 lines
7.3 KiB
JavaScript
Raw Normal View History

2016-08-07 12:43:52 -07:00
define(['visibleinviewport', 'imageFetcher', 'layoutManager', 'events', 'browser', 'dom'], function (visibleinviewport, imageFetcher, layoutManager, events, browser, dom) {
2016-01-16 11:29:08 -07:00
2016-05-08 20:13:38 -07:00
var thresholdX;
var thresholdY;
2016-05-28 11:03:38 -07:00
var supportsIntersectionObserver = function () {
if (window.IntersectionObserver) {
// The api exists in chrome 50 but doesn't work
if (browser.chrome) {
var version = parseInt(browser.version.split('.')[0]);
return version >= 51;
}
return true;
}
return false;
}();
2016-05-08 20:13:38 -07:00
function resetThresholds() {
var x = screen.availWidth;
var y = screen.availHeight;
2016-08-11 20:23:12 -07:00
if (browser.touch) {
2016-05-08 20:13:38 -07:00
x *= 2;
y *= 2;
}
thresholdX = x;
thresholdY = y;
}
2016-05-28 11:03:38 -07:00
if (!supportsIntersectionObserver) {
2016-08-11 20:23:12 -07:00
dom.addEventListener(window, "orientationchange", resetThresholds, { passive: true });
dom.addEventListener(window, 'resize', resetThresholds, { passive: true });
2016-05-28 11:03:38 -07:00
resetThresholds();
2016-05-15 18:22:22 -07:00
}
function isVisible(elem) {
2016-08-11 20:23:12 -07:00
return visibleinviewport(elem, true, thresholdX, thresholdY);
2016-01-16 11:29:08 -07:00
}
2016-05-28 11:03:38 -07:00
var wheelEvent = (document.implementation.hasFeature('Event.wheel', '3.0') ? 'wheel' : 'mousewheel');
2016-01-20 18:05:14 -07:00
var self = {};
2016-08-06 07:07:44 -07:00
var enableFade = browser.animate && !browser.slow;
2016-06-04 10:14:03 -07:00
2016-05-15 18:22:22 -07:00
function fillImage(elem, source, enableEffects) {
if (!source) {
source = elem.getAttribute('data-src');
}
2016-01-16 11:29:08 -07:00
if (source) {
2016-06-04 10:14:03 -07:00
if (enableFade && !layoutManager.tv && enableEffects !== false) {
2016-01-20 18:05:14 -07:00
imageFetcher.loadImage(elem, source).then(fadeIn);
} else {
imageFetcher.loadImage(elem, source);
}
2016-07-31 22:09:09 -07:00
elem.removeAttribute("data-src");
2016-01-16 11:29:08 -07:00
}
}
2016-01-20 18:05:14 -07:00
function fadeIn(elem) {
2016-06-01 11:04:22 -07:00
var duration = layoutManager.tv ? 160 : 300;
2016-05-29 19:15:32 -07:00
2016-01-20 18:05:14 -07:00
var keyframes = [
{ opacity: '0', offset: 0 },
{ opacity: '1', offset: 1 }];
2016-05-29 19:15:32 -07:00
var timing = { duration: duration, iterations: 1 };
2016-01-20 18:05:14 -07:00
elem.animate(keyframes, timing);
}
2016-01-16 11:29:08 -07:00
function cancelAll(tokens) {
for (var i = 0, length = tokens.length; i < length; i++) {
tokens[i] = true;
}
}
2016-06-04 20:50:07 -07:00
function unveilWithIntersection(images, root) {
2016-05-19 07:37:59 -07:00
var filledCount = 0;
2016-05-28 11:03:38 -07:00
var options = {};
2016-06-04 20:50:07 -07:00
//options.rootMargin = "300%";
2016-05-28 11:03:38 -07:00
2016-05-19 07:37:59 -07:00
var observer = new IntersectionObserver(function (entries) {
for (var j = 0, length2 = entries.length; j < length2; j++) {
var entry = entries[j];
2016-06-04 20:50:07 -07:00
var target = entry.target;
observer.unobserve(target);
fillImage(target);
filledCount++;
2016-05-19 07:37:59 -07:00
}
},
2016-05-28 11:03:38 -07:00
options
2016-05-19 07:37:59 -07:00
);
// Start observing an element
for (var i = 0, length = images.length; i < length; i++) {
observer.observe(images[i]);
}
}
2016-06-04 20:50:07 -07:00
function unveilElements(images, root) {
2016-01-16 11:29:08 -07:00
if (!images.length) {
return;
}
2016-05-19 10:27:39 -07:00
if (supportsIntersectionObserver) {
2016-06-04 20:50:07 -07:00
unveilWithIntersection(images, root);
2016-05-19 07:37:59 -07:00
return;
}
2016-05-17 13:04:20 -07:00
var filledImages = [];
2016-01-16 11:29:08 -07:00
var cancellationTokens = [];
2016-05-17 13:04:20 -07:00
2016-01-16 11:29:08 -07:00
function unveilInternal(tokenIndex) {
var anyFound = false;
var out = false;
// TODO: This out construct assumes left to right, top to bottom
for (var i = 0, length = images.length; i < length; i++) {
if (cancellationTokens[tokenIndex]) {
return;
}
2016-05-17 13:04:20 -07:00
if (filledImages[i]) {
continue;
}
2016-01-16 11:29:08 -07:00
var img = images[i];
2016-05-15 18:22:22 -07:00
if (!out && isVisible(img)) {
2016-01-16 11:29:08 -07:00
anyFound = true;
2016-05-17 13:04:20 -07:00
filledImages[i] = true;
2016-01-16 11:29:08 -07:00
fillImage(img);
} else {
if (anyFound) {
out = true;
}
2016-05-15 18:22:22 -07:00
}
2016-01-16 11:29:08 -07:00
}
if (!images.length) {
2016-08-07 12:43:52 -07:00
dom.removeEventListener(document, 'focus', unveil, {
2016-07-08 11:10:20 -07:00
capture: true,
passive: true
});
2016-08-07 12:43:52 -07:00
dom.removeEventListener(document, 'scroll', unveil, {
2016-07-08 11:10:20 -07:00
capture: true,
passive: true
});
2016-08-07 12:43:52 -07:00
dom.removeEventListener(document, wheelEvent, unveil, {
2016-07-08 11:10:20 -07:00
capture: true,
passive: true
});
2016-08-07 12:43:52 -07:00
dom.removeEventListener(window, 'resize', unveil, {
2016-07-08 11:10:20 -07:00
capture: true,
passive: true
});
2016-01-16 11:29:08 -07:00
}
}
function unveil() {
cancelAll(cancellationTokens);
var index = cancellationTokens.length;
cancellationTokens.length++;
setTimeout(function () {
unveilInternal(index);
}, 1);
}
2016-08-07 12:43:52 -07:00
dom.addEventListener(document, 'focus', unveil, {
2016-07-08 11:10:20 -07:00
capture: true,
passive: true
});
2016-08-07 12:43:52 -07:00
dom.addEventListener(document, 'scroll', unveil, {
2016-05-04 21:18:35 -07:00
capture: true,
passive: true
});
2016-08-07 12:43:52 -07:00
dom.addEventListener(document, wheelEvent, unveil, {
2016-05-04 21:18:35 -07:00
capture: true,
passive: true
});
2016-08-07 12:43:52 -07:00
dom.addEventListener(window, 'resize', unveil, {
2016-05-04 21:18:35 -07:00
capture: true,
passive: true
});
2016-01-16 11:29:08 -07:00
unveil();
}
function lazyChildren(elem) {
2016-06-04 20:50:07 -07:00
unveilElements(elem.getElementsByClassName('lazy'), elem);
2016-01-16 11:29:08 -07:00
}
function getPrimaryImageAspectRatio(items) {
var values = [];
for (var i = 0, length = items.length; i < length; i++) {
var ratio = items[i].PrimaryImageAspectRatio || 0;
if (!ratio) {
continue;
}
values[values.length] = ratio;
}
if (!values.length) {
return null;
}
// Use the median
values.sort(function (a, b) { return a - b; });
var half = Math.floor(values.length / 2);
var result;
if (values.length % 2)
result = values[half];
else
result = (values[half - 1] + values[half]) / 2.0;
// If really close to 2:3 (poster image), just return 2:3
var aspect2x3 = 2 / 3;
if (Math.abs(aspect2x3 - result) <= .15) {
return aspect2x3;
}
// If really close to 16:9 (episode image), just return 16:9
var aspect16x9 = 16 / 9;
if (Math.abs(aspect16x9 - result) <= .2) {
return aspect16x9;
}
// If really close to 1 (square image), just return 1
if (Math.abs(1 - result) <= .15) {
return 1;
}
// If really close to 4:3 (poster image), just return 2:3
var aspect4x3 = 4 / 3;
if (Math.abs(aspect4x3 - result) <= .15) {
return aspect4x3;
}
return result;
}
2016-01-20 18:05:14 -07:00
function fillImages(elems) {
for (var i = 0, length = elems.length; i < length; i++) {
var elem = elems[0];
fillImage(elem);
}
}
self.fillImages = fillImages;
2016-05-15 18:22:22 -07:00
self.lazyImage = fillImage;
2016-01-20 18:05:14 -07:00
self.lazyChildren = lazyChildren;
self.getPrimaryImageAspectRatio = getPrimaryImageAspectRatio;
2016-01-16 11:29:08 -07:00
2016-01-20 18:05:14 -07:00
return self;
2016-01-16 11:29:08 -07:00
});