define(['layoutManager', 'cardBuilder', 'datetime', 'mediaInfo', 'backdrop', 'listView', 'itemContextMenu', 'itemHelper', 'userdataButtons', 'dom', 'indicators', 'apphost', 'scrollStyles', 'emby-itemscontainer', 'emby-checkbox'], function (layoutManager, cardBuilder, datetime, mediaInfo, backdrop, listView, itemContextMenu, itemHelper, userdataButtons, dom, indicators, appHost) {
var currentItem;
var currentRecordingFields;
function getPromise(params) {
var id = params.id;
if (id) {
return ApiClient.getItem(Dashboard.getCurrentUserId(), id);
}
var name = params.genre;
if (name) {
return ApiClient.getGenre(name, Dashboard.getCurrentUserId());
}
name = params.musicgenre;
if (name) {
return ApiClient.getMusicGenre(name, Dashboard.getCurrentUserId());
}
name = params.gamegenre;
if (name) {
return ApiClient.getGameGenre(name, Dashboard.getCurrentUserId());
}
name = params.musicartist;
if (name) {
return ApiClient.getArtist(name, Dashboard.getCurrentUserId());
}
else {
throw new Error('Invalid request');
}
}
function reload(page, params) {
Dashboard.showLoadingMsg();
getPromise(params).then(function (item) {
reloadFromItem(page, params, item);
});
}
function hideAll(page, className, show) {
var i, length;
var elems = page.querySelectorAll('.' + className);
for (i = 0, length = elems.length; i < length; i++) {
if (show) {
elems[i].classList.remove('hide');
} else {
elems[i].classList.add('hide');
}
}
}
function getContextMenuOptions(item, button) {
var options = {
item: item,
open: false,
play: false,
queue: false,
playAllFromHere: false,
queueAllFromHere: false,
positionTo: button
};
if (appHost.supports('sync')) {
// Will be displayed via button
options.syncLocal = false;
} else {
// Will be displayed via button
options.sync = false;
}
return options;
}
function updateSyncStatus(page, item) {
var i, length;
var elems = page.querySelectorAll('.chkOffline');
for (i = 0, length = elems.length; i < length; i++) {
elems[i].checked = item.SyncPercent != null;
}
}
function reloadFromItem(page, params, item) {
currentItem = item;
var context = params.context;
LibraryBrowser.renderName(item, page.querySelector('.itemName'), false, context);
LibraryBrowser.renderParentName(item, page.querySelector('.parentName'), context);
LibraryMenu.setTitle('');
Dashboard.getCurrentUser().then(function (user) {
window.scrollTo(0, 0);
renderImage(page, item, user);
setInitialCollapsibleState(page, item, context, user);
renderDetails(page, item, context);
var hasBackdrop = false;
// For these types, make the backdrop a little smaller so that the items are more quickly accessible
if (item.Type == 'MusicArtist' || item.Type == "MusicAlbum" || item.Type == "Playlist" || item.Type == "BoxSet" || item.MediaType == "Audio" || !layoutManager.mobile) {
var itemBackdropElement = page.querySelector('#itemBackdrop');
itemBackdropElement.classList.add('noBackdrop');
itemBackdropElement.style.backgroundImage = 'none';
backdrop.setBackdrops([item]);
}
else {
hasBackdrop = LibraryBrowser.renderDetailPageBackdrop(page, item);
backdrop.clear();
}
var transparentHeader = hasBackdrop && page.classList.contains('noSecondaryNavPage');
LibraryMenu.setTransparentMenu(transparentHeader);
var canPlay = false;
if (item.Type == 'Program') {
var now = new Date();
if (now >= datetime.parseISO8601Date(item.StartDate, true) && now < datetime.parseISO8601Date(item.EndDate, true)) {
hideAll(page, 'btnPlay', true);
canPlay = true;
} else {
hideAll(page, 'btnPlay');
}
}
else if (MediaController.canPlay(item)) {
hideAll(page, 'btnPlay', true);
canPlay = true;
}
else {
hideAll(page, 'btnPlay');
}
if (item.LocalTrailerCount && item.PlayAccess == 'Full') {
hideAll(page, 'btnPlayTrailer', true);
} else {
hideAll(page, 'btnPlayTrailer');
}
if (itemHelper.canSync(user, item)) {
if (appHost.supports('sync')) {
hideAll(page, 'syncLocalContainer', true);
hideAll(page, 'btnSync');
} else {
hideAll(page, 'syncLocalContainer');
hideAll(page, 'btnSync', true);
}
updateSyncStatus(page, item);
} else {
hideAll(page, 'btnSync');
hideAll(page, 'syncLocalContainer');
}
showRecordingFields(page, item, user);
var btnPlayExternalTrailer = page.querySelectorAll('.btnPlayExternalTrailer');
for (var i = 0, length = btnPlayExternalTrailer.length; i < length; i++) {
if (!item.LocalTrailerCount && item.RemoteTrailers.length && item.PlayAccess == 'Full') {
btnPlayExternalTrailer[i].classList.remove('hide');
btnPlayExternalTrailer[i].href = item.RemoteTrailers[0].Url;
} else {
btnPlayExternalTrailer[i].classList.add('hide');
btnPlayExternalTrailer[i].href = '#';
}
}
var groupedVersions = (item.MediaSources || []).filter(function (g) {
return g.Type == "Grouping";
});
if (user.Policy.IsAdministrator && groupedVersions.length) {
page.querySelector('.splitVersionContainer').classList.remove('hide');
} else {
page.querySelector('.splitVersionContainer').classList.add('hide');
}
itemContextMenu.getCommands(getContextMenuOptions(item)).then(function (commands) {
if (commands.length) {
hideAll(page, 'btnMoreCommands', true);
} else {
hideAll(page, 'btnMoreCommands');
}
});
var itemBirthday = page.querySelector('#itemBirthday');
if (item.Type == "Person" && item.PremiereDate) {
try {
var birthday = datetime.parseISO8601Date(item.PremiereDate, true).toDateString();
itemBirthday.classList.remove('hide');
itemBirthday.innerHTML = Globalize.translate('BirthDateValue').replace('{0}', birthday);
}
catch (err) {
itemBirthday.classList.add('hide');
}
} else {
itemBirthday.classList.add('hide');
}
var itemDeathDate = page.querySelector('#itemDeathDate');
if (item.Type == "Person" && item.EndDate) {
try {
var deathday = datetime.parseISO8601Date(item.EndDate, true).toDateString();
itemDeathDate.classList.remove('hide');
itemDeathDate.innerHTML = Globalize.translate('DeathDateValue').replace('{0}', deathday);
}
catch (err) {
itemDeathDate.classList.add('hide');
}
} else {
}
var itemBirthLocation = page.querySelector('#itemBirthLocation');
if (item.Type == "Person" && item.ProductionLocations && item.ProductionLocations.length) {
var gmap = '' + item.ProductionLocations[0] + '';
itemBirthLocation.classList.remove('hide');
itemBirthLocation.innerHTML = Globalize.translate('BirthPlaceValue').replace('{0}', gmap);
} else {
itemBirthLocation.classList.add('hide');
}
});
//if (item.LocationType == "Offline") {
// page.querySelector('.offlineIndicator').classList.remove('hide');
//}
//else {
// page.querySelector('.offlineIndicator').classList.add('hide');
//}
var isMissingEpisode = false;
if (item.LocationType == "Virtual" && item.Type == "Episode") {
try {
if (item.PremiereDate && (new Date().getTime() >= datetime.parseISO8601Date(item.PremiereDate, true).getTime())) {
isMissingEpisode = true;
}
} catch (err) {
}
}
//if (isMissingEpisode) {
// page.querySelector('.missingIndicator').classList.remove('hide');
//}
//else {
// page.querySelector('.missingIndicator').classList.add('hide');
//}
setPeopleHeader(page, item);
page.dispatchEvent(new CustomEvent("displayingitem", {
detail: {
item: item,
context: context
},
bubbles: true
}));
Dashboard.hideLoadingMsg();
}
function showRecordingFields(page, item, user) {
if (currentRecordingFields) {
return;
}
var recordingFieldsElement = page.querySelector('.recordingFields');
if (item.Type == 'Program' && user.Policy.EnableLiveTvManagement) {
require(['recordingFields'], function (recordingFields) {
currentRecordingFields = new recordingFields({
parent: recordingFieldsElement,
programId: item.Id,
serverId: item.ServerId
});
recordingFieldsElement.classList.remove('hide');
});
} else {
recordingFieldsElement.classList.add('hide');
recordingFieldsElement.innerHTML = '';
}
}
function renderLinks(linksElem, item) {
var links = [];
if (item.HomePageUrl) {
links.push('' + Globalize.translate('ButtonWebsite') + '');
}
if (item.ExternalUrls) {
for (var i = 0, length = item.ExternalUrls.length; i < length; i++) {
var url = item.ExternalUrls[i];
links.push('' + url.Name + '');
}
}
if (links.length) {
var html = links.join(' / ');
html = Globalize.translate('ValueLinks', html);
linksElem.innerHTML = html;
linksElem.classList.remove('hide');
} else {
linksElem.classList.add('hide');
}
}
function renderImage(page, item, user) {
LibraryBrowser.renderDetailImage(page.querySelector('.detailImageContainer'), item, user.Policy.IsAdministrator && item.MediaType != 'Photo');
}
function refreshDetailImageUserData(elem, item) {
var detailImageProgressContainer = elem.querySelector('.detailImageProgressContainer');
detailImageProgressContainer.innerHTML = indicators.getProgressBarHtml(item);
}
function refreshImage(page, item, user) {
refreshDetailImageUserData(page.querySelector('.detailImageContainer'), item);
}
function setPeopleHeader(page, item) {
if (item.MediaType == "Audio" || item.Type == "MusicAlbum" || item.MediaType == "Book" || item.MediaType == "Photo") {
page.querySelector('#peopleHeader').innerHTML = Globalize.translate('HeaderPeople');
} else {
page.querySelector('#peopleHeader').innerHTML = Globalize.translate('HeaderCastAndCrew');
}
}
function renderNextUp(page, item, user) {
var section = page.querySelector('.nextUpSection');
if (item.Type != 'Series') {
section.classList.add('hide');
return;
}
ApiClient.getNextUpEpisodes({
SeriesId: item.Id,
UserId: user.Id
}).then(function (result) {
if (result.Items.length) {
section.classList.remove('hide');
} else {
section.classList.add('hide');
}
var html = cardBuilder.getCardsHtml({
items: result.Items,
shape: getThumbShape(false),
showTitle: true,
displayAsSpecial: item.Type == "Season" && item.IndexNumber,
overlayText: true,
lazy: true,
overlayPlayButton: true
});
var itemsContainer = section.querySelector('.nextUpItems');
itemsContainer.innerHTML = html;
ImageLoader.lazyChildren(itemsContainer);
});
}
function setInitialCollapsibleState(page, item, context, user) {
page.querySelector('.collectionItems').innerHTML = '';
if (item.Type == 'TvChannel') {
page.querySelector('#childrenCollapsible').classList.remove('hide');
renderChannelGuide(page, item, user);
}
else if (item.Type == 'Playlist') {
page.querySelector('#childrenCollapsible').classList.remove('hide');
renderPlaylistItems(page, item, user);
}
else if (item.Type == 'Studio' || item.Type == 'Person' || item.Type == 'Genre' || item.Type == 'MusicGenre' || item.Type == 'GameGenre' || item.Type == 'MusicArtist') {
page.querySelector('#childrenCollapsible').classList.remove('hide');
renderItemsByName(page, item, user);
}
else if (item.IsFolder) {
if (item.Type == "BoxSet") {
page.querySelector('#childrenCollapsible').classList.add('hide');
} else {
page.querySelector('#childrenCollapsible').classList.remove('hide');
}
renderChildren(page, item);
}
else {
page.querySelector('#childrenCollapsible').classList.add('hide');
}
if (item.Type == 'Series') {
renderNextUp(page, item, user);
} else {
page.querySelector('.nextUpSection').classList.add('hide');
}
if (item.MediaSources && item.MediaSources.length) {
renderMediaSources(page, item);
}
var chapters = item.Chapters || [];
if (!chapters.length) {
page.querySelector('#scenesCollapsible').classList.add('hide');
} else {
page.querySelector('#scenesCollapsible').classList.remove('hide');
renderScenes(page, item, user, 3);
}
if (!item.SpecialFeatureCount || item.SpecialFeatureCount == 0 || item.Type == "Series") {
page.querySelector('#specialsCollapsible').classList.add('hide');
} else {
page.querySelector('#specialsCollapsible').classList.remove('hide');
renderSpecials(page, item, user, 6);
}
if (!item.People || !item.People.length) {
page.querySelector('#castCollapsible').classList.add('hide');
} else {
page.querySelector('#castCollapsible').classList.remove('hide');
renderCast(page, item, context, enableScrollX() ? null : 12);
}
if (item.PartCount && item.PartCount > 1) {
page.querySelector('#additionalPartsCollapsible').classList.remove('hide');
renderAdditionalParts(page, item, user);
} else {
page.querySelector('#additionalPartsCollapsible').classList.add('hide');
}
page.querySelector('#themeSongsCollapsible').classList.add('hide');
page.querySelector('#themeVideosCollapsible').classList.add('hide');
if (item.Type == "MusicAlbum") {
renderMusicVideos(page, item, user);
} else {
page.querySelector('#musicVideosCollapsible').classList.add('hide');
}
renderThemeMedia(page, item, user);
if (enableScrollX()) {
renderCriticReviews(page, item);
} else {
renderCriticReviews(page, item, 1);
}
}
function renderOverview(elems, item) {
for (var i = 0, length = elems.length; i < length; i++) {
var elem = elems[i];
var overview = item.Overview || '';
if (overview) {
elem.innerHTML = overview;
elem.classList.remove('empty');
var anchors = elem.querySelectorAll('a');
for (var j = 0, length2 = anchors.length; j < length2; j++) {
anchors[j].setAttribute("target", "_blank");
}
} else {
elem.innerHTML = '';
elem.classList.add('empty');
}
}
}
function renderDetails(page, item, context, isStatic) {
renderSimilarItems(page, item, context);
renderMoreFromItems(page, item);
if (!isStatic) {
renderSiblingLinks(page, item, context);
}
var taglineElement = page.querySelector('.tagline');
if (item.Taglines && item.Taglines.length) {
taglineElement.classList.remove('hide');
taglineElement.innerHTML = item.Taglines[0];
} else {
taglineElement.classList.add('hide');
}
var topOverview = page.querySelector('.topOverview');
var bottomOverview = page.querySelector('.bottomOverview');
var seasonOnBottom = screen.availHeight < 800 || screen.availWidth < 600;
if (item.Type == 'MusicAlbum' || item.Type == 'MusicArtist' || (item.Type == 'Season' && seasonOnBottom)) {
renderOverview([bottomOverview], item);
topOverview.classList.add('hide');
bottomOverview.classList.remove('hide');
} else {
renderOverview([topOverview], item);
topOverview.classList.remove('hide');
bottomOverview.classList.add('hide');
}
renderAwardSummary(page.querySelector('#awardSummary'), item);
var i, length;
var itemMiscInfo = page.querySelectorAll('.itemMiscInfo');
for (i = 0, length = itemMiscInfo.length; i < length; i++) {
mediaInfo.fillPrimaryMediaInfo(itemMiscInfo[i], item, {
interactive: true,
episodeTitle: false
});
}
var itemGenres = page.querySelectorAll('.itemGenres');
for (i = 0, length = itemGenres.length; i < length; i++) {
renderGenres(itemGenres[i], item, null, isStatic);
}
renderStudios(page.querySelector('.itemStudios'), item, isStatic);
renderUserDataIcons(page, item);
renderLinks(page.querySelector('.itemExternalLinks'), item);
page.querySelector('.criticRatingScore').innerHTML = (item.CriticRating || '0') + '%';
if (item.CriticRatingSummary) {
page.querySelector('#criticRatingSummary').classList.remove('hide');
page.querySelector('.criticRatingSummaryText').innerHTML = item.CriticRatingSummary;
} else {
page.querySelector('#criticRatingSummary').classList.add('hide');
}
renderTags(page, item);
renderSeriesAirTime(page, item, isStatic);
var playersElement = page.querySelector('#players');
if (item.Players) {
playersElement.classList.remove('hide');
playersElement.innerHTML = item.Players + ' Player';
} else {
playersElement.classList.add('hide');
}
var artist = page.querySelectorAll('.artist');
for (i = 0, length = artist.length; i < length; i++) {
if (item.ArtistItems && item.ArtistItems.length && item.Type != "MusicAlbum") {
artist[i].classList.remove('hide');
artist[i].innerHTML = getArtistLinksHtml(item.ArtistItems, context);
} else {
artist[i].classList.add('hide');
}
}
if (item.MediaSources && item.MediaSources.length && item.Path) {
page.querySelector('.audioVideoMediaInfo').classList.remove('hide');
} else {
page.querySelector('.audioVideoMediaInfo').classList.add('hide');
}
if (item.MediaType == 'Photo') {
page.querySelector('.photoInfo').classList.remove('hide');
renderPhotoInfo(page, item);
} else {
page.querySelector('.photoInfo').classList.add('hide');
}
renderTabButtons(page, item);
}
function renderPhotoInfo(page, item) {
var html = '';
var attributes = [];
if (item.CameraMake) {
attributes.push(createAttribute(Globalize.translate('MediaInfoCameraMake'), item.CameraMake));
}
if (item.CameraModel) {
attributes.push(createAttribute(Globalize.translate('MediaInfoCameraModel'), item.CameraModel));
}
if (item.Altitude) {
attributes.push(createAttribute(Globalize.translate('MediaInfoAltitude'), item.Altitude.toFixed(1)));
}
if (item.Aperture) {
attributes.push(createAttribute(Globalize.translate('MediaInfoAperture'), 'F' + item.Aperture.toFixed(1)));
}
if (item.ExposureTime) {
var val = 1 / item.ExposureTime;
attributes.push(createAttribute(Globalize.translate('MediaInfoExposureTime'), '1/' + val + ' s'));
}
if (item.FocalLength) {
attributes.push(createAttribute(Globalize.translate('MediaInfoFocalLength'), item.FocalLength.toFixed(1) + ' mm'));
}
if (item.ImageOrientation) {
//attributes.push(createAttribute(Globalize.translate('MediaInfoOrientation'), item.ImageOrientation));
}
if (item.IsoSpeedRating) {
attributes.push(createAttribute(Globalize.translate('MediaInfoIsoSpeedRating'), item.IsoSpeedRating));
}
if (item.Latitude) {
attributes.push(createAttribute(Globalize.translate('MediaInfoLatitude'), item.Latitude.toFixed(1)));
}
if (item.Longitude) {
attributes.push(createAttribute(Globalize.translate('MediaInfoLongitude'), item.Longitude.toFixed(1)));
}
if (item.ShutterSpeed) {
attributes.push(createAttribute(Globalize.translate('MediaInfoShutterSpeed'), item.ShutterSpeed));
}
if (item.Software) {
attributes.push(createAttribute(Globalize.translate('MediaInfoSoftware'), item.Software));
}
html += attributes.join('
');
page.querySelector('.photoInfoContent').innerHTML = html;
}
function renderTabButtons(page, item) {
var elem = page.querySelector('.tabDetails');
var text = elem.textContent || elem.innerText || '';
if (text.trim()) {
page.querySelector('.detailsSection').classList.remove('hide');
} else {
page.querySelector('.detailsSection').classList.add('hide');
}
}
function getArtistLinksHtml(artists, context) {
var html = [];
for (var i = 0, length = artists.length; i < length; i++) {
var artist = artists[i];
html.push('' + artist.Name + '');
}
html = html.join(' / ');
if (artists.length == 1) {
return Globalize.translate('ValueArtist', html);
}
if (artists.length > 1) {
return Globalize.translate('ValueArtists', html);
}
return html;
}
function renderSiblingLinks(page, item, context) {
var lnkPreviousItem = page.querySelector('.lnkPreviousItem');
var lnkNextItem = page.querySelector('.lnkNextItem');
if ((item.Type != "Episode" && item.Type != "Season" && item.Type != "Audio" && item.Type != "Photo")) {
lnkNextItem.classList.add('hide');
lnkPreviousItem.classList.add('hide');
return;
}
var promise;
if (item.Type == "Season") {
promise = ApiClient.getSeasons(item.SeriesId, {
userId: Dashboard.getCurrentUserId(),
AdjacentTo: item.Id
});
}
else if (item.Type == "Episode" && item.SeasonId) {
// Use dedicated episodes endpoint
promise = ApiClient.getEpisodes(item.SeriesId, {
seasonId: item.SeasonId,
userId: Dashboard.getCurrentUserId(),
AdjacentTo: item.Id
});
} else {
promise = ApiClient.getItems(Dashboard.getCurrentUserId(), {
AdjacentTo: item.Id,
ParentId: item.ParentId,
SortBy: 'SortName'
});
}
context = context || '';
promise.then(function (result) {
var foundExisting = false;
for (var i = 0, length = result.Items.length; i < length; i++) {
var curr = result.Items[i];
if (curr.Id == item.Id) {
foundExisting = true;
}
else if (!foundExisting) {
lnkPreviousItem.classList.remove('hide');
lnkPreviousItem.href = 'itemdetails.html?id=' + curr.Id + '&context=' + context;
}
else {
lnkNextItem.classList.remove('hide');
lnkNextItem.href = 'itemdetails.html?id=' + curr.Id + '&context=' + context;
}
}
});
}
function enableScrollX() {
return browserInfo.mobile && AppInfo.enableAppLayouts && screen.availWidth <= 1000;
}
function getPortraitShape(scrollX) {
if (scrollX == null) {
scrollX = enableScrollX();
}
return scrollX ? 'overflowPortrait' : 'portrait';
}
function getSquareShape(scrollX) {
if (scrollX == null) {
scrollX = enableScrollX();
}
return scrollX ? 'overflowSquare' : 'square';
}
function getThumbShape(scrollX) {
if (scrollX == null) {
scrollX = enableScrollX();
}
return scrollX ? 'overflowBackdrop' : 'backdrop';
}
function renderMoreFromItems(page, item) {
var moreFromSection = page.querySelector('#moreFromSection');
if (!moreFromSection) {
return;
}
if (item.Type != 'MusicAlbum' || !item.AlbumArtists || !item.AlbumArtists.length) {
moreFromSection.classList.add('hide');
return;
}
ApiClient.getItems(Dashboard.getCurrentUserId(), {
IncludeItemTypes: "MusicAlbum",
ArtistIds: item.AlbumArtists[0].Id,
Recursive: true,
ExcludeItemIds: item.Id
}).then(function (result) {
if (!result.Items.length) {
moreFromSection.classList.add('hide');
return;
}
moreFromSection.classList.remove('hide');
moreFromSection.querySelector('.moreFromHeader').innerHTML = Globalize.translate('MoreFromValue', item.AlbumArtists[0].Name);
var html = '';
if (enableScrollX()) {
html += '
' + Globalize.translate('HeaderTags') + '
'; for (var i = 0, length = item.Tags.length; i < length; i++) { html += '