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

81 lines
2.4 KiB
JavaScript
Raw Normal View History

import { clearBackdrop, setBackdrops } from '../components/backdrop/backdrop';
2020-08-14 08:46:34 +02:00
import * as userSettings from './settings/userSettings';
import libraryMenu from './libraryMenu';
2022-04-10 02:22:13 -04:00
import { pageClassOn } from '../utils/dashboard';
2018-10-23 01:05:09 +03:00
const cache = {};
2020-04-06 23:45:50 +02:00
function enabled() {
return userSettings.enableBackdrops();
}
2018-10-23 01:05:09 +03:00
function getBackdropItemIds(apiClient, userId, types, parentId) {
const key = `backdrops2_${userId + (types || '') + (parentId || '')}`;
let data = cache[key];
2019-10-08 00:56:51 +03:00
if (data) {
console.debug(`Found backdrop id list in cache. Key: ${key}`);
data = JSON.parse(data);
return Promise.resolve(data);
}
2019-10-08 00:56:51 +03:00
const options = {
SortBy: 'IsFavoriteOrLiked,Random',
Limit: 20,
Recursive: true,
IncludeItemTypes: types,
ImageTypes: 'Backdrop',
ParentId: parentId,
EnableTotalRecordCount: false,
2021-03-22 22:02:32 -04:00
MaxOfficialRating: parentId ? '' : 'PG-13'
};
return apiClient.getItems(apiClient.getCurrentUserId(), options).then(function (result) {
const images = result.Items.map(function (i) {
return {
Id: i.Id,
tag: i.BackdropImageTags[0],
ServerId: i.ServerId
};
2019-10-08 00:56:51 +03:00
});
cache[key] = JSON.stringify(images);
return images;
});
}
2018-10-23 01:05:09 +03:00
function showBackdrop(type, parentId) {
const apiClient = window.ApiClient;
2019-10-08 00:56:51 +03:00
if (apiClient) {
getBackdropItemIds(apiClient, apiClient.getCurrentUserId(), type, parentId).then(function (images) {
if (images.length) {
setBackdrops(images.map(function (i) {
i.BackdropImageTags = [i.tag];
return i;
}));
} else {
clearBackdrop();
}
});
2018-10-23 01:05:09 +03:00
}
}
2019-10-08 00:56:51 +03:00
pageClassOn('pageshow', 'page', function () {
const page = this;
2019-10-08 00:56:51 +03:00
if (!page.classList.contains('selfBackdropPage')) {
if (page.classList.contains('backdropPage')) {
if (enabled()) {
const type = page.getAttribute('data-backdroptype');
const parentId = page.classList.contains('globalBackdropPage') ? '' : libraryMenu.getTopParentId();
showBackdrop(type, parentId);
2019-10-08 00:56:51 +03:00
} else {
page.classList.remove('backdropPage');
clearBackdrop();
2019-10-08 00:56:51 +03:00
}
} else {
clearBackdrop();
2019-10-08 00:56:51 +03:00
}
}
2019-10-08 00:56:51 +03:00
});