diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/mail/static/src/components/composer_suggested_recipient | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/mail/static/src/components/composer_suggested_recipient')
3 files changed, 204 insertions, 0 deletions
diff --git a/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.js b/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.js new file mode 100644 index 00000000..efedf662 --- /dev/null +++ b/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.js @@ -0,0 +1,158 @@ +odoo.define('mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.js', function (require) { +'use strict'; + +const useShouldUpdateBasedOnProps = require('mail/static/src/component_hooks/use_should_update_based_on_props/use_should_update_based_on_props.js'); +const useStore = require('mail/static/src/component_hooks/use_store/use_store.js'); +const useUpdate = require('mail/static/src/component_hooks/use_update/use_update.js'); + +const { FormViewDialog } = require('web.view_dialogs'); +const { ComponentAdapter } = require('web.OwlCompatibility'); + +const { Component } = owl; +const { useRef } = owl.hooks; + +class FormViewDialogComponentAdapter extends ComponentAdapter { + + renderWidget() { + // Ensure the dialog is properly reconstructed. Without this line, it is + // impossible to open the dialog again after having it closed a first + // time, because the DOM of the dialog has disappeared. + return this.willStart(); + } + +} + +const components = { + FormViewDialogComponentAdapter, +}; + +class ComposerSuggestedRecipient extends Component { + + constructor(...args) { + super(...args); + this.id = _.uniqueId('o_ComposerSuggestedRecipient_'); + useShouldUpdateBasedOnProps(); + useStore(props => { + const suggestedRecipientInfo = this.env.models['mail.suggested_recipient_info'].get(props.suggestedRecipientLocalId); + const partner = suggestedRecipientInfo && suggestedRecipientInfo.partner; + return { + partner: partner && partner.__state, + suggestedRecipientInfo: suggestedRecipientInfo && suggestedRecipientInfo.__state, + }; + }); + useUpdate({ func: () => this._update() }); + /** + * Form view dialog class. Useful to reference it in the template. + */ + this.FormViewDialog = FormViewDialog; + /** + * Reference of the checkbox. Useful to know whether it was checked or + * not, to properly update the corresponding state in the record or to + * prompt the user with the partner creation dialog. + */ + this._checkboxRef = useRef('checkbox'); + /** + * Reference of the partner creation dialog. Useful to open it, for + * compatibility with old code. + */ + this._dialogRef = useRef('dialog'); + /** + * Whether the dialog is currently open. `_dialogRef` cannot be trusted + * to know if the dialog is open due to manually calling `open` and + * potential out of sync with component adapter. + */ + this._isDialogOpen = false; + this._onDialogSaved = this._onDialogSaved.bind(this); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * @returns {string|undefined} + */ + get ADD_AS_RECIPIENT_AND_FOLLOWER_REASON() { + if (!this.suggestedRecipientInfo) { + return undefined; + } + return this.env._t(_.str.sprintf( + "Add as recipient and follower (reason: %s)", + this.suggestedRecipientInfo.reason + )); + } + + /** + * @returns {string} + */ + get PLEASE_COMPLETE_CUSTOMER_S_INFORMATION() { + return this.env._t("Please complete customer's information"); + } + + /** + * @returns {mail.suggested_recipient_info} + */ + get suggestedRecipientInfo() { + return this.env.models['mail.suggested_recipient_info'].get(this.props.suggestedRecipientInfoLocalId); + } + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + */ + _update() { + if (this._checkboxRef.el && this.suggestedRecipientInfo) { + this._checkboxRef.el.checked = this.suggestedRecipientInfo.isSelected; + } + } + + //-------------------------------------------------------------------------- + // Handler + //-------------------------------------------------------------------------- + + /** + * @private + */ + _onChangeCheckbox() { + const isChecked = this._checkboxRef.el.checked; + this.suggestedRecipientInfo.update({ isSelected: isChecked }); + if (!this.suggestedRecipientInfo.partner) { + // Recipients must always be partners. On selecting a suggested + // recipient that does not have a partner, the partner creation form + // should be opened. + if (isChecked && this._dialogRef && !this._isDialogOpen) { + this._isDialogOpen = true; + this._dialogRef.comp.widget.on('closed', this, () => { + this._isDialogOpen = false; + }); + this._dialogRef.comp.widget.open(); + } + } + } + + /** + * @private + */ + _onDialogSaved() { + const thread = this.suggestedRecipientInfo && this.suggestedRecipientInfo.thread; + if (!thread) { + return; + } + thread.fetchAndUpdateSuggestedRecipients(); + } +} + +Object.assign(ComposerSuggestedRecipient, { + components, + props: { + suggestedRecipientInfoLocalId: String, + }, + template: 'mail.ComposerSuggestedRecipient', +}); + +return ComposerSuggestedRecipient; + +}); diff --git a/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.scss b/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.scss new file mode 100644 index 00000000..c8b6b5ad --- /dev/null +++ b/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.scss @@ -0,0 +1,5 @@ +// Dirty fix: clear modal-body padding, otherwise it create space inside the +// suggested_recipient list. +.o_ComposerSuggestedRecipient .modal-body { + padding: 0; +} diff --git a/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.xml b/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.xml new file mode 100644 index 00000000..4e754359 --- /dev/null +++ b/addons/mail/static/src/components/composer_suggested_recipient/composer_suggested_recipient.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<templates xml:space="preserve"> + <t t-name="mail.ComposerSuggestedRecipient" owl="1"> + <div class="o_ComposerSuggestedRecipient" t-att-data-partner-id="suggestedRecipientInfo.partner and suggestedRecipientInfo.partner.id ? suggestedRecipientInfo.partner.id : false" t-att-title="ADD_AS_RECIPIENT_AND_FOLLOWER_REASON"> + <t t-if="suggestedRecipientInfo"> + <div class="custom-control custom-checkbox"> + <input t-attf-id="{{ id }}_checkbox" class="custom-control-input" type="checkbox" t-att-checked="suggestedRecipientInfo.isSelected ? 'checked' : undefined" t-on-change="_onChangeCheckbox" t-ref="checkbox" /> + <label class="custom-control-label" t-attf-for="{{ id }}_checkbox"> + <t t-if="suggestedRecipientInfo.name"> + <t t-esc="suggestedRecipientInfo.name"/> + </t> + <t t-if="suggestedRecipientInfo.email"> + (<t t-esc="suggestedRecipientInfo.email"/>) + </t> + </label> + </div> + <t t-if="!suggestedRecipientInfo.partner"> + <FormViewDialogComponentAdapter + Component="FormViewDialog" + params="{ + context: { + active_id: suggestedRecipientInfo.thread.id, + active_model: 'mail.compose.message', + default_email: suggestedRecipientInfo.email, + default_name: suggestedRecipientInfo.name, + force_email: true, + ref: 'compound_context', + }, + disable_multiple_selection: true, + on_saved: _onDialogSaved, + res_id: false, + res_model: 'res.partner', + title: PLEASE_COMPLETE_CUSTOMER_S_INFORMATION, + }" + t-ref="dialog" + /> + </t> + </t> + </div> + </t> +</templates> |
