summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/js/many2one_avatar_user.js
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/mail/static/src/js/many2one_avatar_user.js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/mail/static/src/js/many2one_avatar_user.js')
-rw-r--r--addons/mail/static/src/js/many2one_avatar_user.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/addons/mail/static/src/js/many2one_avatar_user.js b/addons/mail/static/src/js/many2one_avatar_user.js
new file mode 100644
index 00000000..6a5b5270
--- /dev/null
+++ b/addons/mail/static/src/js/many2one_avatar_user.js
@@ -0,0 +1,68 @@
+odoo.define('mail.Many2OneAvatarUser', function (require) {
+ "use strict";
+
+ // This module defines an extension of the Many2OneAvatar widget, which is
+ // integrated with the messaging system. The Many2OneAvatarUser is designed
+ // to display people, and when the avatar of those people is clicked, it
+ // opens a DM chat window with the corresponding user.
+ //
+ // This widget is supported on many2one fields pointing to 'res.users'.
+ //
+ // Usage:
+ // <field name="user_id" widget="many2one_avatar_user"/>
+ //
+ // The widget is designed to be extended, to support many2one fields pointing
+ // to other models than 'res.users'.
+
+ const fieldRegistry = require('web.field_registry');
+ const { Many2OneAvatar } = require('web.relational_fields');
+
+ const { Component } = owl;
+
+ const Many2OneAvatarUser = Many2OneAvatar.extend({
+ events: Object.assign({}, Many2OneAvatar.prototype.events, {
+ 'click .o_m2o_avatar': '_onAvatarClicked',
+ }),
+ // This widget is only supported on many2ones pointing to 'res.users'
+ supportedModels: ['res.users'],
+
+ init() {
+ this._super(...arguments);
+ if (!this.supportedModels.includes(this.field.relation)) {
+ throw new Error(`This widget is only supported on many2one fields pointing to ${JSON.stringify(this.supportedModels)}`);
+ }
+ if (this.mode === 'readonly') {
+ this.className += ' o_clickable_m2o_avatar';
+ }
+ },
+
+ //----------------------------------------------------------------------
+ // Handlers
+ //----------------------------------------------------------------------
+
+ /**
+ * When the avatar is clicked, open a DM chat window with the
+ * corresponding user.
+ *
+ * @private
+ * @param {MouseEvent} ev
+ */
+ async _onAvatarClicked(ev) {
+ ev.stopPropagation(); // in list view, prevent from opening the record
+ const env = Component.env;
+ await env.messaging.openChat({ userId: this.value.res_id });
+ }
+ });
+
+ const KanbanMany2OneAvatarUser = Many2OneAvatarUser.extend({
+ _template: 'mail.KanbanMany2OneAvatarUser',
+ });
+
+ fieldRegistry.add('many2one_avatar_user', Many2OneAvatarUser);
+ fieldRegistry.add('kanban.many2one_avatar_user', KanbanMany2OneAvatarUser);
+
+ return {
+ Many2OneAvatarUser,
+ KanbanMany2OneAvatarUser,
+ };
+});