From 433f6c27587cf16e3b48a2624d42b5b96fa0cbbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Mottet=20=28oms=29?= Date: Wed, 8 Sep 2021 14:20:03 +0000 Subject: [PATCH] [FIX] website, web_editor: prevent mega menu removal The user was able to remove the mega menu snippet without removing the corresponding link in the menu. To avoid this flow to happen we removed the delete button for the mega menu snippet. We also handle the case where the user remove the mega menu snippet element by element. On last element removal we put back the original template. task-2636545 closes odoo/odoo#77256 X-original-commit: 97810a9c40396bb27cb5779937734849d185cf1f Signed-off-by: Quentin Smetz (qsm) --- .../static/src/js/editor/snippets.options.js | 34 +++++++++--- .../static/src/js/editor/snippets.options.js | 54 +++++++++++++++++-- addons/website/views/snippets/snippets.xml | 2 + 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index bbe193c813e..3afe536d009 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -6692,6 +6692,31 @@ registry.SelectTemplate = SnippetOptionWidget.extend({ } }, + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Retrieves a template either from cache or through RPC. + * + * @private + * @param {string} xmlid + * @returns {string} + */ + async _getTemplate(xmlid) { + if (!this._templates[xmlid]) { + this._templates[xmlid] = await this._rpc({ + model: 'ir.ui.view', + method: 'render_public_asset', + args: [`${xmlid}`, {}], + kwargs: { + context: this.options.context, + }, + }); + } + return this._templates[xmlid]; + }, + //-------------------------------------------------------------------------- // Handlers //-------------------------------------------------------------------------- @@ -6712,14 +6737,7 @@ registry.SelectTemplate = SnippetOptionWidget.extend({ // TODO should be better and retrieve all rendering in one RPC (but // those ~10 RPC are only done once per edit mode if the option is // opened, so I guess this is acceptable). - this._templates[xmlid] = await this._rpc({ - model: 'ir.ui.view', - method: 'render_public_asset', - args: [`${xmlid}`, {}], - kwargs: { - context: this.options.context, - }, - }); + await this._getTemplate(xmlid); }); this._templatesLoading = Promise.all(proms); }, diff --git a/addons/website/static/src/js/editor/snippets.options.js b/addons/website/static/src/js/editor/snippets.options.js index 42a5e2c8d56..2e027666c6b 100644 --- a/addons/website/static/src/js/editor/snippets.options.js +++ b/addons/website/static/src/js/editor/snippets.options.js @@ -3269,6 +3269,25 @@ options.registry.MegaMenuLayout = options.registry.SelectTemplate.extend({ this.selectTemplateWidgetName = 'mega_menu_template_opt'; }, + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * @override + */ + notify(name, data) { + if (name === 'reset_template') { + const xmlid = this._getCurrentTemplateXMLID(); + this._getTemplate(xmlid).then(template => { + this.containerEl.insertAdjacentHTML('beforeend', template); + data.onSuccess(); + }); + } else { + this._super(...arguments); + } + }, + //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- @@ -3278,12 +3297,41 @@ options.registry.MegaMenuLayout = options.registry.SelectTemplate.extend({ */ _computeWidgetState: function (methodName, params) { if (methodName === 'selectTemplate') { - const templateDefiningClass = this.containerEl.querySelector('section') - .classList.value.split(' ').filter(cl => cl.startsWith('s_mega_menu'))[0]; - return `website.${templateDefiningClass}`; + return this._getCurrentTemplateXMLID(); } return this._super(...arguments); }, + /** + * @private + * @returns {string} xmlid of the current template. + */ + _getCurrentTemplateXMLID: function () { + const templateDefiningClass = this.containerEl.querySelector('section') + .classList.value.split(' ').filter(cl => cl.startsWith('s_mega_menu'))[0]; + return `website.${templateDefiningClass}`; + }, +}); + +/** + * Hides delete button for Mega Menu block. + */ +options.registry.MegaMenuNoDelete = options.Class.extend({ + forceNoDeleteButton: true, + + /** + * @override + */ + async onRemove() { + await new Promise(resolve => { + this.trigger_up('option_update', { + optionName: 'MegaMenuLayout', + name: 'reset_template', + data: { + onSuccess: () => resolve(), + } + }); + }); + }, }); return { diff --git a/addons/website/views/snippets/snippets.xml b/addons/website/views/snippets/snippets.xml index 600419a2624..9c6fe2bc6a2 100644 --- a/addons/website/views/snippets/snippets.xml +++ b/addons/website/views/snippets/snippets.xml @@ -1030,6 +1030,8 @@ +
+