snappymail/dev/Stores/Theme.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2022-10-20 15:33:44 +08:00
import { doc, $htmlCL, elementById, leftPanelDisabled, Settings, SettingsGet } from 'Common/Globals';
import { isArray } from 'Common/Utils';
2021-07-20 22:49:03 +08:00
import { serverRequestRaw } from 'Common/Links';
export const ThemeStore = {
2022-09-26 17:55:26 +08:00
theme: ko.observable(''),
themes: ko.observableArray(),
userBackgroundName: ko.observable(''),
userBackgroundHash: ko.observable(''),
2022-10-20 15:33:44 +08:00
fontSansSerif: ko.observable(''),
fontSerif: ko.observable(''),
fontMono: ko.observable(''),
isMobile: ko.observable($htmlCL.contains('rl-mobile')),
2022-03-08 17:52:40 +08:00
populate: () => {
2021-03-10 18:44:48 +08:00
const themes = Settings.app('themes');
2022-03-08 17:52:40 +08:00
ThemeStore.themes(isArray(themes) ? themes : []);
ThemeStore.theme(SettingsGet('Theme'));
if (!ThemeStore.isMobile()) {
ThemeStore.userBackgroundName(SettingsGet('UserBackgroundName'));
ThemeStore.userBackgroundHash(SettingsGet('UserBackgroundHash'));
}
2022-10-20 15:33:44 +08:00
ThemeStore.fontSansSerif(SettingsGet('fontSansSerif'));
ThemeStore.fontSerif(SettingsGet('fontSerif'));
ThemeStore.fontMono(SettingsGet('fontMono'));
2022-03-08 17:52:40 +08:00
leftPanelDisabled(ThemeStore.isMobile());
}
};
ThemeStore.isMobile.subscribe(value => $htmlCL.toggle('rl-mobile', value));
2021-07-20 22:49:03 +08:00
2022-10-20 15:33:44 +08:00
ThemeStore.fontSansSerif.subscribe(value => {
if (null != value) {
let cl = elementById('rl-app').classList;
cl.forEach(name => {
if (name.startsWith('font') && !/font(Serif|Mono)/.test(name)) {
2022-10-20 15:33:44 +08:00
cl.remove(name);
}
});
value && cl.add('font'+value);
}
});
ThemeStore.fontSerif.subscribe(value => {
if (null != value) {
let cl = elementById('rl-app').classList;
cl.forEach(name => name.startsWith('fontSerif') && cl.remove(name));
2022-10-20 15:33:44 +08:00
value && cl.add('fontSerif'+value);
}
});
ThemeStore.fontMono.subscribe(value => {
if (null != value) {
let cl = elementById('rl-app').classList;
cl.forEach(name => name.startsWith('fontMono') && cl.remove(name));
2022-10-20 15:33:44 +08:00
value && cl.add('fontMono'+value);
}
});
2021-07-20 22:49:03 +08:00
ThemeStore.userBackgroundHash.subscribe(value => {
if (value) {
$htmlCL.add('UserBackground');
doc.body.style.backgroundImage = "url("+serverRequestRaw('UserBackground', value)+")";
} else {
$htmlCL.remove('UserBackground');
doc.body.removeAttribute('style');
}
});