From e1ee05ae478bbbde3f9241b7f57960075b2c6848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Tue, 26 Apr 2022 13:41:50 +0000 Subject: [PATCH] [IMP] mail: introduce view models (step 6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces models that define records being 1:1 map with components, as a step to move further to having essentially all business code in models. Having code in models is desirable to have very maintainable code, thanks to robust and declarative code with an ORM-like architecture. Task-2831082 closes odoo/odoo#90569 Signed-off-by: Alexandre Kühn (aku) --- .../autocomplete_input/autocomplete_input.js | 20 +- .../channel_member/channel_member.js | 33 +++ .../channel_member/channel_member.scss | 12 + .../channel_member/channel_member.xml | 26 ++ .../channel_member_list.scss | 12 - .../channel_member_list.xml | 20 +- .../src/components/chat_window/chat_window.js | 53 ---- .../components/chat_window/chat_window.xml | 4 +- .../composer_suggestion_list.js | 10 +- .../composer_suggestion_list.xml | 12 +- .../composer_text_input.js | 9 +- .../composer_text_input.xml | 4 +- .../static/src/components/discuss/discuss.js | 33 --- .../static/src/components/discuss/discuss.xml | 6 +- .../discuss_sidebar_category.xml | 2 - .../static/src/components/message/message.xml | 14 +- .../messaging_menu/messaging_menu.xml | 1 - addons/mail/static/src/model/model_manager.js | 13 - .../src/models/autocomplete_input_view.js | 37 +++ addons/mail/static/src/models/chat_window.js | 43 ++++ .../static/src/models/chat_window_manager.js | 4 +- .../models/composer_suggestion_list_view.js | 16 ++ .../mail/static/src/models/composer_view.js | 22 ++ addons/mail/static/src/models/discuss_view.js | 21 ++ addons/mail/static/src/models/message.js | 14 ++ addons/mail/static/src/models/partner.js | 3 + addons/mail/static/src/models/record.js | 9 - addons/mail/static/src/models/thread.js | 231 +++++++++++------- addons/mail/static/src/models/user.js | 7 +- .../components/channel_member_list_tests.js | 148 +++++++++++ .../components/follower_subtype_tests.js | 4 +- .../components/thread_view_tests.js | 3 +- .../models/messaging_tests.js | 25 +- 33 files changed, 552 insertions(+), 319 deletions(-) create mode 100644 addons/mail/static/src/components/channel_member/channel_member.js create mode 100644 addons/mail/static/src/components/channel_member/channel_member.scss create mode 100644 addons/mail/static/src/components/channel_member/channel_member.xml delete mode 100644 addons/mail/static/src/components/channel_member_list/channel_member_list.scss create mode 100644 addons/mail/static/src/models/composer_suggestion_list_view.js create mode 100644 addons/mail/static/tests/qunit_suite_tests/components/channel_member_list_tests.js diff --git a/addons/mail/static/src/components/autocomplete_input/autocomplete_input.js b/addons/mail/static/src/components/autocomplete_input/autocomplete_input.js index a2da7d6bdc9..e12c5b0e504 100644 --- a/addons/mail/static/src/components/autocomplete_input/autocomplete_input.js +++ b/addons/mail/static/src/components/autocomplete_input/autocomplete_input.js @@ -25,7 +25,7 @@ export class AutocompleteInput extends Component { if (!this.root.el) { return; } - if (this.props.isFocusOnMount) { + if (this.autocompleteInputView.isFocusOnMount) { this.root.el.focus(); } @@ -34,7 +34,7 @@ export class AutocompleteInput extends Component { select: (ev, ui) => this._onAutocompleteSelect(ev, ui), source: (req, res) => this._onAutocompleteSource(req, res), focus: ev => this._onAutocompleteFocus(ev), - html: this.props.isHtml || false, + html: this.autocompleteInputView.isHtml, }; if (this.props.customClass) { @@ -158,10 +158,6 @@ export class AutocompleteInput extends Component { } Object.assign(AutocompleteInput, { - defaultProps: { - isFocusOnMount: false, - isHtml: false, - }, props: { customClass: { type: String, @@ -171,23 +167,11 @@ Object.assign(AutocompleteInput, { type: Function, optional: true, }, - isFocusOnMount: { - type: Boolean, - optional: true, - }, - isHtml: { - type: Boolean, - optional: true, - }, localId: String, onHide: { type: Function, optional: true, }, - placeholder: { - type: String, - optional: true, - }, select: { type: Function, optional: true, diff --git a/addons/mail/static/src/components/channel_member/channel_member.js b/addons/mail/static/src/components/channel_member/channel_member.js new file mode 100644 index 00000000000..d8274fa6cf6 --- /dev/null +++ b/addons/mail/static/src/components/channel_member/channel_member.js @@ -0,0 +1,33 @@ +/** @odoo-module **/ + +import { registerMessagingComponent } from '@mail/utils/messaging_component'; + +const { Component } = owl; + +export class ChannelMember extends Component { + + /** + * @returns {Thread} + */ + get channel() { + return this.messaging.models['Thread'].get(this.props.channelLocalId); + } + + /** + * @returns {Partner} + */ + get member() { + return this.messaging.models['Partner'].get(this.props.memberLocalId); + } + +} + +Object.assign(ChannelMember, { + props: { + channelLocalId: String, + memberLocalId: String, + }, + template: 'mail.ChannelMember', +}); + +registerMessagingComponent(ChannelMember); diff --git a/addons/mail/static/src/components/channel_member/channel_member.scss b/addons/mail/static/src/components/channel_member/channel_member.scss new file mode 100644 index 00000000000..eac39c5368f --- /dev/null +++ b/addons/mail/static/src/components/channel_member/channel_member.scss @@ -0,0 +1,12 @@ +.o_ChannelMember_avatar { + object-fit: cover; +} + +.o_ChannelMember_avatarContainer { + width: 32px; + height: 32px; +} + +.o_ChannelMember_name { + min-width: 0; +} diff --git a/addons/mail/static/src/components/channel_member/channel_member.xml b/addons/mail/static/src/components/channel_member/channel_member.xml new file mode 100644 index 00000000000..3848cb4202c --- /dev/null +++ b/addons/mail/static/src/components/channel_member/channel_member.xml @@ -0,0 +1,26 @@ + + + + +
+
+ Avatar + + + + +
+ + + +
+
+ +
diff --git a/addons/mail/static/src/components/channel_member_list/channel_member_list.scss b/addons/mail/static/src/components/channel_member_list/channel_member_list.scss deleted file mode 100644 index 6e7b3895046..00000000000 --- a/addons/mail/static/src/components/channel_member_list/channel_member_list.scss +++ /dev/null @@ -1,12 +0,0 @@ -.o_ChannelMemberList_avatar { - object-fit: cover; -} - -.o_ChannelMemberList_avatarContainer { - width: 32px; - height: 32px; -} - -.o_ChannelMemberList_name { - min-width: 0; -} diff --git a/addons/mail/static/src/components/channel_member_list/channel_member_list.xml b/addons/mail/static/src/components/channel_member_list/channel_member_list.xml index ef23ed11ef0..e24d49d7c1e 100644 --- a/addons/mail/static/src/components/channel_member_list/channel_member_list.xml +++ b/addons/mail/static/src/components/channel_member_list/channel_member_list.xml @@ -34,25 +34,7 @@
-
-
-
- Avatar - - - - -
- - - -
+
diff --git a/addons/mail/static/src/components/chat_window/chat_window.js b/addons/mail/static/src/components/chat_window/chat_window.js index e5bb12bccf9..7940cadf10f 100644 --- a/addons/mail/static/src/components/chat_window/chat_window.js +++ b/addons/mail/static/src/components/chat_window/chat_window.js @@ -20,8 +20,6 @@ export class ChatWindow extends Component { */ this._inputRef = { el: null }; // the following are passed as props to children - this._onAutocompleteSelect = this._onAutocompleteSelect.bind(this); - this._onAutocompleteSource = this._onAutocompleteSource.bind(this); } //-------------------------------------------------------------------------- @@ -58,57 +56,6 @@ export class ChatWindow extends Component { } } - //-------------------------------------------------------------------------- - // Handlers - //-------------------------------------------------------------------------- - - /** - * Called when selecting an item in the autocomplete input of the - * 'new_message' chat window. - * - * @private - * @param {Event} ev - * @param {Object} ui - * @param {Object} ui.item - * @param {integer} ui.item.id - */ - async _onAutocompleteSelect(ev, ui) { - const chat = await this.messaging.getChat({ partnerId: ui.item.id }); - if (!chat) { - return; - } - this.messaging.chatWindowManager.openThread(chat, { - makeActive: true, - replaceNewMessage: true, - }); - } - - /** - * Called when typing in the autocomplete input of the 'new_message' chat - * window. - * - * @private - * @param {Object} req - * @param {string} req.term - * @param {function} res - */ - _onAutocompleteSource(req, res) { - this.messaging.models['Partner'].imSearch({ - callback: (partners) => { - const suggestions = partners.map(partner => { - return { - id: partner.id, - value: partner.nameOrDisplayName, - label: partner.nameOrDisplayName, - }; - }); - res(_.sortBy(suggestions, 'label')); - }, - keyword: _.escape(req.term), - limit: 10, - }); - } - } Object.assign(ChatWindow, { diff --git a/addons/mail/static/src/components/chat_window/chat_window.xml b/addons/mail/static/src/components/chat_window/chat_window.xml index 6b35e959af3..94c299dea55 100644 --- a/addons/mail/static/src/components/chat_window/chat_window.xml +++ b/addons/mail/static/src/components/chat_window/chat_window.xml @@ -39,8 +39,8 @@ diff --git a/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.js b/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.js index 12004facd73..7106bcfc504 100644 --- a/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.js +++ b/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.js @@ -11,18 +11,16 @@ export class ComposerSuggestionList extends Component { //-------------------------------------------------------------------------- /** - * @returns {ComposerView} + * @returns {ComposerSuggestionListView} */ - get composerView() { - return this.messaging && this.messaging.models['ComposerView'].get(this.props.composerViewLocalId); + get composerSuggestionListView() { + return this.messaging && this.messaging.models['ComposerSuggestionListView'].get(this.props.localId); } } Object.assign(ComposerSuggestionList, { - props: { - composerViewLocalId: String, - }, + props: { localId: String }, template: 'mail.ComposerSuggestionList', }); diff --git a/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.xml b/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.xml index e3b4b863586..510ec1b620d 100644 --- a/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.xml +++ b/addons/mail/static/src/components/composer_suggestion_list/composer_suggestion_list.xml @@ -2,17 +2,17 @@ - -
-
+ +
+