snappymail/dev/Settings/User/Themes.js

122 lines
3 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
2020-08-14 04:58:41 +08:00
import { SaveSettingsStep, UploadErrorCode, Capa } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { changeTheme, convertThemeName } from 'Common/Utils';
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 } 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 ThemesUserSettings /*extends AbstractViewSettings*/ {
2016-07-16 05:29:42 +08:00
constructor() {
this.theme = ThemeStore.theme;
this.themes = ThemeStore.themes;
this.themesObjects = ko.observableArray();
2016-06-28 04:54:38 +08:00
2022-02-24 19:22:27 +08:00
themeBackground.enabled = SettingsCapa(Capa.UserBackground);
2022-02-21 22:36:34 +08:00
this.background = themeBackground;
2014-08-21 23:08:34 +08:00
this.themeTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 });
2022-02-21 22:36:34 +08:00
ThemeStore.theme.subscribe(value => {
this.themesObjects.forEach(theme => {
2016-07-16 05:29:42 +08:00
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
});
}
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
return true;
})
.on('onComplete', (id, result, data) => {
2022-02-21 22:36:34 +08:00
themeBackground.loading(false);
2016-07-16 05:29:42 +08:00
2019-07-05 03:19:24 +08:00
if (result && id && data && data.Result && data.Result.Name && data.Result.Hash) {
2022-02-21 22:36:34 +08:00
themeBackground.name(data.Result.Name);
themeBackground.hash(data.Result.Hash);
2019-07-05 03:19:24 +08:00
} else {
2022-02-21 22:36:34 +08:00
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
}
}
2019-07-05 03:19:24 +08:00
if (!errorMsg && data.ErrorMessage) {
2016-07-16 05:29:42 +08:00
errorMsg = data.ErrorMessage;
}
2022-02-21 22:36:34 +08:00
themeBackground.error(errorMsg || i18n('SETTINGS_THEMES/ERROR_UNKNOWN'));
}
2016-07-16 05:29:42 +08:00
return true;
});
}
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
}