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';
|
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
|
|
|
|
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
|
|
|
|
2020-09-04 18:05:17 +08:00
|
|
|
this.capaUserBackground = ko.observable(rl.settings.capa(Capa.UserBackground));
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2020-08-14 04:58:41 +08:00
|
|
|
this.themeTrigger = ko.observable(SaveSettingsStep.Idle).extend({ throttle: 100 });
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
this.theme.subscribe((value) => {
|
2020-07-22 20:49:18 +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, {
|
|
|
|
'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) => {
|
2020-08-12 06:25:36 +08:00
|
|
|
const b = document.body, cl = document.documentElement.classList;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (!value) {
|
2020-07-15 20:25:51 +08:00
|
|
|
cl.remove('UserBackground');
|
2020-07-13 21:47:19 +08:00
|
|
|
b.removeAttribute('style');
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2020-07-15 20:25:51 +08:00
|
|
|
cl.add('UserBackground');
|
2020-07-13 21:47:19 +08:00
|
|
|
b.style.backgroundImage = "url("+userBackground(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(
|
2020-07-23 02:09:31 +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
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
this.initUploader();
|
|
|
|
}
|
2014-11-08 04:21:10 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
onShow() {
|
|
|
|
this.background.error('');
|
|
|
|
}
|
2014-11-08 04:21:10 +08:00
|
|
|
|
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('');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 04:21:10 +08:00
|
|
|
|
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({
|
2020-09-01 22:57:14 +08:00
|
|
|
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('');
|
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
|
|
|
}
|
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 };
|