From 4b6f6b1bfcd7ed67466805500144fb6db01dd2ba Mon Sep 17 00:00:00 2001 From: djmaze Date: Tue, 16 Mar 2021 10:59:47 +0100 Subject: [PATCH] Move addObservablesTo, addComputablesTo & addSubscribablesTo to Common/Utils --- dev/Common/Utils.js | 13 +++++++++++++ dev/External/ko.js | 11 ----------- dev/Knoin/AbstractModel.js | 5 +++-- dev/Knoin/AbstractViews.js | 8 ++++---- dev/Settings/Admin/Contacts.js | 4 ++-- dev/Settings/Admin/General.js | 8 +++++--- dev/Settings/Admin/Login.js | 4 ++-- dev/Settings/Admin/Security.js | 7 +++---- dev/Settings/User/Filters.js | 5 +++-- dev/Settings/User/General.js | 4 ++-- dev/Stores/User/Account.js | 3 ++- dev/Stores/User/App.js | 3 ++- dev/Stores/User/Contact.js | 3 ++- dev/Stores/User/Folder.js | 3 ++- dev/Stores/User/Message.js | 4 ++-- dev/Stores/User/Settings.js | 4 ++-- dev/View/Popup/FolderSystem.js | 4 ++-- dev/View/Popup/SieveScript.js | 8 ++++---- 18 files changed, 55 insertions(+), 46 deletions(-) diff --git a/dev/Common/Utils.js b/dev/Common/Utils.js index 9d319eabf..0d089cbfa 100644 --- a/dev/Common/Utils.js +++ b/dev/Common/Utils.js @@ -136,3 +136,16 @@ export function changeTheme(value, themeTrigger = ()=>{}) { .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)); +} diff --git a/dev/External/ko.js b/dev/External/ko.js index 976bb253d..8393b953d 100644 --- a/dev/External/ko.js +++ b/dev/External/ko.js @@ -197,14 +197,3 @@ ko.extenders.falseTimeout = (target, option) => { ko.observable.fn.deleteAccessHelper = function() { 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)); diff --git a/dev/Knoin/AbstractModel.js b/dev/Knoin/AbstractModel.js index 4da7ebcc3..9dd3df28b 100644 --- a/dev/Knoin/AbstractModel.js +++ b/dev/Knoin/AbstractModel.js @@ -1,3 +1,4 @@ +import { addObservablesTo, addComputablesTo } from 'Common/Utils'; function dispose(disposable) { if (disposable && 'function' === typeof disposable.dispose) { @@ -33,11 +34,11 @@ export class AbstractModel { } addObservables(observables) { - ko.addObservablesTo(this, observables); + addObservablesTo(this, observables); } addComputables(computables) { - ko.addComputablesTo(this, computables); + addComputablesTo(this, computables); } addSubscribables(subscribables) { diff --git a/dev/Knoin/AbstractViews.js b/dev/Knoin/AbstractViews.js index 34db09991..bbb6baa93 100644 --- a/dev/Knoin/AbstractViews.js +++ b/dev/Knoin/AbstractViews.js @@ -1,6 +1,6 @@ import ko from 'ko'; -import { inFocus } from 'Common/Utils'; +import { inFocus, addObservablesTo, addComputablesTo, addSubscribablesTo } from 'Common/Utils'; import { KeyState } from 'Common/Enums'; import { keyScope } from 'Common/Globals'; import { ViewType } from 'Knoin/Knoin'; @@ -46,15 +46,15 @@ class AbstractView { } addObservables(observables) { - ko.addObservablesTo(this, observables); + addObservablesTo(this, observables); } addComputables(computables) { - ko.addComputablesTo(this, computables); + addComputablesTo(this, computables); } addSubscribables(subscribables) { - ko.addSubscribablesTo(this, subscribables); + addSubscribablesTo(this, subscribables); } } diff --git a/dev/Settings/Admin/Contacts.js b/dev/Settings/Admin/Contacts.js index 6c5bbcad6..3702fef09 100644 --- a/dev/Settings/Admin/Contacts.js +++ b/dev/Settings/Admin/Contacts.js @@ -2,7 +2,7 @@ import ko from 'ko'; import { SaveSettingsStep } from 'Common/Enums'; 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 { decorateKoCommands } from 'Knoin/Knoin'; @@ -11,7 +11,7 @@ export class ContactsAdminSettings { constructor() { this.defaultOptionsAfterRender = defaultOptionsAfterRender; - ko.addObservablesTo(this, { + addObservablesTo(this, { enableContacts: !!SettingsGet('ContactsEnable'), contactsSync: !!SettingsGet('ContactsSync'), contactsType: '', diff --git a/dev/Settings/Admin/General.js b/dev/Settings/Admin/General.js index c2e8ff102..cd56adfad 100644 --- a/dev/Settings/Admin/General.js +++ b/dev/Settings/Admin/General.js @@ -4,7 +4,9 @@ import { pInt, settingsSaveHelperSimpleFunction, changeTheme, - convertThemeName + convertThemeName, + addObservablesTo, + addSubscribablesTo } from 'Common/Utils'; import { Capa, SaveSettingsStep } from 'Common/Enums'; @@ -34,7 +36,7 @@ export class GeneralAdminSettings { this.theme = ThemeStore.theme; this.themes = ThemeStore.themes; - ko.addObservablesTo(this, { + addObservablesTo(this, { allowLanguagesOnSettings: !!SettingsGet('AllowLanguagesOnSettings'), newMoveToFolder: !!SettingsGet('NewMoveToFolder'), attachmentLimitTrigger: SaveSettingsStep.Idle, @@ -87,7 +89,7 @@ export class GeneralAdminSettings { Remote.saveAdminConfig(fn, data); }; - ko.addSubscribablesTo(this, { + addSubscribablesTo(this, { mainAttachmentLimit: value => Remote.saveAdminConfig(settingsSaveHelperSimpleFunction(this.attachmentLimitTrigger, this), { 'AttachmentLimit': pInt(value) diff --git a/dev/Settings/Admin/Login.js b/dev/Settings/Admin/Login.js index e3f617572..a4da1c855 100644 --- a/dev/Settings/Admin/Login.js +++ b/dev/Settings/Admin/Login.js @@ -1,13 +1,13 @@ import ko from 'ko'; import { Settings, SettingsGet } from 'Common/Globals'; -import { settingsSaveHelperSimpleFunction } from 'Common/Utils'; +import { settingsSaveHelperSimpleFunction, addObservablesTo } from 'Common/Utils'; import Remote from 'Remote/Admin/Fetch'; export class LoginAdminSettings { constructor() { - ko.addObservablesTo(this, { + addObservablesTo(this, { determineUserLanguage: !!SettingsGet('DetermineUserLanguage'), determineUserDomain: !!SettingsGet('DetermineUserDomain'), allowLanguagesOnLogin: !!SettingsGet('AllowLanguagesOnLogin'), diff --git a/dev/Settings/Admin/Security.js b/dev/Settings/Admin/Security.js index 58c774bc1..185cd4aee 100644 --- a/dev/Settings/Admin/Security.js +++ b/dev/Settings/Admin/Security.js @@ -1,7 +1,6 @@ -import ko from 'ko'; - import { Capa } from 'Common/Enums'; import { Settings, SettingsGet } from 'Common/Globals'; +import { addObservablesTo, addSubscribablesTo } from 'Common/Utils'; import { AppAdminStore } from 'Stores/Admin/App'; @@ -13,7 +12,7 @@ export class SecurityAdminSettings { constructor() { this.weakPassword = AppAdminStore.weakPassword; - ko.addObservablesTo(this, { + addObservablesTo(this, { useLocalProxyForExternalImages: !!SettingsGet('UseLocalProxyForExternalImages'), verifySslCertificate: !!SettingsGet('VerifySslCertificate'), @@ -38,7 +37,7 @@ export class SecurityAdminSettings { capaTwoFactorAuthForce: Settings.capa(Capa.TwoFactorForce) }); - ko.addSubscribablesTo(this, { + addSubscribablesTo(this, { adminPassword: () => { this.adminPasswordUpdateError(false); this.adminPasswordUpdateSuccess(false); diff --git a/dev/Settings/User/Filters.js b/dev/Settings/User/Filters.js index 26c1cf0bb..b4016a44c 100644 --- a/dev/Settings/User/Filters.js +++ b/dev/Settings/User/Filters.js @@ -1,8 +1,9 @@ import ko from 'ko'; -import { delegateRunOnDestroy } from 'Common/UtilsUser'; import { Notification } from 'Common/Enums'; import { getNotification } from 'Common/Translator'; +import { addObservablesTo } from 'Common/Utils'; +import { delegateRunOnDestroy } from 'Common/UtilsUser'; import { SieveUserStore } from 'Stores/User/Sieve'; import Remote from 'Remote/User/Fetch'; @@ -18,7 +19,7 @@ export class FiltersUserSettings { this.scripts = SieveUserStore.scripts; this.loading = ko.observable(false).extend({ debounce: 200 }); - ko.addObservablesTo(this, { + addObservablesTo(this, { serverError: false, serverErrorDesc: '' }); diff --git a/dev/Settings/User/General.js b/dev/Settings/User/General.js index 233ccc13e..5b88faaab 100644 --- a/dev/Settings/User/General.js +++ b/dev/Settings/User/General.js @@ -4,7 +4,7 @@ import { MESSAGES_PER_PAGE_VALUES } from 'Common/Consts'; import { SaveSettingsStep } from 'Common/Enums'; import { EditorDefaultType, Layout } from 'Common/EnumsUser'; 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 { showScreenPopup } from 'Knoin/Knoin'; @@ -47,7 +47,7 @@ export class GeneralUserSettings { this.languageFullName = ko.computed(() => convertLangName(this.language())); this.languageTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 }); - ko.addObservablesTo(this, { + addObservablesTo(this, { mppTrigger: SaveSettingsStep.Idle, editorDefaultTypeTrigger: SaveSettingsStep.Idle, layoutTrigger: SaveSettingsStep.Idle diff --git a/dev/Stores/User/Account.js b/dev/Stores/User/Account.js index 54cc2a074..d1f0d3913 100644 --- a/dev/Stores/User/Account.js +++ b/dev/Stores/User/Account.js @@ -1,5 +1,6 @@ import ko from 'ko'; import { SettingsGet } from 'Common/Globals'; +import { addObservablesTo } from 'Common/Utils'; export const AccountUserStore = { accounts: ko.observableArray(), @@ -24,7 +25,7 @@ export const AccountUserStore = { } }; -ko.addObservablesTo(AccountUserStore, { +addObservablesTo(AccountUserStore, { email: '', parentEmail: '', signature: '' diff --git a/dev/Stores/User/App.js b/dev/Stores/User/App.js index f2e5edac1..7454dcf4f 100644 --- a/dev/Stores/User/App.js +++ b/dev/Stores/User/App.js @@ -2,6 +2,7 @@ import ko from 'ko'; import { KeyState } from 'Common/Enums'; import { Focused } from 'Common/EnumsUser'; import { keyScope, leftPanelDisabled, Settings, SettingsGet } from 'Common/Globals'; +import { addObservablesTo } from 'Common/Utils'; import { ThemeStore } from 'Stores/Theme'; export const AppUserStore = { @@ -26,7 +27,7 @@ export const AppUserStore = { } }; -ko.addObservablesTo(AppUserStore, { +addObservablesTo(AppUserStore, { currentAudio: '', focusedState: Focused.None, diff --git a/dev/Stores/User/Contact.js b/dev/Stores/User/Contact.js index 51c601c7a..e5f0fedfc 100644 --- a/dev/Stores/User/Contact.js +++ b/dev/Stores/User/Contact.js @@ -1,5 +1,6 @@ import ko from 'ko'; import { SettingsGet } from 'Common/Globals'; +import { addObservablesTo } from 'Common/Utils'; 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.syncing = ko.observable(false).extend({ debounce: 200 }); -ko.addObservablesTo(ContactUserStore, { +addObservablesTo(ContactUserStore, { allowSync: false, enableSync: false, syncUrl: '', diff --git a/dev/Stores/User/Folder.js b/dev/Stores/User/Folder.js index 538c392a8..36260df1b 100644 --- a/dev/Stores/User/Folder.js +++ b/dev/Stores/User/Folder.js @@ -2,13 +2,14 @@ import ko from 'ko'; import { FolderType } from 'Common/EnumsUser'; import { UNUSED_OPTION_VALUE } from 'Common/Consts'; +import { addObservablesTo } from 'Common/Utils'; import { folderListOptionsBuilder } from 'Common/UtilsUser'; import { getFolderInboxName, getFolderFromCacheList } from 'Common/Cache'; import { SettingsGet } from 'Common/Globals'; export const FolderUserStore = new class { constructor() { - ko.addObservablesTo(this, { + addObservablesTo(this, { /** * To use "checkable" option in /#/settings/folders * When true, getNextFolderNames only lists system and "checkable" folders diff --git a/dev/Stores/User/Message.js b/dev/Stores/User/Message.js index 66bdb0de7..67b2bd644 100644 --- a/dev/Stores/User/Message.js +++ b/dev/Stores/User/Message.js @@ -3,7 +3,7 @@ import ko from 'ko'; import { Notification } from 'Common/Enums'; import { Focused, MessageSetAction } from 'Common/EnumsUser'; 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 { @@ -72,7 +72,7 @@ export const MessageUserStore = new class { this.list = ko.observableArray().extend({ debounce: 0 }); - ko.addObservablesTo(this, { + addObservablesTo(this, { listCount: 0, listSearch: '', listThreadUid: '', diff --git a/dev/Stores/User/Settings.js b/dev/Stores/User/Settings.js index 7908e9f7e..8b829243d 100644 --- a/dev/Stores/User/Settings.js +++ b/dev/Stores/User/Settings.js @@ -2,7 +2,7 @@ import ko from 'ko'; import { MESSAGES_PER_PAGE_VALUES } from 'Common/Consts'; import { Layout, EditorDefaultType } from 'Common/EnumsUser'; -import { pInt } from 'Common/Utils'; +import { pInt, addObservablesTo } from 'Common/Utils'; import { $htmlCL, SettingsGet } from 'Common/Globals'; 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 }); - ko.addObservablesTo(this, { + addObservablesTo(this, { showImages: false, removeColors: false, useCheckboxesInList: true, diff --git a/dev/View/Popup/FolderSystem.js b/dev/View/Popup/FolderSystem.js index c5244a952..d6563d329 100644 --- a/dev/View/Popup/FolderSystem.js +++ b/dev/View/Popup/FolderSystem.js @@ -3,7 +3,7 @@ import ko from 'ko'; import { SetSystemFoldersNotification } from 'Common/EnumsUser'; import { UNUSED_OPTION_VALUE } from 'Common/Consts'; import { Settings } from 'Common/Globals'; -import { defaultOptionsAfterRender } from 'Common/Utils'; +import { defaultOptionsAfterRender, addSubscribablesTo } from 'Common/Utils'; import { folderListOptionsBuilder } from 'Common/UtilsUser'; import { initOnStartOrLangChange, i18n } from 'Common/Translator'; @@ -75,7 +75,7 @@ class FolderSystemPopupView extends AbstractViewPopup { fSaveSystemFolders(); }; - ko.addSubscribablesTo(FolderUserStore, { + addSubscribablesTo(FolderUserStore, { sentFolder: fCallback, draftFolder: fCallback, spamFolder: fCallback, diff --git a/dev/View/Popup/SieveScript.js b/dev/View/Popup/SieveScript.js index 263a08cfd..c3c849274 100644 --- a/dev/View/Popup/SieveScript.js +++ b/dev/View/Popup/SieveScript.js @@ -1,9 +1,9 @@ import ko from 'ko'; -import { delegateRunOnDestroy } from 'Common/UtilsUser'; import { Notification } from 'Common/Enums'; -import { getNotification } from 'Common/Translator'; -import { i18nToNodes } from 'Common/Translator'; +import { getNotification, i18nToNodes } from 'Common/Translator'; +import { addObservablesTo } from 'Common/Utils'; +import { delegateRunOnDestroy } from 'Common/UtilsUser'; import Remote from 'Remote/User/Fetch'; import { FilterModel } from 'Model/Filter'; @@ -18,7 +18,7 @@ class SieveScriptPopupView extends AbstractViewPopup { constructor() { super('SieveScript'); - ko.addObservablesTo(this, { + addObservablesTo(this, { saveError: false, saveErrorText: '', rawActive: false,