Move addObservablesTo, addComputablesTo & addSubscribablesTo to Common/Utils

This commit is contained in:
djmaze 2021-03-16 10:59:47 +01:00
parent eda0ee695d
commit 4b6f6b1bfc
18 changed files with 55 additions and 46 deletions

View file

@ -136,3 +136,16 @@ export function changeTheme(value, themeTrigger = ()=>{}) {
.then(clearTimer, clearTimer); .then(clearTimer, clearTimer);
} }
} }
export function addObservablesTo(target, observables) {
Object.entries(observables).forEach(([key, value]) =>
target[key] = /*Array.isArray(value) ? ko.observableArray(value) :*/ ko.observable(value) );
}
export function addComputablesTo(target, computables) {
Object.entries(computables).forEach(([key, fn]) => target[key] = ko.computed(fn));
}
export function addSubscribablesTo(target, subscribables) {
Object.entries(subscribables).forEach(([key, fn]) => target[key].subscribe(fn));
}

11
dev/External/ko.js vendored
View file

@ -197,14 +197,3 @@ ko.extenders.falseTimeout = (target, option) => {
ko.observable.fn.deleteAccessHelper = function() { ko.observable.fn.deleteAccessHelper = function() {
return this.extend({ falseTimeout: 3000, toggleSubscribeProperty: [this, 'deleteAccess'] }); return this.extend({ falseTimeout: 3000, toggleSubscribeProperty: [this, 'deleteAccess'] });
}; };
ko.addObservablesTo = (target, observables) => {
Object.entries(observables).forEach(([key, value]) =>
target[key] = /*Array.isArray(value) ? ko.observableArray(value) :*/ ko.observable(value) );
};
ko.addComputablesTo = (target, computables) =>
Object.entries(computables).forEach(([key, fn]) => target[key] = ko.computed(fn));
ko.addSubscribablesTo = (target, subscribables) =>
Object.entries(subscribables).forEach(([key, fn]) => target[key].subscribe(fn));

View file

@ -1,3 +1,4 @@
import { addObservablesTo, addComputablesTo } from 'Common/Utils';
function dispose(disposable) { function dispose(disposable) {
if (disposable && 'function' === typeof disposable.dispose) { if (disposable && 'function' === typeof disposable.dispose) {
@ -33,11 +34,11 @@ export class AbstractModel {
} }
addObservables(observables) { addObservables(observables) {
ko.addObservablesTo(this, observables); addObservablesTo(this, observables);
} }
addComputables(computables) { addComputables(computables) {
ko.addComputablesTo(this, computables); addComputablesTo(this, computables);
} }
addSubscribables(subscribables) { addSubscribables(subscribables) {

View file

@ -1,6 +1,6 @@
import ko from 'ko'; import ko from 'ko';
import { inFocus } from 'Common/Utils'; import { inFocus, addObservablesTo, addComputablesTo, addSubscribablesTo } from 'Common/Utils';
import { KeyState } from 'Common/Enums'; import { KeyState } from 'Common/Enums';
import { keyScope } from 'Common/Globals'; import { keyScope } from 'Common/Globals';
import { ViewType } from 'Knoin/Knoin'; import { ViewType } from 'Knoin/Knoin';
@ -46,15 +46,15 @@ class AbstractView {
} }
addObservables(observables) { addObservables(observables) {
ko.addObservablesTo(this, observables); addObservablesTo(this, observables);
} }
addComputables(computables) { addComputables(computables) {
ko.addComputablesTo(this, computables); addComputablesTo(this, computables);
} }
addSubscribables(subscribables) { addSubscribables(subscribables) {
ko.addSubscribablesTo(this, subscribables); addSubscribablesTo(this, subscribables);
} }
} }

View file

@ -2,7 +2,7 @@ import ko from 'ko';
import { SaveSettingsStep } from 'Common/Enums'; import { SaveSettingsStep } from 'Common/Enums';
import { SettingsGet } from 'Common/Globals'; import { SettingsGet } from 'Common/Globals';
import { settingsSaveHelperSimpleFunction, defaultOptionsAfterRender } from 'Common/Utils'; import { settingsSaveHelperSimpleFunction, defaultOptionsAfterRender, addObservablesTo } from 'Common/Utils';
import Remote from 'Remote/Admin/Fetch'; import Remote from 'Remote/Admin/Fetch';
import { decorateKoCommands } from 'Knoin/Knoin'; import { decorateKoCommands } from 'Knoin/Knoin';
@ -11,7 +11,7 @@ export class ContactsAdminSettings {
constructor() { constructor() {
this.defaultOptionsAfterRender = defaultOptionsAfterRender; this.defaultOptionsAfterRender = defaultOptionsAfterRender;
ko.addObservablesTo(this, { addObservablesTo(this, {
enableContacts: !!SettingsGet('ContactsEnable'), enableContacts: !!SettingsGet('ContactsEnable'),
contactsSync: !!SettingsGet('ContactsSync'), contactsSync: !!SettingsGet('ContactsSync'),
contactsType: '', contactsType: '',

View file

@ -4,7 +4,9 @@ import {
pInt, pInt,
settingsSaveHelperSimpleFunction, settingsSaveHelperSimpleFunction,
changeTheme, changeTheme,
convertThemeName convertThemeName,
addObservablesTo,
addSubscribablesTo
} from 'Common/Utils'; } from 'Common/Utils';
import { Capa, SaveSettingsStep } from 'Common/Enums'; import { Capa, SaveSettingsStep } from 'Common/Enums';
@ -34,7 +36,7 @@ export class GeneralAdminSettings {
this.theme = ThemeStore.theme; this.theme = ThemeStore.theme;
this.themes = ThemeStore.themes; this.themes = ThemeStore.themes;
ko.addObservablesTo(this, { addObservablesTo(this, {
allowLanguagesOnSettings: !!SettingsGet('AllowLanguagesOnSettings'), allowLanguagesOnSettings: !!SettingsGet('AllowLanguagesOnSettings'),
newMoveToFolder: !!SettingsGet('NewMoveToFolder'), newMoveToFolder: !!SettingsGet('NewMoveToFolder'),
attachmentLimitTrigger: SaveSettingsStep.Idle, attachmentLimitTrigger: SaveSettingsStep.Idle,
@ -87,7 +89,7 @@ export class GeneralAdminSettings {
Remote.saveAdminConfig(fn, data); Remote.saveAdminConfig(fn, data);
}; };
ko.addSubscribablesTo(this, { addSubscribablesTo(this, {
mainAttachmentLimit: value => mainAttachmentLimit: value =>
Remote.saveAdminConfig(settingsSaveHelperSimpleFunction(this.attachmentLimitTrigger, this), { Remote.saveAdminConfig(settingsSaveHelperSimpleFunction(this.attachmentLimitTrigger, this), {
'AttachmentLimit': pInt(value) 'AttachmentLimit': pInt(value)

View file

@ -1,13 +1,13 @@
import ko from 'ko'; import ko from 'ko';
import { Settings, SettingsGet } from 'Common/Globals'; import { Settings, SettingsGet } from 'Common/Globals';
import { settingsSaveHelperSimpleFunction } from 'Common/Utils'; import { settingsSaveHelperSimpleFunction, addObservablesTo } from 'Common/Utils';
import Remote from 'Remote/Admin/Fetch'; import Remote from 'Remote/Admin/Fetch';
export class LoginAdminSettings { export class LoginAdminSettings {
constructor() { constructor() {
ko.addObservablesTo(this, { addObservablesTo(this, {
determineUserLanguage: !!SettingsGet('DetermineUserLanguage'), determineUserLanguage: !!SettingsGet('DetermineUserLanguage'),
determineUserDomain: !!SettingsGet('DetermineUserDomain'), determineUserDomain: !!SettingsGet('DetermineUserDomain'),
allowLanguagesOnLogin: !!SettingsGet('AllowLanguagesOnLogin'), allowLanguagesOnLogin: !!SettingsGet('AllowLanguagesOnLogin'),

View file

@ -1,7 +1,6 @@
import ko from 'ko';
import { Capa } from 'Common/Enums'; import { Capa } from 'Common/Enums';
import { Settings, SettingsGet } from 'Common/Globals'; import { Settings, SettingsGet } from 'Common/Globals';
import { addObservablesTo, addSubscribablesTo } from 'Common/Utils';
import { AppAdminStore } from 'Stores/Admin/App'; import { AppAdminStore } from 'Stores/Admin/App';
@ -13,7 +12,7 @@ export class SecurityAdminSettings {
constructor() { constructor() {
this.weakPassword = AppAdminStore.weakPassword; this.weakPassword = AppAdminStore.weakPassword;
ko.addObservablesTo(this, { addObservablesTo(this, {
useLocalProxyForExternalImages: !!SettingsGet('UseLocalProxyForExternalImages'), useLocalProxyForExternalImages: !!SettingsGet('UseLocalProxyForExternalImages'),
verifySslCertificate: !!SettingsGet('VerifySslCertificate'), verifySslCertificate: !!SettingsGet('VerifySslCertificate'),
@ -38,7 +37,7 @@ export class SecurityAdminSettings {
capaTwoFactorAuthForce: Settings.capa(Capa.TwoFactorForce) capaTwoFactorAuthForce: Settings.capa(Capa.TwoFactorForce)
}); });
ko.addSubscribablesTo(this, { addSubscribablesTo(this, {
adminPassword: () => { adminPassword: () => {
this.adminPasswordUpdateError(false); this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false); this.adminPasswordUpdateSuccess(false);

View file

@ -1,8 +1,9 @@
import ko from 'ko'; import ko from 'ko';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
import { Notification } from 'Common/Enums'; import { Notification } from 'Common/Enums';
import { getNotification } from 'Common/Translator'; import { getNotification } from 'Common/Translator';
import { addObservablesTo } from 'Common/Utils';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
import { SieveUserStore } from 'Stores/User/Sieve'; import { SieveUserStore } from 'Stores/User/Sieve';
import Remote from 'Remote/User/Fetch'; import Remote from 'Remote/User/Fetch';
@ -18,7 +19,7 @@ export class FiltersUserSettings {
this.scripts = SieveUserStore.scripts; this.scripts = SieveUserStore.scripts;
this.loading = ko.observable(false).extend({ debounce: 200 }); this.loading = ko.observable(false).extend({ debounce: 200 });
ko.addObservablesTo(this, { addObservablesTo(this, {
serverError: false, serverError: false,
serverErrorDesc: '' serverErrorDesc: ''
}); });

View file

@ -4,7 +4,7 @@ import { MESSAGES_PER_PAGE_VALUES } from 'Common/Consts';
import { SaveSettingsStep } from 'Common/Enums'; import { SaveSettingsStep } from 'Common/Enums';
import { EditorDefaultType, Layout } from 'Common/EnumsUser'; import { EditorDefaultType, Layout } from 'Common/EnumsUser';
import { SettingsGet } from 'Common/Globals'; import { SettingsGet } from 'Common/Globals';
import { settingsSaveHelperSimpleFunction } from 'Common/Utils'; import { settingsSaveHelperSimpleFunction, addObservablesTo } from 'Common/Utils';
import { i18n, trigger as translatorTrigger, reload as translatorReload, convertLangName } from 'Common/Translator'; import { i18n, trigger as translatorTrigger, reload as translatorReload, convertLangName } from 'Common/Translator';
import { showScreenPopup } from 'Knoin/Knoin'; import { showScreenPopup } from 'Knoin/Knoin';
@ -47,7 +47,7 @@ export class GeneralUserSettings {
this.languageFullName = ko.computed(() => convertLangName(this.language())); this.languageFullName = ko.computed(() => convertLangName(this.language()));
this.languageTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 }); this.languageTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 });
ko.addObservablesTo(this, { addObservablesTo(this, {
mppTrigger: SaveSettingsStep.Idle, mppTrigger: SaveSettingsStep.Idle,
editorDefaultTypeTrigger: SaveSettingsStep.Idle, editorDefaultTypeTrigger: SaveSettingsStep.Idle,
layoutTrigger: SaveSettingsStep.Idle layoutTrigger: SaveSettingsStep.Idle

View file

@ -1,5 +1,6 @@
import ko from 'ko'; import ko from 'ko';
import { SettingsGet } from 'Common/Globals'; import { SettingsGet } from 'Common/Globals';
import { addObservablesTo } from 'Common/Utils';
export const AccountUserStore = { export const AccountUserStore = {
accounts: ko.observableArray(), accounts: ko.observableArray(),
@ -24,7 +25,7 @@ export const AccountUserStore = {
} }
}; };
ko.addObservablesTo(AccountUserStore, { addObservablesTo(AccountUserStore, {
email: '', email: '',
parentEmail: '', parentEmail: '',
signature: '' signature: ''

View file

@ -2,6 +2,7 @@ import ko from 'ko';
import { KeyState } from 'Common/Enums'; import { KeyState } from 'Common/Enums';
import { Focused } from 'Common/EnumsUser'; import { Focused } from 'Common/EnumsUser';
import { keyScope, leftPanelDisabled, Settings, SettingsGet } from 'Common/Globals'; import { keyScope, leftPanelDisabled, Settings, SettingsGet } from 'Common/Globals';
import { addObservablesTo } from 'Common/Utils';
import { ThemeStore } from 'Stores/Theme'; import { ThemeStore } from 'Stores/Theme';
export const AppUserStore = { export const AppUserStore = {
@ -26,7 +27,7 @@ export const AppUserStore = {
} }
}; };
ko.addObservablesTo(AppUserStore, { addObservablesTo(AppUserStore, {
currentAudio: '', currentAudio: '',
focusedState: Focused.None, focusedState: Focused.None,

View file

@ -1,5 +1,6 @@
import ko from 'ko'; import ko from 'ko';
import { SettingsGet } from 'Common/Globals'; import { SettingsGet } from 'Common/Globals';
import { addObservablesTo } from 'Common/Utils';
export const ContactUserStore = ko.observableArray(); export const ContactUserStore = ko.observableArray();
@ -7,7 +8,7 @@ ContactUserStore.loading = ko.observable(false).extend({ debounce: 200 });
ContactUserStore.importing = ko.observable(false).extend({ debounce: 200 }); ContactUserStore.importing = ko.observable(false).extend({ debounce: 200 });
ContactUserStore.syncing = ko.observable(false).extend({ debounce: 200 }); ContactUserStore.syncing = ko.observable(false).extend({ debounce: 200 });
ko.addObservablesTo(ContactUserStore, { addObservablesTo(ContactUserStore, {
allowSync: false, allowSync: false,
enableSync: false, enableSync: false,
syncUrl: '', syncUrl: '',

View file

@ -2,13 +2,14 @@ import ko from 'ko';
import { FolderType } from 'Common/EnumsUser'; import { FolderType } from 'Common/EnumsUser';
import { UNUSED_OPTION_VALUE } from 'Common/Consts'; import { UNUSED_OPTION_VALUE } from 'Common/Consts';
import { addObservablesTo } from 'Common/Utils';
import { folderListOptionsBuilder } from 'Common/UtilsUser'; import { folderListOptionsBuilder } from 'Common/UtilsUser';
import { getFolderInboxName, getFolderFromCacheList } from 'Common/Cache'; import { getFolderInboxName, getFolderFromCacheList } from 'Common/Cache';
import { SettingsGet } from 'Common/Globals'; import { SettingsGet } from 'Common/Globals';
export const FolderUserStore = new class { export const FolderUserStore = new class {
constructor() { constructor() {
ko.addObservablesTo(this, { addObservablesTo(this, {
/** /**
* To use "checkable" option in /#/settings/folders * To use "checkable" option in /#/settings/folders
* When true, getNextFolderNames only lists system and "checkable" folders * When true, getNextFolderNames only lists system and "checkable" folders

View file

@ -3,7 +3,7 @@ import ko from 'ko';
import { Notification } from 'Common/Enums'; import { Notification } from 'Common/Enums';
import { Focused, MessageSetAction } from 'Common/EnumsUser'; import { Focused, MessageSetAction } from 'Common/EnumsUser';
import { doc, elementById } from 'Common/Globals'; import { doc, elementById } from 'Common/Globals';
import { pInt, pString } from 'Common/Utils'; import { pInt, pString, addObservablesTo } from 'Common/Utils';
import { plainToHtml } from 'Common/UtilsUser'; import { plainToHtml } from 'Common/UtilsUser';
import { import {
@ -72,7 +72,7 @@ export const MessageUserStore = new class {
this.list = ko.observableArray().extend({ debounce: 0 }); this.list = ko.observableArray().extend({ debounce: 0 });
ko.addObservablesTo(this, { addObservablesTo(this, {
listCount: 0, listCount: 0,
listSearch: '', listSearch: '',
listThreadUid: '', listThreadUid: '',

View file

@ -2,7 +2,7 @@ import ko from 'ko';
import { MESSAGES_PER_PAGE_VALUES } from 'Common/Consts'; import { MESSAGES_PER_PAGE_VALUES } from 'Common/Consts';
import { Layout, EditorDefaultType } from 'Common/EnumsUser'; import { Layout, EditorDefaultType } from 'Common/EnumsUser';
import { pInt } from 'Common/Utils'; import { pInt, addObservablesTo } from 'Common/Utils';
import { $htmlCL, SettingsGet } from 'Common/Globals'; import { $htmlCL, SettingsGet } from 'Common/Globals';
import { ThemeStore } from 'Stores/Theme'; import { ThemeStore } from 'Stores/Theme';
@ -23,7 +23,7 @@ export const SettingsUserStore = new class {
this.messagesPerPage = ko.observable(20).extend({ limitedList: MESSAGES_PER_PAGE_VALUES }); this.messagesPerPage = ko.observable(20).extend({ limitedList: MESSAGES_PER_PAGE_VALUES });
ko.addObservablesTo(this, { addObservablesTo(this, {
showImages: false, showImages: false,
removeColors: false, removeColors: false,
useCheckboxesInList: true, useCheckboxesInList: true,

View file

@ -3,7 +3,7 @@ import ko from 'ko';
import { SetSystemFoldersNotification } from 'Common/EnumsUser'; import { SetSystemFoldersNotification } from 'Common/EnumsUser';
import { UNUSED_OPTION_VALUE } from 'Common/Consts'; import { UNUSED_OPTION_VALUE } from 'Common/Consts';
import { Settings } from 'Common/Globals'; import { Settings } from 'Common/Globals';
import { defaultOptionsAfterRender } from 'Common/Utils'; import { defaultOptionsAfterRender, addSubscribablesTo } from 'Common/Utils';
import { folderListOptionsBuilder } from 'Common/UtilsUser'; import { folderListOptionsBuilder } from 'Common/UtilsUser';
import { initOnStartOrLangChange, i18n } from 'Common/Translator'; import { initOnStartOrLangChange, i18n } from 'Common/Translator';
@ -75,7 +75,7 @@ class FolderSystemPopupView extends AbstractViewPopup {
fSaveSystemFolders(); fSaveSystemFolders();
}; };
ko.addSubscribablesTo(FolderUserStore, { addSubscribablesTo(FolderUserStore, {
sentFolder: fCallback, sentFolder: fCallback,
draftFolder: fCallback, draftFolder: fCallback,
spamFolder: fCallback, spamFolder: fCallback,

View file

@ -1,9 +1,9 @@
import ko from 'ko'; import ko from 'ko';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
import { Notification } from 'Common/Enums'; import { Notification } from 'Common/Enums';
import { getNotification } from 'Common/Translator'; import { getNotification, i18nToNodes } from 'Common/Translator';
import { i18nToNodes } from 'Common/Translator'; import { addObservablesTo } from 'Common/Utils';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
import Remote from 'Remote/User/Fetch'; import Remote from 'Remote/User/Fetch';
import { FilterModel } from 'Model/Filter'; import { FilterModel } from 'Model/Filter';
@ -18,7 +18,7 @@ class SieveScriptPopupView extends AbstractViewPopup {
constructor() { constructor() {
super('SieveScript'); super('SieveScript');
ko.addObservablesTo(this, { addObservablesTo(this, {
saveError: false, saveError: false,
saveErrorText: '', saveErrorText: '',
rawActive: false, rawActive: false,