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/bower_components/emby-webcomponents/images/imagehelper.js

273 lines
7.1 KiB
JavaScript
Raw Normal View History

2016-08-07 15:43:52 -04:00
define(['visibleinviewport', 'imageFetcher', 'layoutManager', 'events', 'browser', 'dom'], function (visibleinviewport, imageFetcher, layoutManager, events, browser, dom) {
2016-01-16 13:29:08 -05:00
2016-05-08 23:13:38 -04:00
var thresholdX;
var thresholdY;
2016-05-28 14:03:38 -04:00
var supportsIntersectionObserver = function () {
if (window.IntersectionObserver) {
return true;
}
return false;
}();
2016-05-08 23:13:38 -04:00
function resetThresholds() {
var x = screen.availWidth;
var y = screen.availHeight;
2016-08-11 23:23:12 -04:00
if (browser.touch) {
2016-05-08 23:13:38 -04:00
x *= 2;
y *= 2;
}
thresholdX = x;
thresholdY = y;
}
2016-05-28 14:03:38 -04:00
if (!supportsIntersectionObserver) {
2016-08-11 23:23:12 -04:00
dom.addEventListener(window, "orientationchange", resetThresholds, { passive: true });
dom.addEventListener(window, 'resize', resetThresholds, { passive: true });
2016-05-28 14:03:38 -04:00
resetThresholds();
2016-05-15 21:22:22 -04:00
}
function isVisible(elem) {
2016-08-11 23:23:12 -04:00
return visibleinviewport(elem, true, thresholdX, thresholdY);
2016-01-16 13:29:08 -05:00
}
2016-05-28 14:03:38 -04:00
var wheelEvent = (document.implementation.hasFeature('Event.wheel', '3.0') ? 'wheel' : 'mousewheel');
2016-01-20 20:05:14 -05:00
var self = {};
2016-08-06 10:07:44 -04:00
var enableFade = browser.animate && !browser.slow;
2016-06-04 13:14:03 -04:00
2016-05-15 21:22:22 -04:00
function fillImage(elem, source, enableEffects) {
if (!source) {
source = elem.getAttribute('data-src');
}
2016-01-16 13:29:08 -05:00
if (source) {
2016-06-04 13:14:03 -04:00
if (enableFade && !layoutManager.tv && enableEffects !== false) {
2016-01-20 20:05:14 -05:00
imageFetcher.loadImage(elem, source).then(fadeIn);
} else {
imageFetcher.loadImage(elem, source);
}
2016-08-01 01:09:09 -04:00
elem.removeAttribute("data-src");
2016-01-16 13:29:08 -05:00
}
}
2016-01-20 20:05:14 -05:00
function fadeIn(elem) {
2016-06-01 14:04:22 -04:00
var duration = layoutManager.tv ? 160 : 300;
2016-05-29 22:15:32 -04:00
2016-01-20 20:05:14 -05:00
var keyframes = [
{ opacity: '0', offset: 0 },
{ opacity: '1', offset: 1 }];
2016-05-29 22:15:32 -04:00
var timing = { duration: duration, iterations: 1 };
2016-01-20 20:05:14 -05:00
elem.animate(keyframes, timing);
}
2016-01-16 13:29:08 -05:00
function cancelAll(tokens) {
for (var i = 0, length = tokens.length; i < length; i++) {
tokens[i] = true;
}
}
2016-06-04 23:50:07 -04:00
function unveilWithIntersection(images, root) {
2016-05-19 10:37:59 -04:00
var filledCount = 0;
2016-05-28 14:03:38 -04:00
var options = {};
2016-06-04 23:50:07 -04:00
//options.rootMargin = "300%";
2016-05-28 14:03:38 -04:00
2016-05-19 10:37:59 -04:00
var observer = new IntersectionObserver(function (entries) {
for (var j = 0, length2 = entries.length; j < length2; j++) {
var entry = entries[j];
2016-06-04 23:50:07 -04:00
var target = entry.target;
observer.unobserve(target);
fillImage(target);
filledCount++;
2016-05-19 10:37:59 -04:00
}
},
2016-05-28 14:03:38 -04:00
options
2016-05-19 10:37:59 -04:00
);
// Start observing an element
for (var i = 0, length = images.length; i < length; i++) {
observer.observe(images[i]);
}
}
2016-06-04 23:50:07 -04:00
function unveilElements(images, root) {
2016-01-16 13:29:08 -05:00
if (!images.length) {
return;
}
2016-05-19 13:27:39 -04:00
if (supportsIntersectionObserver) {
2016-06-04 23:50:07 -04:00
unveilWithIntersection(images, root);
2016-05-19 10:37:59 -04:00
return;
}
2016-05-17 16:04:20 -04:00
var filledImages = [];
2016-01-16 13:29:08 -05:00
var cancellationTokens = [];
2016-05-17 16:04:20 -04:00
2016-01-16 13:29:08 -05: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 16:04:20 -04:00
if (filledImages[i]) {
continue;
}
2016-01-16 13:29:08 -05:00
var img = images[i];
2016-05-15 21:22:22 -04:00
if (!out && isVisible(img)) {
2016-01-16 13:29:08 -05:00
anyFound = true;
2016-05-17 16:04:20 -04:00
filledImages[i] = true;
2016-01-16 13:29:08 -05:00
fillImage(img);
} else {
if (anyFound) {
out = true;
}
2016-05-15 21:22:22 -04:00
}
2016-01-16 13:29:08 -05:00
}
if (!images.length) {
2016-08-07 15:43:52 -04:00
dom.removeEventListener(document, 'focus', unveil, {
2016-07-08 14:10:20 -04:00
capture: true,
passive: true
});
2016-08-07 15:43:52 -04:00
dom.removeEventListener(document, 'scroll', unveil, {
2016-07-08 14:10:20 -04:00
capture: true,
passive: true
});
2016-08-07 15:43:52 -04:00
dom.removeEventListener(document, wheelEvent, unveil, {
2016-07-08 14:10:20 -04:00
capture: true,
passive: true
});
2016-08-07 15:43:52 -04:00
dom.removeEventListener(window, 'resize', unveil, {
2016-07-08 14:10:20 -04:00
capture: true,
passive: true
});
2016-01-16 13:29:08 -05:00
}
}
function unveil() {
cancelAll(cancellationTokens);
var index = cancellationTokens.length;
cancellationTokens.length++;
setTimeout(function () {
unveilInternal(index);
}, 1);
}
2016-08-07 15:43:52 -04:00
dom.addEventListener(document, 'focus', unveil, {
2016-07-08 14:10:20 -04:00
capture: true,
passive: true
});
2016-08-07 15:43:52 -04:00
dom.addEventListener(document, 'scroll', unveil, {
2016-05-05 00:18:35 -04:00
capture: true,
passive: true
});
2016-08-07 15:43:52 -04:00
dom.addEventListener(document, wheelEvent, unveil, {
2016-05-05 00:18:35 -04:00
capture: true,
passive: true
});
2016-08-07 15:43:52 -04:00
dom.addEventListener(window, 'resize', unveil, {
2016-05-05 00:18:35 -04:00
capture: true,
passive: true
});
2016-01-16 13:29:08 -05:00
unveil();
}
function lazyChildren(elem) {
2016-06-04 23:50:07 -04:00
unveilElements(elem.getElementsByClassName('lazy'), elem);
2016-01-16 13:29:08 -05: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 20:05:14 -05: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 21:22:22 -04:00
self.lazyImage = fillImage;
2016-01-20 20:05:14 -05:00
self.lazyChildren = lazyChildren;
self.getPrimaryImageAspectRatio = getPrimaryImageAspectRatio;
2016-01-16 13:29:08 -05:00
2016-01-20 20:05:14 -05:00
return self;
2016-01-16 13:29:08 -05:00
});