define(['imageLoader', 'itemShortcuts', 'connectionManager'], function (imageLoader, itemShortcuts, connectionManager) {
function buildPeopleCardsHtml(people, options) {
var className = 'card ' + (options.shape || 'portrait') + 'Card personCard';
if (options.block || options.rows) {
className += ' block';
}
var html = '';
var itemsInRow = 0;
var serverId = options.serverId;
var apiClient = connectionManager.getApiClient(serverId);
for (var i = 0, length = people.length; i < length; i++) {
if (options.rows && itemsInRow == 0) {
html += '
';
}
var person = people[i];
html += buildPersonCard(person, apiClient, serverId, options, className);
itemsInRow++;
if (options.rows && itemsInRow >= options.rows) {
itemsInRow = 0;
html += '
';
}
}
return html;
}
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) {
className += " itemAction scalableCard";
var imgUrl = getImgUrl(person, options.width, apiClient);
var cardImageContainerClass = 'cardImageContainer';
if (options.coverImage) {
cardImageContainerClass += ' coveredImage';
}
var cardImageContainer = imgUrl ? ('') : ('
');
if (!imgUrl) {
cardImageContainer += '
person';
}
var nameHtml = '';
nameHtml += '
' + person.Name + '
';
if (person.Role) {
nameHtml += '
as ' + person.Role + '
';
}
else if (person.Type) {
nameHtml += '
' + Globalize.translate('core#' + person.Type) + '
';
} else {
nameHtml += '
';
}
var html = '\
\
'
;
return html;
}
function buildPeopleCards(items, options) {
if (options.parentContainer) {
// Abort if the container has been disposed
if (!document.body.contains(options.parentContainer)) {
return;
}
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
};
});