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 7667291ca0b..509fa04089f 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -294,6 +294,27 @@ var SnippetOption = Widget.extend({ this.$target.addClass(value); } }, + /** + * Default option method which allows to select one and only one value in + * the option values set and set it on the associated snippet as a data + * attribute. The name of the data attribute is given by the closest + * ancestor's data value for 'attributeName'. + * + * @param {boolean} previewMode - @see this.selectClass + * @param {Object} value - the data attribute value to set + * @param {jQuery} $opt - the related DOMElement option + */ + selectDataAttribute: function (previewMode, value, $opt) { + const $ancestor = $opt && $opt.closest('[data-attribute-name]'); + if (!$ancestor || !$ancestor.length) { + return; + } + const dataName = $ancestor[0].dataset.attributeName; + if (!dataName) { + return; + } + this.$target[0].dataset[dataName] = value || ''; + }, /** * Default option method which allows to select one or multiple classes in * the option classes set and set it on the associated snippet. The common @@ -528,6 +549,23 @@ var SnippetOption = Widget.extend({ .addClass('active'); } + // --- SELECT DATA ATTRIBUTE --- + + this.$el.find('[data-select-data-attribute]') + .removeClass('active') + .filter((i, el) => { + const ancestorEl = el.closest('[data-attribute-name]'); + if (!ancestorEl) { + return false; + } + const dataName = ancestorEl.dataset.attributeName; + const defaultValue = ancestorEl.dataset.attributeDefaultValue; + const dataValue = el.dataset.selectDataAttribute; + const targetValue = this.$target[0].dataset[dataName] || defaultValue; + return (!targetValue && !dataValue) || targetValue === dataValue; + }) + .addClass('active'); + // --- SET DATA ATTRIBUTE --- (note: important to be done last because of active removal) this.el.querySelectorAll('[data-set-data-attribute]').forEach(el => { diff --git a/addons/website/static/src/js/editor/snippets.options.js b/addons/website/static/src/js/editor/snippets.options.js index d159c10ee16..2d5747e57e3 100644 --- a/addons/website/static/src/js/editor/snippets.options.js +++ b/addons/website/static/src/js/editor/snippets.options.js @@ -9,8 +9,28 @@ var options = require('web_editor.snippets.options'); var _t = core._t; var qweb = core.qweb; +// TODO should we refresh public widgets for all option changes by default ? options.Class.include({ + //-------------------------------------------------------------------------- + // Options + //-------------------------------------------------------------------------- + + /** + * @override + */ + setDataAttribute: function (previewMode, dataName, $opt) { + this._super(...arguments); + this._refreshPublicWidgets(); + }, + /** + * @override + */ + selectDataAttribute: function (previewMode, value, $opt) { + this._super(...arguments); + this._refreshPublicWidgets(); + }, + //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- @@ -690,33 +710,6 @@ options.registry.parallax = options.Class.extend({ onMove: function () { this._refreshPublicWidgets(); }, - - //-------------------------------------------------------------------------- - // Options - //-------------------------------------------------------------------------- - - /** - * Changes the scrolling speed of the parallax effect. - * - * @see this.selectClass for parameters - */ - scroll: function (previewMode, value) { - this.$target.attr('data-scroll-background-ratio', value); - this._refreshPublicWidgets(); - }, - - //-------------------------------------------------------------------------- - // Private - //-------------------------------------------------------------------------- - - /** - * @override - */ - _setActive: function () { - this._super.apply(this, arguments); - this.$el.find('[data-scroll]').removeClass('active') - .filter('[data-scroll="' + (this.$target.attr('data-scroll-background-ratio') || 0) + '"]').addClass('active'); - }, }); var FacebookPageDialog = weWidgets.Dialog.extend({ diff --git a/addons/website/views/snippets.xml b/addons/website/views/snippets.xml index 79fbc30e51b..c8c89c0bebe 100644 --- a/addons/website/views/snippets.xml +++ b/addons/website/views/snippets.xml @@ -1244,15 +1244,15 @@