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

333 lines
11 KiB
JavaScript
Raw Normal View History

2016-07-29 13:06:58 -07:00
define(['libraryBrowser', 'components/categorysyncbuttons', 'cardBuilder', 'scrollStyles', 'emby-itemscontainer'], function (libraryBrowser, categorysyncbuttons, cardBuilder) {
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
2016-07-29 13:06:58 -07:00
var container = page.querySelector('#recentlyAddedItems');
cardBuilder.buildCards(items, {
itemsContainer: container,
shape: getPortraitShape(),
scalable: true,
overlayPlayButton: true
});
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
}
2016-07-29 13:06:58 -07:00
var container = page.querySelector('#resumableItems');
cardBuilder.buildCards(result.Items, {
itemsContainer: container,
preferThumb: true,
shape: getThumbShape(),
scalable: true,
overlayPlayButton: true
});
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()) {
2016-07-29 13:06:58 -07:00
html += '<div is="emby-itemscontainer" class="itemsContainer hiddenScrollX">';
2015-05-16 12:09:02 -07:00
} else {
2016-07-29 22:25:07 -07:00
html += '<div is="emby-itemscontainer" class="itemsContainer vertical-wrap">';
2015-05-16 12:09:02 -07:00
}
2015-05-12 21:55:19 -07:00
2016-07-29 13:06:58 -07:00
html += cardBuilder.getCardsHtml(recommendation.Items, {
shape: getPortraitShape(),
scalable: 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');
2016-07-29 22:25:07 -07:00
containers[i].classList.remove('vertical-wrap');
2016-05-23 10:54:38 -07:00
} else {
containers[i].classList.remove('hiddenScrollX');
2016-07-29 22:25:07 -07:00
containers[i].classList.add('vertical-wrap');
2016-05-23 10:54:38 -07:00
}
}
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-07-22 10:30:39 -07:00
loadSuggestions(tabContent, userId, parentId);
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 () {
2016-07-18 20:57:55 -07:00
2016-05-15 09:30:32 -07:00
var tabContent = view.querySelector('.pageTabContent[data-index=\'' + 0 + '\']');
2016-07-18 20:57:55 -07:00
categorysyncbuttons.init(tabContent);
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-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
});