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

188 lines
6.3 KiB
JavaScript
Raw Normal View History

import layoutManager from 'components/layoutManager';
2023-10-01 02:49:36 -04:00
import globalize from 'scripts/globalize';
2023-10-02 12:22:36 -04:00
import { DEFAULT_SECTIONS, HomeSectionType } from 'types/homeSectionType';
2023-10-01 02:49:36 -04:00
import Dashboard from 'utils/dashboard';
2023-10-02 12:22:36 -04:00
import { loadRecordings } from './sections/activeRecordings';
import { loadLibraryButtons } from './sections/libraryButtons';
import { loadLibraryTiles } from './sections/libraryTiles';
import { loadLiveTV } from './sections/liveTv';
import { loadNextUp } from './sections/nextUp';
import { loadRecentlyAdded } from './sections/recentlyAdded';
import { loadResume } from './sections/resume';
2023-10-01 02:49:36 -04:00
import 'elements/emby-button/paper-icon-button-light';
import 'elements/emby-itemscontainer/emby-itemscontainer';
import 'elements/emby-scroller/emby-scroller';
import 'elements/emby-button/emby-button';
import './homesections.scss';
2023-04-19 01:56:05 -04:00
export function getDefaultSection(index) {
2023-10-02 12:22:36 -04:00
if (index < 0 || index > DEFAULT_SECTIONS.length) return '';
return DEFAULT_SECTIONS[index];
2023-04-19 01:56:05 -04:00
}
function getAllSectionsToShow(userSettings, sectionCount) {
const sections = [];
for (let i = 0, length = sectionCount; i < length; i++) {
let section = userSettings.get('homesection' + i) || getDefaultSection(i);
if (section === 'folders') {
section = getDefaultSection(0);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
sections.push(section);
2018-10-23 01:05:09 +03:00
}
// Ensure libraries are visible in TV layout
if (
layoutManager.tv
&& !sections.includes(HomeSectionType.SmallLibraryTiles)
&& !sections.includes(HomeSectionType.LibraryButtons)
) {
return [
HomeSectionType.SmallLibraryTiles,
...sections
];
}
2023-04-19 01:56:05 -04:00
return sections;
}
export function loadSections(elem, apiClient, user, userSettings) {
return getUserViews(apiClient, user.Id).then(function (userViews) {
let html = '';
if (userViews.length) {
const userSectionCount = 7;
// TV layout can have an extra section to ensure libraries are visible
const totalSectionCount = layoutManager.tv ? userSectionCount + 1 : userSectionCount;
for (let i = 0; i < totalSectionCount; i++) {
2023-04-19 01:56:05 -04:00
html += '<div class="verticalSection section' + i + '"></div>';
}
2023-04-19 01:56:05 -04:00
elem.innerHTML = html;
elem.classList.add('homeSectionsContainer');
2023-04-19 01:56:05 -04:00
const promises = [];
const sections = getAllSectionsToShow(userSettings, userSectionCount);
2023-04-19 01:56:05 -04:00
for (let i = 0; i < sections.length; i++) {
promises.push(loadSection(elem, apiClient, user, userSettings, userViews, sections, i));
}
2018-10-23 01:05:09 +03:00
return Promise.all(promises)
// Timeout for polyfilled CustomElements (webOS 1.2)
.then(() => new Promise((resolve) => setTimeout(resolve, 0)))
.then(() => {
return resume(elem, {
refresh: true
});
});
2023-04-19 01:56:05 -04:00
} else {
let noLibDescription;
2023-07-06 13:39:48 -04:00
if (user.Policy?.IsAdministrator) {
2023-04-19 01:56:05 -04:00
noLibDescription = globalize.translate('NoCreatedLibraries', '<br><a id="button-createLibrary" class="button-link">', '</a>');
} else {
2023-04-19 01:56:05 -04:00
noLibDescription = globalize.translate('AskAdminToCreateLibrary');
}
2023-04-19 01:56:05 -04:00
html += '<div class="centerMessage padded-left padded-right">';
html += '<h2>' + globalize.translate('MessageNothingHere') + '</h2>';
html += '<p>' + noLibDescription + '</p>';
html += '</div>';
elem.innerHTML = html;
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
const createNowLink = elem.querySelector('#button-createLibrary');
if (createNowLink) {
createNowLink.addEventListener('click', function () {
2023-09-25 00:00:36 -04:00
Dashboard.navigate('dashboard/libraries');
2023-04-19 01:56:05 -04:00
});
}
}
2023-04-19 01:56:05 -04:00
});
}
export function destroySections(elem) {
const elems = elem.querySelectorAll('.itemsContainer');
for (const e of elems) {
e.fetchData = null;
e.parentContainer = null;
e.getItemsHtml = null;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
elem.innerHTML = '';
}
2023-04-19 01:56:05 -04:00
export function pause(elem) {
const elems = elem.querySelectorAll('.itemsContainer');
for (const e of elems) {
e.pause();
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
export function resume(elem, options) {
const elems = elem.querySelectorAll('.itemsContainer');
const promises = [];
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
for (let i = 0, length = elems.length; i < length; i++) {
promises.push(elems[i].resume(options));
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
return Promise.all(promises);
}
function loadSection(page, apiClient, user, userSettings, userViews, allSections, index) {
const section = allSections[index];
const elem = page.querySelector('.section' + index);
2023-10-02 12:22:36 -04:00
const options = { enableOverflow: enableScrollX() };
switch (section) {
case HomeSectionType.ActiveRecordings:
loadRecordings(elem, true, apiClient, options);
break;
case HomeSectionType.LatestMedia:
loadRecentlyAdded(elem, apiClient, user, userViews, options);
break;
case HomeSectionType.LibraryButtons:
loadLibraryButtons(elem, userViews);
break;
case HomeSectionType.LiveTv:
return loadLiveTV(elem, apiClient, user, options);
case HomeSectionType.NextUp:
loadNextUp(elem, apiClient, userSettings, options);
break;
case HomeSectionType.Resume:
return loadResume(elem, apiClient, 'HeaderContinueWatching', 'Video', userSettings, options);
case HomeSectionType.ResumeAudio:
return loadResume(elem, apiClient, 'HeaderContinueListening', 'Audio', userSettings, options);
case HomeSectionType.ResumeBook:
return loadResume(elem, apiClient, 'HeaderContinueReading', 'Book', userSettings, options);
case HomeSectionType.SmallLibraryTiles:
loadLibraryTiles(elem, userViews, options);
break;
default:
elem.innerHTML = '';
2018-10-23 01:05:09 +03:00
}
2023-10-02 12:22:36 -04:00
2023-04-19 01:56:05 -04:00
return Promise.resolve();
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function getUserViews(apiClient, userId) {
return apiClient.getUserViews({}, userId || apiClient.getCurrentUserId()).then(function (result) {
return result.Items;
});
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function enableScrollX() {
return true;
}
2018-10-23 01:05:09 +03:00
export default {
2023-10-02 12:22:36 -04:00
getDefaultSection,
loadSections,
destroySections,
pause,
resume
};