2020-08-14 08:46:34 +02:00
|
|
|
import * as webSettings from './settings/webSettings';
|
2020-08-02 17:28:25 +09:00
|
|
|
|
2020-10-07 21:12:14 +09:00
|
|
|
let themeStyleElement = document.querySelector('#cssTheme');
|
|
|
|
let currentThemeId;
|
2020-08-02 17:28:25 +09:00
|
|
|
|
|
|
|
function unloadTheme() {
|
2020-10-07 21:12:14 +09:00
|
|
|
const elem = themeStyleElement;
|
2020-08-02 17:28:25 +09:00
|
|
|
if (elem) {
|
2020-08-13 00:15:02 +03:00
|
|
|
elem.removeAttribute('href');
|
2020-08-02 17:28:25 +09:00
|
|
|
currentThemeId = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getThemes() {
|
|
|
|
return webSettings.getThemes();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getThemeStylesheetInfo(id) {
|
|
|
|
return getThemes().then(themes => {
|
2020-10-07 21:12:14 +09:00
|
|
|
const theme = themes.find(theme => {
|
2020-08-02 17:28:25 +09:00
|
|
|
return id ? theme.id === id : theme.default;
|
|
|
|
});
|
|
|
|
|
2020-08-16 20:24:45 +02:00
|
|
|
if (!theme) {
|
|
|
|
theme = {
|
|
|
|
'name': 'Dark',
|
|
|
|
'id': 'dark',
|
|
|
|
'default': true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-02 17:28:25 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-07 21:12:14 +09:00
|
|
|
const linkUrl = info.stylesheetPath;
|
2020-08-02 17:28:25 +09:00
|
|
|
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);
|
2020-08-02 17:28:25 +09:00
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
2020-08-13 00:15:02 +03:00
|
|
|
link.addEventListener('load', onLoad);
|
|
|
|
|
2020-08-02 17:28:25 +09:00
|
|
|
link.setAttribute('href', linkUrl);
|
|
|
|
themeStyleElement = link;
|
|
|
|
currentThemeId = info.themeId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
getThemes: getThemes,
|
|
|
|
setTheme: setTheme
|
|
|
|
};
|