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

123 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-08-11 22:59:45 -07:00
define(['imageLoader', 'itemShortcuts', 'connectionManager', 'layoutManager'], function (imageLoader, itemShortcuts, connectionManager, layoutManager) {
2016-10-02 20:49:52 -07:00
'use strict';
2016-07-28 19:42:42 -07:00
function buildPeopleCardsHtml(people, options) {
2016-07-30 21:11:23 -07:00
var className = 'card ' + (options.shape || 'portrait') + 'Card personCard';
2016-07-28 19:42:42 -07:00
if (options.block || options.rows) {
className += ' block';
}
var html = '';
var itemsInRow = 0;
var serverId = options.serverId;
2016-07-30 21:11:23 -07:00
var apiClient = connectionManager.getApiClient(serverId);
2016-07-28 19:42:42 -07:00
for (var i = 0, length = people.length; i < length; i++) {
2016-10-02 20:49:52 -07:00
if (options.rows && itemsInRow === 0) {
2016-07-28 19:42:42 -07:00
html += '<div class="cardColumn">';
}
var person = people[i];
2016-07-30 21:11:23 -07:00
html += buildPersonCard(person, apiClient, serverId, options, className);
2016-07-28 19:42:42 -07:00
itemsInRow++;
if (options.rows && itemsInRow >= options.rows) {
itemsInRow = 0;
html += '</div>';
}
}
return html;
}
2016-07-30 21:11:23 -07:00
function getImgUrl(person, maxWidth, apiClient) {
if (person.PrimaryImageTag) {
return apiClient.getScaledImageUrl(person.Id, {
maxWidth: maxWidth,
tag: person.PrimaryImageTag,
type: "Primary"
});
}
return null;
}
function buildPersonCard(person, apiClient, serverId, options, className) {
2016-07-28 19:42:42 -07:00
2016-08-26 10:24:04 -07:00
className += " itemAction scalableCard personCard-scalable";
className += " " + (options.shape || 'portrait') + 'Card-scalable';
2016-07-28 19:42:42 -07:00
2016-07-30 21:11:23 -07:00
var imgUrl = getImgUrl(person, options.width, apiClient);
2016-07-28 19:42:42 -07:00
var cardImageContainerClass = 'cardImageContainer';
if (options.coverImage) {
cardImageContainerClass += ' coveredImage';
}
var cardImageContainer = imgUrl ? ('<div class="' + cardImageContainerClass + ' lazy" data-src="' + imgUrl + '">') : ('<div class="' + cardImageContainerClass + '">');
2016-07-31 22:09:09 -07:00
if (!imgUrl) {
cardImageContainer += '<i class="md-icon cardImageIcon">person</i>';
}
2016-07-28 19:42:42 -07:00
var nameHtml = '';
2016-10-07 22:57:38 -07:00
nameHtml += '<div class="cardText cardTextCentered">' + person.Name + '</div>';
2016-07-28 19:42:42 -07:00
if (person.Role) {
2016-10-07 22:57:38 -07:00
nameHtml += '<div class="cardText cardText-secondary cardTextCentered">as ' + person.Role + '</div>';
2016-07-28 19:42:42 -07:00
}
else if (person.Type) {
2016-10-07 22:57:38 -07:00
nameHtml += '<div class="cardText cardText-secondary cardTextCentered">' + Globalize.translate('core#' + person.Type) + '</div>';
2016-07-28 19:42:42 -07:00
} else {
2016-10-07 22:57:38 -07:00
nameHtml += '<div class="cardText cardText-secondary cardTextCentered">&nbsp;</div>';
2016-07-28 19:42:42 -07:00
}
2016-08-11 22:59:45 -07:00
var cardBoxCssClass = 'visualCardBox cardBox';
if (layoutManager.tv) {
cardBoxCssClass += ' cardBox-focustransform';
}
2016-10-02 20:49:52 -07:00
var html = '<button type="button" data-isfolder="' + person.IsFolder + '" data-type="' + person.Type + '" data-action="link" data-id="' + person.Id + '" data-serverid="' + serverId + '" raised class="' + className + '"><div class="' + cardBoxCssClass + '"><div class="cardScalable visualCardBox-cardScalable"><div class="cardPadder-portrait"></div><div class="cardContent">' + cardImageContainer + '</div></div></div><div class="cardFooter visualCardBox-cardFooter">' + nameHtml + '</div></div></button>';
2016-07-28 19:42:42 -07:00
return html;
}
function buildPeopleCards(items, options) {
if (options.parentContainer) {
2016-07-30 21:11:23 -07:00
// Abort if the container has been disposed
if (!document.body.contains(options.parentContainer)) {
return;
}
2016-07-28 19:42:42 -07:00
if (items.length) {
options.parentContainer.classList.remove('hide');
} else {
options.parentContainer.classList.add('hide');
return;
}
}
var html = buildPeopleCardsHtml(items, options);
options.itemsContainer.innerHTML = html;
imageLoader.lazyChildren(options.itemsContainer);
itemShortcuts.off(options.itemsContainer);
itemShortcuts.on(options.itemsContainer);
}
return {
buildPeopleCards: buildPeopleCards
};
});