From 24b638cd8200efb8097df480aad5fde8f1bec6a1 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 21 Feb 2022 15:36:34 +0100 Subject: [PATCH] Cleanup some JavaScript --- dev/Knoin/Knoin.js | 4 ++ dev/Model/FolderCollection.js | 7 --- dev/Settings/User/Themes.js | 55 ++++++++++--------- dev/View/Popup/AdvancedSearch.js | 7 +-- dev/View/Popup/Contacts.js | 6 +- dev/View/Popup/Filter.js | 7 +-- dev/View/Popup/FolderClear.js | 14 ++--- dev/View/Popup/SieveScript.js | 9 +-- dev/View/User/MailBox/MessageList.js | 10 ++-- dev/View/User/MailBox/MessageView.js | 3 +- .../templates/Views/User/MailMessageList.html | 6 +- .../templates/Views/User/MailMessageView.html | 4 +- .../Views/User/PopupsAdvancedSearch.html | 12 ++-- .../templates/Views/User/PopupsContacts.html | 4 +- .../templates/Views/User/PopupsFilter.html | 2 +- .../Views/User/PopupsSieveScript.html | 2 +- 16 files changed, 64 insertions(+), 88 deletions(-) diff --git a/dev/Knoin/Knoin.js b/dev/Knoin/Knoin.js index 58657f517..24b2189a5 100644 --- a/dev/Knoin/Knoin.js +++ b/dev/Knoin/Knoin.js @@ -328,6 +328,10 @@ export const l && l.remove(); }, + /** + * Used by ko.bindingHandlers.command (template data-bind="command: ") + * to enable/disable click/submit action. + */ decorateKoCommands = (thisArg, commands) => forEachObjectEntry(commands, (key, canExecute) => { let command = thisArg[key], diff --git a/dev/Model/FolderCollection.js b/dev/Model/FolderCollection.js index 5ef152cff..809bba185 100644 --- a/dev/Model/FolderCollection.js +++ b/dev/Model/FolderCollection.js @@ -411,11 +411,4 @@ export class FolderModel extends AbstractModel { : 'icon-none' ); } - - /** - * @returns {string} - */ - printableFullName() { - return this.fullName.replace(this.delimiter, ' / '); - } } diff --git a/dev/Settings/User/Themes.js b/dev/Settings/User/Themes.js index 3a3f1786c..22c378545 100644 --- a/dev/Settings/User/Themes.js +++ b/dev/Settings/User/Themes.js @@ -1,4 +1,4 @@ -import ko from 'ko'; +import { addObservablesTo } from 'External/ko'; import { SaveSettingsStep, UploadErrorCode, Capa } from 'Common/Enums'; import { changeTheme, convertThemeName } from 'Common/Utils'; @@ -10,24 +10,29 @@ import { ThemeStore } from 'Stores/Theme'; import Remote from 'Remote/User/Fetch'; +const themeBackground = { + name: ThemeStore.userBackgroundName, + hash: ThemeStore.userBackgroundHash +}; +addObservablesTo(themeBackground, { + uploaderButton: null, + loading: false, + error: '' +}); + export class ThemesUserSettings /*extends AbstractViewSettings*/ { constructor() { this.theme = ThemeStore.theme; this.themes = ThemeStore.themes; this.themesObjects = ko.observableArray(); - this.background = {}; - this.background.name = ThemeStore.userBackgroundName; - this.background.hash = ThemeStore.userBackgroundHash; - this.background.uploaderButton = ko.observable(null); - this.background.loading = ko.observable(false); - this.background.error = ko.observable(''); + this.background = themeBackground; - this.capaUserBackground = ko.observable(Settings.capa(Capa.UserBackground)); + this.capaUserBackground = Settings.capa(Capa.UserBackground); this.themeTrigger = ko.observable(SaveSettingsStep.Idle).extend({ debounce: 100 }); - this.theme.subscribe((value) => { + ThemeStore.theme.subscribe(value => { this.themesObjects.forEach(theme => { theme.selected(value === theme.name); }); @@ -41,10 +46,10 @@ export class ThemesUserSettings /*extends AbstractViewSettings*/ { } onBuild() { - const currentTheme = this.theme(); + const currentTheme = ThemeStore.theme(); this.themesObjects( - this.themes.map(theme => ({ + ThemeStore.themes.map(theme => ({ name: theme, nameDisplay: convertThemeName(theme), selected: ko.observable(theme === currentTheme), @@ -54,28 +59,28 @@ export class ThemesUserSettings /*extends AbstractViewSettings*/ { // initUploader - if (this.background.uploaderButton() && this.capaUserBackground()) { + if (themeBackground.uploaderButton() && this.capaUserBackground) { const oJua = new Jua({ action: serverRequest('UploadBackground'), limit: 1, - clickElement: this.background.uploaderButton() + clickElement: themeBackground.uploaderButton() }); oJua .on('onStart', () => { - this.background.loading(true); - this.background.error(''); + themeBackground.loading(true); + themeBackground.error(''); return true; }) .on('onComplete', (id, result, data) => { - this.background.loading(false); + themeBackground.loading(false); if (result && id && data && data.Result && data.Result.Name && data.Result.Hash) { - this.background.name(data.Result.Name); - this.background.hash(data.Result.Hash); + themeBackground.name(data.Result.Name); + themeBackground.hash(data.Result.Hash); } else { - this.background.name(''); - this.background.hash(''); + themeBackground.name(''); + themeBackground.hash(''); let errorMsg = ''; if (data.ErrorCode) { @@ -94,7 +99,7 @@ export class ThemesUserSettings /*extends AbstractViewSettings*/ { errorMsg = data.ErrorMessage; } - this.background.error(errorMsg || i18n('SETTINGS_THEMES/ERROR_UNKNOWN')); + themeBackground.error(errorMsg || i18n('SETTINGS_THEMES/ERROR_UNKNOWN')); } return true; @@ -103,14 +108,14 @@ export class ThemesUserSettings /*extends AbstractViewSettings*/ { } onShow() { - this.background.error(''); + themeBackground.error(''); } clearBackground() { - if (this.capaUserBackground()) { + if (this.capaUserBackground) { Remote.request('ClearUserBackground', () => { - this.background.name(''); - this.background.hash(''); + themeBackground.name(''); + themeBackground.hash(''); }); } } diff --git a/dev/View/Popup/AdvancedSearch.js b/dev/View/Popup/AdvancedSearch.js index 4a61ce587..7d75edf74 100644 --- a/dev/View/Popup/AdvancedSearch.js +++ b/dev/View/Popup/AdvancedSearch.js @@ -4,7 +4,6 @@ import { i18n, trigger as translatorTrigger } from 'Common/Translator'; import { MessageUserStore } from 'Stores/User/Message'; -import { decorateKoCommands } from 'Knoin/Knoin'; import { AbstractViewPopup } from 'Knoin/AbstractViews'; import { FolderUserStore } from 'Stores/User/Folder'; @@ -50,13 +49,9 @@ class AdvancedSearchPopupView extends AbstractViewPopup { { id: 'subtree', name: i18n(prefix + 'SUBTREE') } ]; }); - - decorateKoCommands(this, { - searchCommand: 1 - }); } - searchCommand() { + submitForm() { const search = this.buildSearchString(); if (search) { MessageUserStore.mainMessageListSearch(search); diff --git a/dev/View/Popup/Contacts.js b/dev/View/Popup/Contacts.js index 364cf8c15..93b7572ab 100644 --- a/dev/View/Popup/Contacts.js +++ b/dev/View/Popup/Contacts.js @@ -136,17 +136,15 @@ class ContactsPopupView extends AbstractViewPopup { }); decorateKoCommands(this, { - newCommand: 1, deleteCommand: self => 0 < self.contactsCheckedOrSelected().length, newMessageCommand: self => 0 < self.contactsCheckedOrSelected().length, - clearCommand: 1, saveCommand: self => !self.viewSaving() && !self.viewReadOnly() && (self.contactHasValidName() || self.viewProperties.find(prop => propertyIsMail(prop) && prop.isValid())), syncCommand: self => !self.contacts.syncing() && !self.contacts.importing() }); } - newCommand() { + newContact() { this.populateViewContact(null); this.currentContact(null); } @@ -207,7 +205,7 @@ class ContactsPopupView extends AbstractViewPopup { return true; } - clearCommand() { + clearSearch() { this.search(''); } diff --git a/dev/View/Popup/Filter.js b/dev/View/Popup/Filter.js index ecbf0a738..ab57d24ad 100644 --- a/dev/View/Popup/Filter.js +++ b/dev/View/Popup/Filter.js @@ -9,7 +9,6 @@ import { i18n, initOnStartOrLangChange } from 'Common/Translator'; import { SieveUserStore } from 'Stores/User/Sieve'; -import { decorateKoCommands } from 'Knoin/Knoin'; import { AbstractViewPopup } from 'Knoin/AbstractViews'; import { folderListOptionsBuilder } from 'Common/UtilsUser'; @@ -45,13 +44,9 @@ class FilterPopupView extends AbstractViewPopup { initOnStartOrLangChange(this.populateOptions.bind(this)); SieveUserStore.capa.subscribe(this.populateOptions, this); - - decorateKoCommands(this, { - saveFilterCommand: 1 - }); } - saveFilterCommand() { + saveFilter() { if (this.filter()) { if (FilterAction.MoveTo === this.filter().actionType()) { this.filter().actionValue(this.selectedFolderValue()); diff --git a/dev/View/Popup/FolderClear.js b/dev/View/Popup/FolderClear.js index d80161ff4..e3c118f2a 100644 --- a/dev/View/Popup/FolderClear.js +++ b/dev/View/Popup/FolderClear.js @@ -19,17 +19,11 @@ class FolderClearPopupView extends AbstractViewPopup { }); this.addComputables({ - folderFullNameForClear: () => { + dangerDescHtml: () => { const folder = this.selectedFolder(); - return folder ? folder.printableFullName() : ''; - }, - - folderNameForClear: () => { - const folder = this.selectedFolder(); - return folder ? folder.localName() : ''; - }, - - dangerDescHtml: () => i18n('POPUPS_CLEAR_FOLDER/DANGER_DESC_HTML_1', { FOLDER: this.folderNameForClear() }) +// return i18n('POPUPS_CLEAR_FOLDER/DANGER_DESC_HTML_1', { FOLDER: folder ? folder.fullName.replace(folder.delimiter, ' / ') : '' }); + return i18n('POPUPS_CLEAR_FOLDER/DANGER_DESC_HTML_1', { FOLDER: folder ? folder.localName() : '' }); + } }); decorateKoCommands(this, { diff --git a/dev/View/Popup/SieveScript.js b/dev/View/Popup/SieveScript.js index bc66642ec..cc00666af 100644 --- a/dev/View/Popup/SieveScript.js +++ b/dev/View/Popup/SieveScript.js @@ -7,7 +7,7 @@ import Remote from 'Remote/User/Fetch'; import { FilterModel } from 'Model/Filter'; import { SieveUserStore } from 'Stores/User/Sieve'; -import { showScreenPopup/*, decorateKoCommands*/ } from 'Knoin/Knoin'; +import { showScreenPopup } from 'Knoin/Knoin'; import { AbstractViewPopup } from 'Knoin/AbstractViews'; import { FilterPopupView } from 'View/Popup/Filter'; @@ -28,14 +28,9 @@ class SieveScriptPopupView extends AbstractViewPopup { this.saving = false; this.filterForDeletion = ko.observable(null).askDeleteHelper(); -/* - decorateKoCommands(this, { - saveScriptCommand: 1 - }); -*/ } - saveScriptCommand() { + saveScript() { let self = this, script = self.script(); if (!self.saving/* && script.hasChanges()*/) { diff --git a/dev/View/User/MailBox/MessageList.js b/dev/View/User/MailBox/MessageList.js index 269ba98e0..9aa1bd7ff 100644 --- a/dev/View/User/MailBox/MessageList.js +++ b/dev/View/User/MailBox/MessageList.js @@ -237,8 +237,6 @@ export class MailMessageList extends AbstractViewRight { ).throttle(50)); decorateKoCommands(this, { - clearCommand: 1, - reloadCommand: 1, multyForwardCommand: canBeMovedHelper, deleteWithoutMoveCommand: canBeMovedHelper, deleteCommand: canBeMovedHelper, @@ -252,16 +250,16 @@ export class MailMessageList extends AbstractViewRight { changeSort(self, event) { FolderUserStore.sortMode(event.target.closest('li').dataset.sort); - this.reloadCommand(); + this.reload(); } - clearCommand() { + clear() { if (SettingsCapa(Capa.DangerousActions)) { showScreenPopup(FolderClearPopupView, [FolderUserStore.currentFolder()]); } } - reloadCommand() { + reload() { if (!MessageUserStore.listIsLoading()) { rl.app.reloadMessageList(false, true); } @@ -707,7 +705,7 @@ export class MailMessageList extends AbstractViewRight { // check mail shortcuts.add('r', 'meta', [Scope.FolderList, Scope.MessageList, Scope.MessageView], () => { - this.reloadCommand(); + this.reload(); return false; }); diff --git a/dev/View/User/MailBox/MessageView.js b/dev/View/User/MailBox/MessageView.js index c63d1f8c6..6deaffaa7 100644 --- a/dev/View/User/MailBox/MessageView.js +++ b/dev/View/User/MailBox/MessageView.js @@ -231,14 +231,13 @@ export class MailMessageView extends AbstractViewRight { addEventListener('mailbox.message-view.toggle-full-screen', () => this.toggleFullScreen()); decorateKoCommands(this, { - closeMessageCommand: 1, messageEditCommand: self => self.messageVisibility(), goUpCommand: self => !self.messageListOrViewLoading(), goDownCommand: self => !self.messageListOrViewLoading() }); } - closeMessageCommand() { + closeMessage() { MessageUserStore.message(null); } diff --git a/snappymail/v/0.0.0/app/templates/Views/User/MailMessageList.html b/snappymail/v/0.0.0/app/templates/Views/User/MailMessageList.html index 0d0fea378..6603b9ebb 100644 --- a/snappymail/v/0.0.0/app/templates/Views/User/MailMessageList.html +++ b/snappymail/v/0.0.0/app/templates/Views/User/MailMessageList.html @@ -5,7 +5,7 @@ - + @@ -62,7 +62,7 @@