diff options
| author | Debauche Stéphane <std@odoo.com> | 2019-10-10 12:39:25 +0000 |
|---|---|---|
| committer | Debauche Stéphane <std@odoo.com> | 2019-10-16 13:57:49 +0000 |
| commit | e981e3312d66cd6fcddef05feda0f49b37a4c858 (patch) | |
| tree | 4bb3b43be79ce8a69e48768f993aacff1a94ccf9 /addons/web/static/src | |
| parent | 594206d4f3c2465ca104d5ef65365d9339b034e0 (diff) | |
[IMP] web: many2many_binary, binary, image, add the option ``accepted_file_extensions``
Purpose
=======
We want to add an option on the widgets
- many2many_binary,
- binary,
- image
This option specifies what file extensions the user can pick from the file input dialog box.
Examples
========
```xml
<field widget="many2many_binary" options="{'accepted_file_extensions': 'image/*'}"/>
<field widget="many2many_binary" options="{'accepted_file_extensions': '.png,.jpeg'}"/>
<field widget="many2many_binary" options="{'accepted_file_extensions': 'application/pdf'}"/>
<field widget="image" options="{'accepted_file_extensions': '.png,.jpeg'}"/>
<field widget="binary" options="{'accepted_file_extensions': '.pdf,.svg'}"/>
```
How
===
Add an option (accepted_file_extensions) in the template ``HiddenInputFile`` (the widget many2many_binary is using this template)
So, we can also use this new option in others widgets using ``HiddenInputFile``
In the many2many_binary, read the ``nodeOptions`` and set the widget attribute ``accepted_file_extensions``
We also have to fix some other widget, because an property ``image_only`` was already existing in the template ``HiddenInputFile``
(we just need to replace ``image_only=True`` to ``accepted_file_extensions='image/*'``
The widget ``FieldPdfViewer`` (pdf_viewer) now use the new option to filtrate PDF
(instead of removing the <input/> and adding <input accept='.pdf'/>).
Tests
=====
We also test if the option is correctly set on the <input/>
- binary
- image
- many2many_binary
Impacted widgets
===============
- many2many_binary
- image: this widget use ``options="{accepted_file_extensions='image/*'}"`` instead of ``image_only=True``
- tablet_image: same as ``image``
Task #2082815
closes odoo/odoo#38351
Signed-off-by: VincentSchippefilt <VincentSchippefilt@users.noreply.github.com>
Diffstat (limited to 'addons/web/static/src')
| -rw-r--r-- | addons/web/static/src/js/fields/basic_fields.js | 3 | ||||
| -rw-r--r-- | addons/web/static/src/js/fields/relational_fields.js | 1 | ||||
| -rw-r--r-- | addons/web/static/src/js/views/kanban/kanban_record.js | 2 | ||||
| -rw-r--r-- | addons/web/static/src/xml/base.xml | 15 |
4 files changed, 8 insertions, 13 deletions
diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index 2c3e03681db..350dc5f5674 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -1634,6 +1634,7 @@ var AbstractFieldBinary = AbstractField.extend({ this.fields = record.fields; this.useFileAPI = !!window.FileReader; this.max_upload_size = 64 * 1024 * 1024; // 64Mo + this.accepted_file_extensions = (this.nodeOptions && this.nodeOptions.accepted_file_extensions) || this.accepted_file_extensions || '*'; if (!this.useFileAPI) { var self = this; this.fileupload_id = _.uniqueId('o_fileupload'); @@ -1765,6 +1766,7 @@ var FieldBinaryImage = AbstractFieldBinary.extend({ 'i': 'png', 'P': 'svg+xml', }, + accepted_file_extensions: 'image/*', /** * Returns the image URL from a model. * @@ -1976,6 +1978,7 @@ var FieldPdfViewer = FieldBinaryFile.extend({ description: _lt("PDF Viewer"), supportedFieldTypes: ['binary'], template: 'FieldPdfViewer', + accepted_file_extensions: 'application/pdf', /** * @override */ diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 2b6a0d44854..4a68c140056 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -2007,6 +2007,7 @@ var FieldMany2ManyBinaryMultiFiles = AbstractField.extend({ this.uploadedFiles = {}; this.uploadingFiles = []; this.fileupload_id = _.uniqueId('oe_fileupload_temp'); + this.accepted_file_extensions = (this.nodeOptions && this.nodeOptions.accepted_file_extensions) || this.accepted_file_extensions || '*'; $(window).on(this.fileupload_id, this._onFileLoaded.bind(this)); this.metadata = {}; diff --git a/addons/web/static/src/js/views/kanban/kanban_record.js b/addons/web/static/src/js/views/kanban/kanban_record.js index 0c4213c60b7..ab19e73d2eb 100644 --- a/addons/web/static/src/js/views/kanban/kanban_record.js +++ b/addons/web/static/src/js/views/kanban/kanban_record.js @@ -395,7 +395,7 @@ var KanbanRecord = Widget.extend({ fields: ['id', 'name'], }).then(function (attachmentIds) { self.imageUploadID = _.uniqueId('o_cover_image_upload'); - self.image_only = true; // prevent uploading of other file types + self.accepted_file_extensions = 'image/*'; // prevent uploading of other file types var coverId = self.record[fieldName] && self.record[fieldName].raw_value; var $content = $(QWeb.render('KanbanView.SetCoverModal', { coverId: coverId, diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index b76e86c036d..7800bc1a218 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -872,7 +872,6 @@ <span class="o_form_binary_progress">Uploading...</span> <t t-call="HiddenInputFile"> - <t t-set="image_only" t-value="true"/> <t t-set="fileupload_id" t-value="widget.fileupload_id"/> </t> </div> @@ -924,8 +923,8 @@ method="post" enctype="multipart/form-data" t-att-action="fileupload_action || '/web/binary/upload'"> <input type="hidden" name="csrf_token" t-att-value="csrf_token"/> <input type="hidden" name="callback" t-att-value="fileupload_id"/> - <input t-if="widget.image_only" type="file" class="o_input_file" name="ufile" accept="image/*"/> - <input t-if="!widget.image_only" type="file" class="o_input_file" name="ufile" t-att="{'multiple': multi_upload ? 'multiple' : null}"/> + <input type="file" class="o_input_file" name="ufile" + t-att="{'multiple': multi_upload ? 'multiple' : null, 'accept': widget.accepted_file_extensions || '*'}"/> <t t-raw="0"/> </form> <iframe t-att-id="fileupload_id" t-att-name="fileupload_id" style="display: none"/> @@ -985,21 +984,13 @@ <button class="btn btn-secondary fa fa-trash-o o_clear_file_button" title="Clear" aria-label="Clear" type="button"></button> </div> <iframe class="o_pdfview_iframe o_field_pdfviewer"/> - <t t-call="HiddenInputPDFFile"> + <t t-call="HiddenInputFile"> <t t-set="fileupload_id" t-value="widget.fileupload_id"/> <t t-set="fileupload_style" t-translation="off">overflow-x: hidden</t> </t> </div> </t> -<t t-name="HiddenInputPDFFile" t-extend="HiddenInputFile"> - <!-- remove the inputs and put a new one for pdf only --> - <t t-jquery="input[type='file']" t-operation="replace"/> - <t t-jquery="form" t-operation="append"> - <input type="file" class="o_input_file" name="ufile" accept="application/pdf"/> - </t> -</t> - <div t-name="FieldBinaryFileUploader" t-attf-class="oe_fileupload #{widget.attrs.class ? widget.attrs.class :''}" aria-atomic="true"> <div class="oe_placeholder_files"/> <div t-if="widget.mode !== 'readonly'" class="oe_add"> |
