snappymail/dev/Settings/User/Themes.js

130 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-02-21 22:36:34 +08:00
import { addObservablesTo } from 'External/ko';
2014-08-25 23:49:01 +08:00
import { SaveSettingStatus, UploadErrorCode } from 'Common/Enums';
2021-07-20 22:49:03 +08:00
import { themePreviewLink, serverRequest } from 'Common/Links';
2019-07-05 03:19:24 +08:00
import { i18n } from 'Common/Translator';
2022-02-24 19:22:27 +08:00
import { SettingsCapa } from 'Common/Globals';
2014-08-25 15:10:51 +08:00
import { ThemeStore, convertThemeName, changeTheme } from 'Stores/Theme';
import Remote from 'Remote/User/Fetch';
2014-08-22 23:08:56 +08:00
2022-02-21 22:36:34 +08:00
const themeBackground = {
name: ThemeStore.userBackgroundName,
hash: ThemeStore.userBackgroundHash
};
addObservablesTo(themeBackground, {
uploaderButton: null,
loading: false,
error: ''
});
export class UserSettingsThemes /*extends AbstractViewSettings*/ {
2016-07-16 05:29:42 +08:00
constructor() {
2022-10-20 15:33:44 +08:00
this.fontSansSerif = ThemeStore.fontSansSerif;
this.fontSerif = ThemeStore.fontSerif;
this.fontMono = ThemeStore.fontMono;
ThemeStore.fontSansSerif.subscribe(value => {
Remote.saveSettings(null, {
fontSansSerif: value
});
});
ThemeStore.fontSerif.subscribe(value => {
Remote.saveSettings(null, {
fontSerif: value
});
});
ThemeStore.fontMono.subscribe(value => {
Remote.saveSettings(null, {
fontMono: value
});
});
2016-07-16 05:29:42 +08:00
this.theme = ThemeStore.theme;
this.themes = ThemeStore.themes;
this.themesObjects = ko.observableArray();
2016-06-28 04:54:38 +08:00
2022-03-06 05:25:32 +08:00
themeBackground.enabled = SettingsCapa('UserBackground');
2022-02-21 22:36:34 +08:00
this.background = themeBackground;
2014-08-21 23:08:34 +08:00
this.themeTrigger = ko.observable(SaveSettingStatus.Idle).extend({ debounce: 100 });
2022-02-21 22:36:34 +08:00
ThemeStore.theme.subscribe(value => {
2022-10-10 19:52:56 +08:00
this.themesObjects.forEach(theme => theme.selected(value === theme.name));
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
changeTheme(value, this.themeTrigger);
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
Remote.saveSettings(null, {
Theme: value
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
}
setTheme(theme) {
ThemeStore.theme(theme.name);
}
2016-07-16 05:29:42 +08:00
onBuild() {
2022-02-21 22:36:34 +08:00
const currentTheme = ThemeStore.theme();
2019-07-05 03:19:24 +08:00
this.themesObjects(
2022-02-21 22:36:34 +08:00
ThemeStore.themes.map(theme => ({
2019-07-05 03:19:24 +08:00
name: theme,
nameDisplay: convertThemeName(theme),
selected: ko.observable(theme === currentTheme),
themePreviewSrc: themePreviewLink(theme)
}))
);
2021-08-13 02:17:37 +08:00
// initUploader
2022-02-24 19:22:27 +08:00
if (themeBackground.uploaderButton() && themeBackground.enabled) {
2019-07-05 03:19:24 +08:00
const oJua = new Jua({
2021-02-04 18:25:00 +08:00
action: serverRequest('UploadBackground'),
2021-09-14 18:50:56 +08:00
limit: 1,
2022-02-21 22:36:34 +08:00
clickElement: themeBackground.uploaderButton()
2019-07-05 03:19:24 +08:00
});
2016-07-16 05:29:42 +08:00
oJua
.on('onStart', () => {
2022-02-21 22:36:34 +08:00
themeBackground.loading(true);
themeBackground.error('');
2016-07-16 05:29:42 +08:00
})
.on('onComplete', (id, result, data) => {
2022-02-21 22:36:34 +08:00
themeBackground.loading(false);
themeBackground.name(data?.Result?.name || '');
themeBackground.hash(data?.Result?.hash || '');
2022-10-10 19:52:56 +08:00
if (!themeBackground.name() || !themeBackground.hash()) {
2016-07-16 05:29:42 +08:00
let errorMsg = '';
2019-07-05 03:19:24 +08:00
if (data.ErrorCode) {
switch (data.ErrorCode) {
2016-07-16 05:29:42 +08:00
case UploadErrorCode.FileIsTooBig:
errorMsg = i18n('SETTINGS_THEMES/ERROR_FILE_IS_TOO_BIG');
break;
case UploadErrorCode.FileType:
errorMsg = i18n('SETTINGS_THEMES/ERROR_FILE_TYPE_ERROR');
break;
// no default
}
}
2022-09-02 17:52:07 +08:00
themeBackground.error(errorMsg || data.ErrorMessage || i18n('SETTINGS_THEMES/ERROR_UNKNOWN'));
}
2016-07-16 05:29:42 +08:00
});
}
2016-06-30 08:02:45 +08:00
}
2021-08-13 02:17:37 +08:00
onShow() {
2022-02-21 22:36:34 +08:00
themeBackground.error('');
2021-08-13 02:17:37 +08:00
}
clearBackground() {
2022-02-24 19:22:27 +08:00
if (themeBackground.enabled) {
Remote.request('ClearUserBackground', () => {
2022-02-21 22:36:34 +08:00
themeBackground.name('');
themeBackground.hash('');
2021-08-13 02:17:37 +08:00
});
}
}
2016-07-16 05:29:42 +08:00
}