From 03b59ae41014eea0dc748e0eac2ea509bb7163e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Mon, 11 Apr 2022 16:44:10 +0000 Subject: [PATCH] [IMP] mail: move component handlers to models (step 5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit moves some handler methods from components to models, as a step closer to having most of business code in models. Having business code in models is desirable so that the code is much more maintainable: easier to change and more robust code. Task-2579306 closes odoo/odoo#88878 Related: odoo/enterprise#26277 Signed-off-by: Alexandre Kühn (aku) --- .../src/components/activity/activity.js | 9 ++ .../chatter_topbar/chatter_topbar.xml | 1 + .../composer_text_input.js | 127 +----------------- .../composer_text_input.xml | 2 +- .../src/components/follower/follower.js | 19 --- .../src/components/follower/follower.xml | 4 +- .../follower_list_menu/follower_list_menu.js | 8 ++ .../follower_list_menu/follower_list_menu.xml | 2 +- .../follower_subtype/follower_subtype.js | 18 --- .../follower_subtype/follower_subtype.xml | 2 +- .../follower_subtype_list.js | 24 ---- .../follower_subtype_list.xml | 6 +- .../components/mail_template/mail_template.js | 24 ---- .../mail_template/mail_template.xml | 4 +- .../static/src/components/message/message.js | 75 ----------- .../static/src/components/message/message.xml | 10 +- .../messaging_menu/messaging_menu.js | 34 ----- .../messaging_menu/messaging_menu.xml | 6 +- .../mail/static/src/models/activity_view.js | 4 + addons/mail/static/src/models/chatter.js | 15 +++ .../mail/static/src/models/composer_view.js | 120 ++++++++++++++++- addons/mail/static/src/models/follower.js | 15 +++ .../src/models/follower_list_menu_view.js | 15 +++ .../src/models/follower_subtype_list.js | 16 +++ .../src/models/follower_subtype_view.js | 14 ++ .../static/src/models/mail_template_view.js | 18 +++ addons/mail/static/src/models/message_view.js | 64 ++++++++- .../mail/static/src/models/messaging_menu.js | 28 ++++ .../mail/static/tests/helpers/test_utils.js | 13 ++ .../follower_list_menu_view_qunit_tests.js | 17 +++ addons/mail/static/tests/models/qunit_test.js | 4 + .../components/follower_list_menu_tests.js | 33 ++--- 32 files changed, 383 insertions(+), 368 deletions(-) create mode 100644 addons/mail/static/src/models/follower_list_menu_view.js create mode 100644 addons/mail/static/tests/models/follower_list_menu_view_qunit_tests.js diff --git a/addons/mail/static/src/components/activity/activity.js b/addons/mail/static/src/components/activity/activity.js index 2580bcb6264..835363cf78d 100644 --- a/addons/mail/static/src/components/activity/activity.js +++ b/addons/mail/static/src/components/activity/activity.js @@ -1,5 +1,6 @@ /** @odoo-module **/ +import { useComponentToModel } from '@mail/component_hooks/use_component_to_model'; import { registerMessagingComponent } from '@mail/utils/messaging_component'; import Popover from "web.Popover"; @@ -7,6 +8,14 @@ import { LegacyComponent } from "@web/legacy/legacy_component"; export class Activity extends LegacyComponent { + /** + * @override + */ + setup() { + super.setup(); + useComponentToModel({ fieldName: 'component', modelName: 'ActivityView' }); + } + /** * @returns {ActivityView} */ diff --git a/addons/mail/static/src/components/chatter_topbar/chatter_topbar.xml b/addons/mail/static/src/components/chatter_topbar/chatter_topbar.xml index 873fd66795f..c0a59d51462 100644 --- a/addons/mail/static/src/components/chatter_topbar/chatter_topbar.xml +++ b/addons/mail/static/src/components/chatter_topbar/chatter_topbar.xml @@ -64,6 +64,7 @@ diff --git a/addons/mail/static/src/components/composer_text_input/composer_text_input.js b/addons/mail/static/src/components/composer_text_input/composer_text_input.js index 1e8ac030574..757a202eb88 100644 --- a/addons/mail/static/src/components/composer_text_input/composer_text_input.js +++ b/addons/mail/static/src/components/composer_text_input/composer_text_input.js @@ -1,6 +1,5 @@ /** @odoo-module **/ -import { useComponentToModel } from '@mail/component_hooks/use_component_to_model'; import { useRefToModel } from '@mail/component_hooks/use_ref_to_model'; import { useUpdate } from '@mail/component_hooks/use_update'; import { registerMessagingComponent } from '@mail/utils/messaging_component'; @@ -14,7 +13,6 @@ export class ComposerTextInput extends Component { */ setup() { super.setup(); - useComponentToModel({ fieldName: 'textInputComponent', modelName: 'ComposerView' }); useRefToModel({ fieldName: 'mirroredTextareaRef', modelName: 'ComposerView', refName: 'mirroredTextarea' }); useRefToModel({ fieldName: 'textareaRef', modelName: 'ComposerView', refName: 'textarea' }); /** @@ -40,18 +38,6 @@ export class ComposerTextInput extends Component { return this.messaging && this.messaging.models['ComposerView'].get(this.props.localId); } - /** - * Saves the composer text input state in store - */ - saveStateInStore() { - this.composerView.composer.update({ - textInputContent: this.composerView.textareaRef.el.value, - textInputCursorEnd: this.composerView.textareaRef.el.selectionEnd, - textInputCursorStart: this.composerView.textareaRef.el.selectionStart, - textInputSelectionDirection: this.composerView.textareaRef.el.selectionDirection, - }); - } - //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- @@ -113,28 +99,6 @@ export class ComposerTextInput extends Component { // Handlers //-------------------------------------------------------------------------- - /** - * @private - */ - _onClickTextarea() { - if (!this.composerView) { - return; - } - // clicking might change the cursor position - this.saveStateInStore(); - } - - /** - * @private - */ - _onFocusoutTextarea() { - if (!this.composerView) { - return; - } - this.saveStateInStore(); - this.composerView.update({ isFocused: false }); - } - /** * @private */ @@ -142,7 +106,7 @@ export class ComposerTextInput extends Component { if (!this.composerView) { return; } - this.saveStateInStore(); + this.composerView.saveStateInStore(); if (this._textareaLastInputValue !== this.composerView.textareaRef.el.value) { this.composerView.handleCurrentPartnerIsTyping(); } @@ -150,95 +114,6 @@ export class ComposerTextInput extends Component { this._updateHeight(); } - /** - * Key events management is performed in a Keyup to avoid intempestive RPC calls - * - * @private - * @param {KeyboardEvent} ev - */ - _onKeyupTextarea(ev) { - if (!this.composerView) { - return; - } - switch (ev.key) { - case 'Escape': - // already handled in _onKeydownTextarea, break to avoid default - break; - // ENTER, HOME, END, UP, DOWN, PAGE UP, PAGE DOWN, TAB: check if navigation in mention suggestions - case 'Enter': - if (this.composerView.hasSuggestions) { - this.composerView.insertSuggestion(); - this.composerView.closeSuggestions(); - this.composerView.update({ doFocus: true }); - } - break; - case 'ArrowUp': - case 'PageUp': - if (ev.key === 'ArrowUp' && !this.composerView.hasSuggestions && !this.composerView.composer.textInputContent && this.composerView.threadView) { - this.composerView.threadView.startEditingLastMessageFromCurrentUser(); - break; - } - if (this.composerView.hasSuggestions) { - this.composerView.setPreviousSuggestionActive(); - this.composerView.update({ hasToScrollToActiveSuggestion: true }); - } - break; - case 'ArrowDown': - case 'PageDown': - if (ev.key === 'ArrowDown' && !this.composerView.hasSuggestions && !this.composerView.composer.textInputContent && this.composerView.threadView) { - this.composerView.threadView.startEditingLastMessageFromCurrentUser(); - break; - } - if (this.composerView.hasSuggestions) { - this.composerView.setNextSuggestionActive(); - this.composerView.update({ hasToScrollToActiveSuggestion: true }); - } - break; - case 'Home': - if (this.composerView.hasSuggestions) { - this.composerView.setFirstSuggestionActive(); - this.composerView.update({ hasToScrollToActiveSuggestion: true }); - } - break; - case 'End': - if (this.composerView.hasSuggestions) { - this.composerView.setLastSuggestionActive(); - this.composerView.update({ hasToScrollToActiveSuggestion: true }); - } - break; - case 'Tab': - if (this.composerView.hasSuggestions) { - if (ev.shiftKey) { - this.composerView.setPreviousSuggestionActive(); - this.composerView.update({ hasToScrollToActiveSuggestion: true }); - } else { - this.composerView.setNextSuggestionActive(); - this.composerView.update({ hasToScrollToActiveSuggestion: true }); - } - } - break; - case 'Alt': - case 'AltGraph': - case 'CapsLock': - case 'Control': - case 'Fn': - case 'FnLock': - case 'Hyper': - case 'Meta': - case 'NumLock': - case 'ScrollLock': - case 'Shift': - case 'ShiftSuper': - case 'Symbol': - case 'SymbolLock': - // prevent modifier keys from resetting the suggestion state - break; - // Otherwise, check if a mention is typed - default: - this.saveStateInStore(); - } - } - } Object.assign(ComposerTextInput, { diff --git a/addons/mail/static/src/components/composer_text_input/composer_text_input.xml b/addons/mail/static/src/components/composer_text_input/composer_text_input.xml index 23e2b813f21..727e825d6ff 100644 --- a/addons/mail/static/src/components/composer_text_input/composer_text_input.xml +++ b/addons/mail/static/src/components/composer_text_input/composer_text_input.xml @@ -10,7 +10,7 @@ isBelow="props.hasMentionSuggestionsBelowPosition" /> -