snappymail/dev/Settings/User/Themes.js

135 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import ko from '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';
import { userBackground, themePreviewLink, uploadBackground } from 'Common/Links';
import { i18n } from 'Common/Translator';
2014-08-25 15:10:51 +08:00
2016-07-16 05:29:42 +08:00
import ThemeStore from 'Stores/Theme';
import Remote from 'Remote/User/Fetch';
2014-08-22 23:08:56 +08:00
2019-07-05 03:19:24 +08:00
class ThemesUserSettings {
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
2016-07-16 05:29:42 +08:00
this.background = {};
this.background.name = ThemeStore.themeBackgroundName;
this.background.hash = ThemeStore.themeBackgroundHash;
this.background.uploaderButton = ko.observable(null);
this.background.loading = ko.observable(false);
this.background.error = ko.observable('');
2014-08-21 23:08:34 +08:00
this.capaUserBackground = ko.observable(rl.settings.capa(Capa.UserBackground));
2020-08-14 04:58:41 +08:00
this.themeTrigger = ko.observable(SaveSettingsStep.Idle).extend({ throttle: 100 });
2016-07-16 05:29:42 +08:00
this.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-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.background.hash.subscribe((value) => {
const b = document.body, cl = document.documentElement.classList;
2019-07-05 03:19:24 +08:00
if (!value) {
cl.remove('UserBackground');
b.removeAttribute('style');
2019-07-05 03:19:24 +08:00
} else {
cl.add('UserBackground');
b.style.backgroundImage = "url("+userBackground(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() {
const currentTheme = this.theme();
2019-07-05 03:19:24 +08:00
this.themesObjects(
this.themes().map(theme => ({
2019-07-05 03:19:24 +08:00
name: theme,
nameDisplay: convertThemeName(theme),
selected: ko.observable(theme === currentTheme),
themePreviewSrc: themePreviewLink(theme)
}))
);
2016-07-16 05:29:42 +08:00
this.initUploader();
}
2016-07-16 05:29:42 +08:00
onShow() {
this.background.error('');
}
2016-07-16 05:29:42 +08:00
clearBackground() {
2019-07-05 03:19:24 +08:00
if (this.capaUserBackground()) {
2016-07-16 05:29:42 +08:00
Remote.clearUserBackground(() => {
this.background.name('');
this.background.hash('');
});
}
}
2016-07-16 05:29:42 +08:00
initUploader() {
2019-07-05 03:19:24 +08:00
if (this.background.uploaderButton() && this.capaUserBackground()) {
const oJua = new Jua({
action: uploadBackground(),
name: 'uploader',
queueSize: 1,
multipleSizeLimit: 1,
disableMultiple: true,
clickElement: this.background.uploaderButton()
2019-07-05 03:19:24 +08:00
});
2016-07-16 05:29:42 +08:00
oJua
.on('onStart', () => {
this.background.loading(true);
this.background.error('');
return true;
})
.on('onComplete', (id, result, data) => {
this.background.loading(false);
2019-07-05 03:19:24 +08:00
if (result && id && data && data.Result && data.Result.Name && data.Result.Hash) {
2016-07-16 05:29:42 +08:00
this.background.name(data.Result.Name);
this.background.hash(data.Result.Hash);
2019-07-05 03:19:24 +08:00
} else {
2016-07-16 05:29:42 +08:00
this.background.name('');
this.background.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;
}
2016-07-16 05:29:42 +08:00
this.background.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
}
2016-07-16 05:29:42 +08:00
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
export { ThemesUserSettings, ThemesUserSettings as default };