From 8a755d58330218b550efc0fea2f98800151c09a5 Mon Sep 17 00:00:00 2001 From: Romeo Fragomeli Date: Wed, 5 May 2021 10:35:58 +0000 Subject: [PATCH] [REF] web,*: hide buttons that doesn't work in webviews Some features of the PDF.js library doesn't work in the webview of the mobile apps. Initially 'window.print' is defined as an empty function in webviews unlike browsers where it is already ready. After that, PDF.js needs to monkey patch 'window.print' and saves a reference to the original definition, which is not yet fulfilled in by the mobile app (Java part). So the print of PDF.js doesn't work in webviews and end users will need to download the file before printing it. Regarding the Download button, the 'download' attribute is not supported by the webview as you can see in: https://bugs.chromium.org/p/chromium/issues/detail?id=432414 As there's many ways to download a file in Odoo it's not a big deal to simply hide it in PDF.js. Because it's quite complicated to fix this, we decided to hide the features that don't work (Download / Print) or don't make sense (Open file). Task-id: 2200168 --- .../static/src/legacy/public_livechat.js | 4 ++ .../attachment_viewer/attachment_viewer.js | 19 ++++++++ .../attachment_viewer/attachment_viewer.xml | 2 +- addons/mail/static/src/js/document_viewer.js | 20 ++++++++ addons/web/__manifest__.py | 1 + .../web/static/src/js/fields/basic_fields.js | 10 +--- addons/web/static/src/js/libs/pdfjs.js | 46 ++++++++++++++++++- 7 files changed, 92 insertions(+), 10 deletions(-) diff --git a/addons/im_livechat/static/src/legacy/public_livechat.js b/addons/im_livechat/static/src/legacy/public_livechat.js index 46db2d8cd81..8a714e99cd2 100644 --- a/addons/im_livechat/static/src/legacy/public_livechat.js +++ b/addons/im_livechat/static/src/legacy/public_livechat.js @@ -3530,6 +3530,7 @@ odoo.define('im_livechat.legacy.mail.DocumentViewer', function (require) { var core = require('web.core'); var Widget = require('web.Widget'); +var { hidePDFJSButtons } = require('@web/js/libs/pdfjs'); var QWeb = core.qweb; @@ -3658,6 +3659,9 @@ var DocumentViewer = Widget.extend({ this.$('.o_viewer_content').html(QWeb.render('im_livechat.legacy.mail.DocumentViewer.Content', { widget: this })); + if (this.activeAttachment.fileType === 'application/pdf') { + hidePDFJSButtons(this.$('.o_viewer_content')[0]); + } this.$('.o_viewer_img').on("load", _.bind(this._onImageLoaded, this)); this.$('[data-toggle="tooltip"]').tooltip({ delay: 0 }); this._reset(); diff --git a/addons/mail/static/src/components/attachment_viewer/attachment_viewer.js b/addons/mail/static/src/components/attachment_viewer/attachment_viewer.js index bd2f3896c73..108006b14d3 100644 --- a/addons/mail/static/src/components/attachment_viewer/attachment_viewer.js +++ b/addons/mail/static/src/components/attachment_viewer/attachment_viewer.js @@ -5,6 +5,8 @@ import useShouldUpdateBasedOnProps from '@mail/component_hooks/use_should_update import useStore from '@mail/component_hooks/use_store/use_store'; import { link } from '@mail/model/model_field_command'; +import { hidePDFJSButtons } from '@web/js/libs/pdfjs'; + const { Component, QWeb } = owl; const { useRef } = owl.hooks; @@ -53,6 +55,10 @@ class AttachmentViewer extends Component { * transformation on image visualisation. */ this._zoomerRef = useRef('zoomer'); + /** + * Reference of the IFRAME node when the attachment is a PDF. + */ + this._iframeViewerPdfRef = useRef('iframeViewerPdf'); /** * Tracked translate transformations on image visualisation. This is * not observed with `useStore` because they are used to compute zoomer @@ -67,6 +73,7 @@ class AttachmentViewer extends Component { mounted() { this.el.focus(); this._handleImageLoad(); + this._hideUnwantedPdfJsButtons(); document.addEventListener('click', this._onClickGlobal); } @@ -75,6 +82,7 @@ class AttachmentViewer extends Component { */ patched() { this._handleImageLoad(); + this._hideUnwantedPdfJsButtons(); } willUnmount() { @@ -169,6 +177,17 @@ class AttachmentViewer extends Component { } } + /** + * @see 'hidePDFJSButtons' + * + * @private + */ + _hideUnwantedPdfJsButtons() { + if (this._iframeViewerPdfRef.el) { + hidePDFJSButtons(this._iframeViewerPdfRef.el); + } + } + /** * Display the previous attachment in the list of attachments. * diff --git a/addons/mail/static/src/components/attachment_viewer/attachment_viewer.xml b/addons/mail/static/src/components/attachment_viewer/attachment_viewer.xml index af0651291d9..e3c5ef1f312 100644 --- a/addons/mail/static/src/components/attachment_viewer/attachment_viewer.xml +++ b/addons/mail/static/src/components/attachment_viewer/attachment_viewer.xml @@ -46,7 +46,7 @@ -