snappymail/dev/Settings/User/General.js

162 lines
6 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import ko from 'ko';
2016-06-30 08:02:45 +08:00
2021-01-25 05:58:06 +08:00
import { SaveSettingsStep } from 'Common/Enums';
import { EditorDefaultType, Layout } from 'Common/EnumsUser';
2021-07-22 03:34:17 +08:00
import { Settings, SettingsGet } from 'Common/Globals';
import { isArray, settingsSaveHelperSimpleFunction, addObservablesTo, addSubscribablesTo, addComputablesTo } from 'Common/Utils';
import { i18n, trigger as translatorTrigger, reload as translatorReload, convertLangName } from 'Common/Translator';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
import { showScreenPopup } from 'Knoin/Knoin';
2016-07-16 05:29:42 +08:00
import { AppUserStore } from 'Stores/User/App';
import { LanguageStore } from 'Stores/Language';
import { SettingsUserStore } from 'Stores/User/Settings';
import { IdentityUserStore } from 'Stores/User/Identity';
import { NotificationUserStore } from 'Stores/User/Notification';
import { MessageUserStore } from 'Stores/User/Message';
2016-07-16 05:29:42 +08:00
import Remote from 'Remote/User/Fetch';
2016-07-16 05:29:42 +08:00
import { IdentityPopupView } from 'View/Popup/Identity';
import { LanguagesPopupView } from 'View/Popup/Languages';
2021-01-22 23:32:08 +08:00
export class GeneralUserSettings {
2016-07-16 05:29:42 +08:00
constructor() {
this.language = LanguageStore.language;
this.languages = LanguageStore.languages;
2021-09-02 20:21:50 +08:00
this.messageReadDelay = SettingsUserStore.messageReadDelay;
this.messagesPerPage = SettingsUserStore.messagesPerPage;
2016-06-30 08:02:45 +08:00
this.editorDefaultType = SettingsUserStore.editorDefaultType;
this.layout = SettingsUserStore.layout;
2016-06-30 08:02:45 +08:00
this.enableSoundNotification = NotificationUserStore.enableSoundNotification;
2021-07-21 17:19:52 +08:00
this.notificationSound = ko.observable(SettingsGet('NotificationSound'));
this.notificationSounds = ko.observableArray(SettingsGet('NewMailSounds'));
2016-06-30 08:02:45 +08:00
this.enableDesktopNotification = NotificationUserStore.enableDesktopNotification;
this.isDesktopNotificationDenied = NotificationUserStore.isDesktopNotificationDenied;
2016-06-17 07:23:49 +08:00
this.showImages = SettingsUserStore.showImages;
this.removeColors = SettingsUserStore.removeColors;
this.useCheckboxesInList = SettingsUserStore.useCheckboxesInList;
this.threadsAllowed = AppUserStore.threadsAllowed;
this.useThreads = SettingsUserStore.useThreads;
this.replySameFolder = SettingsUserStore.replySameFolder;
2021-03-10 18:44:48 +08:00
this.allowLanguagesOnSettings = !!SettingsGet('AllowLanguagesOnSettings');
2016-06-30 08:02:45 +08:00
this.languageTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 });
2014-08-21 23:08:34 +08:00
addObservablesTo(this, {
2020-10-27 18:09:24 +08:00
mppTrigger: SaveSettingsStep.Idle,
2021-09-02 20:21:50 +08:00
messageReadDelayTrigger: SaveSettingsStep.Idle,
2020-10-27 18:09:24 +08:00
editorDefaultTypeTrigger: SaveSettingsStep.Idle,
layoutTrigger: SaveSettingsStep.Idle
});
2014-08-21 23:08:34 +08:00
this.identities = IdentityUserStore;
2016-07-16 05:29:42 +08:00
addComputablesTo(this, {
languageFullName: () => convertLangName(this.language()),
identityMain: () => {
const list = this.identities();
return isArray(list) ? list.find(item => item && !item.id()) : null;
},
identityMainDesc: () => {
const identity = this.identityMain();
return identity ? identity.formattedName() : '---';
},
editorDefaultTypes: () => {
translatorTrigger();
return [
{ id: EditorDefaultType.Html, name: i18n('SETTINGS_GENERAL/LABEL_EDITOR_HTML') },
{ id: EditorDefaultType.Plain, name: i18n('SETTINGS_GENERAL/LABEL_EDITOR_PLAIN') },
{ id: EditorDefaultType.HtmlForced, name: i18n('SETTINGS_GENERAL/LABEL_EDITOR_HTML_FORCED') },
{ id: EditorDefaultType.PlainForced, name: i18n('SETTINGS_GENERAL/LABEL_EDITOR_PLAIN_FORCED') }
];
},
layoutTypes: () => {
translatorTrigger();
return [
{ id: Layout.NoPreview, name: i18n('SETTINGS_GENERAL/LABEL_LAYOUT_NO_SPLIT') },
{ id: Layout.SidePreview, name: i18n('SETTINGS_GENERAL/LABEL_LAYOUT_VERTICAL_SPLIT') },
{ id: Layout.BottomPreview, name: i18n('SETTINGS_GENERAL/LABEL_LAYOUT_HORIZONTAL_SPLIT') }
];
}
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2021-03-16 18:38:40 +08:00
const fReloadLanguageHelper = (saveSettingsStep) => () => {
this.languageTrigger(saveSettingsStep);
setTimeout(() => this.languageTrigger(SaveSettingsStep.Idle), 1000);
};
addSubscribablesTo(this, {
language: value => {
2016-07-16 05:29:42 +08:00
this.languageTrigger(SaveSettingsStep.Animate);
2019-07-05 03:19:24 +08:00
translatorReload(false, value)
2021-03-18 23:12:24 +08:00
.then(fReloadLanguageHelper(SaveSettingsStep.TrueResult),
fReloadLanguageHelper(SaveSettingsStep.FalseResult))
.then(() => Remote.saveSetting('Language', value));
2021-03-16 18:38:40 +08:00
},
2021-03-18 23:12:24 +08:00
editorDefaultType: value => Remote.saveSetting('EditorDefaultType', value,
settingsSaveHelperSimpleFunction(this.editorDefaultTypeTrigger, this)),
2021-03-16 18:38:40 +08:00
2021-09-02 20:21:50 +08:00
messageReadDelay: value => Remote.saveSetting('MessageReadDelay', value,
settingsSaveHelperSimpleFunction(this.messageReadDelayTrigger, this)),
2021-03-18 23:12:24 +08:00
messagesPerPage: value => Remote.saveSetting('MPP', value,
settingsSaveHelperSimpleFunction(this.mppTrigger, this)),
2014-08-21 23:08:34 +08:00
2021-03-18 23:12:24 +08:00
showImages: value => Remote.saveSetting('ShowImages', value ? 1 : 0),
2014-08-21 23:08:34 +08:00
2021-03-18 23:12:24 +08:00
removeColors: value => {
2021-03-18 23:12:40 +08:00
MessageUserStore.messagesBodiesDom().innerHTML = '';
2021-03-18 23:12:24 +08:00
Remote.saveSetting('RemoveColors', value ? 1 : 0);
},
2014-08-21 23:08:34 +08:00
2021-03-18 23:12:24 +08:00
useCheckboxesInList: value => Remote.saveSetting('UseCheckboxesInList', value ? 1 : 0),
2021-03-16 18:38:40 +08:00
2021-03-18 23:12:24 +08:00
enableDesktopNotification: value => Remote.saveSetting('DesktopNotifications', value ? 1 : 0),
2014-08-21 23:08:34 +08:00
2021-03-18 23:12:24 +08:00
enableSoundNotification: value => Remote.saveSetting('SoundNotification', value ? 1 : 0),
2021-07-21 17:19:52 +08:00
notificationSound: value => {
Remote.saveSetting('NotificationSound', value);
2021-07-22 03:34:17 +08:00
Settings.set('NotificationSound', value);
2021-07-21 17:19:52 +08:00
},
2016-07-16 05:29:42 +08:00
2021-03-18 23:12:24 +08:00
replySameFolder: value => Remote.saveSetting('ReplySameFolder', value ? 1 : 0),
2014-08-21 23:08:34 +08:00
2021-03-16 18:38:40 +08:00
useThreads: value => {
2021-03-12 23:54:37 +08:00
MessageUserStore.list([]);
2021-03-18 23:12:24 +08:00
Remote.saveSetting('UseThreads', value ? 1 : 0);
2021-03-16 18:38:40 +08:00
},
2014-08-21 23:08:34 +08:00
2021-03-16 18:38:40 +08:00
layout: value => {
2021-03-12 23:54:37 +08:00
MessageUserStore.list([]);
2021-03-18 23:12:24 +08:00
Remote.saveSetting('Layout', value, settingsSaveHelperSimpleFunction(this.layoutTrigger, this));
2021-03-16 18:38:40 +08:00
}
});
}
editMainIdentity() {
const identity = this.identityMain();
2021-07-22 03:34:17 +08:00
identity && showScreenPopup(IdentityPopupView, [identity]);
2021-03-16 18:38:40 +08:00
}
testSoundNotification() {
NotificationUserStore.playSoundNotification(true);
}
testSystemNotification() {
NotificationUserStore.displayDesktopNotification('SnappyMail', 'Test notification');
2016-07-16 05:29:42 +08:00
}
2014-08-25 15:10:51 +08:00
2016-07-16 05:29:42 +08:00
selectLanguage() {
showScreenPopup(LanguagesPopupView, [this.language, this.languages(), LanguageStore.userLanguage()]);
2016-07-16 05:29:42 +08:00
}
}