1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/controllers/shows/tvrecommended.js

389 lines
12 KiB
JavaScript
Raw Normal View History

2023-10-01 02:49:36 -04:00
import autoFocuser from 'components/autoFocuser';
import cardBuilder from 'components/cardbuilder/cardBuilder';
import layoutManager from 'components/layoutManager';
import loading from 'components/loading/loading';
import * as mainTabsManager from 'components/maintabsmanager';
import { playbackManager } from 'components/playback/playbackmanager';
import dom from 'scripts/dom';
import globalize from 'scripts/globalize';
import inputManager from 'scripts/inputManager';
import libraryMenu from 'scripts/libraryMenu';
import * as userSettings from 'scripts/settings/userSettings';
import { LibraryTab } from 'types/libraryTab';
import { getBackdropShape } from 'utils/card';
import Dashboard from 'utils/dashboard';
import Events from 'utils/events';
import 'elements/emby-itemscontainer/emby-itemscontainer';
import 'elements/emby-button/emby-button';
import 'styles/scrollstyles.scss';
2023-04-19 01:56:05 -04:00
function getTabs() {
return [{
name: globalize.translate('Shows')
}, {
name: globalize.translate('Suggestions')
}, {
name: globalize.translate('TabUpcoming')
}, {
name: globalize.translate('Genres')
}, {
name: globalize.translate('TabNetworks')
}, {
name: globalize.translate('Episodes')
}];
}
function getDefaultTabIndex(folderId) {
switch (userSettings.get('landing-' + folderId)) {
2023-05-09 10:28:29 -04:00
case LibraryTab.Suggestions:
2023-04-19 01:56:05 -04:00
return 1;
2023-05-09 10:28:29 -04:00
case LibraryTab.Upcoming:
2023-04-19 01:56:05 -04:00
return 2;
2023-05-09 10:28:29 -04:00
case LibraryTab.Genres:
2023-04-19 01:56:05 -04:00
return 3;
2023-05-09 10:28:29 -04:00
case LibraryTab.Networks:
2023-04-19 01:56:05 -04:00
return 4;
2023-05-09 10:28:29 -04:00
case LibraryTab.Episodes:
2023-04-19 01:56:05 -04:00
return 5;
default:
return 0;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function setScrollClasses(elem, scrollX) {
if (scrollX) {
elem.classList.add('hiddenScrollX');
2023-04-19 01:56:05 -04:00
if (layoutManager.tv) {
elem.classList.add('smoothScrollX');
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
elem.classList.add('scrollX');
elem.classList.remove('vertical-wrap');
} else {
elem.classList.remove('hiddenScrollX');
elem.classList.remove('smoothScrollX');
elem.classList.remove('scrollX');
elem.classList.add('vertical-wrap');
}
}
2023-04-19 01:56:05 -04:00
function initSuggestedTab(page, tabContent) {
const containers = tabContent.querySelectorAll('.itemsContainer');
2023-04-19 01:56:05 -04:00
for (let i = 0, length = containers.length; i < length; i++) {
setScrollClasses(containers[i], enableScrollX());
}
}
function loadSuggestionsTab(view, params, tabContent) {
const parentId = params.topParentId;
const userId = ApiClient.getCurrentUserId();
console.debug('loadSuggestionsTab');
loadResume(tabContent, userId, parentId);
loadLatest(tabContent, userId, parentId);
loadNextUp(tabContent, userId, parentId);
}
function loadResume(view, userId, parentId) {
const screenWidth = dom.getWindowSize().innerWidth;
const options = {
SortBy: 'DatePlayed',
SortOrder: 'Descending',
IncludeItemTypes: 'Episode',
Filters: 'IsResumable',
Limit: screenWidth >= 1600 ? 5 : 3,
Recursive: true,
2024-01-11 02:24:16 -05:00
Fields: 'PrimaryImageAspectRatio,MediaSourceCount',
2023-04-19 01:56:05 -04:00
CollapseBoxSetItems: false,
ParentId: parentId,
ImageTypeLimit: 1,
EnableImageTypes: 'Primary,Backdrop,Banner,Thumb',
EnableTotalRecordCount: false
};
ApiClient.getItems(userId, options).then(function (result) {
if (result.Items.length) {
view.querySelector('#resumableSection').classList.remove('hide');
} else {
2023-04-19 01:56:05 -04:00
view.querySelector('#resumableSection').classList.add('hide');
}
2023-04-19 01:56:05 -04:00
const allowBottomPadding = !enableScrollX();
const container = view.querySelector('#resumableItems');
cardBuilder.buildCards(result.Items, {
itemsContainer: container,
preferThumb: true,
inheritThumb: !userSettings.useEpisodeImagesInNextUpAndResume(),
2023-10-01 02:49:36 -04:00
shape: getBackdropShape(enableScrollX()),
2023-04-19 01:56:05 -04:00
scalable: true,
overlayPlayButton: true,
allowBottomPadding: allowBottomPadding,
cardLayout: false,
showTitle: true,
showYear: true,
centerText: true
});
loading.hide();
autoFocuser.autoFocus(view);
});
}
function loadLatest(view, userId, parentId) {
const options = {
userId: userId,
IncludeItemTypes: 'Episode',
Limit: 30,
2024-01-11 02:24:16 -05:00
Fields: 'PrimaryImageAspectRatio',
2023-04-19 01:56:05 -04:00
ParentId: parentId,
ImageTypeLimit: 1,
EnableImageTypes: 'Primary,Backdrop,Thumb'
};
ApiClient.getLatestItems(options).then(function (items) {
const section = view.querySelector('#latestItemsSection');
const allowBottomPadding = !enableScrollX();
const container = section.querySelector('#latestEpisodesItems');
cardBuilder.buildCards(items, {
parentContainer: section,
itemsContainer: container,
items: items,
shape: 'backdrop',
preferThumb: true,
showTitle: true,
showSeriesYear: true,
showParentTitle: true,
overlayText: false,
cardLayout: false,
allowBottomPadding: allowBottomPadding,
showUnplayedIndicator: false,
showChildCountIndicator: true,
centerText: true,
lazy: true,
overlayPlayButton: true,
lines: 2
});
loading.hide();
autoFocuser.autoFocus(view);
});
}
function loadNextUp(view, userId, parentId) {
const query = {
userId: userId,
Limit: 24,
2024-01-11 02:24:16 -05:00
Fields: 'PrimaryImageAspectRatio,DateCreated,MediaSourceCount',
2023-04-19 01:56:05 -04:00
ParentId: parentId,
ImageTypeLimit: 1,
EnableImageTypes: 'Primary,Backdrop,Thumb',
EnableTotalRecordCount: false
};
query.ParentId = libraryMenu.getTopParentId();
ApiClient.getNextUpEpisodes(query).then(function (result) {
if (result.Items.length) {
view.querySelector('.noNextUpItems').classList.add('hide');
} else {
view.querySelector('.noNextUpItems').classList.remove('hide');
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
const section = view.querySelector('#nextUpItemsSection');
const container = section.querySelector('#nextUpItems');
cardBuilder.buildCards(result.Items, {
parentContainer: section,
itemsContainer: container,
preferThumb: true,
inheritThumb: !userSettings.useEpisodeImagesInNextUpAndResume(),
shape: 'backdrop',
scalable: true,
showTitle: true,
showParentTitle: true,
overlayText: false,
centerText: true,
overlayPlayButton: true,
cardLayout: false
});
2023-04-19 01:56:05 -04:00
loading.hide();
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
autoFocuser.autoFocus(view);
});
}
2023-04-19 01:56:05 -04:00
function enableScrollX() {
return !layoutManager.desktop;
}
2023-04-19 01:56:05 -04:00
export default function (view, params) {
function onBeforeTabChange(e) {
preLoadTab(view, parseInt(e.detail.selectedTabIndex, 10));
}
2023-04-19 01:56:05 -04:00
function onTabChange(e) {
const newIndex = parseInt(e.detail.selectedTabIndex, 10);
loadTab(view, newIndex);
}
2023-04-19 01:56:05 -04:00
function getTabContainers() {
return view.querySelectorAll('.pageTabContent');
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function initTabs() {
mainTabsManager.setTabs(view, currentTabIndex, getTabs, getTabContainers, onBeforeTabChange, onTabChange);
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function getTabController(page, index, callback) {
let depends;
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
switch (index) {
case 0:
depends = 'tvshows';
break;
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
case 1:
depends = 'tvrecommended';
break;
2023-04-19 01:56:05 -04:00
case 2:
depends = 'tvupcoming';
break;
2023-04-19 01:56:05 -04:00
case 3:
depends = 'tvgenres';
break;
2023-04-19 01:56:05 -04:00
case 4:
depends = 'tvstudios';
break;
2023-04-19 01:56:05 -04:00
case 5:
depends = 'episodes';
break;
}
2023-07-06 11:49:55 -04:00
import(`../shows/${depends}`).then(({ default: ControllerFactory }) => {
2023-04-19 01:56:05 -04:00
let tabContent;
2023-04-19 01:56:05 -04:00
if (index === 1) {
tabContent = view.querySelector(".pageTabContent[data-index='" + index + "']");
self.tabContent = tabContent;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
let controller = tabControllers[index];
if (!controller) {
tabContent = view.querySelector(".pageTabContent[data-index='" + index + "']");
if (index === 1) {
2023-04-19 01:56:05 -04:00
controller = self;
} else {
2023-07-06 11:49:55 -04:00
controller = new ControllerFactory(view, params, tabContent);
}
2023-04-19 01:56:05 -04:00
tabControllers[index] = controller;
2023-04-19 01:56:05 -04:00
if (controller.initTab) {
controller.initTab();
}
}
2023-04-19 01:56:05 -04:00
callback(controller);
});
}
2023-04-19 01:56:05 -04:00
function preLoadTab(page, index) {
getTabController(page, index, function (controller) {
if (renderedTabs.indexOf(index) == -1 && controller.preRender) {
controller.preRender();
}
});
}
2023-04-19 01:56:05 -04:00
function loadTab(page, index) {
currentTabIndex = index;
getTabController(page, index, function (controller) {
if (renderedTabs.indexOf(index) == -1) {
renderedTabs.push(index);
controller.renderTab();
}
});
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onPlaybackStop(e, state) {
if (state.NowPlayingItem && state.NowPlayingItem.MediaType == 'Video') {
renderedTabs = [];
mainTabsManager.getTabsElement().triggerTabChange();
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onWebSocketMessage(e, data) {
const msg = data;
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
if (msg.MessageType === 'UserDataChanged' && msg.Data.UserId == ApiClient.getCurrentUserId()) {
renderedTabs = [];
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onInputCommand(e) {
if (e.detail.command === 'search') {
e.preventDefault();
Dashboard.navigate('search.html?collectionType=tv&parentId=' + params.topParentId);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
const self = this;
let currentTabIndex = parseInt(params.tab || getDefaultTabIndex(params.topParentId), 10);
const suggestionsTabIndex = 1;
self.initTab = function () {
const tabContent = view.querySelector(".pageTabContent[data-index='" + suggestionsTabIndex + "']");
initSuggestedTab(view, tabContent);
};
self.renderTab = function () {
const tabContent = view.querySelector(".pageTabContent[data-index='" + suggestionsTabIndex + "']");
loadSuggestionsTab(view, params, tabContent);
};
const tabControllers = [];
let renderedTabs = [];
view.addEventListener('viewshow', function () {
initTabs();
if (!view.getAttribute('data-title')) {
const parentId = params.topParentId;
if (parentId) {
ApiClient.getItem(ApiClient.getCurrentUserId(), parentId).then(function (item) {
view.setAttribute('data-title', item.Name);
libraryMenu.setTitle(item.Name);
});
} else {
view.setAttribute('data-title', globalize.translate('Shows'));
libraryMenu.setTitle(globalize.translate('Shows'));
2018-10-23 01:05:09 +03:00
}
}
2023-04-19 01:56:05 -04:00
Events.on(playbackManager, 'playbackstop', onPlaybackStop);
Events.on(ApiClient, 'message', onWebSocketMessage);
inputManager.on(window, onInputCommand);
});
view.addEventListener('viewbeforehide', function () {
inputManager.off(window, onInputCommand);
Events.off(playbackManager, 'playbackstop', onPlaybackStop);
Events.off(ApiClient, 'message', onWebSocketMessage);
});
view.addEventListener('viewdestroy', function () {
tabControllers.forEach(function (t) {
if (t.destroy) {
t.destroy();
2018-10-23 01:05:09 +03:00
}
});
2023-04-19 01:56:05 -04:00
});
}