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/settings/webSettings.js

157 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-03-10 10:24:29 -05:00
import DefaultConfig from '../../config.json';
2020-04-02 19:44:21 +09:00
2021-03-10 10:24:29 -05:00
let data;
2020-09-27 17:48:29 +03:00
const urlResolver = document.createElement('a');
2020-09-26 12:27:14 +03:00
// `fetch` with `file:` support
// Recent browsers seem to support `file` protocol under some conditions.
// Based on https://github.com/github/fetch/pull/92#issuecomment-174730593
2020-09-27 17:48:29 +03:00
// https://github.com/github/fetch/pull/92#issuecomment-512187452
2020-09-26 12:27:14 +03:00
async function fetchLocal(url, options) {
2020-09-27 17:48:29 +03:00
urlResolver.href = url;
const requestURL = urlResolver.href;
2020-09-26 12:27:14 +03:00
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest;
xhr.onload = () => {
// `file` protocol has invalid OK status
let status = xhr.status;
2020-09-27 17:48:29 +03:00
if (requestURL.startsWith('file:') && status === 0) {
2020-09-26 12:27:14 +03:00
status = 200;
}
2020-09-27 17:48:29 +03:00
/* eslint-disable-next-line compat/compat */
2023-03-29 00:38:22 -04:00
resolve(new Response(xhr.responseText, { status: status }));
2020-09-27 17:48:29 +03:00
};
2020-09-26 12:27:14 +03:00
xhr.onerror = () => {
reject(new TypeError('Local request failed'));
2020-09-27 17:48:29 +03:00
};
2020-09-26 12:27:14 +03:00
xhr.open('GET', url);
if (options && options.cache) {
xhr.setRequestHeader('Cache-Control', options.cache);
}
xhr.send(null);
});
}
async function getConfig() {
2020-04-02 19:44:21 +09:00
if (data) return Promise.resolve(data);
try {
2020-09-26 12:27:14 +03:00
const response = await fetchLocal('config.json', {
cache: 'no-cache'
});
if (!response.ok) {
throw new Error('network response was not ok');
}
data = await response.json();
2020-05-12 06:28:14 +09:00
return data;
} catch (error) {
console.warn('failed to fetch the web config file:', error);
data = DefaultConfig;
return data;
}
2020-04-02 03:51:22 +09:00
}
export function getIncludeCorsCredentials() {
return getConfig()
.then(config => !!config.includeCorsCredentials)
.catch(error => {
console.log('cannot get web config:', error);
return false;
});
}
export function getMultiServer() {
// Enable multi-server support when served by webpack
if (__WEBPACK_SERVE__) { // eslint-disable-line no-undef
return Promise.resolve(true);
}
2020-04-02 03:51:22 +09:00
return getConfig().then(config => {
return !!config.multiserver;
2020-04-24 13:46:57 +03:00
}).catch(error => {
2020-05-04 12:44:12 +02:00
console.log('cannot get web config:', error);
2020-04-24 13:46:57 +03:00
return false;
2020-04-02 03:51:22 +09:00
});
}
export function getServers() {
return getConfig().then(config => {
return config.servers || [];
}).catch(error => {
console.log('cannot get web config:', error);
return [];
});
}
const baseDefaultTheme = {
'name': 'Dark',
'id': 'dark',
'default': true
};
let internalDefaultTheme = baseDefaultTheme;
const checkDefaultTheme = (themes) => {
if (themes) {
const defaultTheme = themes.find((theme) => theme.default);
if (defaultTheme) {
internalDefaultTheme = defaultTheme;
return;
}
}
internalDefaultTheme = baseDefaultTheme;
};
export function getThemes() {
return getConfig().then(config => {
2021-03-10 10:30:34 -05:00
if (!Array.isArray(config.themes)) {
console.error('web config is invalid, missing themes:', config);
}
2021-03-10 10:24:29 -05:00
const themes = Array.isArray(config.themes) ? config.themes : DefaultConfig.themes;
checkDefaultTheme(themes);
return themes;
}).catch(error => {
console.log('cannot get web config:', error);
checkDefaultTheme();
2021-03-10 10:24:29 -05:00
return DefaultConfig.themes;
});
}
export const getDefaultTheme = () => internalDefaultTheme;
export function getMenuLinks() {
return getConfig().then(config => {
if (!config.menuLinks) {
console.error('web config is invalid, missing menuLinks:', config);
}
return config.menuLinks || [];
}).catch(error => {
console.log('cannot get web config:', error);
return [];
});
}
export function getPlugins() {
return getConfig().then(config => {
2021-03-10 10:30:34 -05:00
if (!config.plugins) {
console.error('web config is invalid, missing plugins:', config);
}
2021-03-10 10:24:29 -05:00
return config.plugins || DefaultConfig.plugins;
}).catch(error => {
console.log('cannot get web config:', error);
2021-03-10 10:24:29 -05:00
return DefaultConfig.plugins;
});
}