diff --git a/addons/web_editor/static/src/js/wysiwyg/widgets/media.js b/addons/web_editor/static/src/js/wysiwyg/widgets/media.js index acff4183aab..2170fda3259 100644 --- a/addons/web_editor/static/src/js/wysiwyg/widgets/media.js +++ b/addons/web_editor/static/src/js/wysiwyg/widgets/media.js @@ -97,10 +97,9 @@ var SearchableMediaWidget = MediaWidget.extend({ }); /** - * Let users choose an image, including uploading a new image in odoo. + * Let users choose a file, including uploading a new file in odoo. */ -var ImageWidget = SearchableMediaWidget.extend({ - template: 'wysiwyg.widgets.image', +var FileWidget = SearchableMediaWidget.extend({ events: _.extend({}, SearchableMediaWidget.prototype.events || {}, { 'click .o_upload_media_button': '_onUploadButtonClick', 'click .o_upload_media_button_no_optimization': '_onUploadButtonNoOptimizationClick', @@ -112,7 +111,9 @@ var ImageWidget = SearchableMediaWidget.extend({ 'click .o_existing_attachment_remove': '_onRemoveClick', 'click .o_load_more': '_onLoadMoreClick', }), + existingAttachmentsTemplate: undefined, + IMAGE_MIMETYPES: ['image/gif', 'image/jpe', 'image/jpeg', 'image/jpg', 'image/gif', 'image/png'], IMAGES_PER_ROW: 6, IMAGES_ROWS: 5, @@ -128,7 +129,7 @@ var ImageWidget = SearchableMediaWidget.extend({ this.options = options; this.context = options.context; - this.accept = options.accept || (options.document ? '*/*' : 'image/*'); + this.accept = options.accept; this.multiImages = options.multiImages; @@ -299,10 +300,7 @@ var ImageWidget = SearchableMediaWidget.extend({ domain = domain.concat(attachedDocumentDomain); } domain = ['|', ['public', '=', true]].concat(domain); - - domain.push('|', - ['mimetype', '=', false], - ['mimetype', this.options.document ? 'not in' : 'in', ['image/gif', 'image/jpe', 'image/jpeg', 'image/jpg', 'image/gif', 'image/png']]); + domain = domain.concat(this.options.mimetypeDomain); if (needle && needle.length) { domain.push('|', ['datas_fname', 'ilike', needle], ['name', 'ilike', needle]); } @@ -337,6 +335,20 @@ var ImageWidget = SearchableMediaWidget.extend({ this.search(this.$('.o_we_search').val() || ''); } }, + /** + * Renders the existing attachments and returns the result as a string. + * + * @param {Object[]} rows + * @param {boolean} withEffect + * @returns {string} + */ + _renderExisting: function (rows, withEffect) { + return QWeb.render(this.existingAttachmentsTemplate, { + rows: rows, + widget: this, + withEffect: withEffect, + }); + }, /** * @private */ @@ -352,11 +364,7 @@ var ImageWidget = SearchableMediaWidget.extend({ // Render menu & content this.$('.existing-attachments').replaceWith( - QWeb.render('wysiwyg.widgets.files.existing.content', { - rows: rows, - isDocument: this.options.document, - withEffect: withEffect, - }) + this._renderExisting(rows, withEffect) ); var $divs = this.$('.o_image'); @@ -481,6 +489,26 @@ var ImageWidget = SearchableMediaWidget.extend({ this.search(''); } }, + /** + * Updates the add by URL UI. + * + * @private + * @param {boolean} emptyValue + * @param {boolean} isURL + * @param {boolean} isImage + */ + _updateAddUrlUi(emptyValue, isURL, isImage) { + var $button = this.$('.o_upload_media_url_button'); + var $success = this.$('.o_we_url_success'); + var $error = this.$('.o_we_url_error'); + + $button.toggleClass('btn-secondary', emptyValue) + .toggleClass('btn-primary', !emptyValue) + .prop('disabled', !isURL); + + $success.toggleClass('d-none', !isURL); + $error.toggleClass('d-none', emptyValue || isURL); + }, /** * @private */ @@ -604,6 +632,7 @@ var ImageWidget = SearchableMediaWidget.extend({ } $helpBlock.replaceWith(QWeb.render('wysiwyg.widgets.image.existing.error', { views: prevented[id], + widget: self, })); }); } @@ -614,10 +643,6 @@ var ImageWidget = SearchableMediaWidget.extend({ */ _onURLInputChange: function (ev) { var $input = $(ev.currentTarget); - var $button = this.$('.o_upload_media_url_button'); - var $success = this.$('.o_we_url_success'); - var $warning = this.$('.o_we_url_warning'); - var $error = this.$('.o_we_url_error'); var inputValue = $input.val(); var emptyValue = (inputValue === ''); @@ -627,14 +652,7 @@ var ImageWidget = SearchableMediaWidget.extend({ return inputValue.endsWith(format); }); - $button.toggleClass('btn-secondary', emptyValue).toggleClass('btn-primary', !emptyValue) - .prop('disabled', !isURL); - if (!this.options.document) { - $button.text((isURL && !isImage) ? _t("Add as document") : _t("Add image")); - } - $success.toggleClass('d-none', !isURL); - $warning.toggleClass('d-none', !isURL || this.options.document || isImage); - $error.toggleClass('d-none', emptyValue || isURL); + this._updateAddUrlUi(emptyValue, isURL, isImage); }, /** * @private @@ -671,6 +689,79 @@ var ImageWidget = SearchableMediaWidget.extend({ }, }); +/** + * Let users choose an image, including uploading a new image in odoo. + */ +var ImageWidget = FileWidget.extend({ + template: 'wysiwyg.widgets.image', + existingAttachmentsTemplate: 'wysiwyg.widgets.image.existing.attachments', + + /** + * @constructor + */ + init: function (parent, media, options) { + options = _.extend({ + accept: 'image/*', + mimetypeDomain: [['mimetype', 'in', this.IMAGE_MIMETYPES]], + }, options || {}); + this._super(parent, media, options); + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @override + */ + _updateAddUrlUi: function (emptyValue, isURL, isImage) { + this._super.apply(this, arguments); + + var $button = this.$('.o_upload_media_url_button'); + var $warning = this.$('.o_we_url_warning'); + + $button.text((isURL && !isImage) ? _t("Add as document") : _t("Add image")); + $warning.toggleClass('d-none', !isURL || isImage); + }, +}); + + +/** + * Let users choose a document, including uploading a new document in odoo. + */ +var DocumentWidget = FileWidget.extend({ + template: 'wysiwyg.widgets.document', + existingAttachmentsTemplate: 'wysiwyg.widgets.document.existing.attachments', + + /** + * @constructor + */ + init: function (parent, media, options) { + options = _.extend({ + accept: '*/*', + mimetypeDomain: [['mimetype', 'not in', this.IMAGE_MIMETYPES]], + }, options || {}); + this._super(parent, media, options); + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @override + */ + _updateAddUrlUi: function (emptyValue, isURL, isImage) { + this._super.apply(this, arguments); + + var $button = this.$('.o_upload_media_url_button'); + var $warning = this.$('.o_we_url_warning'); + + $button.text((isURL && isImage) ? _t("Add as image") : _t("Add document")); + $warning.toggleClass('d-none', !isURL || !isImage); + }, +}); + /** * Let users choose a font awesome icon, support all font awesome loaded in the * css files. @@ -758,7 +849,7 @@ var IconWidget = SearchableMediaWidget.extend({ }); } this.$('div.font-icons-icons').html( - QWeb.render('wysiwyg.widgets.font-icons.icons', {iconsParser: iconsParser}) + QWeb.render('wysiwyg.widgets.font-icons.icons', {iconsParser: iconsParser, widget: this}) ); return Promise.resolve(); }, @@ -1118,7 +1209,9 @@ var VideoWidget = MediaWidget.extend({ return { MediaWidget: MediaWidget, SearchableMediaWidget: SearchableMediaWidget, + FileWidget: FileWidget, ImageWidget: ImageWidget, + DocumentWidget: DocumentWidget, IconWidget: IconWidget, VideoWidget: VideoWidget, }; diff --git a/addons/web_editor/static/src/js/wysiwyg/widgets/media_dialog.js b/addons/web_editor/static/src/js/wysiwyg/widgets/media_dialog.js index 4297d5f4dd3..3e68294cf8b 100644 --- a/addons/web_editor/static/src/js/wysiwyg/widgets/media_dialog.js +++ b/addons/web_editor/static/src/js/wysiwyg/widgets/media_dialog.js @@ -55,9 +55,7 @@ var MediaDialog = Dialog.extend({ this.noVideos = onlyImages || options.noVideos; if (!this.noDocuments) { - this.documentDialog = new MediaModules.ImageWidget(this, this.media, _.extend({}, options, { - document: true, - })); + this.documentDialog = new MediaModules.DocumentWidget(this, this.media, options); this.documentDialog.tabToShow = 'document'; } if (!this.noIcons) { diff --git a/addons/web_editor/static/src/js/wysiwyg/widgets/widgets.js b/addons/web_editor/static/src/js/wysiwyg/widgets/widgets.js index 2f52fe6c5fa..cb17523ac19 100644 --- a/addons/web_editor/static/src/js/wysiwyg/widgets/widgets.js +++ b/addons/web_editor/static/src/js/wysiwyg/widgets/widgets.js @@ -20,7 +20,9 @@ return { MediaWidget: media.MediaWidget, SearchableMediaWidget: media.SearchableMediaWidget, + FileWidget: media.FileWidget, ImageWidget: media.ImageWidget, + DocumentWidget: media.DocumentWidget, IconWidget: media.IconWidget, VideoWidget: media.VideoWidget, }; diff --git a/addons/web_editor/static/src/xml/wysiwyg.xml b/addons/web_editor/static/src/xml/wysiwyg.xml index cd20ee3f4ef..9a93066ee48 100644 --- a/addons/web_editor/static/src/xml/wysiwyg.xml +++ b/addons/web_editor/static/src/xml/wysiwyg.xml @@ -93,8 +93,8 @@ - - + +
- - - - +
@@ -122,48 +119,62 @@
- +
- - - - +
- +
- - - + + +
- - - Search a document - Search an image - - +
+ + + Upload an image + https://www.odoo.com/logo.png + Add image + Search an image + The URL does not contain any image. The file will be added in the document section. + + + + + + + + + + Upload a document + https://www.odoo.com/mydocument + Add document + Search a document + The URL contains an image. The file will be added in the image section. + + + - +
- - -
+ + + + +

The image could not be deleted because it is used in the diff --git a/addons/web_editor/static/tests/field_html_tests.js b/addons/web_editor/static/tests/field_html_tests.js index 5c7bd70d117..477519d4060 100644 --- a/addons/web_editor/static/tests/field_html_tests.js +++ b/addons/web_editor/static/tests/field_html_tests.js @@ -193,21 +193,6 @@ QUnit.module('web_editor', {}, function () { if (args.method === "generate_access_token") { return Promise.resolve(); } - if (args.kwargs.domain[7][2].join(',') === "image/gif,image/jpe,image/jpeg,image/jpg,image/gif,image/png") { - return Promise.resolve([{ - "id": 1, - "public": true, - "name": "image", - "datas_fname": "image.png", - "mimetype": "image/png", - "checksum": false, - "url": "/web_editor/static/src/img/transparent.png", - "type": "url", - "res_id": 0, - "res_model": false, - "access_token": false - }]); - } } return this._super(route, args); }, diff --git a/addons/web_editor/static/tests/wysiwyg_tests.js b/addons/web_editor/static/tests/wysiwyg_tests.js index 7462f5a330b..17f9951fe6f 100644 --- a/addons/web_editor/static/tests/wysiwyg_tests.js +++ b/addons/web_editor/static/tests/wysiwyg_tests.js @@ -3406,12 +3406,8 @@ QUnit.module('Media', { }, mockRPC: function (route, args) { - if (args.model === 'ir.attachment' || !args.length) { - if (!args.length && route.indexOf('data:image/png;base64') === 0 || - args.method === "search_read" && - args.kwargs.domain[7][2].join(',') === "image/gif,image/jpe,image/jpeg,image/jpg,image/gif,image/png") { - return Promise.resolve(this.data.records); - } + if (route.indexOf('data:image/png;base64') === 0) { + return Promise.resolve(); } if (route.indexOf('youtube') !== -1) { return Promise.resolve(); diff --git a/addons/web_unsplash/static/src/js/unsplash_image_widget.js b/addons/web_unsplash/static/src/js/unsplash_image_widget.js index 3c0d1964513..5f5100c3da2 100644 --- a/addons/web_unsplash/static/src/js/unsplash_image_widget.js +++ b/addons/web_unsplash/static/src/js/unsplash_image_widget.js @@ -56,9 +56,7 @@ widgetsMedia.ImageWidget.include({ * @override */ start: function () { - if (!this.options.document) { - this.$('.o_we_search_icon').replaceWith(core.qweb.render('web_unsplash.dialog.media.search_icon')); - } + this.$('.o_we_search_icon').replaceWith(core.qweb.render('web_unsplash.dialog.media.search_icon')); return this._super.apply(this, arguments); }, /** @@ -286,18 +284,16 @@ widgetsMedia.ImageWidget.include({ * @override */ _onSearchInput: function (ev) { - if (!this.options.document) { - var $input = $(ev.currentTarget); - var inputValue = $input.val(); - this.$('.o_search_value_input').text(inputValue); + var $input = $(ev.currentTarget); + var inputValue = $input.val(); + this.$('.o_search_value_input').text(inputValue); - var $icon = this.$('.o_we_search_icon'); - if ($icon.parent().is('.show')) { - $icon.dropdown('update'); - } else if (!this.hasSearched) { - $icon.dropdown('toggle'); - $input.focus(); - } + var $icon = this.$('.o_we_search_icon'); + if ($icon.parent().is('.show')) { + $icon.dropdown('update'); + } else if (!this.hasSearched) { + $icon.dropdown('toggle'); + $input.focus(); } this._super.apply(this, arguments); }, diff --git a/addons/web_unsplash/static/src/xml/unsplash_image_widget.xml b/addons/web_unsplash/static/src/xml/unsplash_image_widget.xml index dc680ef00c4..6ecf304034b 100644 --- a/addons/web_unsplash/static/src/xml/unsplash_image_widget.xml +++ b/addons/web_unsplash/static/src/xml/unsplash_image_widget.xml @@ -1,7 +1,7 @@ - +