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 */
|
2020-09-26 12:27:14 +03: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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-09 13:24:16 +02:00
|
|
|
async function getConfig() {
|
2020-04-02 19:44:21 +09:00
|
|
|
if (data) return Promise.resolve(data);
|
2020-08-09 13:24:16 +02:00
|
|
|
try {
|
2020-09-26 12:27:14 +03:00
|
|
|
const response = await fetchLocal('config.json', {
|
2020-08-09 13:24:16 +02:00
|
|
|
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;
|
2020-08-09 13:24:16 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.warn('failed to fetch the web config file:', error);
|
2021-03-10 10:24:29 -05:00
|
|
|
return DefaultConfig;
|
2020-08-09 13:24:16 +02:00
|
|
|
}
|
2020-04-02 03:51:22 +09:00
|
|
|
}
|
|
|
|
|
2020-11-30 14:38:03 -05:00
|
|
|
export function getIncludeCorsCredentials() {
|
|
|
|
return getConfig()
|
2021-03-10 09:53:42 -05:00
|
|
|
.then(config => !!config.includeCorsCredentials)
|
2020-11-30 14:38:03 -05:00
|
|
|
.catch(error => {
|
|
|
|
console.log('cannot get web config:', error);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-02 17:28:25 +09:00
|
|
|
export function getMultiServer() {
|
2020-04-02 03:51:22 +09:00
|
|
|
return getConfig().then(config => {
|
2021-03-10 09:53:42 -05:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2020-08-02 17:28:25 +09:00
|
|
|
|
2020-09-12 09:12:40 +09:00
|
|
|
export function getServers() {
|
|
|
|
return getConfig().then(config => {
|
2020-10-30 22:13:56 +09:00
|
|
|
return config.servers || [];
|
2020-09-12 09:12:40 +09:00
|
|
|
}).catch(error => {
|
|
|
|
console.log('cannot get web config:', error);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-08 11:39:46 +00:00
|
|
|
const baseDefaultTheme = {
|
2020-11-07 11:56:46 +00:00
|
|
|
'name': 'Dark',
|
|
|
|
'id': 'dark',
|
|
|
|
'default': true
|
|
|
|
};
|
|
|
|
|
2020-11-08 11:39:46 +00:00
|
|
|
let internalDefaultTheme = baseDefaultTheme;
|
|
|
|
|
2020-11-07 11:56:46 +00:00
|
|
|
const checkDefaultTheme = (themes) => {
|
|
|
|
if (themes) {
|
|
|
|
const defaultTheme = themes.find((theme) => theme.default);
|
|
|
|
|
2020-11-08 11:39:46 +00:00
|
|
|
if (defaultTheme) {
|
|
|
|
internalDefaultTheme = defaultTheme;
|
|
|
|
return;
|
2020-11-07 11:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 11:39:46 +00:00
|
|
|
internalDefaultTheme = baseDefaultTheme;
|
2020-11-07 11:56:46 +00:00
|
|
|
};
|
|
|
|
|
2020-08-02 17:28:25 +09:00
|
|
|
export function getThemes() {
|
|
|
|
return getConfig().then(config => {
|
2021-03-10 10:24:29 -05:00
|
|
|
const themes = Array.isArray(config.themes) ? config.themes : DefaultConfig.themes;
|
2020-11-08 11:39:46 +00:00
|
|
|
checkDefaultTheme(themes);
|
|
|
|
return themes;
|
2020-08-02 17:28:25 +09:00
|
|
|
}).catch(error => {
|
|
|
|
console.log('cannot get web config:', error);
|
2020-11-08 11:39:46 +00:00
|
|
|
checkDefaultTheme();
|
2021-03-10 10:24:29 -05:00
|
|
|
return DefaultConfig.themes;
|
2020-08-02 17:28:25 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-08 11:39:46 +00:00
|
|
|
export const getDefaultTheme = () => internalDefaultTheme;
|
|
|
|
|
2020-08-02 17:28:25 +09:00
|
|
|
export function getPlugins() {
|
|
|
|
return getConfig().then(config => {
|
2021-03-10 10:24:29 -05:00
|
|
|
return config.plugins || DefaultConfig.plugins;
|
2020-08-02 17:28:25 +09:00
|
|
|
}).catch(error => {
|
|
|
|
console.log('cannot get web config:', error);
|
2021-03-10 10:24:29 -05:00
|
|
|
return DefaultConfig.plugins;
|
2020-08-02 17:28:25 +09:00
|
|
|
});
|
|
|
|
}
|