From e7d6a68a56c67d6130df1e6c07f25f5df6086753 Mon Sep 17 00:00:00 2001 From: Romeo Fragomeli Date: Fri, 30 Apr 2021 07:28:42 +0000 Subject: [PATCH 1/3] [REV][FIX] web: pdfjs: hide buttons that doesn't work in webviews The commit odoo/odoo@7a40d528 is reverted as the following commit will do same behaviour without "Monkey Patching" the lib PDF.js --- addons/web/static/lib/pdfjs/web/viewer.js | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/addons/web/static/lib/pdfjs/web/viewer.js b/addons/web/static/lib/pdfjs/web/viewer.js index fa1ec99eabf..54f95117ab9 100644 --- a/addons/web/static/lib/pdfjs/web/viewer.js +++ b/addons/web/static/lib/pdfjs/web/viewer.js @@ -253,25 +253,6 @@ function getViewerConfiguration() { function webViewerLoad() { var config = getViewerConfiguration(); - // Start Odoo patch - // Source https://github.com/mozilla/pdf.js/pull/11246 (Detect Android and iOS) - const userAgent = (typeof navigator !== 'undefined' && navigator.userAgent) || ''; - const platform = (typeof navigator !== 'undefined' && navigator.platform) || ''; - const maxTouchPoints = (typeof navigator !== 'undefined' && navigator.maxTouchPoints) || 1; - const isAndroid = /Android/.test(userAgent); - const isIOS = /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent) || (platform === 'MacIntel' && maxTouchPoints > 1); - // Hide Open Button - config.toolbar.openFile.setAttribute('hidden', 'true'); - config.secondaryToolbar.openFileButton.setAttribute('hidden', 'true'); - // Hide Download Button - config.toolbar.download.setAttribute('hidden', 'true'); - config.secondaryToolbar.downloadButton.setAttribute('hidden', 'true'); - if (isAndroid || isIOS) { - // Hide Print Button - config.toolbar.print.setAttribute('hidden', 'true'); - config.secondaryToolbar.printButton.setAttribute('hidden', 'true'); - } - // End Odoo patch window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication; window.PDFViewerApplicationOptions = pdfjsWebAppOptions.AppOptions; var event = document.createEvent('CustomEvent'); From c1c225eff2e3fa4289ff7d75590f76f39d23816d Mon Sep 17 00:00:00 2001 From: Romeo Fragomeli Date: Mon, 26 Apr 2021 12:11:07 +0000 Subject: [PATCH 2/3] [IMP] web: add the detection of iPadOS Before this commit, the iPad with iPadOS was not detected as 'isMobileDevice'. Now the detection is more accurate. --- addons/web/static/src/js/services/config.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/addons/web/static/src/js/services/config.js b/addons/web/static/src/js/services/config.js index 6c576c351bb..dde878f102d 100644 --- a/addons/web/static/src/js/services/config.js +++ b/addons/web/static/src/js/services/config.js @@ -14,6 +14,11 @@ const bus = new Bus(); * this file someday. */ +const maxTouchPoints = navigator.maxTouchPoints || 1; +const isAndroid = /Android/i.test(navigator.userAgent); +const isIOS = /(iPad|iPhone|iPod)/i.test(navigator.userAgent) || (navigator.platform === 'MacIntel' && maxTouchPoints > 1); +const isOtherMobileDevice = /(webOS|BlackBerry|Windows Phone)/i.test(navigator.userAgent); + var config = { device: { /** @@ -52,13 +57,7 @@ var config = { * * @return {boolean} */ - isMobileDevice: navigator.userAgent.match(/Android/i) || - navigator.userAgent.match(/webOS/i) || - navigator.userAgent.match(/iPhone/i) || - navigator.userAgent.match(/iPad/i) || - navigator.userAgent.match(/iPod/i) || - navigator.userAgent.match(/BlackBerry/i) || - navigator.userAgent.match(/Windows Phone/i), + isMobileDevice: isAndroid || isIOS || isOtherMobileDevice, /** * Mapping between the numbers 0,1,2,3,4,5,6 and some descriptions */ From 8a755d58330218b550efc0fea2f98800151c09a5 Mon Sep 17 00:00:00 2001 From: Romeo Fragomeli Date: Wed, 5 May 2021 10:35:58 +0000 Subject: [PATCH 3/3] [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 @@ -