diff --git a/addons/mail/static/src/widgets/form_renderer/form_renderer.js b/addons/mail/static/src/widgets/form_renderer/form_renderer.js index 14dcd00929e..cd788990e63 100644 --- a/addons/mail/static/src/widgets/form_renderer/form_renderer.js +++ b/addons/mail/static/src/widgets/form_renderer/form_renderer.js @@ -1,6 +1,7 @@ /** @odoo-module **/ import { ChatterContainer } from '@mail/components/chatter_container/chatter_container'; +import { AttachmentViewer } from '@mail/widgets/attachment_viewer/attachment_viewer'; import dom from 'web.dom'; import FormRenderer from 'web.FormRenderer'; @@ -47,6 +48,7 @@ FormRenderer.include({ * when applying the rendering into the DOM. */ this.chatterContainerTargetPlaceholder = undefined; + this.attachmentViewer = undefined; this.attachmentViewerTarget = undefined; if (this.hasAttachmentViewerFeature) { this.attachmentViewerTarget = document.createElement("div"); @@ -238,12 +240,51 @@ FormRenderer.include({ } }, /** - * @abstract * @private * @param {OdooEvent} ev * @param {Object} ev.data * @param {Attachment[]} ev.data.attachments * @param {Thread} ev.data.thread */ - _onChatterRendered(ev) {}, + _onChatterRendered(ev) { + if (!this.hasAttachmentViewer()) { + if (this.attachmentViewer) { + this.attachmentViewer.destroy(); + this.attachmentViewer = undefined; + this._interchangeChatter(); + } + return; + } + var self = this; + const thread = ev.data.thread; + const attachments = thread.attachmentsInWebClientView; + if (attachments.length || this.attachmentViewer) { + if (this.attachmentViewer) { + // FIXME should be improved : what if somehow an attachment is replaced in a thread ? + if ( + this.attachmentViewer.thread !== thread || + this.attachmentViewer.attachments.length !== attachments.length + ) { + if (attachments.length) { + this.attachmentViewer.updateContents(thread); + } + } else { + // The attachmentViewer lose its event listeners when it is reused, + // we just need to reregister them. + if (this.attachmentViewer.$el) { + this.attachmentViewer._undelegateEvents(); + this.attachmentViewer._delegateEvents(); + } + } + this.trigger_up('preview_attachment_validation'); + this._updateChatterContainerTarget(); + } else { + this.attachmentViewer = new AttachmentViewer(this, thread); + this.attachmentViewer.appendTo($(this.attachmentViewerTarget)).then(function () { + self.trigger_up('preview_attachment_validation'); + self._interchangeChatter(); + }); + } + } + }, });