From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/mail/static/src/js/many2one_avatar_user.js | 68 +++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 addons/mail/static/src/js/many2one_avatar_user.js (limited to 'addons/mail/static/src/js/many2one_avatar_user.js') 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: + // + // + // 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, + }; +}); -- cgit v1.2.3