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

Apply styles using user events and also immediately when changed.

This commit is contained in:
Ian Walton 2021-04-24 16:42:21 -04:00
parent 99c902a1a1
commit 8d59d2a0fe
2 changed files with 48 additions and 55 deletions

View file

@ -176,7 +176,7 @@ function initSyncPlay() {
Events.on(ServerConnections, 'apiclientcreated', (e, newApiClient) => SyncPlay.Manager.init(newApiClient));
}
function onAppReady() {
async function onAppReady() {
console.debug('begin onAppReady');
console.debug('onAppReady: loading dependencies');
@ -217,57 +217,66 @@ function onAppReady() {
}
}
let cssHasLoadedTrigger;
const cssHasLoadedPromise = new Promise(resolve => {
cssHasLoadedTrigger = resolve;
});
const apiClient = ServerConnections.currentApiClient();
if (apiClient) {
fetch(apiClient.getUrl('Branding/Css'))
const updateStyle = (css) => {
let style = document.querySelector('#cssBranding');
if (!style) {
// Inject the branding css as a dom element in body so it will take
// precedence over other stylesheets
style = document.createElement('style');
style.id = 'cssBranding';
document.body.appendChild(style);
}
style.textContent = css;
};
const style = fetch(apiClient.getUrl('Branding/Css'))
.then(function(response) {
if (!response.ok) {
throw new Error(response.status + ' ' + response.statusText);
}
return response.text();
})
.then(function(css) {
let style = document.querySelector('#cssBranding');
if (!style) {
// Inject the branding css as a dom element in body so it will take
// precedence over other stylesheets
style = document.createElement('style');
style.id = 'cssBranding';
document.body.appendChild(style);
}
style.textContent = css;
cssHasLoadedTrigger(style);
})
.catch(function(err) {
console.warn('Error applying custom css', err);
});
}
currentSettings.userIsSet().then(() => {
if (currentSettings.disableCustomCss()) {
cssHasLoadedPromise.then(style => {
style.textContent = '';
});
}
const localCss = currentSettings.customCss();
if (localCss) {
let style = document.querySelector('#localCssBranding');
if (!style) {
// Inject the branding css as a dom element in body so it will take
// precedence over other stylesheets
style = document.createElement('style');
style.id = 'localCssBranding';
document.body.appendChild(style);
const handleStyleChange = async () => {
if (currentSettings.disableCustomCss()) {
updateStyle('');
} else {
updateStyle(await style);
}
style.textContent = localCss;
}
});
const localCss = currentSettings.customCss();
let localStyle = document.querySelector('#localCssBranding');
if (localCss) {
if (!localStyle) {
// Inject the branding css as a dom element in body so it will take
// precedence over other stylesheets
localStyle = document.createElement('style');
localStyle.id = 'localCssBranding';
document.body.appendChild(localStyle);
}
localStyle.textContent = localCss;
} else {
if (localStyle) {
localStyle.textContent = '';
}
}
};
Events.on(ServerConnections, 'localusersignedin', handleStyleChange);
Events.on(ServerConnections, 'localusersignedout', handleStyleChange);
Events.on(currentSettings, 'change', (e, prop) => {
if (prop == 'disableCustomCss' || prop == 'customCss') {
handleStyleChange();
}
});
style.then(updateStyle);
}
}
function registerServiceWorker() {