some ko.computed() to addComputables()/addComputablesTo()

This commit is contained in:
djmaze 2021-09-03 16:37:20 +02:00
parent 099560ca6a
commit 023c8f603b
9 changed files with 110 additions and 107 deletions

View file

@ -7,7 +7,8 @@ import {
changeTheme,
convertThemeName,
addObservablesTo,
addSubscribablesTo
addSubscribablesTo,
addComputablesTo
} from 'Common/Utils';
import { Capa, SaveSettingsStep } from 'Common/Enums';
@ -74,12 +75,12 @@ export class GeneralAdminSettings {
].join('')
: '';
this.themesOptions = ko.computed(() =>
this.themes.map(theme => ({ optValue: theme, optText: convertThemeName(theme) }))
);
addComputablesTo(this, {
themesOptions: () => this.themes.map(theme => ({ optValue: theme, optText: convertThemeName(theme) })),
this.languageFullName = ko.computed(() => convertLangName(this.language()));
this.languageAdminFullName = ko.computed(() => convertLangName(this.languageAdmin()));
languageFullName: () => convertLangName(this.language()),
languageAdminFullName: () => convertLangName(this.languageAdmin())
});
this.languageAdminTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 });

View file

@ -9,6 +9,7 @@ import Remote from 'Remote/Admin/Fetch';
import { showScreenPopup } from 'Knoin/Knoin';
import { PluginPopupView } from 'View/Popup/Plugin';
import { SettingsGet } from 'Common/Globals';
import { addComputablesTo } from 'Common/Utils';
export class PackagesAdminSettings {
constructor() {
@ -16,17 +17,13 @@ export class PackagesAdminSettings {
this.packages = PackageAdminStore;
this.packagesCurrent = ko.computed(() =>
PackageAdminStore.filter(item => item && item.installed && !item.canBeUpdated)
);
this.packagesAvailableForUpdate = ko.computed(() =>
PackageAdminStore.filter(item => item && item.installed && !!item.canBeUpdated)
);
this.packagesAvailableForInstallation = ko.computed(() =>
PackageAdminStore.filter(item => item && !item.installed)
);
addComputablesTo(this, {
packagesCurrent: () => PackageAdminStore.filter(item => item && item.installed && !item.canBeUpdated),
packagesAvailableForUpdate: () => PackageAdminStore.filter(item => item && item.installed && !!item.canBeUpdated),
packagesAvailableForInstallation: () => PackageAdminStore.filter(item => item && !item.installed),
this.visibility = ko.computed(() => (PackageAdminStore.loading() ? 'visible' : 'hidden'));
visibility: () => (PackageAdminStore.loading() ? 'visible' : 'hidden')
});
this.enabledPlugins = ko.observable(!!SettingsGet('EnabledPlugins'));
this.enabledPlugins.subscribe(value =>

View file

@ -4,7 +4,7 @@ import { MESSAGES_PER_PAGE_VALUES } from 'Common/Consts';
import { SaveSettingsStep } from 'Common/Enums';
import { EditorDefaultType, Layout } from 'Common/EnumsUser';
import { Settings, SettingsGet } from 'Common/Globals';
import { isArray, settingsSaveHelperSimpleFunction, addObservablesTo, addSubscribablesTo } from 'Common/Utils';
import { isArray, settingsSaveHelperSimpleFunction, addObservablesTo, addSubscribablesTo, addComputablesTo } from 'Common/Utils';
import { i18n, trigger as translatorTrigger, reload as translatorReload, convertLangName } from 'Common/Translator';
import { showScreenPopup } from 'Knoin/Knoin';
@ -47,7 +47,6 @@ export class GeneralUserSettings {
this.replySameFolder = SettingsUserStore.replySameFolder;
this.allowLanguagesOnSettings = !!SettingsGet('AllowLanguagesOnSettings');
this.languageFullName = ko.computed(() => convertLangName(this.language()));
this.languageTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 });
addObservablesTo(this, {
@ -59,33 +58,37 @@ export class GeneralUserSettings {
this.identities = IdentityUserStore;
this.identityMain = ko.computed(() => {
const list = this.identities();
return isArray(list) ? list.find(item => item && !item.id()) : null;
});
addComputablesTo(this, {
languageFullName: () => convertLangName(this.language()),
this.identityMainDesc = ko.computed(() => {
const identity = this.identityMain();
return identity ? identity.formattedName() : '---';
});
identityMain: () => {
const list = this.identities();
return isArray(list) ? list.find(item => item && !item.id()) : null;
},
this.editorDefaultTypes = ko.computed(() => {
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') }
];
});
identityMainDesc: () => {
const identity = this.identityMain();
return identity ? identity.formattedName() : '---';
},
this.layoutTypes = ko.computed(() => {
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') }
];
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') }
];
}
});
const fReloadLanguageHelper = (saveSettingsStep) => () => {

View file

@ -8,15 +8,17 @@ import Remote from 'Remote/User/Fetch';
import { showScreenPopup } from 'Knoin/Knoin';
import { TemplatePopupView } from 'View/Popup/Template';
import { addComputablesTo } from 'Common/Utils';
export class TemplatesUserSettings {
constructor() {
this.templates = TemplateUserStore.templates;
this.processText = ko.computed(() =>
TemplateUserStore.templates.loading() ? i18n('SETTINGS_TEMPLETS/LOADING_PROCESS') : ''
);
this.visibility = ko.computed(() => this.processText() ? 'visible' : 'hidden');
addComputablesTo({
processText: () => TemplateUserStore.templates.loading() ? i18n('SETTINGS_TEMPLETS/LOADING_PROCESS') : '',
visibility: () => this.processText() ? 'visible' : 'hidden'
});
this.templateForDeletion = ko.observable(null).deleteAccessHelper();
}