jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/cardbuilder/cardbuilder.js

1632 lines
60 KiB
JavaScript
Raw Normal View History

2016-08-11 20:23:12 -07:00
define(['datetime', 'imageLoader', 'connectionManager', 'itemHelper', 'mediaInfo', 'focusManager', 'indicators', 'globalize', 'layoutManager', 'apphost', 'dom', 'emby-button', 'css!./card', 'paper-icon-button-light', 'clearButtonStyle'],
function (datetime, imageLoader, connectionManager, itemHelper, mediaInfo, focusManager, indicators, globalize, layoutManager, appHost, dom) {
2016-10-02 20:49:52 -07:00
'use strict';
2016-07-29 12:51:58 -07:00
// Regular Expressions for parsing tags and attributes
var SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
// Match everything outside of normal chars and " (quote character)
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g;
/**
* Escapes all potentially dangerous characters, so that the
* resulting string can be safely inserted into attribute or
* element text.
* @param value
* @returns {string} escaped text
*/
function htmlEncode(value) {
return value.
replace(/&/g, '&').
replace(SURROGATE_PAIR_REGEXP, function (value) {
var hi = value.charCodeAt(0);
var low = value.charCodeAt(1);
return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';';
}).
replace(NON_ALPHANUMERIC_REGEXP, function (value) {
return '&#' + value.charCodeAt(0) + ';';
}).
replace(/</g, '&lt;').
replace(/>/g, '&gt;');
}
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
function getCardsHtml(items, options) {
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
var apiClient = connectionManager.currentApiClient();
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
if (arguments.length === 1) {
2016-07-29 12:51:58 -07:00
options = arguments[0];
items = options.items;
}
2016-07-29 08:54:43 -07:00
var html = buildCardsHtmlInternal(items, apiClient, options);
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
return html;
}
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
function getPostersPerRow(shape, screenWidth) {
switch (shape) {
case 'portrait':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 2200) {
return 10;
}
if (screenWidth >= 2100) {
return 9;
}
if (screenWidth >= 1600) {
return 8;
}
if (screenWidth >= 1400) {
return 7;
}
if (screenWidth >= 1200) {
return 6;
}
if (screenWidth >= 800) {
return 5;
}
if (screenWidth >= 640) {
return 4;
}
2016-07-29 08:54:43 -07:00
return 3;
case 'square':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 2100) {
return 9;
}
if (screenWidth >= 1800) {
return 8;
}
if (screenWidth >= 1400) {
return 7;
}
if (screenWidth >= 1200) {
return 6;
}
if (screenWidth >= 900) {
return 5;
}
if (screenWidth >= 700) {
return 4;
}
if (screenWidth >= 500) {
return 3;
}
2016-07-29 08:54:43 -07:00
return 2;
case 'banner':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 2200) {
return 4;
}
if (screenWidth >= 1200) {
return 3;
}
if (screenWidth >= 800) {
return 2;
}
2016-07-29 08:54:43 -07:00
return 1;
case 'backdrop':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 2500) {
return 6;
}
2016-10-08 21:58:57 -07:00
if (screenWidth >= 1600) {
2016-10-02 20:49:52 -07:00
return 5;
}
if (screenWidth >= 1200) {
return 4;
}
if (screenWidth >= 770) {
return 3;
}
if (screenWidth >= 420) {
return 2;
}
2016-07-29 08:54:43 -07:00
return 1;
case 'smallBackdrop':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 1440) {
return 8;
}
if (screenWidth >= 1100) {
return 6;
}
if (screenWidth >= 800) {
return 5;
}
if (screenWidth >= 600) {
return 4;
}
if (screenWidth >= 540) {
return 3;
}
if (screenWidth >= 420) {
return 2;
}
2016-07-29 08:54:43 -07:00
return 1;
case 'overflowPortrait':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 1000) {
2016-10-13 11:43:47 -07:00
return 100 / 22;
2016-10-02 20:49:52 -07:00
}
2016-10-13 11:43:47 -07:00
if (screenWidth >= 540) {
return 100 / 30;
2016-10-02 20:49:52 -07:00
}
2016-10-14 09:22:04 -07:00
return 100 / 42;
2016-07-29 08:54:43 -07:00
case 'overflowSquare':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 1000) {
return 100 / 22;
}
2016-10-13 11:43:47 -07:00
if (screenWidth >= 540) {
2016-10-02 20:49:52 -07:00
return 100 / 30;
}
2016-10-14 09:22:04 -07:00
return 100 / 42;
2016-07-29 08:54:43 -07:00
case 'overflowBackdrop':
2016-10-02 20:49:52 -07:00
if (screenWidth >= 1000) {
return 100 / 40;
}
if (screenWidth >= 640) {
2016-10-13 11:43:47 -07:00
return 100 / 56;
}
if (screenWidth >= 540) {
return 100 / 64;
2016-10-02 20:49:52 -07:00
}
2016-10-14 09:22:04 -07:00
return 100 / 72;
2016-07-29 08:54:43 -07:00
default:
return 4;
2016-07-28 19:42:42 -07:00
}
}
2016-08-06 07:07:44 -07:00
function isResizable(windowWidth) {
var screen = window.screen;
if (screen) {
var screenWidth = screen.availWidth;
if ((screenWidth - windowWidth) > 20) {
return true;
}
}
return false;
}
2016-07-29 08:54:43 -07:00
function getImageWidth(shape) {
2016-07-28 19:42:42 -07:00
2016-08-11 20:23:12 -07:00
var screenWidth = dom.getWindowSize().innerWidth;
2016-08-02 18:32:16 -07:00
2016-08-06 07:07:44 -07:00
if (isResizable(screenWidth)) {
2016-08-02 18:32:16 -07:00
var roundScreenTo = 100;
2016-10-12 11:23:09 -07:00
screenWidth = Math.floor(screenWidth / roundScreenTo) * roundScreenTo;
2016-08-02 18:32:16 -07:00
}
2016-08-06 07:07:44 -07:00
if (window.screen) {
screenWidth = Math.min(screenWidth, screen.availWidth || screenWidth);
}
2016-07-29 08:54:43 -07:00
var imagesPerRow = getPostersPerRow(shape, screenWidth);
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
var shapeWidth = screenWidth / imagesPerRow;
return Math.round(shapeWidth);
2016-07-28 19:42:42 -07:00
}
2016-07-29 08:54:43 -07:00
function setCardData(items, options) {
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
options.shape = options.shape || "auto";
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
var primaryImageAspectRatio = imageLoader.getPrimaryImageAspectRatio(items);
2016-10-02 20:49:52 -07:00
var isThumbAspectRatio = primaryImageAspectRatio && Math.abs(primaryImageAspectRatio - 1.777777778) < 0.3;
var isSquareAspectRatio = primaryImageAspectRatio && Math.abs(primaryImageAspectRatio - 1) < 0.33 ||
primaryImageAspectRatio && Math.abs(primaryImageAspectRatio - 1.3333334) < 0.01;
2016-07-29 08:54:43 -07:00
2016-10-02 20:49:52 -07:00
if (options.shape === 'auto' || options.shape === 'autohome' || options.shape === 'autooverflow' || options.shape === 'autoVertical') {
2016-07-29 08:54:43 -07:00
2016-09-14 09:20:03 -07:00
if (options.preferThumb === true || isThumbAspectRatio) {
2016-10-02 20:49:52 -07:00
options.shape = options.shape === 'autooverflow' ? 'overflowBackdrop' : 'backdrop';
2016-07-29 08:54:43 -07:00
} else if (isSquareAspectRatio) {
options.coverImage = true;
2016-10-02 20:49:52 -07:00
options.shape = options.shape === 'autooverflow' ? 'overflowSquare' : 'square';
2016-07-29 08:54:43 -07:00
} else if (primaryImageAspectRatio && primaryImageAspectRatio > 1.9) {
options.shape = 'banner';
options.coverImage = true;
2016-10-02 20:49:52 -07:00
} else if (primaryImageAspectRatio && Math.abs(primaryImageAspectRatio - 0.6666667) < 0.2) {
options.shape = options.shape === 'autooverflow' ? 'overflowPortrait' : 'portrait';
2016-07-29 08:54:43 -07:00
} else {
2016-10-02 20:49:52 -07:00
options.shape = options.defaultShape || (options.shape === 'autooverflow' ? 'overflowSquare' : 'square');
2016-07-28 19:42:42 -07:00
}
}
2016-10-02 20:49:52 -07:00
if (options.preferThumb === 'auto') {
options.preferThumb = options.shape === 'backdrop' || options.shape === 'overflowBackdrop';
2016-09-14 09:20:03 -07:00
}
2016-07-29 08:54:43 -07:00
options.uiAspect = getDesiredAspect(options.shape);
options.primaryImageAspectRatio = primaryImageAspectRatio;
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
if (!options.width && options.widths) {
options.width = options.widths[options.shape];
}
2016-07-28 19:42:42 -07:00
2016-07-29 08:54:43 -07:00
if (options.rows && typeof (options.rows) !== 'number') {
options.rows = options.rows[options.shape];
}
2016-07-28 19:42:42 -07:00
2016-08-01 22:55:52 -07:00
if (layoutManager.tv) {
2016-10-02 20:49:52 -07:00
if (options.shape === 'backdrop') {
2016-08-01 22:55:52 -07:00
options.width = options.width || 500;
}
2016-10-02 20:49:52 -07:00
else if (options.shape === 'portrait') {
2016-08-01 22:55:52 -07:00
options.width = options.width || 243;
}
2016-10-02 20:49:52 -07:00
else if (options.shape === 'square') {
2016-08-01 22:55:52 -07:00
options.width = options.width || 243;
}
2016-07-29 08:54:43 -07:00
}
options.width = options.width || getImageWidth(options.shape);
2016-07-28 19:42:42 -07:00
}
function buildCardsHtmlInternal(items, apiClient, options) {
var isVertical;
2016-10-02 20:49:52 -07:00
if (options.shape === 'autoVertical') {
2016-07-28 19:42:42 -07:00
isVertical = true;
}
2016-10-05 21:28:10 -07:00
if (options.vibrant && !appHost.supports('imageanalysis')) {
options.vibrant = false;
}
2016-07-29 08:54:43 -07:00
setCardData(items, options);
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
if (options.indexBy === 'Genres') {
2016-07-28 19:42:42 -07:00
return buildCardsByGenreHtmlInternal(items, apiClient, options);
}
var className = 'card';
if (options.shape) {
2016-07-29 08:54:43 -07:00
className += ' ' + options.shape + 'Card';
2016-07-28 19:42:42 -07:00
}
2016-10-10 23:46:59 -07:00
if (options.cardCssClass) {
className += ' ' + options.cardCssClass;
}
2016-07-28 19:42:42 -07:00
var html = '';
var itemsInRow = 0;
var currentIndexValue;
var hasOpenRow;
var hasOpenSection;
2016-07-29 08:54:43 -07:00
var sectionTitleTagName = options.sectionTitleTagName || 'div';
2016-07-28 19:42:42 -07:00
for (var i = 0, length = items.length; i < length; i++) {
var item = items[i];
if (options.indexBy) {
var newIndexValue = '';
2016-10-02 20:49:52 -07:00
if (options.indexBy === 'PremiereDate') {
2016-07-28 19:42:42 -07:00
if (item.PremiereDate) {
try {
2016-09-15 23:32:40 -07:00
newIndexValue = datetime.toLocaleDateString(datetime.parseISO8601Date(item.PremiereDate), { weekday: 'long', month: 'long', day: 'numeric' });
2016-07-28 19:42:42 -07:00
} catch (err) {
}
}
}
2016-10-02 20:49:52 -07:00
else if (options.indexBy === 'Genres') {
2016-07-28 19:42:42 -07:00
newIndexValue = item.Name;
}
2016-10-02 20:49:52 -07:00
else if (options.indexBy === 'ProductionYear') {
2016-07-28 19:42:42 -07:00
newIndexValue = item.ProductionYear;
}
2016-10-02 20:49:52 -07:00
else if (options.indexBy === 'CommunityRating') {
newIndexValue = item.CommunityRating ? (Math.floor(item.CommunityRating) + (item.CommunityRating % 1 >= 0.5 ? 0.5 : 0)) + '+' : null;
2016-07-28 19:42:42 -07:00
}
2016-10-02 20:49:52 -07:00
if (newIndexValue !== currentIndexValue) {
2016-07-28 19:42:42 -07:00
if (hasOpenRow) {
html += '</div>';
hasOpenRow = false;
itemsInRow = 0;
}
if (hasOpenSection) {
html += '</div>';
if (isVertical) {
html += '</div>';
}
hasOpenSection = false;
}
if (isVertical) {
html += '<div class="verticalSection">';
} else {
html += '<div class="horizontalSection">';
}
2016-07-29 08:54:43 -07:00
html += '<' + sectionTitleTagName + ' class="sectionTitle">' + newIndexValue + '</' + sectionTitleTagName + '>';
2016-07-28 19:42:42 -07:00
if (isVertical) {
2016-07-29 22:25:07 -07:00
html += '<div class="itemsContainer vertical-wrap">';
2016-07-28 19:42:42 -07:00
}
currentIndexValue = newIndexValue;
hasOpenSection = true;
}
}
2016-10-02 20:49:52 -07:00
if (options.rows && itemsInRow === 0) {
2016-07-28 19:42:42 -07:00
if (hasOpenRow) {
html += '</div>';
hasOpenRow = false;
}
html += '<div class="cardColumn">';
hasOpenRow = true;
}
var cardClass = className;
html += buildCard(i, item, apiClient, options, cardClass);
itemsInRow++;
if (options.rows && itemsInRow >= options.rows) {
html += '</div>';
hasOpenRow = false;
itemsInRow = 0;
}
}
if (hasOpenRow) {
html += '</div>';
}
if (hasOpenSection) {
html += '</div>';
if (isVertical) {
html += '</div>';
}
}
return html;
}
2016-10-02 20:49:52 -07:00
function filterItemsByGenre(items, genre) {
var genreLower = genre.toLowerCase();
return items.filter(function (currentItem) {
return currentItem.Genres.filter(function (g) {
return g.toLowerCase() === genreLower;
}).length > 0;
});
}
2016-07-28 19:42:42 -07:00
function buildCardsByGenreHtmlInternal(items, apiClient, options) {
var className = 'card';
if (options.shape) {
2016-07-29 08:54:43 -07:00
className += ' ' + options.shape + 'Card';
2016-07-28 19:42:42 -07:00
}
var html = '';
var loopItems = options.genres;
2016-10-02 20:49:52 -07:00
var itemsInRow;
var hasOpenRow;
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
var onGenre = function (renderItem) {
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
var currentItemHtml = '';
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
if (options.rows && itemsInRow === 0) {
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
if (hasOpenRow) {
currentItemHtml += '</div>';
hasOpenRow = false;
}
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
currentItemHtml += '<div class="cardColumn">';
hasOpenRow = true;
}
var cardClass = className;
currentItemHtml += buildCard(i, renderItem, apiClient, options, cardClass);
itemsInRow++;
if (options.rows && itemsInRow >= options.rows) {
currentItemHtml += '</div>';
hasOpenRow = false;
itemsInRow = 0;
}
return currentItemHtml;
};
for (var i = 0, length = loopItems.length; i < length; i++) {
var item = loopItems[i];
var renderItems = filterItemsByGenre(items, item.Name);
2016-07-28 19:42:42 -07:00
if (!renderItems.length) {
continue;
}
html += '<div class="horizontalSection focuscontainer-down">';
html += '<div class="sectionTitle">' + item.Name + '</div>';
var showMoreButton = false;
if (renderItems.length > options.indexLimit) {
renderItems.length = Math.min(renderItems.length, options.indexLimit);
showMoreButton = true;
}
2016-10-02 20:49:52 -07:00
itemsInRow = 0;
hasOpenRow = false;
2016-07-28 19:42:42 -07:00
2016-10-02 20:49:52 -07:00
html += renderItems.map(onGenre).join('');
2016-07-28 19:42:42 -07:00
if (showMoreButton) {
html += '<div class="listItemsMoreButtonContainer">';
2016-07-30 21:57:54 -07:00
html += '<button is="emby-button" class="listItemsMoreButton raised" data-parentid="' + options.parentId + '" data-indextype="Genres" data-indexvalue="' + item.Id + '">' + globalize.translate('sharedcomponents#More') + '</button>';
2016-07-28 19:42:42 -07:00
html += '</div>';
}
html += '</div>';
html += '</div>';
}
return html;
}
function getDesiredAspect(shape) {
if (shape) {
shape = shape.toLowerCase();
2016-10-02 20:49:52 -07:00
if (shape.indexOf('portrait') !== -1) {
2016-07-28 19:42:42 -07:00
return (2 / 3);
}
2016-10-02 20:49:52 -07:00
if (shape.indexOf('backdrop') !== -1) {
2016-07-28 19:42:42 -07:00
return (16 / 9);
}
2016-10-02 20:49:52 -07:00
if (shape.indexOf('square') !== -1) {
2016-07-28 19:42:42 -07:00
return 1;
}
}
return null;
}
function getCardImageUrl(item, apiClient, options) {
2016-08-03 13:14:39 -07:00
var imageItem = item.ProgramInfo || item;
item = imageItem;
2016-07-28 19:42:42 -07:00
var width = options.width;
var height = null;
var primaryImageAspectRatio = imageLoader.getPrimaryImageAspectRatio([item]);
var forceName = false;
var imgUrl = null;
var coverImage = false;
2016-10-02 20:49:52 -07:00
var uiAspect = null;
2016-07-28 19:42:42 -07:00
2016-09-14 09:20:03 -07:00
if (options.preferThumb && item.ImageTags && item.ImageTags.Thumb) {
2016-07-28 19:42:42 -07:00
imgUrl = apiClient.getScaledImageUrl(item.Id, {
type: "Thumb",
maxWidth: width,
tag: item.ImageTags.Thumb
});
} else if (options.preferBanner && item.ImageTags && item.ImageTags.Banner) {
imgUrl = apiClient.getScaledImageUrl(item.Id, {
type: "Banner",
maxWidth: width,
tag: item.ImageTags.Banner
});
} else if (options.preferThumb && item.SeriesThumbImageTag && options.inheritThumb !== false) {
imgUrl = apiClient.getScaledImageUrl(item.SeriesId, {
type: "Thumb",
maxWidth: width,
tag: item.SeriesThumbImageTag
});
2016-10-10 23:46:59 -07:00
} else if (options.preferThumb && item.ParentThumbItemId && options.inheritThumb !== false && item.MediaType !== 'Photo') {
2016-07-28 19:42:42 -07:00
imgUrl = apiClient.getScaledImageUrl(item.ParentThumbItemId, {
type: "Thumb",
maxWidth: width,
tag: item.ParentThumbImageTag
});
} else if (options.preferThumb && item.BackdropImageTags && item.BackdropImageTags.length) {
imgUrl = apiClient.getScaledImageUrl(item.Id, {
type: "Backdrop",
maxWidth: width,
tag: item.BackdropImageTags[0]
});
forceName = true;
} else if (item.ImageTags && item.ImageTags.Primary) {
height = width && primaryImageAspectRatio ? Math.round(width / primaryImageAspectRatio) : null;
2016-08-01 22:55:52 -07:00
imgUrl = apiClient.getScaledImageUrl(item.Id, {
2016-07-28 19:42:42 -07:00
type: "Primary",
maxHeight: height,
maxWidth: width,
tag: item.ImageTags.Primary
});
if (options.preferThumb && options.showTitle !== false) {
forceName = true;
}
if (primaryImageAspectRatio) {
2016-10-02 20:49:52 -07:00
uiAspect = getDesiredAspect(options.shape);
2016-07-28 19:42:42 -07:00
if (uiAspect) {
2016-10-02 20:49:52 -07:00
coverImage = Math.abs(primaryImageAspectRatio - uiAspect) <= 0.2;
2016-07-28 19:42:42 -07:00
}
}
} else if (item.PrimaryImageTag) {
height = width && primaryImageAspectRatio ? Math.round(width / primaryImageAspectRatio) : null;
2016-08-01 22:55:52 -07:00
imgUrl = apiClient.getScaledImageUrl(item.Id || item.ItemId, {
2016-07-28 19:42:42 -07:00
type: "Primary",
maxHeight: height,
maxWidth: width,
tag: item.PrimaryImageTag
});
if (options.preferThumb && options.showTitle !== false) {
forceName = true;
}
if (primaryImageAspectRatio) {
2016-10-02 20:49:52 -07:00
uiAspect = getDesiredAspect(options.shape);
2016-07-28 19:42:42 -07:00
if (uiAspect) {
2016-10-02 20:49:52 -07:00
coverImage = Math.abs(primaryImageAspectRatio - uiAspect) <= 0.2;
2016-07-28 19:42:42 -07:00
}
}
}
else if (item.ParentPrimaryImageTag) {
2016-08-01 22:55:52 -07:00
imgUrl = apiClient.getScaledImageUrl(item.ParentPrimaryImageItemId, {
2016-07-28 19:42:42 -07:00
type: "Primary",
maxWidth: width,
tag: item.ParentPrimaryImageTag
});
}
else if (item.AlbumId && item.AlbumPrimaryImageTag) {
width = primaryImageAspectRatio ? Math.round(height * primaryImageAspectRatio) : null;
imgUrl = apiClient.getScaledImageUrl(item.AlbumId, {
type: "Primary",
maxHeight: height,
maxWidth: width,
tag: item.AlbumPrimaryImageTag
});
if (primaryImageAspectRatio) {
2016-10-02 20:49:52 -07:00
uiAspect = getDesiredAspect(options.shape);
2016-07-28 19:42:42 -07:00
if (uiAspect) {
2016-10-02 20:49:52 -07:00
coverImage = Math.abs(primaryImageAspectRatio - uiAspect) <= 0.2;
2016-07-28 19:42:42 -07:00
}
}
}
2016-10-02 20:49:52 -07:00
else if (item.Type === 'Season' && item.ImageTags && item.ImageTags.Thumb) {
2016-07-28 19:42:42 -07:00
imgUrl = apiClient.getScaledImageUrl(item.Id, {
type: "Thumb",
maxWidth: width,
tag: item.ImageTags.Thumb
});
}
else if (item.BackdropImageTags && item.BackdropImageTags.length) {
imgUrl = apiClient.getScaledImageUrl(item.Id, {
type: "Backdrop",
maxWidth: width,
tag: item.BackdropImageTags[0]
});
} else if (item.ImageTags && item.ImageTags.Thumb) {
imgUrl = apiClient.getScaledImageUrl(item.Id, {
type: "Thumb",
maxWidth: width,
tag: item.ImageTags.Thumb
});
2016-08-31 12:17:11 -07:00
} else if (item.SeriesThumbImageTag && options.inheritThumb !== false) {
2016-07-28 19:42:42 -07:00
imgUrl = apiClient.getScaledImageUrl(item.SeriesId, {
type: "Thumb",
maxWidth: width,
tag: item.SeriesThumbImageTag
});
2016-08-31 12:17:11 -07:00
} else if (item.ParentThumbItemId && options.inheritThumb !== false) {
2016-07-28 19:42:42 -07:00
2016-08-31 12:17:11 -07:00
imgUrl = apiClient.getScaledImageUrl(item.ParentThumbItemId, {
2016-07-28 19:42:42 -07:00
type: "Thumb",
maxWidth: width,
tag: item.ParentThumbImageTag
});
}
return {
imgUrl: imgUrl,
forceName: forceName,
coverImage: coverImage
};
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2016-07-30 12:17:09 -07:00
var numRandomColors = 5;
function getDefaultColorIndex(str) {
if (str) {
2016-07-31 22:09:09 -07:00
var charIndex = Math.floor(str.length / 2);
var character = String(str.substr(charIndex, 1).charCodeAt());
2016-07-30 12:17:09 -07:00
var sum = 0;
for (var i = 0; i < character.length; i++) {
sum += parseInt(character.charAt(i));
}
var index = String(sum).substr(-1);
2016-07-30 13:09:07 -07:00
return (index % numRandomColors) + 1;
2016-07-30 12:17:09 -07:00
} else {
return getRandomInt(1, numRandomColors);
}
}
2016-07-30 13:09:07 -07:00
function getDefaultColorClass(str) {
return 'defaultCardColor' + getDefaultColorIndex(str);
}
2016-10-10 23:46:59 -07:00
function getCardTextLines(lines, cssClass, forceLines, isOuterFooter, cardLayout, addRightMargin) {
2016-07-28 19:42:42 -07:00
2016-07-29 12:51:58 -07:00
var html = '';
2016-07-29 08:54:43 -07:00
2016-07-29 12:51:58 -07:00
var valid = 0;
var i, length;
for (i = 0, length = lines.length; i < length; i++) {
2016-10-05 00:15:29 -07:00
var currentCssClass = cssClass;
2016-07-29 12:51:58 -07:00
var text = lines[i];
2016-10-05 00:15:29 -07:00
if (valid > 0 && isOuterFooter) {
currentCssClass += ' cardText-secondary';
2016-08-08 19:10:12 -07:00
}
2016-10-10 23:46:59 -07:00
if (addRightMargin) {
2016-10-05 00:15:29 -07:00
currentCssClass += ' cardText-rightmargin';
2016-09-05 13:07:36 -07:00
}
2016-07-29 12:51:58 -07:00
if (text) {
2016-10-05 00:15:29 -07:00
html += "<div class='" + currentCssClass + "'>";
2016-07-29 12:51:58 -07:00
html += text;
html += "</div>";
valid++;
}
}
if (forceLines) {
while (valid < length) {
html += "<div class='" + cssClass + "'>&nbsp;</div>";
valid++;
}
}
return html;
}
2016-10-05 00:15:29 -07:00
var uniqueFooterIndex = 0;
function getCardFooterText(item, apiClient, options, showTitle, forceName, overlayText, imgUrl, footerClass, progressHtml, isOuterFooter, cardFooterId, vibrantSwatch) {
2016-07-29 12:51:58 -07:00
var html = '';
2016-08-24 19:54:21 -07:00
var showOtherText = isOuterFooter ? !overlayText : overlayText;
2016-07-29 12:51:58 -07:00
if (isOuterFooter && options.cardLayout && !layoutManager.tv) {
2016-09-05 13:07:36 -07:00
2016-10-10 23:46:59 -07:00
if (options.cardFooterAside !== 'none') {
2016-10-02 20:49:52 -07:00
var moreIcon = appHost.moreIcon === 'dots-horiz' ? '&#xE5D3;' : '&#xE5D4;';
2016-09-05 13:07:36 -07:00
html += '<button is="paper-icon-button-light" class="itemAction btnCardOptions autoSize" data-action="menu"><i class="md-icon">' + moreIcon + '</i></button>';
}
2016-07-29 12:51:58 -07:00
}
2016-10-10 23:46:59 -07:00
var cssClass = options.centerText ? "cardText cardTextCentered" : "cardText";
2016-07-29 12:51:58 -07:00
var lines = [];
2016-10-02 20:49:52 -07:00
var parentTitleUnderneath = item.Type === 'MusicAlbum' || item.Type === 'Audio' || item.Type === 'MusicVideo';
2016-10-02 23:28:45 -07:00
var titleAdded;
2016-07-29 12:51:58 -07:00
if (showOtherText) {
2016-08-24 19:54:21 -07:00
if ((options.showParentTitle || options.showParentTitleOrTitle) && !parentTitleUnderneath) {
2016-07-29 12:51:58 -07:00
2016-10-02 20:49:52 -07:00
if (isOuterFooter && item.Type === 'Episode' && item.SeriesName && item.SeriesId) {
2016-07-29 12:51:58 -07:00
lines.push(getTextActionButton({
Id: item.SeriesId,
Name: item.SeriesName,
Type: 'Series',
IsFolder: true
}));
}
else {
2016-10-02 23:28:45 -07:00
if (item.Type === 'Program') {
2016-08-24 19:54:21 -07:00
2016-10-02 23:28:45 -07:00
lines.push(item.Name);
if (!item.IsSeries) {
titleAdded = true;
}
} else {
var parentTitle = item.SeriesName || item.Album || item.AlbumArtist || item.GameSystem || "";
2016-10-07 08:08:13 -07:00
if (parentTitle || showTitle) {
2016-10-02 23:28:45 -07:00
lines.push(parentTitle);
}
2016-08-24 19:54:21 -07:00
}
2016-07-29 12:51:58 -07:00
}
}
}
2016-10-07 08:08:13 -07:00
var showMediaTitle = (showTitle && !titleAdded) || (options.showParentTitleOrTitle && !lines.length);
2016-10-11 14:33:38 -07:00
if (!showMediaTitle && !titleAdded && (showTitle || forceName)) {
2016-10-07 08:08:13 -07:00
showMediaTitle = true;
}
if (showMediaTitle) {
2016-07-29 12:51:58 -07:00
2016-10-02 20:49:52 -07:00
var name = options.showTitle === 'auto' && !item.IsFolder && item.MediaType === 'Photo' ? '' : itemHelper.getDisplayName(item);
2016-07-29 12:51:58 -07:00
2016-10-02 23:28:45 -07:00
lines.push(name);
2016-07-29 08:54:43 -07:00
}
2016-07-28 19:42:42 -07:00
2016-07-29 12:51:58 -07:00
if (showOtherText) {
if (options.showParentTitle && parentTitleUnderneath) {
if (isOuterFooter && item.AlbumArtists && item.AlbumArtists.length) {
item.AlbumArtists[0].Type = 'MusicArtist';
item.AlbumArtists[0].IsFolder = true;
lines.push(getTextActionButton(item.AlbumArtists[0]));
} else {
2016-10-02 20:49:52 -07:00
lines.push(item.Type === 'Program' ? item.Name : (item.SeriesName || item.Album || item.AlbumArtist || item.GameSystem || ""));
2016-07-29 12:51:58 -07:00
}
}
if (options.showItemCounts) {
var itemCountHtml = getItemCountsHtml(options, item);
lines.push(itemCountHtml);
}
if (options.textLines) {
var additionalLines = options.textLines(item);
for (var i = 0, length = additionalLines.length; i < length; i++) {
lines.push(additionalLines[i]);
}
}
if (options.showSongCount) {
var songLine = '';
if (item.SongCount) {
2016-10-02 20:49:52 -07:00
songLine = item.SongCount === 1 ?
2016-07-30 21:57:54 -07:00
globalize.translate('sharedcomponents#ValueOneSong') :
globalize.translate('sharedcomponents#ValueSongCount', item.SongCount);
2016-07-29 12:51:58 -07:00
}
lines.push(songLine);
}
if (options.showPremiereDate) {
if (item.PremiereDate) {
try {
lines.push(getPremiereDateText(item));
} catch (err) {
lines.push('');
}
} else {
lines.push('');
}
}
if (options.showYear) {
lines.push(item.ProductionYear || '');
}
2016-09-12 14:24:05 -07:00
if (options.showRuntime) {
if (item.RunTimeTicks) {
lines.push(datetime.getDisplayRunningTime(item.RunTimeTicks));
} else {
lines.push('');
}
}
2016-07-29 12:51:58 -07:00
if (options.showAirTime) {
2016-09-30 11:43:59 -07:00
var airTimeText = '';
2016-07-29 12:51:58 -07:00
if (item.StartDate) {
try {
var date = datetime.parseISO8601Date(item.StartDate);
2016-09-30 11:43:59 -07:00
if (options.showAirDateTime) {
airTimeText += datetime.toLocaleDateString(date, { weekday: 'short', month: 'short', day: 'numeric' }) + ' ';
}
2016-07-29 12:51:58 -07:00
2016-09-30 11:43:59 -07:00
airTimeText += datetime.getDisplayTime(date);
2016-07-29 12:51:58 -07:00
2016-09-30 11:43:59 -07:00
if (item.EndDate && options.showAirEndTime) {
2016-07-29 12:51:58 -07:00
date = datetime.parseISO8601Date(item.EndDate);
airTimeText += ' - ' + datetime.getDisplayTime(date);
}
}
catch (e) {
console.log("Error parsing date: " + item.PremiereDate);
}
}
lines.push(airTimeText || '');
}
2016-09-30 11:43:59 -07:00
if (options.showChannelName) {
2016-07-29 12:51:58 -07:00
2016-09-05 13:07:36 -07:00
if (item.ChannelId) {
var channelText = item.ChannelName;
//var logoHeight = 32;
//if (item.ChannelPrimaryImageTag) {
// channelText = '<img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" class="lazy cardFooterLogo" style="height:' + logoHeight + 'px" data-src="' + apiClient.getScaledImageUrl(item.ChannelId, {
// type: "Primary",
// height: logoHeight,
// tag: item.ChannelPrimaryImageTag
// }) + '" />' + channelText;
//} else {
// channelText += '<div style="height:' + logoHeight + 'px;width:0;"></div>';
//}
lines.push(getTextActionButton({
Id: item.ChannelId,
Name: item.ChannelName,
Type: 'TvChannel',
MediaType: item.MediaType,
IsFolder: false
}, channelText));
} else {
lines.push(item.ChannelName || '&nbsp;');
}
2016-07-29 12:51:58 -07:00
}
2016-09-30 11:43:59 -07:00
2016-10-02 20:49:52 -07:00
if (options.showCurrentProgram && item.Type === 'TvChannel') {
2016-09-30 11:43:59 -07:00
if (item.CurrentProgram) {
lines.push(item.CurrentProgram.Name);
} else {
lines.push('');
}
}
if (options.showSeriesYear) {
2016-10-02 20:49:52 -07:00
if (item.Status === "Continuing") {
2016-09-30 11:43:59 -07:00
lines.push(globalize.translate('sharedcomponents#SeriesYearToPresent', item.ProductionYear || ''));
} else {
2016-10-11 14:33:38 -07:00
if (item.EndDate && item.ProductionYear) {
lines.push(item.ProductionYear + ' - ' + datetime.parseISO8601Date(item.EndDate).getFullYear());
} else {
lines.push(item.ProductionYear || '');
}
2016-09-30 11:43:59 -07:00
}
}
2016-10-02 20:49:52 -07:00
if (options.showSeriesTimerTime) {
if (item.RecordAnyTime) {
lines.push(globalize.translate('sharedcomponents#Anytime'));
} else {
lines.push(datetime.getDisplayTime(item.StartDate));
}
}
if (options.showSeriesTimerChannel) {
if (item.RecordAnyChannel) {
lines.push(globalize.translate('sharedcomponents#AllChannels'));
}
else if (item.ChannelId) {
lines.push(item.ChannelName || '');
}
}
2016-10-10 23:46:59 -07:00
if (options.showPersonRoleOrType) {
if (item.Role) {
lines.push('as ' + item.Role);
}
else if (item.Type) {
lines.push(globalize.translate('core#' + item.Type));
} else {
lines.push('');
}
}
2016-07-29 12:51:58 -07:00
}
2016-10-02 20:49:52 -07:00
if ((showTitle || !imgUrl) && forceName && overlayText && lines.length === 1) {
2016-08-16 22:46:15 -07:00
lines = [];
}
2016-10-10 23:46:59 -07:00
html += getCardTextLines(lines, cssClass, !options.overlayText, isOuterFooter, options.cardLayout, isOuterFooter && options.cardLayout && !options.centerText);
2016-07-29 12:51:58 -07:00
if (progressHtml) {
html += progressHtml;
}
if (html) {
2016-10-05 00:15:29 -07:00
var style = '';
if (options.vibrant && vibrantSwatch) {
var swatch = vibrantSwatch.split('|');
if (swatch.length) {
var index = 0;
style = ' style="color:' + swatch[index + 1] + ';background-color:' + swatch[index] + ';"';
}
}
html = '<div id="' + cardFooterId + '" class="' + footerClass + '"' + style + '>' + html;
2016-07-29 12:51:58 -07:00
//cardFooter
html += "</div>";
}
return html;
}
function getTextActionButton(item, text) {
if (!text) {
text = itemHelper.getDisplayName(item);
}
var html = '<button data-id="' + item.Id + '" data-type="' + item.Type + '" data-mediatype="' + item.MediaType + '" data-channelid="' + item.ChannelId + '" data-isfolder="' + item.IsFolder + '" type="button" class="itemAction textActionButton" data-action="link">';
html += text;
html += '</button>';
return html;
}
function getItemCountsHtml(options, item) {
var counts = [];
var childText;
2016-10-02 20:49:52 -07:00
if (item.Type === 'Playlist') {
2016-07-29 12:51:58 -07:00
childText = '';
if (item.CumulativeRunTimeTicks) {
var minutes = item.CumulativeRunTimeTicks / 600000000;
minutes = minutes || 1;
childText += globalize.translate('ValueMinutes', Math.round(minutes));
} else {
childText += globalize.translate('ValueMinutes', 0);
}
counts.push(childText);
}
2016-10-02 20:49:52 -07:00
else if (item.Type === 'Genre' || item.Type === 'Studio') {
2016-07-29 12:51:58 -07:00
if (item.MovieCount) {
2016-10-02 20:49:52 -07:00
childText = item.MovieCount === 1 ?
globalize.translate('sharedcomponents#ValueOneMovie') :
globalize.translate('sharedcomponents#ValueMovieCount', item.MovieCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
if (item.SeriesCount) {
2016-10-02 20:49:52 -07:00
childText = item.SeriesCount === 1 ?
globalize.translate('sharedcomponents#ValueOneSeries') :
globalize.translate('sharedcomponents#ValueSeriesCount', item.SeriesCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
if (item.EpisodeCount) {
2016-10-02 20:49:52 -07:00
childText = item.EpisodeCount === 1 ?
globalize.translate('sharedcomponents#ValueOneEpisode') :
globalize.translate('sharedcomponents#ValueEpisodeCount', item.EpisodeCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
if (item.GameCount) {
2016-10-02 20:49:52 -07:00
childText = item.GameCount === 1 ?
globalize.translate('sharedcomponents#ValueOneGame') :
globalize.translate('sharedcomponents#ValueGameCount', item.GameCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
2016-10-02 20:49:52 -07:00
} else if (item.Type === 'GameGenre') {
2016-07-29 12:51:58 -07:00
if (item.GameCount) {
2016-10-02 20:49:52 -07:00
childText = item.GameCount === 1 ?
2016-07-30 21:57:54 -07:00
globalize.translate('sharedcomponents#ValueOneGame') :
globalize.translate('sharedcomponents#ValueGameCount', item.GameCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
2016-10-02 20:49:52 -07:00
} else if (item.Type === 'MusicGenre' || options.context === "MusicArtist") {
2016-07-29 12:51:58 -07:00
if (item.AlbumCount) {
2016-10-02 20:49:52 -07:00
childText = item.AlbumCount === 1 ?
2016-07-30 21:57:54 -07:00
globalize.translate('sharedcomponents#ValueOneAlbum') :
globalize.translate('sharedcomponents#ValueAlbumCount', item.AlbumCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
if (item.SongCount) {
2016-10-02 20:49:52 -07:00
childText = item.SongCount === 1 ?
2016-07-30 21:57:54 -07:00
globalize.translate('sharedcomponents#ValueOneSong') :
globalize.translate('sharedcomponents#ValueSongCount', item.SongCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
if (item.MusicVideoCount) {
2016-10-02 20:49:52 -07:00
childText = item.MusicVideoCount === 1 ?
2016-07-30 21:57:54 -07:00
globalize.translate('sharedcomponents#ValueOneMusicVideo') :
globalize.translate('sharedcomponents#ValueMusicVideoCount', item.MusicVideoCount);
2016-07-29 12:51:58 -07:00
counts.push(childText);
}
2016-10-02 20:49:52 -07:00
} else if (item.Type === 'Series') {
2016-10-02 20:49:52 -07:00
childText = item.RecursiveItemCount === 1 ?
globalize.translate('sharedcomponents#ValueOneEpisode') :
globalize.translate('sharedcomponents#ValueEpisodeCount', item.RecursiveItemCount);
counts.push(childText);
2016-07-29 12:51:58 -07:00
}
2016-08-04 09:39:19 -07:00
return counts.join(', ');
2016-07-29 12:51:58 -07:00
}
function buildCard(index, item, apiClient, options, className) {
var action = options.action || 'link';
var scalable = options.scalable !== false;
if (scalable) {
2016-08-26 10:24:04 -07:00
className += " scalableCard " + options.shape + "Card-scalable";
2016-07-28 19:42:42 -07:00
}
var imgInfo = getCardImageUrl(item, apiClient, options);
var imgUrl = imgInfo.imgUrl;
2016-10-11 14:33:38 -07:00
var forceName = imgInfo.forceName;
2016-08-11 21:33:56 -07:00
2016-10-02 20:49:52 -07:00
var showTitle = options.showTitle === 'auto' ? true : (options.showTitle || item.Type === 'PhotoAlbum' || item.Type === 'Folder');
2016-08-11 21:33:56 -07:00
var overlayText = options.overlayText;
if (forceName && !options.cardLayout) {
if (overlayText == null) {
overlayText = true;
}
}
2016-07-28 19:42:42 -07:00
var cardImageContainerClass = 'cardImageContainer';
2016-10-05 00:15:29 -07:00
var coveredImage = options.coverImage || imgInfo.coverImage;
if (coveredImage) {
2016-07-28 19:42:42 -07:00
cardImageContainerClass += ' coveredImage';
2016-10-10 23:46:59 -07:00
if (item.MediaType === 'Photo' || item.Type === 'PhotoAlbum' || item.Type === 'Folder' || item.ProgramInfo || item.Type === 'Program' || item.Type === 'Recording') {
2016-08-19 23:27:48 -07:00
cardImageContainerClass += ' coveredImage-noScale';
2016-07-28 19:42:42 -07:00
}
}
if (!imgUrl) {
2016-07-30 13:09:07 -07:00
cardImageContainerClass += ' ' + getDefaultColorClass(item.Name);
2016-07-28 19:42:42 -07:00
}
2016-07-29 12:51:58 -07:00
var separateCardBox = scalable;
2016-07-30 13:09:07 -07:00
var cardBoxClass = options.cardLayout ? 'cardBox visualCardBox' : 'cardBox';
2016-08-11 21:33:56 -07:00
if (!layoutManager.tv) {
cardBoxClass += ' cardBox-mobile';
2016-08-11 22:59:45 -07:00
} else {
cardBoxClass += ' cardBox-focustransform';
2016-08-11 21:33:56 -07:00
}
var footerCssClass;
var progressHtml = indicators.getProgressBarHtml(item);
var innerCardFooter = '';
var footerOverlayed = false;
2016-10-05 00:15:29 -07:00
var cardFooterId = 'cardFooter' + uniqueFooterIndex;
uniqueFooterIndex++;
2016-08-11 21:33:56 -07:00
if (overlayText) {
footerCssClass = progressHtml ? 'innerCardFooter fullInnerCardFooter' : 'innerCardFooter';
2016-10-05 00:15:29 -07:00
innerCardFooter += getCardFooterText(item, apiClient, options, showTitle, forceName, overlayText, imgUrl, footerCssClass, progressHtml, false, cardFooterId);
2016-08-11 21:33:56 -07:00
footerOverlayed = true;
}
else if (progressHtml) {
innerCardFooter += '<div class="innerCardFooter fullInnerCardFooter innerCardFooterClear">';
innerCardFooter += progressHtml;
innerCardFooter += '</div>';
progressHtml = '';
}
var mediaSourceCount = item.MediaSourceCount || 1;
if (mediaSourceCount > 1) {
innerCardFooter += '<div class="mediaSourceIndicator">' + mediaSourceCount + '</div>';
}
2016-10-05 00:15:29 -07:00
var vibrantSwatch = options.vibrant && imgUrl ? imageLoader.getCachedVibrantInfo(imgUrl) : null;
2016-08-11 21:33:56 -07:00
var outerCardFooter = '';
if (!overlayText && !footerOverlayed) {
2016-08-23 10:07:50 -07:00
footerCssClass = options.cardLayout ? 'cardFooter visualCardBox-cardFooter' : 'cardFooter transparent';
2016-10-05 00:15:29 -07:00
outerCardFooter = getCardFooterText(item, apiClient, options, showTitle, forceName, overlayText, imgUrl, footerCssClass, progressHtml, true, cardFooterId, vibrantSwatch);
2016-08-11 21:33:56 -07:00
}
2016-08-12 08:54:37 -07:00
if (outerCardFooter && !options.cardLayout && options.allowBottomPadding !== false) {
2016-08-11 21:33:56 -07:00
cardBoxClass += ' cardBox-bottompadded';
}
2016-07-28 19:42:42 -07:00
if (!separateCardBox) {
2016-07-30 13:09:07 -07:00
cardImageContainerClass += " " + cardBoxClass;
2016-07-28 19:42:42 -07:00
}
2016-07-30 19:54:17 -07:00
var overlayButtons = '';
2016-08-01 09:26:42 -07:00
if (!layoutManager.tv) {
2016-07-30 19:54:17 -07:00
var overlayPlayButton = options.overlayPlayButton;
2016-08-04 09:39:19 -07:00
if (overlayPlayButton == null && !options.overlayMoreButton && !options.cardLayout) {
2016-10-02 20:49:52 -07:00
overlayPlayButton = item.MediaType === 'Video';
2016-07-30 19:54:17 -07:00
}
2016-10-02 20:49:52 -07:00
if (overlayPlayButton && !item.IsPlaceHolder && (item.LocationType !== 'Virtual' || !item.MediaType || item.Type === 'Program') && item.Type !== 'Person' && item.PlayAccess === 'Full') {
2016-07-30 19:54:17 -07:00
overlayButtons += '<button is="paper-icon-button-light" class="cardOverlayButton itemAction autoSize" data-action="playmenu" onclick="return false;"><i class="md-icon">play_arrow</i></button>';
}
if (options.overlayMoreButton) {
2016-10-02 20:49:52 -07:00
var moreIcon = appHost.moreIcon === 'dots-horiz' ? '&#xE5D3;' : '&#xE5D4;';
2016-07-30 19:54:17 -07:00
overlayButtons += '<button is="paper-icon-button-light" class="cardOverlayButton itemAction autoSize" data-action="menu" onclick="return false;"><i class="md-icon">' + moreIcon + '</i></button>';
}
}
if (options.showChildCountIndicator && item.ChildCount) {
className += ' groupedCard';
}
2016-07-28 19:42:42 -07:00
// cardBox can be it's own separate element if an outer footer is ever needed
2016-07-30 19:54:17 -07:00
var cardImageContainerOpen;
2016-07-29 08:54:43 -07:00
var cardImageContainerClose = '';
2016-07-30 19:54:17 -07:00
var cardBoxClose = '';
2016-07-29 08:54:43 -07:00
var cardContentClose = '';
var cardScalableClose = '';
2016-07-28 19:42:42 -07:00
if (separateCardBox) {
2016-07-29 08:54:43 -07:00
var cardContentOpen;
if (layoutManager.tv) {
cardContentOpen = '<div class="cardContent">';
cardContentClose = '</div>';
} else {
cardContentOpen = '<button type="button" class="clearButton cardContent itemAction" data-action="' + action + '">';
cardContentClose = '</button>';
}
2016-10-05 00:15:29 -07:00
if (options.vibrant && imgUrl && !vibrantSwatch) {
2016-10-07 08:08:13 -07:00
cardImageContainerOpen = '<div class="' + cardImageContainerClass + '">';
2016-10-05 00:15:29 -07:00
var imgClass = 'cardImage cardImage-img lazy';
if (coveredImage) {
imgClass += ' coveredImage-img';
}
2016-10-06 11:55:01 -07:00
cardImageContainerOpen += '<img crossOrigin="Anonymous" class="' + imgClass + '" data-vibrant="' + cardFooterId + '" data-swatch="db" data-src="' + imgUrl + '" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" />';
2016-10-05 00:15:29 -07:00
} else {
cardImageContainerOpen = imgUrl ? ('<div class="' + cardImageContainerClass + ' lazy" data-src="' + imgUrl + '">') : ('<div class="' + cardImageContainerClass + '">');
}
2016-08-23 23:13:15 -07:00
var cardScalableClass = options.cardLayout ? 'cardScalable visualCardBox-cardScalable' : 'cardScalable';
cardImageContainerOpen = '<div class="' + cardBoxClass + '"><div class="' + cardScalableClass + '"><div class="cardPadder-' + options.shape + '"></div>' + cardContentOpen + cardImageContainerOpen;
2016-07-29 08:54:43 -07:00
cardBoxClose = '</div>';
cardScalableClose = '</div>';
cardImageContainerClose = '</div>';
2016-07-30 19:54:17 -07:00
} else {
if (overlayButtons && !separateCardBox) {
2016-08-08 19:10:12 -07:00
cardImageContainerClass += ' cardImageContainerClass-button';
2016-07-30 19:54:17 -07:00
cardImageContainerOpen = imgUrl ? ('<button type="button" data-action="' + action + '" class="itemAction ' + cardImageContainerClass + ' lazy" data-src="' + imgUrl + '">') : ('<button type="button" data-action="' + action + '" class="itemAction ' + cardImageContainerClass + '">');
cardImageContainerClose = '</button>';
className += ' forceRelative';
} else {
cardImageContainerOpen = imgUrl ? ('<div class="' + cardImageContainerClass + ' lazy" data-src="' + imgUrl + '">') : ('<div class="' + cardImageContainerClass + '">');
cardImageContainerClose = '</div>';
}
2016-07-28 19:42:42 -07:00
}
var indicatorsHtml = '';
2016-08-02 23:38:19 -07:00
indicatorsHtml += indicators.getSyncIndicator(item);
2016-07-28 19:42:42 -07:00
indicatorsHtml += indicators.getTimerIndicator(item);
if (options.showGroupCount) {
indicatorsHtml += indicators.getChildCountIndicatorHtml(item, {
minCount: 1
});
}
else {
indicatorsHtml += indicators.getPlayedIndicatorHtml(item);
}
if (indicatorsHtml) {
2016-08-08 11:13:52 -07:00
cardImageContainerOpen += '<div class="cardIndicators ' + options.shape + 'CardIndicators">' + indicatorsHtml + '</div>';
2016-07-28 19:42:42 -07:00
}
2016-07-29 12:51:58 -07:00
if (!imgUrl) {
2016-10-12 11:23:09 -07:00
var defaultName = item.Type === 'Program' || item.Type === 'Timer' || item.EpisodeTitle ? item.Name : itemHelper.getDisplayName(item);
2016-10-11 14:33:38 -07:00
cardImageContainerOpen += '<div class="cardText cardDefaultText">' + defaultName + '</div>';
2016-07-28 19:42:42 -07:00
}
2016-07-30 19:54:17 -07:00
var tagName = (layoutManager.tv || !scalable) && !overlayButtons ? 'button' : 'div';
2016-07-28 19:42:42 -07:00
var prefix = (item.SortName || item.Name || '')[0];
if (prefix) {
prefix = prefix.toUpperCase();
}
var timerAttributes = '';
if (item.TimerId) {
timerAttributes += ' data-timerid="' + item.TimerId + '"';
}
if (item.SeriesTimerId) {
timerAttributes += ' data-seriestimerid="' + item.SeriesTimerId + '"';
}
2016-07-29 12:51:58 -07:00
var actionAttribute;
2016-10-02 20:49:52 -07:00
if (tagName === 'button') {
2016-07-29 12:51:58 -07:00
className += " itemAction";
actionAttribute = ' data-action="' + action + '"';
} else {
actionAttribute = '';
}
2016-10-02 20:49:52 -07:00
if (item.Type !== 'MusicAlbum' && item.Type !== 'MusicArtist' && item.Type !== 'Audio') {
2016-09-09 09:58:08 -07:00
className += ' card-withuserdata';
}
2016-08-29 00:12:24 -07:00
2016-07-29 21:21:03 -07:00
var positionTicksData = item.UserData && item.UserData.PlaybackPositionTicks ? (' data-positionticks="' + item.UserData.PlaybackPositionTicks + '"') : '';
var collectionIdData = options.collectionId ? (' data-collectionid="' + options.collectionId + '"') : '';
var playlistIdData = options.playlistId ? (' data-playlistid="' + options.playlistId + '"') : '';
var mediaTypeData = item.MediaType ? (' data-mediatype="' + item.MediaType + '"') : '';
var collectionTypeData = item.CollectionType ? (' data-collectiontype="' + item.CollectionType + '"') : '';
var channelIdData = item.ChannelId ? (' data-channelid="' + item.ChannelId + '"') : '';
2016-08-24 20:07:31 -07:00
var contextData = options.context ? (' data-context="' + options.context + '"') : '';
2016-07-29 21:21:03 -07:00
2016-10-10 23:46:59 -07:00
return '<' + tagName + ' data-index="' + index + '"' + timerAttributes + actionAttribute + ' data-isfolder="' + (item.IsFolder || false) + '" data-serverid="' + (item.ServerId || options.serverId) + '" data-id="' + (item.Id || item.ItemId) + '" data-type="' + item.Type + '"' + mediaTypeData + collectionTypeData + channelIdData + positionTicksData + collectionIdData + playlistIdData + contextData + ' data-prefix="' + prefix + '" class="' + className + '">' + cardImageContainerOpen + innerCardFooter + cardImageContainerClose + cardContentClose + overlayButtons + cardScalableClose + outerCardFooter + cardBoxClose + '</' + tagName + '>';
2016-07-28 19:42:42 -07:00
}
function buildCards(items, options) {
// Abort if the container has been disposed
if (!document.body.contains(options.itemsContainer)) {
return;
}
if (options.parentContainer) {
if (items.length) {
options.parentContainer.classList.remove('hide');
} else {
options.parentContainer.classList.add('hide');
return;
}
}
var apiClient = connectionManager.currentApiClient();
var html = buildCardsHtmlInternal(items, apiClient, options);
if (html) {
2016-10-02 20:49:52 -07:00
if (options.itemsContainer.cardBuilderHtml !== html) {
2016-07-28 19:42:42 -07:00
options.itemsContainer.innerHTML = html;
if (items.length < 50) {
options.itemsContainer.cardBuilderHtml = html;
} else {
options.itemsContainer.cardBuilderHtml = null;
}
}
imageLoader.lazyChildren(options.itemsContainer);
} else {
options.itemsContainer.innerHTML = html;
options.itemsContainer.cardBuilderHtml = null;
}
if (options.autoFocus) {
focusManager.autoFocus(options.itemsContainer, true);
}
2016-10-02 20:49:52 -07:00
if (options.indexBy === 'Genres') {
2016-07-28 19:42:42 -07:00
options.itemsContainer.addEventListener('click', onItemsContainerClick);
}
}
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function onItemsContainerClick(e) {
var listItemsMoreButton = parentWithClass(e.target, 'listItemsMoreButton');
if (listItemsMoreButton) {
var value = listItemsMoreButton.getAttribute('data-indexvalue');
var parentid = listItemsMoreButton.getAttribute('data-parentid');
Emby.Page.showGenre({
ParentId: parentid,
Id: value
});
}
}
function ensureIndicators(card, indicatorsElem) {
if (indicatorsElem) {
return indicatorsElem;
}
2016-09-09 09:58:08 -07:00
indicatorsElem = card.querySelector('.cardIndicators');
2016-07-28 19:42:42 -07:00
if (!indicatorsElem) {
var cardImageContainer = card.querySelector('.cardImageContainer');
indicatorsElem = document.createElement('div');
2016-09-09 09:58:08 -07:00
indicatorsElem.classList.add('cardIndicators');
2016-07-28 19:42:42 -07:00
cardImageContainer.appendChild(indicatorsElem);
}
return indicatorsElem;
}
function updateUserData(card, userData) {
var type = card.getAttribute('data-type');
2016-10-02 20:49:52 -07:00
var enableCountIndicator = type === 'Series' || type === 'BoxSet' || type === 'Season';
var indicatorsElem = null;
var playedIndicator = null;
var countIndicator = null;
var itemProgressBar = null;
2016-07-28 19:42:42 -07:00
if (userData.Played) {
2016-10-02 20:49:52 -07:00
playedIndicator = card.querySelector('.playedIndicator');
2016-07-28 19:42:42 -07:00
if (!playedIndicator) {
playedIndicator = document.createElement('div');
playedIndicator.classList.add('playedIndicator');
2016-10-05 00:15:29 -07:00
playedIndicator.classList.add('indicator');
2016-07-28 19:42:42 -07:00
indicatorsElem = ensureIndicators(card, indicatorsElem);
indicatorsElem.appendChild(playedIndicator);
}
2016-10-05 00:15:29 -07:00
playedIndicator.innerHTML = '<i class="md-icon indicatorIcon">check</i>';
2016-07-28 19:42:42 -07:00
} else {
2016-10-02 20:49:52 -07:00
playedIndicator = card.querySelector('.playedIndicator');
2016-07-28 19:42:42 -07:00
if (playedIndicator) {
playedIndicator.parentNode.removeChild(playedIndicator);
}
}
if (userData.UnplayedItemCount) {
2016-10-02 20:49:52 -07:00
countIndicator = card.querySelector('.countIndicator');
2016-07-28 19:42:42 -07:00
if (!countIndicator) {
countIndicator = document.createElement('div');
countIndicator.classList.add('countIndicator');
indicatorsElem = ensureIndicators(card, indicatorsElem);
indicatorsElem.appendChild(countIndicator);
}
countIndicator.innerHTML = userData.UnplayedItemCount;
} else if (enableCountIndicator) {
2016-10-02 20:49:52 -07:00
countIndicator = card.querySelector('.countIndicator');
2016-07-28 19:42:42 -07:00
if (countIndicator) {
countIndicator.parentNode.removeChild(countIndicator);
}
}
var progressHtml = indicators.getProgressBarHtml({
Type: type,
UserData: userData,
MediaType: 'Video'
});
if (progressHtml) {
2016-10-02 20:49:52 -07:00
itemProgressBar = card.querySelector('.itemProgressBar');
2016-07-28 19:42:42 -07:00
if (!itemProgressBar) {
itemProgressBar = document.createElement('div');
itemProgressBar.classList.add('itemProgressBar');
var innerCardFooter = card.querySelector('.innerCardFooter');
if (!innerCardFooter) {
innerCardFooter = document.createElement('div');
innerCardFooter.classList.add('innerCardFooter');
var cardImageContainer = card.querySelector('.cardImageContainer');
cardImageContainer.appendChild(innerCardFooter);
}
innerCardFooter.appendChild(itemProgressBar);
}
itemProgressBar.innerHTML = progressHtml;
}
else {
2016-10-02 20:49:52 -07:00
itemProgressBar = card.querySelector('.itemProgressBar');
2016-07-28 19:42:42 -07:00
if (itemProgressBar) {
itemProgressBar.parentNode.removeChild(itemProgressBar);
}
}
}
2016-08-29 21:33:24 -07:00
function onUserDataChanged(userData, scope) {
2016-07-28 19:42:42 -07:00
2016-08-29 21:33:24 -07:00
var cards = (scope || document.body).querySelectorAll('.card-withuserdata[data-id="' + userData.ItemId + '"]');
2016-07-28 19:42:42 -07:00
for (var i = 0, length = cards.length; i < length; i++) {
updateUserData(cards[i], userData);
}
}
2016-09-19 08:41:35 -07:00
function onTimerCreated(programId, newTimerId, itemsContainer) {
2016-09-30 11:43:59 -07:00
2016-09-19 08:41:35 -07:00
var cells = itemsContainer.querySelectorAll('.card[data-id="' + programId + '"]');
for (var i = 0, length = cells.length; i < length; i++) {
var cell = cells[i];
var icon = cell.querySelector('.timerIndicator');
if (!icon) {
var indicatorsElem = ensureIndicators(cell);
indicatorsElem.insertAdjacentHTML('beforeend', '<i class="md-icon timerIndicator indicatorIcon">&#xE061;</i>');
}
cell.setAttribute('data-timerid', newTimerId);
}
}
function onTimerCancelled(id, itemsContainer) {
var cells = itemsContainer.querySelectorAll('.card[data-timerid="' + id + '"]');
for (var i = 0, length = cells.length; i < length; i++) {
var cell = cells[i];
var icon = cell.querySelector('.timerIndicator');
if (icon) {
icon.parentNode.removeChild(icon);
}
cell.removeAttribute('data-timerid');
}
}
function onSeriesTimerCancelled(id, itemsContainer) {
var cells = itemsContainer.querySelectorAll('.card[data-seriestimerid="' + id + '"]');
for (var i = 0, length = cells.length; i < length; i++) {
var cell = cells[i];
var icon = cell.querySelector('.timerIndicator');
if (icon) {
icon.parentNode.removeChild(icon);
}
cell.removeAttribute('data-seriestimerid');
}
}
2016-07-28 19:42:42 -07:00
return {
2016-07-29 08:54:43 -07:00
getCardsHtml: getCardsHtml,
2016-07-28 19:42:42 -07:00
buildCards: buildCards,
2016-07-30 13:09:07 -07:00
onUserDataChanged: onUserDataChanged,
2016-09-19 08:41:35 -07:00
getDefaultColorClass: getDefaultColorClass,
onTimerCreated: onTimerCreated,
onTimerCancelled: onTimerCancelled,
onSeriesTimerCancelled: onSeriesTimerCancelled
2016-07-28 19:42:42 -07:00
};
});