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/music/musicrecommended.js

407 lines
13 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import browser from '../../scripts/browser';
import layoutManager from '../../components/layoutManager';
import * as userSettings from '../../scripts/settings/userSettings';
import inputManager from '../../scripts/inputManager';
import loading from '../../components/loading/loading';
import cardBuilder from '../../components/cardbuilder/cardBuilder';
import dom from '../../scripts/dom';
import imageLoader from '../../components/images/imageLoader';
import libraryMenu from '../../scripts/libraryMenu';
import * as mainTabsManager from '../../components/maintabsmanager';
import globalize from '../../scripts/globalize';
2021-01-26 15:31:58 -05:00
import '../../assets/css/scrollstyles.scss';
2020-08-14 08:46:34 +02:00
import '../../elements/emby-itemscontainer/emby-itemscontainer';
import '../../elements/emby-tabs/emby-tabs';
import '../../elements/emby-button/emby-button';
import '../../assets/css/flexstyles.scss';
2022-04-10 02:22:13 -04:00
import Dashboard from '../../utils/dashboard';
/* eslint-disable indent */
2018-10-23 01:05:09 +03:00
function itemsPerRow() {
const screenWidth = dom.getWindowSize().innerWidth;
if (screenWidth >= 1920) {
return 9;
}
if (screenWidth >= 1200) {
return 12;
}
if (screenWidth >= 1000) {
return 10;
}
return 8;
2018-10-23 01:05:09 +03:00
}
function enableScrollX() {
return !layoutManager.desktop;
2018-10-23 01:05:09 +03:00
}
function getSquareShape() {
2020-05-04 12:44:12 +02:00
return enableScrollX() ? 'overflowSquare' : 'square';
2018-10-23 01:05:09 +03:00
}
function loadLatest(page, parentId) {
loading.show();
const userId = ApiClient.getCurrentUserId();
const options = {
2020-05-04 12:44:12 +02:00
IncludeItemTypes: 'Audio',
Limit: enableScrollX() ? 3 * itemsPerRow() : 2 * itemsPerRow(),
2020-05-04 12:44:12 +02:00
Fields: 'PrimaryImageAspectRatio,BasicSyncInfo',
ParentId: parentId,
ImageTypeLimit: 1,
2020-05-04 12:44:12 +02:00
EnableImageTypes: 'Primary,Backdrop,Banner,Thumb',
EnableTotalRecordCount: false
};
2020-05-04 12:44:12 +02:00
ApiClient.getJSON(ApiClient.getUrl('Users/' + userId + '/Items/Latest', options)).then(function (items) {
2020-10-07 21:12:14 +09:00
const elem = page.querySelector('#recentlyAddedSongs');
elem.innerHTML = cardBuilder.getCardsHtml({
2018-10-23 01:05:09 +03:00
items: items,
showUnplayedIndicator: false,
showLatestItemsPopup: false,
2018-10-23 01:05:09 +03:00
shape: getSquareShape(),
showTitle: true,
showParentTitle: true,
lazy: true,
2020-07-19 16:15:11 +02:00
centerText: true,
overlayPlayButton: true,
2018-10-23 01:05:09 +03:00
allowBottomPadding: !enableScrollX(),
2020-07-19 16:15:11 +02:00
cardLayout: false,
coverImage: true
});
imageLoader.lazyChildren(elem);
loading.hide();
2019-11-02 20:38:58 +03:00
2020-08-14 08:46:34 +02:00
import('../../components/autoFocuser').then(({default: autoFocuser}) => {
2019-11-02 20:38:58 +03:00
autoFocuser.autoFocus(page);
});
});
2018-10-23 01:05:09 +03:00
}
function loadRecentlyPlayed(page, parentId) {
const options = {
2020-05-04 12:44:12 +02:00
SortBy: 'DatePlayed',
SortOrder: 'Descending',
IncludeItemTypes: 'Audio',
2018-10-23 01:05:09 +03:00
Limit: itemsPerRow(),
Recursive: true,
2020-05-04 12:44:12 +02:00
Fields: 'PrimaryImageAspectRatio,AudioInfo',
Filters: 'IsPlayed',
2018-10-23 01:05:09 +03:00
ParentId: parentId,
ImageTypeLimit: 1,
2020-05-04 12:44:12 +02:00
EnableImageTypes: 'Primary,Backdrop,Banner,Thumb',
EnableTotalRecordCount: false
2018-10-23 01:05:09 +03:00
};
ApiClient.getItems(ApiClient.getCurrentUserId(), options).then(function (result) {
const elem = page.querySelector('#recentlyPlayed');
if (result.Items.length) {
2020-05-04 12:44:12 +02:00
elem.classList.remove('hide');
} else {
2020-05-04 12:44:12 +02:00
elem.classList.add('hide');
}
2020-10-07 21:12:14 +09:00
const itemsContainer = elem.querySelector('.itemsContainer');
itemsContainer.innerHTML = cardBuilder.getCardsHtml({
2018-10-23 01:05:09 +03:00
items: result.Items,
showUnplayedIndicator: false,
2018-10-23 01:05:09 +03:00
shape: getSquareShape(),
showTitle: true,
showParentTitle: true,
2020-05-04 12:44:12 +02:00
action: 'instantmix',
lazy: true,
2020-07-19 16:15:11 +02:00
centerText: true,
overlayMoreButton: true,
2018-10-23 01:05:09 +03:00
allowBottomPadding: !enableScrollX(),
2020-07-19 16:15:11 +02:00
cardLayout: false,
coverImage: true
});
imageLoader.lazyChildren(itemsContainer);
});
2018-10-23 01:05:09 +03:00
}
function loadFrequentlyPlayed(page, parentId) {
const options = {
2020-05-04 12:44:12 +02:00
SortBy: 'PlayCount',
SortOrder: 'Descending',
IncludeItemTypes: 'Audio',
2018-10-23 01:05:09 +03:00
Limit: itemsPerRow(),
Recursive: true,
2020-05-04 12:44:12 +02:00
Fields: 'PrimaryImageAspectRatio,AudioInfo',
Filters: 'IsPlayed',
2018-10-23 01:05:09 +03:00
ParentId: parentId,
ImageTypeLimit: 1,
2020-05-04 12:44:12 +02:00
EnableImageTypes: 'Primary,Backdrop,Banner,Thumb',
EnableTotalRecordCount: false
2018-10-23 01:05:09 +03:00
};
ApiClient.getItems(ApiClient.getCurrentUserId(), options).then(function (result) {
const elem = page.querySelector('#topPlayed');
if (result.Items.length) {
2020-05-04 12:44:12 +02:00
elem.classList.remove('hide');
} else {
2020-05-04 12:44:12 +02:00
elem.classList.add('hide');
}
2020-10-07 21:12:14 +09:00
const itemsContainer = elem.querySelector('.itemsContainer');
itemsContainer.innerHTML = cardBuilder.getCardsHtml({
2018-10-23 01:05:09 +03:00
items: result.Items,
showUnplayedIndicator: false,
2018-10-23 01:05:09 +03:00
shape: getSquareShape(),
showTitle: true,
showParentTitle: true,
2020-05-04 12:44:12 +02:00
action: 'instantmix',
lazy: true,
2020-07-19 16:15:11 +02:00
centerText: true,
overlayMoreButton: true,
2018-10-23 01:05:09 +03:00
allowBottomPadding: !enableScrollX(),
2020-07-19 16:15:11 +02:00
cardLayout: false,
coverImage: true
});
imageLoader.lazyChildren(itemsContainer);
});
2018-10-23 01:05:09 +03:00
}
function loadSuggestionsTab(page, tabContent, parentId) {
2020-05-04 12:44:12 +02:00
console.debug('loadSuggestionsTab');
loadLatest(tabContent, parentId);
loadRecentlyPlayed(tabContent, parentId);
loadFrequentlyPlayed(tabContent, parentId);
2020-08-14 08:46:34 +02:00
import('../../components/favoriteitems').then(({default: favoriteItems}) => {
2020-05-04 12:44:12 +02:00
favoriteItems.render(tabContent, ApiClient.getCurrentUserId(), parentId, ['favoriteArtists', 'favoriteAlbums', 'favoriteSongs']);
});
2018-10-23 01:05:09 +03:00
}
function getTabs() {
return [{
2020-08-13 21:23:51 +09:00
name: globalize.translate('Albums')
}, {
name: globalize.translate('Suggestions')
2018-10-23 01:05:09 +03:00
}, {
2020-08-16 20:34:39 +09:00
name: globalize.translate('HeaderAlbumArtists')
2018-10-23 01:05:09 +03:00
}, {
name: globalize.translate('Artists')
2018-10-23 01:05:09 +03:00
}, {
2020-08-13 21:56:01 +09:00
name: globalize.translate('Playlists')
2018-10-23 01:05:09 +03:00
}, {
2020-08-13 21:31:29 +09:00
name: globalize.translate('Songs')
2018-10-23 01:05:09 +03:00
}, {
2020-08-13 21:38:57 +09:00
name: globalize.translate('Genres')
}];
2018-10-23 01:05:09 +03:00
}
function getDefaultTabIndex(folderId) {
2020-05-04 12:44:12 +02:00
switch (userSettings.get('landing-' + folderId)) {
case 'suggestions':
2018-10-23 01:05:09 +03:00
return 1;
2020-05-04 12:44:12 +02:00
case 'albumartists':
2018-10-23 01:05:09 +03:00
return 2;
2020-05-04 12:44:12 +02:00
case 'artists':
2018-10-23 01:05:09 +03:00
return 3;
2020-05-04 12:44:12 +02:00
case 'playlists':
2018-10-23 01:05:09 +03:00
return 4;
2020-05-04 12:44:12 +02:00
case 'songs':
2018-10-23 01:05:09 +03:00
return 5;
2020-05-04 12:44:12 +02:00
case 'genres':
2018-10-23 01:05:09 +03:00
return 6;
2018-10-23 01:05:09 +03:00
default:
return 0;
2018-10-23 01:05:09 +03:00
}
}
export default function (view, params) {
2018-10-23 01:05:09 +03:00
function reload() {
loading.show();
const tabContent = view.querySelector(".pageTabContent[data-index='" + suggestionsTabIndex + "']");
loadSuggestionsTab(view, tabContent, params.topParentId);
2018-10-23 01:05:09 +03:00
}
function enableScrollX() {
return browser.mobile;
2018-10-23 01:05:09 +03:00
}
function setScrollClasses(elem, scrollX) {
if (scrollX) {
2020-05-04 12:44:12 +02:00
elem.classList.add('hiddenScrollX');
if (layoutManager.tv) {
2020-05-04 12:44:12 +02:00
elem.classList.add('smoothScrollX');
}
2020-05-04 12:44:12 +02:00
elem.classList.add('scrollX');
elem.classList.remove('vertical-wrap');
} else {
2020-05-04 12:44:12 +02:00
elem.classList.remove('hiddenScrollX');
elem.classList.remove('smoothScrollX');
elem.classList.remove('scrollX');
elem.classList.add('vertical-wrap');
}
2018-10-23 01:05:09 +03:00
}
function onBeforeTabChange(e) {
preLoadTab(view, parseInt(e.detail.selectedTabIndex));
2018-10-23 01:05:09 +03:00
}
function onTabChange(e) {
loadTab(view, parseInt(e.detail.selectedTabIndex));
2018-10-23 01:05:09 +03:00
}
function getTabContainers() {
2020-05-04 12:44:12 +02:00
return view.querySelectorAll('.pageTabContent');
2018-10-23 01:05:09 +03:00
}
function initTabs() {
mainTabsManager.setTabs(view, currentTabIndex, getTabs, getTabContainers, onBeforeTabChange, onTabChange);
2018-10-23 01:05:09 +03:00
}
2020-08-02 12:16:14 +01:00
const getTabController = (page, index, callback) => {
let depends;
2018-10-23 01:05:09 +03:00
switch (index) {
case 0:
2020-09-08 02:26:20 -04:00
depends = 'musicalbums';
2018-10-23 01:05:09 +03:00
break;
2018-10-23 01:05:09 +03:00
case 1:
2020-09-08 02:26:20 -04:00
depends = 'musicrecommended';
2018-10-23 01:05:09 +03:00
break;
2018-10-23 01:05:09 +03:00
case 2:
case 3:
2020-09-08 02:26:20 -04:00
depends = 'musicartists';
2018-10-23 01:05:09 +03:00
break;
2018-10-23 01:05:09 +03:00
case 4:
2020-09-08 02:26:20 -04:00
depends = 'musicplaylists';
2018-10-23 01:05:09 +03:00
break;
2018-10-23 01:05:09 +03:00
case 5:
2020-09-08 02:26:20 -04:00
depends = 'songs';
2018-10-23 01:05:09 +03:00
break;
2018-10-23 01:05:09 +03:00
case 6:
2020-09-08 02:26:20 -04:00
depends = 'musicgenres';
2018-10-23 01:05:09 +03:00
break;
}
2020-09-08 02:26:20 -04:00
import(`../music/${depends}`).then(({default: controllerFactory}) => {
let tabContent;
if (index == 1) {
tabContent = view.querySelector(".pageTabContent[data-index='" + index + "']");
2020-08-02 12:16:14 +01:00
this.tabContent = tabContent;
}
let controller = tabControllers[index];
if (!controller) {
tabContent = view.querySelector(".pageTabContent[data-index='" + index + "']");
if (index === 1) {
2020-08-02 12:16:14 +01:00
controller = this;
} else {
controller = new controllerFactory(view, params, tabContent);
}
if (index == 2) {
2020-05-04 12:44:12 +02:00
controller.mode = 'albumartists';
} else if (index == 3) {
2020-05-04 12:44:12 +02:00
controller.mode = 'artists';
}
tabControllers[index] = controller;
if (controller.initTab) {
controller.initTab();
}
}
callback(controller);
});
2020-08-02 19:13:13 +01:00
};
2018-10-23 01:05:09 +03:00
function preLoadTab(page, index) {
getTabController(page, index, function (controller) {
if (renderedTabs.indexOf(index) == -1 && controller.preRender) {
controller.preRender();
}
});
2018-10-23 01:05:09 +03: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
}
function onInputCommand(e) {
switch (e.detail.command) {
2020-05-04 12:44:12 +02:00
case 'search':
e.preventDefault();
2020-05-04 12:44:12 +02:00
Dashboard.navigate('search.html?collectionType=music&parentId=' + params.topParentId);
2018-10-23 01:05:09 +03:00
}
}
2020-08-02 12:16:14 +01:00
let currentTabIndex = parseInt(params.tab || getDefaultTabIndex(params.topParentId));
const suggestionsTabIndex = 1;
2020-08-02 12:16:14 +01:00
this.initTab = function () {
const tabContent = view.querySelector(".pageTabContent[data-index='" + suggestionsTabIndex + "']");
const containers = tabContent.querySelectorAll('.itemsContainer');
for (let i = 0, length = containers.length; i < length; i++) {
setScrollClasses(containers[i], enableScrollX());
}
};
2020-08-02 12:16:14 +01:00
this.renderTab = function () {
reload();
2018-10-23 01:05:09 +03:00
};
const tabControllers = [];
const renderedTabs = [];
2021-01-26 22:20:12 -05:00
view.addEventListener('viewshow', function () {
initTabs();
2020-05-04 12:44:12 +02:00
if (!view.getAttribute('data-title')) {
const parentId = params.topParentId;
if (parentId) {
ApiClient.getItem(ApiClient.getCurrentUserId(), parentId).then(function (item) {
2020-05-04 12:44:12 +02:00
view.setAttribute('data-title', item.Name);
libraryMenu.setTitle(item.Name);
});
} else {
2020-05-04 12:44:12 +02:00
view.setAttribute('data-title', globalize.translate('TabMusic'));
libraryMenu.setTitle(globalize.translate('TabMusic'));
}
2018-10-23 01:05:09 +03:00
}
inputManager.on(window, onInputCommand);
});
2021-01-26 22:20:12 -05:00
view.addEventListener('viewbeforehide', function () {
inputManager.off(window, onInputCommand);
});
2021-01-26 22:20:12 -05:00
view.addEventListener('viewdestroy', function () {
tabControllers.forEach(function (t) {
if (t.destroy) {
t.destroy();
}
});
});
}
/* eslint-enable indent */