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/themeManager.js

77 lines
2 KiB
JavaScript
Raw Normal View History

import * as webSettings from 'webSettings';
2020-08-13 00:15:02 +03:00
var themeStyleElement = document.querySelector('#cssTheme');
var currentThemeId;
function unloadTheme() {
var elem = themeStyleElement;
if (elem) {
2020-08-13 00:15:02 +03:00
elem.removeAttribute('href');
currentThemeId = null;
}
}
function getThemes() {
return webSettings.getThemes();
}
function getThemeStylesheetInfo(id) {
return getThemes().then(themes => {
var theme = themes.find(theme => {
return id ? theme.id === id : theme.default;
});
return {
stylesheetPath: 'themes/' + theme.id + '/theme.css',
themeId: theme.id
};
});
}
function setTheme(id) {
return new Promise(function (resolve, reject) {
if (currentThemeId && currentThemeId === id) {
resolve();
return;
}
getThemeStylesheetInfo(id).then(function (info) {
if (currentThemeId && currentThemeId === info.themeId) {
resolve();
return;
}
var linkUrl = info.stylesheetPath;
unloadTheme();
2020-08-13 00:15:02 +03:00
let link = themeStyleElement;
if (!link) {
// Inject the theme css as a dom element in body so it will take
// precedence over other stylesheets
link = document.createElement('link');
link.id = 'cssTheme';
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
document.body.appendChild(link);
}
const onLoad = function (e) {
e.target.removeEventListener('load', onLoad);
resolve();
};
2020-08-13 00:15:02 +03:00
link.addEventListener('load', onLoad);
link.setAttribute('href', linkUrl);
themeStyleElement = link;
currentThemeId = info.themeId;
});
});
}
export default {
getThemes: getThemes,
setTheme: setTheme
};