jellyfin-web/dashboard-ui/scripts/moviesrecommended.js

393 lines
12 KiB
JavaScript
Raw Normal View History

2013-04-01 08:59:56 -07:00
(function ($, document) {
2015-05-12 21:55:19 -07:00
function getView() {
2015-05-16 12:09:02 -07:00
2015-05-29 16:51:33 -07:00
return 'Poster';
2015-05-12 21:55:19 -07:00
}
function getResumeView() {
2015-05-29 16:51:33 -07:00
return 'Thumb';
2015-05-12 21:55:19 -07:00
}
2015-05-16 12:09:02 -07:00
function enableScrollX() {
2015-12-14 08:43:03 -07:00
return browserInfo.mobile && AppInfo.enableAppLayouts;
2015-05-16 12:09:02 -07:00
}
function getPortraitShape() {
return enableScrollX() ? 'overflowPortrait' : 'portrait';
}
function getThumbShape() {
return enableScrollX() ? 'overflowBackdrop' : 'backdrop';
}
2015-04-02 09:26:42 -07:00
function loadLatest(page, userId, parentId) {
2015-05-12 21:55:19 -07:00
var limit = 18;
2015-05-07 07:04:10 -07:00
2015-04-02 09:26:42 -07:00
var options = {
IncludeItemTypes: "Movie",
2015-05-07 07:04:10 -07:00
Limit: limit,
2015-04-02 09:26:42 -07:00
Fields: "PrimaryImageAspectRatio,MediaSourceCount,SyncInfo",
ParentId: parentId,
ImageTypeLimit: 1,
EnableImageTypes: "Primary,Backdrop,Banner,Thumb"
};
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(ApiClient.getUrl('Users/' + userId + '/Items/Latest', options)).then(function (items) {
2015-04-02 09:26:42 -07:00
2015-05-12 21:55:19 -07:00
var view = getView();
var html = '';
if (view == 'PosterCard') {
html += LibraryBrowser.getPosterViewHtml({
items: items,
lazy: true,
2015-05-16 12:09:02 -07:00
shape: getPortraitShape(),
2015-05-12 21:55:19 -07:00
overlayText: false,
showTitle: true,
showYear: true,
2015-05-14 19:16:57 -07:00
cardLayout: true,
showDetailsMenu: true
2015-05-12 21:55:19 -07:00
});
} else if (view == 'Poster') {
2015-05-16 12:09:02 -07:00
2015-05-12 21:55:19 -07:00
html += LibraryBrowser.getPosterViewHtml({
items: items,
2015-05-16 12:09:02 -07:00
shape: getPortraitShape(),
2015-05-12 21:55:19 -07:00
centerText: true,
lazy: true,
overlayText: false,
2016-02-12 13:26:03 -07:00
showDetailsMenu: true,
overlayPlayButton: true
2015-05-12 21:55:19 -07:00
});
}
2015-04-02 09:26:42 -07:00
2015-09-28 20:35:50 -07:00
var recentlyAddedItems = page.querySelector('#recentlyAddedItems');
recentlyAddedItems.innerHTML = html;
ImageLoader.lazyChildren(recentlyAddedItems);
2015-08-15 11:46:57 -07:00
LibraryBrowser.setLastRefreshed(page);
2015-04-02 09:26:42 -07:00
});
}
2015-05-07 07:04:10 -07:00
function loadResume(page, userId, parentId) {
var screenWidth = $(window).width();
var options = {
SortBy: "DatePlayed",
SortOrder: "Descending",
IncludeItemTypes: "Movie",
Filters: "IsResumable",
Limit: screenWidth >= 1920 ? 6 : (screenWidth >= 1600 ? 4 : 3),
Recursive: true,
Fields: "PrimaryImageAspectRatio,MediaSourceCount,SyncInfo",
CollapseBoxSetItems: false,
ParentId: parentId,
ImageTypeLimit: 1,
EnableImageTypes: "Primary,Backdrop,Banner,Thumb"
};
2015-12-14 08:43:03 -07:00
ApiClient.getItems(userId, options).then(function (result) {
2015-05-07 07:04:10 -07:00
if (result.Items.length) {
$('#resumableSection', page).show();
} else {
$('#resumableSection', page).hide();
}
2015-05-12 21:55:19 -07:00
var view = getResumeView();
var html = '';
if (view == 'ThumbCard') {
html += LibraryBrowser.getPosterViewHtml({
items: result.Items,
preferThumb: true,
2015-05-16 12:09:02 -07:00
shape: getThumbShape(),
2015-05-12 21:55:19 -07:00
showTitle: true,
showYear: true,
lazy: true,
2015-05-14 19:16:57 -07:00
cardLayout: true,
showDetailsMenu: true
2015-05-07 07:04:10 -07:00
2015-05-12 21:55:19 -07:00
});
} else if (view == 'Thumb') {
html += LibraryBrowser.getPosterViewHtml({
items: result.Items,
preferThumb: true,
2015-05-16 12:09:02 -07:00
shape: getThumbShape(),
2015-05-12 21:55:19 -07:00
overlayText: true,
2015-10-25 22:29:32 -07:00
showTitle: false,
2015-05-14 19:16:57 -07:00
lazy: true,
2015-07-17 15:32:00 -07:00
showDetailsMenu: true,
overlayPlayButton: true
2015-05-12 21:55:19 -07:00
});
}
2015-12-14 08:43:03 -07:00
var resumableItems = page.querySelector('#resumableItems');
resumableItems.innerHTML = html;
ImageLoader.lazyChildren(resumableItems);
2015-05-07 07:04:10 -07:00
});
}
function getRecommendationHtml(recommendation) {
2013-04-01 08:59:56 -07:00
var html = '';
2014-01-30 14:23:54 -07:00
var title = '';
2014-05-10 22:11:53 -07:00
switch (recommendation.RecommendationType) {
2014-05-10 22:11:53 -07:00
case 'SimilarToRecentlyPlayed':
2014-05-30 12:23:56 -07:00
title = Globalize.translate('RecommendationBecauseYouWatched').replace("{0}", recommendation.BaselineItemName);
break;
case 'SimilarToLikedItem':
2014-05-30 12:23:56 -07:00
title = Globalize.translate('RecommendationBecauseYouLike').replace("{0}", recommendation.BaselineItemName);
break;
case 'HasDirectorFromRecentlyPlayed':
case 'HasLikedDirector':
2014-05-30 12:23:56 -07:00
title = Globalize.translate('RecommendationDirectedBy').replace("{0}", recommendation.BaselineItemName);
break;
case 'HasActorFromRecentlyPlayed':
case 'HasLikedActor':
2014-05-30 12:23:56 -07:00
title = Globalize.translate('RecommendationStarring').replace("{0}", recommendation.BaselineItemName);
break;
}
2014-05-10 22:11:53 -07:00
2015-05-16 12:09:02 -07:00
html += '<div class="homePageSection">';
html += '<h1 class="listHeader">' + title + '</h1>';
2013-04-01 08:59:56 -07:00
2015-05-16 12:09:02 -07:00
if (enableScrollX()) {
html += '<div class="hiddenScrollX">';
} else {
html += '<div>';
}
2015-05-12 21:55:19 -07:00
var view = getView();
if (view == 'PosterCard') {
html += LibraryBrowser.getPosterViewHtml({
items: recommendation.Items,
lazy: true,
2015-05-16 12:09:02 -07:00
shape: getPortraitShape(),
2015-05-12 21:55:19 -07:00
overlayText: false,
showTitle: true,
showYear: true,
2015-05-14 19:16:57 -07:00
cardLayout: true,
showDetailsMenu: true
2015-05-12 21:55:19 -07:00
});
} else if (view == 'Poster') {
html += LibraryBrowser.getPosterViewHtml({
items: recommendation.Items,
2015-05-16 12:09:02 -07:00
shape: getPortraitShape(),
2015-05-12 21:55:19 -07:00
centerText: true,
lazy: true,
2015-05-14 19:16:57 -07:00
showDetailsMenu: true
2015-05-12 21:55:19 -07:00
});
}
html += '</div>';
2015-05-16 12:09:02 -07:00
html += '</div>';
return html;
}
2014-05-10 22:11:53 -07:00
2015-05-07 07:04:10 -07:00
function loadSuggestions(page, userId, parentId) {
2014-05-01 19:54:33 -07:00
var screenWidth = $(window).width();
2013-04-01 08:59:56 -07:00
var url = ApiClient.getUrl("Movies/Recommendations", {
2014-05-10 22:11:53 -07:00
2015-04-02 09:26:42 -07:00
userId: userId,
categoryLimit: screenWidth >= 1200 ? 4 : 3,
2015-04-15 20:23:13 -07:00
ItemLimit: screenWidth >= 1920 ? 9 : (screenWidth >= 1600 ? 8 : (screenWidth >= 1200 ? 7 : 6)),
2014-12-10 23:20:28 -07:00
Fields: "PrimaryImageAspectRatio,MediaSourceCount,SyncInfo",
2014-11-30 12:01:33 -07:00
ImageTypeLimit: 1,
2014-12-01 05:43:34 -07:00
EnableImageTypes: "Primary,Backdrop,Banner,Thumb"
});
2013-04-04 21:15:00 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(url).then(function (recommendations) {
2013-04-04 21:15:00 -07:00
if (!recommendations.length) {
2013-04-04 21:15:00 -07:00
2014-05-30 12:23:56 -07:00
$('.noItemsMessage', page).show();
2015-09-28 20:35:50 -07:00
page.querySelector('.recommendations').innerHTML = '';
return;
2013-04-04 21:15:00 -07:00
}
var html = recommendations.map(getRecommendationHtml).join('');
2013-04-04 21:15:00 -07:00
2014-05-30 12:23:56 -07:00
$('.noItemsMessage', page).hide();
2015-09-28 20:35:50 -07:00
var recs = page.querySelector('.recommendations');
recs.innerHTML = html;
ImageLoader.lazyChildren(recs);
2013-04-04 21:15:00 -07:00
});
2015-05-07 07:04:10 -07:00
}
2015-09-28 20:35:50 -07:00
function initSuggestedTab(page, tabContent) {
2015-05-07 07:04:10 -07:00
2015-08-18 09:11:56 -07:00
var containers = tabContent.querySelectorAll('.itemsContainer');
2015-05-16 12:09:02 -07:00
if (enableScrollX()) {
2015-06-28 07:45:21 -07:00
$(containers).addClass('hiddenScrollX');
2015-05-16 12:09:02 -07:00
} else {
2015-06-28 07:45:21 -07:00
$(containers).removeClass('hiddenScrollX');
2015-05-16 12:09:02 -07:00
}
2015-09-28 20:35:50 -07:00
$(containers).createCardMenus();
}
function loadSuggestionsTab(page, tabContent) {
var parentId = LibraryMenu.getTopParentId();
var userId = Dashboard.getCurrentUserId();
2015-08-18 09:11:56 -07:00
if (LibraryBrowser.needsRefresh(tabContent)) {
2015-08-24 20:13:04 -07:00
console.log('loadSuggestionsTab');
2015-08-18 09:11:56 -07:00
loadResume(tabContent, userId, parentId);
loadLatest(tabContent, userId, parentId);
2015-05-07 07:04:10 -07:00
2015-06-29 19:52:23 -07:00
if (AppInfo.enableMovieHomeSuggestions) {
2015-08-18 09:11:56 -07:00
loadSuggestions(tabContent, userId, parentId);
2015-06-29 19:52:23 -07:00
}
2015-05-07 07:04:10 -07:00
}
2015-08-15 11:17:22 -07:00
}
function loadTab(page, index) {
2015-08-18 09:11:56 -07:00
var tabContent = page.querySelector('.pageTabContent[data-index=\'' + index + '\']');
var depends = [];
var scope = 'MoviesPage';
var renderMethod = '';
var initMethod = '';
2015-08-15 11:17:22 -07:00
switch (index) {
case 0:
2015-09-28 20:35:50 -07:00
initMethod = 'initSuggestedTab';
2015-08-18 09:11:56 -07:00
renderMethod = 'renderSuggestedTab';
break;
case 1:
depends.push('scripts/movies');
2015-08-25 20:09:16 -07:00
depends.push('scripts/queryfilters');
2015-08-18 09:11:56 -07:00
renderMethod = 'renderMoviesTab';
initMethod = 'initMoviesTab';
break;
case 2:
depends.push('scripts/movietrailers');
renderMethod = 'renderTrailerTab';
initMethod = 'initTrailerTab';
break;
case 3:
depends.push('scripts/moviecollections');
renderMethod = 'renderCollectionsTab';
initMethod = 'initCollectionsTab';
break;
case 4:
depends.push('scripts/moviegenres');
renderMethod = 'renderGenresTab';
break;
case 5:
depends.push('scripts/moviestudios');
renderMethod = 'renderStudiosTab';
2015-08-15 11:17:22 -07:00
break;
default:
break;
}
2015-08-18 09:11:56 -07:00
require(depends, function () {
if (initMethod && !tabContent.initComplete) {
window[scope][initMethod](page, tabContent);
tabContent.initComplete = true;
}
window[scope][renderMethod](page, tabContent);
});
2015-08-15 11:17:22 -07:00
}
2015-08-18 09:11:56 -07:00
window.MoviesPage = window.MoviesPage || {};
window.MoviesPage.renderSuggestedTab = loadSuggestionsTab;
2015-09-28 20:35:50 -07:00
window.MoviesPage.initSuggestedTab = initSuggestedTab;
2015-08-18 09:11:56 -07:00
2015-09-20 11:17:57 -07:00
pageIdOn('pageinit', "moviesPage", function () {
2015-08-15 11:17:22 -07:00
var page = this;
$('.recommendations', page).createCardMenus();
var tabs = page.querySelector('paper-tabs');
var pages = page.querySelector('neon-animated-pages');
2015-09-01 08:31:50 -07:00
var baseUrl = 'movies.html';
var topParentId = LibraryMenu.getTopParentId();
if (topParentId) {
baseUrl += '?topParentId=' + topParentId;
}
2015-08-15 11:17:22 -07:00
2015-09-01 08:31:50 -07:00
LibraryBrowser.configurePaperLibraryTabs(page, tabs, pages, baseUrl);
2015-08-15 11:17:22 -07:00
2015-12-14 08:43:03 -07:00
pages.addEventListener('tabchange', function (e) {
loadTab(page, parseInt(e.target.selected));
2015-08-15 11:17:22 -07:00
});
2015-09-20 11:17:57 -07:00
});
2015-08-15 11:17:22 -07:00
2015-12-14 08:43:03 -07:00
pageIdOn('pagebeforeshow', "moviesPage", function () {
2015-08-15 11:17:22 -07:00
var page = this;
if (!page.getAttribute('data-title')) {
var parentId = LibraryMenu.getTopParentId();
if (parentId) {
2015-12-14 08:43:03 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), parentId).then(function (item) {
2015-08-15 11:17:22 -07:00
page.setAttribute('data-title', item.Name);
LibraryMenu.setTitle(item.Name);
});
} else {
page.setAttribute('data-title', Globalize.translate('TabMovies'));
LibraryMenu.setTitle(Globalize.translate('TabMovies'));
}
}
2014-05-10 22:11:53 -07:00
2015-12-23 10:46:01 -07:00
Events.on(MediaController, 'playbackstop', onPlaybackStop);
2015-09-20 11:17:57 -07:00
});
pageIdOn('pagebeforehide', "moviesPage", function () {
var page = this;
2015-12-23 10:46:01 -07:00
Events.off(MediaController, 'playbackstop', onPlaybackStop);
2013-04-01 08:59:56 -07:00
});
2015-09-20 11:17:57 -07:00
function onPlaybackStop(e, state) {
if (state.NowPlayingItem && state.NowPlayingItem.MediaType == 'Video') {
var page = $($.mobile.activePage)[0];
var pages = page.querySelector('neon-animated-pages');
2015-12-14 08:43:03 -07:00
pages.dispatchEvent(new CustomEvent("tabchange", {}));
2015-09-20 11:17:57 -07:00
}
}
2013-04-01 08:59:56 -07:00
})(jQuery, document);