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

418 lines
13 KiB
JavaScript
Raw Normal View History

2016-05-23 10:54:38 -07:00
define(['libraryBrowser', 'scrollStyles'], function (libraryBrowser) {
2013-04-01 08:59:56 -07:00
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) {
var options = {
IncludeItemTypes: "Movie",
2016-05-17 12:18:50 -07:00
Limit: 18,
2015-04-02 09:26:42 -07:00
Fields: "PrimaryImageAspectRatio,MediaSourceCount,SyncInfo",
ParentId: parentId,
ImageTypeLimit: 1,
2016-05-08 20:13:38 -07:00
EnableImageTypes: "Primary,Backdrop,Banner,Thumb",
EnableTotalRecordCount: false
2015-04-02 09:26:42 -07:00
};
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') {
2016-04-03 11:47:49 -07:00
html += libraryBrowser.getPosterViewHtml({
2015-05-12 21:55:19 -07:00
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
2016-04-03 11:47:49 -07:00
html += libraryBrowser.getPosterViewHtml({
2015-05-12 21:55:19 -07:00
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-04-02 09:26:42 -07:00
});
}
2015-05-07 07:04:10 -07:00
function loadResume(page, userId, parentId) {
2016-05-22 11:09:09 -07:00
var screenWidth = window.innerWidth;
2015-05-07 07:04:10 -07:00
var options = {
SortBy: "DatePlayed",
SortOrder: "Descending",
IncludeItemTypes: "Movie",
Filters: "IsResumable",
2016-05-22 11:09:09 -07:00
Limit: screenWidth >= 1920 ? 5 : (screenWidth >= 1600 ? 4 : 3),
2015-05-07 07:04:10 -07:00
Recursive: true,
Fields: "PrimaryImageAspectRatio,MediaSourceCount,SyncInfo",
CollapseBoxSetItems: false,
ParentId: parentId,
ImageTypeLimit: 1,
2016-05-08 20:13:38 -07:00
EnableImageTypes: "Primary,Backdrop,Banner,Thumb",
EnableTotalRecordCount: false
2015-05-07 07:04:10 -07:00
};
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) {
2016-05-23 10:54:38 -07:00
page.querySelector('#resumableSection').classList.remove('hide');
2015-05-07 07:04:10 -07:00
} else {
2016-05-23 10:54:38 -07:00
page.querySelector('#resumableSection').classList.add('hide');
2015-05-07 07:04:10 -07:00
}
2015-05-12 21:55:19 -07:00
var view = getResumeView();
var html = '';
if (view == 'ThumbCard') {
2016-04-03 11:47:49 -07:00
html += libraryBrowser.getPosterViewHtml({
2015-05-12 21:55:19 -07:00
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') {
2016-04-03 11:47:49 -07:00
html += libraryBrowser.getPosterViewHtml({
2015-05-12 21:55:19 -07:00
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') {
2016-04-03 11:47:49 -07:00
html += libraryBrowser.getPosterViewHtml({
2015-05-12 21:55:19 -07:00
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') {
2016-04-03 11:47:49 -07:00
html += libraryBrowser.getPosterViewHtml({
2015-05-12 21:55:19 -07:00
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,
2016-02-12 13:31:48 -07:00
showDetailsMenu: true,
overlayPlayButton: 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
2016-05-23 10:54:38 -07:00
var screenWidth = window.innerWidth;
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,
2016-05-17 12:18:50 -07:00
categoryLimit: 6,
2016-06-28 21:25:48 -07:00
ItemLimit: screenWidth >= 1920 ? 8 : (screenWidth >= 1600 ? 8 : (screenWidth >= 1200 ? 6 : 5)),
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
2016-05-23 10:54:38 -07:00
page.querySelector('.noItemsMessage').classList.remove('hide');
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
2016-05-23 10:54:38 -07:00
page.querySelector('.noItemsMessage').classList.add('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');
2016-05-23 10:54:38 -07:00
for (var i = 0, length = containers.length; i < length; i++) {
if (enableScrollX()) {
containers[i].classList.add('hiddenScrollX');
} else {
containers[i].classList.remove('hiddenScrollX');
}
2015-05-16 12:09:02 -07:00
2016-05-23 10:54:38 -07:00
libraryBrowser.createCardMenus(containers[i]);
}
2015-09-28 20:35:50 -07:00
}
2016-04-03 11:47:49 -07:00
function loadSuggestionsTab(view, params, tabContent) {
2015-09-28 20:35:50 -07:00
2016-04-03 11:47:49 -07:00
var parentId = params.topParentId;
2015-09-28 20:35:50 -07:00
var userId = Dashboard.getCurrentUserId();
2016-04-03 11:47:49 -07:00
console.log('loadSuggestionsTab');
loadResume(tabContent, userId, parentId);
loadLatest(tabContent, userId, parentId);
2015-05-07 07:04:10 -07:00
2016-04-03 11:47:49 -07:00
if (AppInfo.enableMovieHomeSuggestions) {
loadSuggestions(tabContent, userId, parentId);
2015-05-07 07:04:10 -07:00
}
2015-08-15 11:17:22 -07:00
}
2016-04-03 11:47:49 -07:00
return function (view, params) {
var self = this;
2015-08-18 09:11:56 -07:00
2016-05-15 09:30:32 -07:00
self.initTab = function () {
var tabContent = view.querySelector('.pageTabContent[data-index=\'' + 0 + '\']');
2016-04-03 11:47:49 -07:00
initSuggestedTab(view, tabContent);
};
2015-08-15 11:17:22 -07:00
2016-04-03 11:47:49 -07:00
self.renderTab = function () {
2016-05-15 09:30:32 -07:00
var tabContent = view.querySelector('.pageTabContent[data-index=\'' + 0 + '\']');
2016-04-03 11:47:49 -07:00
loadSuggestionsTab(view, params, tabContent);
};
2015-08-15 11:17:22 -07:00
2016-05-23 10:54:38 -07:00
libraryBrowser.createCardMenus(view.querySelector('.recommendations'));
2015-08-15 11:17:22 -07:00
2016-05-15 09:30:32 -07:00
var mdlTabs = view.querySelector('.libraryViewNav');
2016-05-09 12:27:38 -07:00
2015-09-01 08:31:50 -07:00
var baseUrl = 'movies.html';
2016-04-03 11:47:49 -07:00
var topParentId = params.topParentId;
2015-09-01 08:31:50 -07:00
if (topParentId) {
baseUrl += '?topParentId=' + topParentId;
}
2015-08-15 11:17:22 -07:00
2016-05-18 14:46:56 -07:00
libraryBrowser.configurePaperLibraryTabs(view, mdlTabs, view.querySelectorAll('.pageTabContent'), [0, 3, 4, 5]);
2016-04-03 11:47:49 -07:00
var tabControllers = [];
var renderedTabs = [];
2016-06-29 10:43:34 -07:00
function getTabController(page, index, callback) {
2016-04-03 11:47:49 -07:00
var depends = [];
switch (index) {
case 0:
break;
case 1:
depends.push('scripts/movies');
break;
case 2:
depends.push('scripts/movietrailers');
break;
case 3:
depends.push('scripts/moviecollections');
break;
case 4:
depends.push('scripts/moviegenres');
break;
case 5:
depends.push('scripts/moviestudios');
break;
default:
break;
}
2015-08-15 11:17:22 -07:00
2016-04-03 11:47:49 -07:00
require(depends, function (controllerFactory) {
2016-06-29 10:43:34 -07:00
var tabContent;
2016-04-03 11:47:49 -07:00
if (index == 0) {
2016-06-29 10:43:34 -07:00
tabContent = view.querySelector('.pageTabContent[data-index=\'' + index + '\']');
2016-04-03 11:47:49 -07:00
self.tabContent = tabContent;
}
var controller = tabControllers[index];
if (!controller) {
2016-06-29 10:43:34 -07:00
tabContent = view.querySelector('.pageTabContent[data-index=\'' + index + '\']');
2016-04-03 11:47:49 -07:00
controller = index ? new controllerFactory(view, params, tabContent) : self;
tabControllers[index] = controller;
if (controller.initTab) {
controller.initTab();
}
}
2015-08-15 11:17:22 -07:00
2016-06-29 10:43:34 -07:00
callback(controller);
});
}
function preLoadTab(page, index) {
getTabController(page, index, function (controller) {
if (renderedTabs.indexOf(index) == -1) {
if (controller.preRender) {
controller.preRender();
}
}
});
}
function loadTab(page, index) {
getTabController(page, index, function (controller) {
2016-04-03 11:47:49 -07:00
if (renderedTabs.indexOf(index) == -1) {
renderedTabs.push(index);
controller.renderTab();
}
});
}
2015-08-15 11:17:22 -07:00
2016-06-29 10:43:34 -07:00
mdlTabs.addEventListener('beforetabchange', function (e) {
preLoadTab(view, parseInt(e.detail.selectedTabIndex));
});
2016-05-09 12:27:38 -07:00
mdlTabs.addEventListener('tabchange', function (e) {
2016-04-03 11:47:49 -07:00
loadTab(view, parseInt(e.detail.selectedTabIndex));
});
2015-08-15 11:17:22 -07:00
2016-04-03 11:47:49 -07:00
view.addEventListener('viewbeforeshow', function (e) {
if (!view.getAttribute('data-title')) {
2015-08-15 11:17:22 -07:00
2016-04-03 11:47:49 -07:00
var parentId = params.topParentId;
2015-08-15 11:17:22 -07:00
2016-04-03 11:47:49 -07:00
if (parentId) {
2014-05-10 22:11:53 -07:00
2016-04-03 11:47:49 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), parentId).then(function (item) {
2015-09-20 11:17:57 -07:00
2016-04-03 11:47:49 -07:00
view.setAttribute('data-title', item.Name);
LibraryMenu.setTitle(item.Name);
});
2015-09-20 11:17:57 -07:00
2013-04-01 08:59:56 -07:00
2016-04-03 11:47:49 -07:00
} else {
view.setAttribute('data-title', Globalize.translate('TabMovies'));
LibraryMenu.setTitle(Globalize.translate('TabMovies'));
}
}
});
2015-09-20 11:17:57 -07:00
2016-05-25 10:03:52 -07:00
function onPlaybackStop(e, state) {
if (state.NowPlayingItem && state.NowPlayingItem.MediaType == 'Video') {
renderedTabs = [];
mdlTabs.dispatchEvent(new CustomEvent("tabchange", {
detail: {
selectedTabIndex: libraryBrowser.selectedTab(mdlTabs)
}
}));
}
}
2016-04-03 11:47:49 -07:00
view.addEventListener('viewshow', function (e) {
Events.on(MediaController, 'playbackstop', onPlaybackStop);
});
2015-09-20 11:17:57 -07:00
2016-04-03 11:47:49 -07:00
view.addEventListener('viewbeforehide', function (e) {
Events.off(MediaController, 'playbackstop', onPlaybackStop);
});
};
2015-09-20 11:17:57 -07:00
});