From d3307aaa0834280a324a33e63c785826e46e8d5e Mon Sep 17 00:00:00 2001 From: "Arthur Detroux (ard)" Date: Fri, 19 Nov 2021 16:32:01 +0000 Subject: [PATCH] [IMP] website, *: give flexibility to dynamic snippet templates *: website_blog, website_event Commit [1] introduced new templates. This commit's goal is to build on that template system to provide more flexibility for designers. It adds new data-attributes to set on the first node of a template. - data-row-per-slide: Allows for the template to tell a dynamic snippet carousel how many rows of cards it should display. - data-arrow-position: Allows for the template to tell a dynamic snippet carousel where the arrows should be positioned. (for now just bottom, or side) - data-extra-classes: give the template the possibility to add extra classes to the dynamic snippet instead of just the card. This commit also removes options to select the amount of elements displayed on each row. This is now only changeable through templates, or by directly editing the snippet's DOM through debug tools. This was done to reduce the amount of options visible, so that the user just chooses a template and it is ready to go. [1]: https://github.com/odoo/odoo/commit/49bd79b0bdca76415780bdfa199195371c2692e5 task-2677203 Part-of: odoo/odoo#80128 --- addons/website/controllers/main.py | 3 + .../src/snippets/s_dynamic_snippet/000.js | 33 ++++++---- .../src/snippets/s_dynamic_snippet/000.xml | 2 +- .../src/snippets/s_dynamic_snippet/options.js | 43 +++---------- .../s_dynamic_snippet_carousel/000.js | 3 + .../s_dynamic_snippet_carousel/000.scss | 5 ++ .../s_dynamic_snippet_carousel/000.xml | 64 +++++++++++++------ .../s_dynamic_snippet_carousel/options.js | 23 ++++++- .../views/snippets/s_dynamic_snippet.xml | 14 ---- .../snippets/s_dynamic_snippet_carousel.xml | 1 - .../src/snippets/s_blog_posts/options.js | 1 - .../views/snippets/s_blog_posts.xml | 8 +-- .../website_event/views/snippets/s_events.xml | 1 - 13 files changed, 110 insertions(+), 91 deletions(-) diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index fee5216974a..060c603ab1c 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -398,6 +398,9 @@ class Website(Home): t['numOfEl'] = attribs.get('data-number-of-elements') t['numOfElSm'] = attribs.get('data-number-of-elements-sm') t['numOfElFetch'] = attribs.get('data-number-of-elements-fetch') + t['rowPerSlide'] = attribs.get('data-row-per-slide') + t['arrowPosition'] = attribs.get('data-arrow-position') + t['extraClasses'] = attribs.get('data-extra-classes') return templates @http.route('/website/get_current_currency', type='json', auth="public", website=True) diff --git a/addons/website/static/src/snippets/s_dynamic_snippet/000.js b/addons/website/static/src/snippets/s_dynamic_snippet/000.js index 4424ba0c078..3213d9da99a 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet/000.js +++ b/addons/website/static/src/snippets/s_dynamic_snippet/000.js @@ -5,6 +5,8 @@ const core = require('web.core'); const config = require('web.config'); const publicWidget = require('web.public.widget'); const {Markup} = require('web.utils'); +const DEFAULT_NUMBER_OF_ELEMENTS = 4; +const DEFAULT_NUMBER_OF_ELEMENTS_SM = 1; const DynamicSnippet = publicWidget.Widget.extend({ selector: '.s_dynamic_snippet', @@ -134,28 +136,31 @@ const DynamicSnippet = publicWidget.Widget.extend({ * @private */ _prepareContent: function () { - if (this.$target[0].dataset.numberOfElements && this.$target[0].dataset.numberOfElementsSmallDevices) { - this.renderedContent = core.qweb.render( - this.template_key, - this._getQWebRenderOptions()); - } else { - this.renderedContent = ''; - } + this.renderedContent = core.qweb.render( + this.template_key, + this._getQWebRenderOptions() + ); }, /** * Method to be overridden in child components in order to prepare QWeb * options. * @private */ - _getQWebRenderOptions: function () { + _getQWebRenderOptions: function () { + const dataset = this.$target[0].dataset; + const numberOfRecords = parseInt(dataset.numberOfRecords); + let numberOfElements; + if (config.device.isMobile) { + numberOfElements = parseInt(dataset.numberOfElementsSmallDevices) || DEFAULT_NUMBER_OF_ELEMENTS_SM; + } else { + numberOfElements = parseInt(dataset.numberOfElements) || DEFAULT_NUMBER_OF_ELEMENTS; + } + const chunkSize = numberOfRecords < numberOfElements ? numberOfRecords : numberOfElements; return { - chunkSize: parseInt( - config.device.isMobile - ? this.$target[0].dataset.numberOfElementsSmallDevices - : this.$target[0].dataset.numberOfElements - ), + chunkSize: chunkSize, data: this.data, - uniqueId: this.uniqueId + uniqueId: this.uniqueId, + extraClasses: dataset.extraClasses || '', }; }, /** diff --git a/addons/website/static/src/snippets/s_dynamic_snippet/000.xml b/addons/website/static/src/snippets/s_dynamic_snippet/000.xml index 68153dd3453..2498e6c1a85 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet/000.xml +++ b/addons/website/static/src/snippets/s_dynamic_snippet/000.xml @@ -6,7 +6,7 @@ -
+
diff --git a/addons/website/static/src/snippets/s_dynamic_snippet/options.js b/addons/website/static/src/snippets/s_dynamic_snippet/options.js index a0ae018dc21..0540aad9d4e 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet/options.js +++ b/addons/website/static/src/snippets/s_dynamic_snippet/options.js @@ -47,18 +47,6 @@ const dynamicSnippetOptions = options.Class.extend({ if (params.attributeName === 'templateKey' && previewMode === false) { this._templateUpdated(widgetValue, params.activeValue); } - if (params.attributeName === 'numberOfRecords' && previewMode === false) { - const dataSet = this.$target.get(0).dataset; - const numberOfElements = parseInt(dataSet.numberOfElements); - const numberOfRecords = parseInt(dataSet.numberOfRecords); - const numberOfElementsSmallDevices = parseInt(dataSet.numberOfElementsSmallDevices); - if (numberOfElements > numberOfRecords) { - dataSet.numberOfElements = numberOfRecords; - } - if (numberOfElementsSmallDevices > numberOfRecords) { - dataSet.numberOfElementsSmallDevices = numberOfRecords; - } - } }, //-------------------------------------------------------------------------- @@ -79,16 +67,6 @@ const dynamicSnippetOptions = options.Class.extend({ await this._super(...arguments); }, - /** - * @override - */ - async updateUIVisibility() { - await this._super(...arguments); - const template = this._getCurrentTemplate(); - const groupingMessage = this.el.querySelector('.o_grouping_message'); - groupingMessage.classList.toggle('d-none', template && !!template.numOfEl && !!template.numOfElSm); - }, - //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- @@ -112,16 +90,6 @@ const dynamicSnippetOptions = options.Class.extend({ return Object.keys(this.dynamicFilters).length !== 1; } - if (widgetName === 'number_of_elements_opt') { - const template = this._getCurrentTemplate(); - return template && !template.numOfEl; - } - - if (widgetName === 'number_of_elements_small_devices_opt') { - const template = this._getCurrentTemplate(); - return template && !template.numOfElSm; - } - if (widgetName === 'number_of_records_opt') { const template = this._getCurrentTemplate(); return template && !template.numOfElFetch; @@ -249,8 +217,6 @@ const dynamicSnippetOptions = options.Class.extend({ // numberOfElements or numberOfElementsSmallDevices) might throw an // exception by not finding the attribute on the element. this.options.wysiwyg.odooEditor.observerUnactive(); - this._setOptionValue('numberOfElements', 4); - this._setOptionValue('numberOfElementsSmallDevices', 1); const filterKeys = this.$el.find("we-select[data-attribute-name='filterId'] we-selection-items we-button"); if (filterKeys.length > 0) { this._setOptionValue('numberOfRecords', this.dynamicFilters[Object.keys(this.dynamicFilters)[0]].limit); @@ -285,13 +251,22 @@ const dynamicSnippetOptions = options.Class.extend({ const template = this.dynamicFilterTemplates[newTemplate]; if (template.numOfEl) { this.$target[0].dataset.numberOfElements = template.numOfEl; + } else { + delete this.$target[0].dataset.numberOfElements; } if (template.numOfElSm) { this.$target[0].dataset.numberOfElementsSmallDevices = template.numOfElSm; + } else { + delete this.$target[0].dataset.numberOfElementsSmallDevices; } if (template.numOfElFetch) { this.$target[0].dataset.numberOfRecords = template.numOfElFetch; } + if (template.extraClasses) { + this.$target[0].dataset.extraClasses = template.extraClasses; + } else { + delete this.$target[0].dataset.extraClasses; + } }, /** * Sets the option value. diff --git a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.js b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.js index adae0ab86bd..0948f4acbaf 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.js +++ b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.js @@ -3,6 +3,7 @@ odoo.define('website.s_dynamic_snippet_carousel', function (require) { const publicWidget = require('web.public.widget'); const DynamicSnippet = require('website.s_dynamic_snippet'); +const config = require('web.config'); const DynamicSnippetCarousel = DynamicSnippet.extend({ selector: '.s_dynamic_snippet_carousel', @@ -26,6 +27,8 @@ const DynamicSnippetCarousel = DynamicSnippet.extend({ this._super.apply(this, arguments), { interval: parseInt(this.$target[0].dataset.carouselInterval), + rowPerSlide: parseInt(config.device.isMobile ? 1 : this.$target[0].dataset.rowPerSlide || 1), + arrowPosition: this.$target[0].dataset.arrowPosition || '', }, ); }, diff --git a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.scss b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.scss index 16b2c2ba523..2e2756de413 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.scss +++ b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.scss @@ -18,4 +18,9 @@ text-overflow: ellipsis; overflow: hidden; } + .s_dynamic_snippet_arrow_bottom { + > .carousel-control-prev, .carousel-control-next { + position: relative; + } + } } diff --git a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml index 9cafb768cf8..6968ced6539 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml +++ b/addons/website/static/src/snippets/s_dynamic_snippet_carousel/000.xml @@ -1,36 +1,60 @@ + + + + Previous + + + + Next + + @@ -47,7 +34,6 @@ - Items per row diff --git a/addons/website/views/snippets/s_dynamic_snippet_carousel.xml b/addons/website/views/snippets/s_dynamic_snippet_carousel.xml index b7042e3cebe..289d807a1f8 100644 --- a/addons/website/views/snippets/s_dynamic_snippet_carousel.xml +++ b/addons/website/views/snippets/s_dynamic_snippet_carousel.xml @@ -9,7 +9,6 @@ - Items per slide diff --git a/addons/website_blog/static/src/snippets/s_blog_posts/options.js b/addons/website_blog/static/src/snippets/s_blog_posts/options.js index 00c82c8fd79..41e5c5c9e95 100644 --- a/addons/website_blog/static/src/snippets/s_blog_posts/options.js +++ b/addons/website_blog/static/src/snippets/s_blog_posts/options.js @@ -78,7 +78,6 @@ const dynamicSnippetBlogPostsOptions = dynamicSnippetOptions.extend({ * @private */ _setOptionsDefaultValues: function () { - this._setOptionValue('numberOfElements', 3); this._setOptionValue('filterByBlogId', -1); this._super.apply(this, arguments); }, diff --git a/addons/website_blog/views/snippets/s_blog_posts.xml b/addons/website_blog/views/snippets/s_blog_posts.xml index d3c58e7e742..d6364c9d926 100644 --- a/addons/website_blog/views/snippets/s_blog_posts.xml +++ b/addons/website_blog/views/snippets/s_blog_posts.xml @@ -11,7 +11,7 @@