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

443 lines
12 KiB
JavaScript
Raw Normal View History

2016-10-12 11:23:09 -07:00
define(['visibleinviewport', 'imageFetcher', 'layoutManager', 'events', 'browser', 'dom', 'appSettings', 'require'], function (visibleinviewport, imageFetcher, layoutManager, events, browser, dom, appSettings, require) {
'use strict';
2016-01-16 11:29:08 -07:00
2016-05-08 20:13:38 -07:00
var thresholdX;
var thresholdY;
2016-10-12 11:23:09 -07:00
var requestIdleCallback = window.requestIdleCallback || function (fn) {
fn();
};
//var imagesWorker = new Worker(require.toUrl('.').split('?')[0] + '/imagesworker.js');
2016-05-28 11:03:38 -07:00
var supportsIntersectionObserver = function () {
if (window.IntersectionObserver) {
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-10-12 11:23:09 -07:00
x *= 1.5;
y *= 1.5;
2016-05-08 20:13:38 -07:00
}
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-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
if (!source) {
return;
}
2016-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
fillImageElement(elem, source, enableEffects);
}
2016-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
function fillImageElement(elem, source, enableEffects) {
imageFetcher.loadImage(elem, source).then(function () {
2016-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
var fillingVibrant = elem.tagName !== 'IMG' ? false : fillVibrant(elem, source);
2016-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
if (enableFade && !layoutManager.tv && enableEffects !== false && !fillingVibrant) {
fadeIn(elem);
}
elem.removeAttribute("data-src");
});
2016-10-05 00:15:29 -07:00
}
2016-10-12 11:23:09 -07:00
//var placeholder = document.createElement('div');
//imagesWorker.onmessage = function (evt) {
// placeholder.dispatchEvent(new CustomEvent('decoded', {
// detail: evt.data,
// bubbles: false,
// cancellable: false
// }));
//};
2016-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
//var uniqueId = 0;
//function fillCanvas(elem, source) {
// var newUniqueId = (++uniqueId);
// imagesWorker.postMessage({
// url: source,
// id: newUniqueId
// });
// placeholder.addEventListener('decoded', function (e) {
// if (e.detail.id == newUniqueId) {
// var imageBitmap = e.detail.imageBitmap;
// var canvas = document.createElement('canvas');
// var canvasContext = canvas.getContext('2d');
// //drawWidth *= ratio;
// //drawHeight *= ratio;
// // https://stackoverflow.com/questions/21961839/simulation-background-size-cover-in-canvas/21961894#21961894
// canvasContext.imageSmoothingEnabled = false;
// var width = canvas.width = elem.offsetWidth;
// var height = canvas.height = elem.offsetHeight;
// canvasContext.drawImage(imageBitmap, 0, 0, imageBitmap.width, imageBitmap.height, 0, 0, width, height);
// fillVibrant(elem, source, canvas, canvasContext);
// elem.insertBefore(canvas, elem.firstChild);
// elem.removeAttribute("data-src");
// }
// });
//}
function fillVibrant(img, url, canvas, canvasContext) {
2016-10-05 00:15:29 -07:00
var vibrantElement = img.getAttribute('data-vibrant');
if (!vibrantElement) {
2016-10-07 08:08:13 -07:00
return false;
2016-01-16 11:29:08 -07:00
}
2016-10-05 00:15:29 -07:00
2016-10-05 21:28:10 -07:00
if (window.Vibrant) {
2016-10-12 11:23:09 -07:00
fillVibrantOnLoaded(img, url, vibrantElement, canvas, canvasContext);
2016-10-07 08:08:13 -07:00
return true;
2016-10-05 21:28:10 -07:00
}
require(['vibrant'], function () {
2016-10-12 11:23:09 -07:00
fillVibrantOnLoaded(img, url, vibrantElement, canvas, canvasContext);
2016-10-05 21:28:10 -07:00
});
2016-10-07 08:08:13 -07:00
return true;
2016-10-05 21:28:10 -07:00
}
2016-10-13 14:13:30 -07:00
function fillVibrantOnLoaded(img, url, vibrantElement) {
2016-10-05 21:28:10 -07:00
2016-10-05 00:15:29 -07:00
vibrantElement = document.getElementById(vibrantElement);
if (!vibrantElement) {
return;
}
2016-10-12 11:23:09 -07:00
requestIdleCallback(function () {
2016-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
//var now = new Date().getTime();
2016-10-13 14:13:30 -07:00
var swatch = getVibrantInfo(img, url).split('|');
2016-10-12 11:23:09 -07:00
//console.log('vibrant took ' + (new Date().getTime() - now) + 'ms');
if (swatch.length) {
var index = 0;
vibrantElement.style.backgroundColor = swatch[index];
vibrantElement.style.color = swatch[index + 1];
}
});
2016-10-05 00:15:29 -07:00
/*
* Results into:
* Vibrant #7a4426
* Muted #7b9eae
* DarkVibrant #348945
* DarkMuted #141414
* LightVibrant #f3ccb4
*/
}
function getSettingsKey(url) {
2016-10-05 21:28:10 -07:00
var parts = url.split('://');
url = parts[parts.length - 1];
url = url.substring(url.indexOf('/') + 1);
url = url.split('?')[0];
2016-10-13 14:13:30 -07:00
var cacheKey = 'vibrant21';
2016-10-12 11:23:09 -07:00
return cacheKey + url;
2016-10-05 00:15:29 -07:00
}
function getCachedVibrantInfo(url) {
return appSettings.get(getSettingsKey(url));
}
2016-10-13 14:13:30 -07:00
function getVibrantInfo(img, url) {
2016-10-05 00:15:29 -07:00
var value = getCachedVibrantInfo(url);
if (value) {
return value;
}
2016-10-13 14:13:30 -07:00
var vibrant = new Vibrant(img);
2016-10-05 00:15:29 -07:00
var swatches = vibrant.swatches();
value = '';
2016-10-12 11:23:09 -07:00
var swatch = swatches.DarkVibrant;
2016-10-05 00:15:29 -07:00
if (swatch) {
value += swatch.getHex() + '|' + swatch.getBodyTextColor();
}
2016-10-12 11:50:48 -07:00
//swatch = swatches.DarkMuted;
//if (swatch) {
// value += '|' + swatch.getHex() + '|' + swatch.getBodyTextColor();
//} else {
// value += '||';
//}
//swatch = swatches.Vibrant;
//if (swatch) {
// value += '|' + swatch.getHex() + '|' + swatch.getBodyTextColor();
//} else {
// value += '||';
//}
//swatch = swatches.Muted;
//if (swatch) {
// value += '|' + swatch.getHex() + '|' + swatch.getBodyTextColor();
//} else {
// value += '||';
//}
2016-10-05 00:15:29 -07:00
2016-10-12 11:23:09 -07:00
appSettings.set(getSettingsKey(url), value);
2016-10-05 00:15:29 -07:00
return value;
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;
2016-10-12 11:23:09 -07:00
if (values.length % 2) {
2016-01-16 11:29:08 -07:00
result = values[half];
2016-10-12 11:23:09 -07:00
}
else {
2016-01-16 11:29:08 -07:00
result = (values[half - 1] + values[half]) / 2.0;
2016-10-12 11:23:09 -07:00
}
2016-01-16 11:29:08 -07:00
// If really close to 2:3 (poster image), just return 2:3
var aspect2x3 = 2 / 3;
2016-10-12 11:23:09 -07:00
if (Math.abs(aspect2x3 - result) <= 0.15) {
2016-01-16 11:29:08 -07:00
return aspect2x3;
}
// If really close to 16:9 (episode image), just return 16:9
var aspect16x9 = 16 / 9;
2016-10-12 11:23:09 -07:00
if (Math.abs(aspect16x9 - result) <= 0.2) {
2016-01-16 11:29:08 -07:00
return aspect16x9;
}
// If really close to 1 (square image), just return 1
2016-10-12 11:23:09 -07:00
if (Math.abs(1 - result) <= 0.15) {
2016-01-16 11:29:08 -07:00
return 1;
}
// If really close to 4:3 (poster image), just return 2:3
var aspect4x3 = 4 / 3;
2016-10-12 11:23:09 -07:00
if (Math.abs(aspect4x3 - result) <= 0.15) {
2016-01-16 11:29:08 -07:00
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-10-05 00:15:29 -07:00
self.getCachedVibrantInfo = getCachedVibrantInfo;
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
});