From d3eff197a597671a5b0f41238d8cd8b2fc32cd4e Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Thu, 4 Aug 2022 15:17:03 +0000 Subject: [PATCH] [FIX] web: Fix binary and pdf_viewer fields This commit aims to make the BinaryField and PdfViewerField appear and act the same way as they did before their conversion to Owl. This mostly consists of a few tweaks in the conditional rendering of certain elements in the template, and the rest of the changes are meant to clean and simplify the values the component is working with. closes odoo/odoo#98251 Signed-off-by: Bruno Boi (boi) --- .../src/views/fields/binary/binary_field.js | 47 +++-------- .../src/views/fields/binary/binary_field.xml | 52 ++++++------ .../static/src/views/fields/file_handler.js | 1 + .../static/src/views/fields/file_handler.xml | 24 +++--- .../fields/pdf_viewer/pdf_viewer_field.js | 66 ++++++++-------- .../fields/pdf_viewer/pdf_viewer_field.xml | 57 ++++++------- .../tests/views/fields/binary_field_tests.js | 79 +++++++++++-------- .../views/fields/pdf_viewer_field_tests.js | 52 ++++++------ 8 files changed, 186 insertions(+), 192 deletions(-) diff --git a/addons/web/static/src/views/fields/binary/binary_field.js b/addons/web/static/src/views/fields/binary/binary_field.js index aaa34ff561e..fe537bc72c5 100644 --- a/addons/web/static/src/views/fields/binary/binary_field.js +++ b/addons/web/static/src/views/fields/binary/binary_field.js @@ -17,35 +17,25 @@ export class BinaryField extends Component { setup() { this.notification = useService("notification"); this.state = useState({ - fileName: this.fileName || "", - isValid: true, + fileName: this.props.record.data[this.props.fileNameField] || "", }); onWillUpdateProps((nextProps) => { if (nextProps.readonly) { - this.state.fileName = ""; + this.state.fileName = nextProps.record.data[nextProps.fileNameField] || ""; } }); } get fileName() { - return this.props.record.data[this.props.fileNameField]; - } - get file() { - return { - data: this.props.value, - name: this.state.fileName || this.props.value || null, - }; - } - get isDownloadable() { - return !( - this.props.record.isReadonly(this.props.name) && this.props.record.mode === "edit" - ); + return this.state.fileName || this.props.value || ""; } - update(file) { - const changes = { [this.props.name]: file.data || false }; - if (this.props.fileNameField && this.props.fileNameField !== this.props.name) { - changes[this.props.fileNameField] = file.name || false; + update({ data, name }) { + this.state.fileName = name || ""; + const { fileNameField, record } = this.props; + const changes = { [this.props.name]: data || false }; + if (fileNameField in record.fields && record.data[fileNameField] !== name) { + changes[fileNameField] = name || false; } return this.props.record.update(changes); } @@ -56,29 +46,14 @@ export class BinaryField extends Component { model: this.props.record.resModel, id: this.props.record.resId, field: this.props.name, - filename_field: this.file.name, - filename: this.file.name || "", + filename_field: this.fileName, + filename: this.fileName || "", download: true, data: isBinarySize(this.props.value) ? null : this.props.value, }, url: "/web/content", }); } - onFileRemove() { - this.state.isValid = true; - this.update(false); - } - onFileUploaded(file) { - this.state.fileName = file.name; - this.state.isValid = true; - this.update(file); - } - onLoadFailed() { - this.state.isValid = false; - this.notification.add(this.env._t("Could not display the selected binary"), { - type: "danger", - }); - } } BinaryField.template = "web.BinaryField"; diff --git a/addons/web/static/src/views/fields/binary/binary_field.xml b/addons/web/static/src/views/fields/binary/binary_field.xml index 8c9a9475a7c..0dc046d9de6 100644 --- a/addons/web/static/src/views/fields/binary/binary_field.xml +++ b/addons/web/static/src/views/fields/binary/binary_field.xml @@ -3,45 +3,47 @@ - - - - + +
+ + + +
- - - - + + + +
diff --git a/addons/web/static/src/views/fields/file_handler.js b/addons/web/static/src/views/fields/file_handler.js index d193a5b9b3f..e02a4711f85 100644 --- a/addons/web/static/src/views/fields/file_handler.js +++ b/addons/web/static/src/views/fields/file_handler.js @@ -92,5 +92,6 @@ export class FileUploader extends Component { this.fileInputRef.el.click(); } } + FileUploader.template = "web.FileUploader"; FileUploader.nextId = 0; diff --git a/addons/web/static/src/views/fields/file_handler.xml b/addons/web/static/src/views/fields/file_handler.xml index fe9ee318b71..24686bf6a3f 100644 --- a/addons/web/static/src/views/fields/file_handler.xml +++ b/addons/web/static/src/views/fields/file_handler.xml @@ -2,20 +2,18 @@ - - - - - - - + Uploading... + + + + diff --git a/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.js b/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.js index 7c57903dd8c..de5d405b575 100644 --- a/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.js +++ b/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.js @@ -13,56 +13,60 @@ export class PdfViewerField extends Component { setup() { this.notification = useService("notification"); this.state = useState({ - fileName: this.fileName || "", + fileName: this.props.record.data[this.props.fileNameField] || "", isValid: true, objectUrl: "", }); onWillUpdateProps((nextProps) => { if (nextProps.readonly) { - this.state.fileName = ""; + this.state.fileName = nextProps.record.data[nextProps.fileNameField] || ""; this.state.objectUrl = ""; } }); } - get defaultPage() { - return this.props.record.data[`${this.props.name}_page`]; - } + get fileName() { - return this.props.record.data[this.props.fileNameField]; - } - get file() { - return { - data: this.props.value || "", - name: this.state.fileName || this.props.value || null, - }; + return this.state.fileName || this.props.value || ""; } + get url() { - if (this.state.isValid && this.props.value) { - return ( - "/web/static/lib/pdfjs/web/viewer.html?file=" + - encodeURIComponent( - this.state.objectUrl || - url("/web/content", { - model: this.props.record.resModel, - id: this.props.record.resId, - field: this.props.previewImage || this.props.name, - }) - ) + - `#page=${this.props.defaultPage || 1}` - ); + if (!this.state.isValid || !this.props.value) { + return null; } - return null; + const page = this.props.record.data[`${this.props.name}_page`] || 1; + const file = encodeURIComponent( + this.state.objectUrl || + url("/web/content", { + model: this.props.record.resModel, + field: this.props.previewImage || this.props.name, + id: this.props.record.resId, + }) + ); + return `/web/static/lib/pdfjs/web/viewer.html?file=${file}#page=${page}`; } + + update({ data, name }) { + this.state.fileName = name || ""; + const { fileNameField, record } = this.props; + const changes = { [this.props.name]: data || false }; + if (fileNameField in record.fields && record.data[fileNameField] !== name) { + changes[fileNameField] = name || false; + } + return this.props.record.update(changes); + } + onFileRemove() { this.state.isValid = true; - this.props.update(false); + this.update({}); } - onFileUploaded(file) { - this.state.fileName = file.name; + + onFileUploaded({ data, name, objectUrl }) { + this.state.fileName = name; this.state.isValid = true; - this.props.update(file.data); - this.state.objectUrl = file.objectUrl; + this.state.objectUrl = objectUrl; + this.update({ data, name }); } + onLoadFailed() { this.state.isValid = false; this.notification.add(this.env._t("Could not display the selected pdf"), { diff --git a/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.xml b/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.xml index db85ef2aef9..e9d3170a3c3 100644 --- a/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.xml +++ b/addons/web/static/src/views/fields/pdf_viewer/pdf_viewer_field.xml @@ -3,42 +3,43 @@ - - - - -