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';
|
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';
|
2021-07-20 22:49:03 +08:00
|
|
|
import { Settings } from 'Common/Globals';
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2021-01-27 07:26:31 +08:00
|
|
|
import { ThemeStore } from 'Stores/Theme';
|
2015-01-26 07:09:22 +08:00
|
|
|
|
2020-09-15 01:40:56 +08:00
|
|
|
import Remote from 'Remote/User/Fetch';
|
2014-08-22 23:08:56 +08:00
|
|
|
|
2021-09-23 02:17:44 +08:00
|
|
|
export class ThemesUserSettings /*extends AbstractViewSettings*/ {
|
2016-07-16 05:29:42 +08:00
|
|
|
constructor() {
|
|
|
|
this.theme = ThemeStore.theme;
|
|
|
|
this.themes = ThemeStore.themes;
|
2021-01-22 19:23:20 +08:00
|
|
|
this.themesObjects = ko.observableArray();
|
2016-06-28 04:54:38 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
this.background = {};
|
2021-02-17 21:40:21 +08:00
|
|
|
this.background.name = ThemeStore.userBackgroundName;
|
|
|
|
this.background.hash = ThemeStore.userBackgroundHash;
|
2016-07-16 05:29:42 +08:00
|
|
|
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
|
|
|
|
2021-03-10 18:44:48 +08:00
|
|
|
this.capaUserBackground = ko.observable(Settings.capa(Capa.UserBackground));
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2021-02-10 19:12:36 +08:00
|
|
|
this.themeTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 });
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
this.theme.subscribe((value) => {
|
2021-01-22 19:23:20 +08:00
|
|
|
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, {
|
2021-03-25 04:26:40 +08:00
|
|
|
Theme: value
|
2016-07-16 05:29:42 +08:00
|
|
|
});
|
2016-06-30 08:02:45 +08:00
|
|
|
});
|
|
|
|
}
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
onBuild() {
|
|
|
|
const currentTheme = this.theme();
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
this.themesObjects(
|
2021-01-22 19:23:20 +08:00
|
|
|
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)
|
|
|
|
}))
|
|
|
|
);
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2021-08-13 02:17:37 +08:00
|
|
|
// initUploader
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.background.uploaderButton() && this.capaUserBackground()) {
|
|
|
|
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,
|
2020-09-01 22:57:14 +08:00
|
|
|
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('');
|
2014-11-08 04:21:10 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (!errorMsg && data.ErrorMessage) {
|
2016-07-16 05:29:42 +08:00
|
|
|
errorMsg = data.ErrorMessage;
|
2014-11-08 04:21:10 +08:00
|
|
|
}
|
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
this.background.error(errorMsg || i18n('SETTINGS_THEMES/ERROR_UNKNOWN'));
|
2014-11-08 04:21:10 +08:00
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
this.background.error('');
|
|
|
|
}
|
|
|
|
|
|
|
|
clearBackground() {
|
|
|
|
if (this.capaUserBackground()) {
|
2021-12-03 06:15:24 +08:00
|
|
|
Remote.request('ClearUserBackground', () => {
|
2021-08-13 02:17:37 +08:00
|
|
|
this.background.name('');
|
|
|
|
this.background.hash('');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|