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_suggestion/composer_suggestion.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/mail/static/src/components/composer_suggestion/composer_suggestion.js')
| -rw-r--r-- | addons/mail/static/src/components/composer_suggestion/composer_suggestion.js | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/addons/mail/static/src/components/composer_suggestion/composer_suggestion.js b/addons/mail/static/src/components/composer_suggestion/composer_suggestion.js new file mode 100644 index 00000000..da54ab54 --- /dev/null +++ b/addons/mail/static/src/components/composer_suggestion/composer_suggestion.js @@ -0,0 +1,143 @@ +odoo.define('mail/static/src/components/composer_suggestion/composer_suggestion.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 components = { + PartnerImStatusIcon: require('mail/static/src/components/partner_im_status_icon/partner_im_status_icon.js'), +}; + +const { Component } = owl; + +class ComposerSuggestion extends Component { + + /** + * @override + */ + constructor(...args) { + super(...args); + useShouldUpdateBasedOnProps(); + useStore(props => { + const composer = this.env.models['mail.composer'].get(this.props.composerLocalId); + const record = this.env.models[props.modelName].get(props.recordLocalId); + return { + composerHasToScrollToActiveSuggestion: composer && composer.hasToScrollToActiveSuggestion, + record: record ? record.__state : undefined, + }; + }); + useUpdate({ func: () => this._update() }); + } + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * @returns {mail.composer} + */ + get composer() { + return this.env.models['mail.composer'].get(this.props.composerLocalId); + } + + get isCannedResponse() { + return this.props.modelName === "mail.canned_response"; + } + + get isChannel() { + return this.props.modelName === "mail.thread"; + } + + get isCommand() { + return this.props.modelName === "mail.channel_command"; + } + + get isPartner() { + return this.props.modelName === "mail.partner"; + } + + get record() { + return this.env.models[this.props.modelName].get(this.props.recordLocalId); + } + + /** + * Returns a descriptive title for this suggestion. Useful to be able to + * read both parts when they are overflowing the UI. + * + * @returns {string} + */ + title() { + if (this.isCannedResponse) { + return _.str.sprintf("%s: %s", this.record.source, this.record.substitution); + } + if (this.isChannel) { + return this.record.name; + } + if (this.isCommand) { + return _.str.sprintf("%s: %s", this.record.name, this.record.help); + } + if (this.isPartner) { + if (this.record.email) { + return _.str.sprintf("%s (%s)", this.record.nameOrDisplayName, this.record.email); + } + return this.record.nameOrDisplayName; + } + return ""; + } + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + */ + _update() { + if ( + this.composer && + this.composer.hasToScrollToActiveSuggestion && + this.props.isActive + ) { + this.el.scrollIntoView({ + block: 'center', + }); + this.composer.update({ hasToScrollToActiveSuggestion: false }); + } + } + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @private + * @param {Event} ev + */ + _onClick(ev) { + ev.preventDefault(); + this.composer.update({ activeSuggestedRecord: [['link', this.record]] }); + this.composer.insertSuggestion(); + this.composer.closeSuggestions(); + this.trigger('o-composer-suggestion-clicked'); + } + +} + +Object.assign(ComposerSuggestion, { + components, + defaultProps: { + isActive: false, + }, + props: { + composerLocalId: String, + isActive: Boolean, + modelName: String, + recordLocalId: String, + }, + template: 'mail.ComposerSuggestion', +}); + +return ComposerSuggestion; + +}); |
