Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions addons/website/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@
'web.assets_unit_tests_setup': [
'website/static/src/core/**/*',
'website/static/src/interactions/**/*',
'website/static/src/snippets/s_dynamic_snippet/000.js',
'website/static/src/snippets/s_dynamic_snippet/000.xml',
'website/static/src/snippets/s_table_of_content/000.js',
'website/static/src/snippets/s_table_of_content/000.scss',

Expand Down
247 changes: 106 additions & 141 deletions addons/website/static/src/snippets/s_dynamic_snippet/000.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import publicWidget from "@web/legacy/js/public/public_widget";
import { registry } from "@web/core/registry";
import { Interaction } from "@website/core/interaction";
import { rpc } from "@web/core/network/rpc";
import { uniqueId } from "@web/core/utils/functions";
import { renderToString } from "@web/core/utils/render";
import { renderToElement } from "@web/core/utils/render";
import { listenSizeChange, utils as uiUtils } from "@web/core/ui/ui_service";

import { markup } from "@odoo/owl";

const DEFAULT_NUMBER_OF_ELEMENTS = 4;
const DEFAULT_NUMBER_OF_ELEMENTS_SM = 1;

const DynamicSnippet = publicWidget.Widget.extend({
selector: '.s_dynamic_snippet',
read_events: {
'click [data-url]': '_onCallToAction',
},
disabledInEditableMode: false,
export class DynamicSnippet extends Interaction {
static selector = ".s_dynamic_snippet";
static dynamicContent = {
"[data-url]": {
"t-on-click": "callToAction", // TODO Disable in edit mode.
},
};
// TODO Support edit-mode enabled.
static disabledInEditableMode = false;

/**
*
* @override
*/
init: function () {
this._super.apply(this, arguments);
setup() {
/**
* The dynamic filter data source data formatted with the chosen template.
* Can be accessed when overriding the _render_content() function in order to generate
Expand All @@ -30,127 +29,100 @@ const DynamicSnippet = publicWidget.Widget.extend({
* @type {*|jQuery.fn.init|jQuery|HTMLElement}
*/
this.data = [];
this.renderedContent = '';
this.renderedContentEl = document.createTextNode("");
this.isDesplayedAsMobile = uiUtils.isSmall();
this.unique_id = uniqueId("s_dynamic_snippet_");
this.template_key = 'website.s_dynamic_snippet.grid';
},
/**
*
* @override
*/
willStart: function () {
return this._super.apply(this, arguments).then(
() => Promise.all([
this._fetchData(),
])
);
},
/**
*
* @override
*/
start: function () {
return this._super.apply(this, arguments)
.then(() => {
this._setupSizeChangedManagement(true);
this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerUnactive();
this._render();
this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerActive();
});
},
/**
*
* @override
*/
destroy: function () {
this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerUnactive();
this._toggleVisibility(false);
this._setupSizeChangedManagement(false);
this._clearContent();
this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerActive();
this._super.apply(this, arguments);
},

//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------

/**
* @private
*/
_clearContent: function () {
const $templateArea = this.$el.find('.dynamic_snippet_template');
this.uniqueId = uniqueId("s_dynamic_snippet_");
this.templateKey = "website.s_dynamic_snippet.grid";
}
async willStart() {
return this.fetchData();
}
start() {
this.setupSizeChangedManagement(true);
// TODO Editor behavior.
// this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerUnactive();
this.render();
// TODO Editor behavior.
// this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerActive();
}
destroy() {
// TODO Editor behavior.
// this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerUnactive();
this.toggleVisibility(false);
this.setupSizeChangedManagement(false);
this.clearContent();
// TODO Editor behavior.
// this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerActive();
}
clearContent() {
const templateAreaEl = this.el.querySelector(".dynamic_snippet_template");
/*
this.stopInteraction(templateAreaEl); // TODO Support something like that.
this.trigger_up('widgets_stop_request', {
$target: $templateArea,
target: templateAreaEl,
});
$templateArea.html('');
},
*/
templateAreaEl.replaceChildren();
}
/**
* Method to be overridden in child components if additional configuration elements
* are required in order to fetch data.
* @private
*/
_isConfigComplete: function () {
return this.$el.get(0).dataset.filterId !== undefined && this.$el.get(0).dataset.templateKey !== undefined;
},
isConfigComplete() {
return this.el.dataset.filterId !== undefined && this.el.dataset.templateKey !== undefined;
}
/**
* Method to be overridden in child components in order to provide a search
* domain if needed.
* @private
*/
_getSearchDomain: function () {
getSearchDomain() {
return [];
},
}
/**
* Method to be overridden in child components in order to add custom parameters if needed.
* @private
*/
_getRpcParameters: function () {
getRpcParameters() {
return {};
},
}
/**
* Fetches the data.
* @private
*/
async _fetchData() {
if (this._isConfigComplete()) {
async fetchData() {
if (this.isConfigComplete()) {
const nodeData = this.el.dataset;
const filterFragments = await rpc(
'/website/snippet/filters',
Object.assign({
'filter_id': parseInt(nodeData.filterId),
'template_key': nodeData.templateKey,
'limit': parseInt(nodeData.numberOfRecords),
'search_domain': this._getSearchDomain(),
'search_domain': this.getSearchDomain(),
'with_sample': this.editableMode,
},
this._getRpcParameters(),
this.getRpcParameters(),
JSON.parse(this.el.dataset?.customTemplateData || "{}")
)
);
this.data = filterFragments.map(markup);
} else {
this.data = [];
}
},
}
/**
* Method to be overridden in child components in order to prepare content
* before rendering.
* @private
*/
_prepareContent: function () {
this.renderedContent = renderToString(
this.template_key,
this._getQWebRenderOptions()
prepareContent() {
this.renderedContentEl = renderToElement(
this.templateKey,
this.getQWebRenderOptions()
);
},
}
/**
* Method to be overridden in child components in order to prepare QWeb
* options.
* @private
*/
_getQWebRenderOptions: function () {
getQWebRenderOptions() {
const dataset = this.el.dataset;
const numberOfRecords = parseInt(dataset.numberOfRecords);
let numberOfElements;
Expand All @@ -163,60 +135,62 @@ const DynamicSnippet = publicWidget.Widget.extend({
return {
chunkSize: chunkSize,
data: this.data,
unique_id: this.unique_id,
unique_id: this.uniqueId,
extraClasses: dataset.extraClasses || '',
columnClasses: dataset.columnClasses || '',
};
},
/**
*
* @private
*/
_render: function () {
}
render() {
if (this.data.length > 0 || this.editableMode) {
this.$el.removeClass('o_dynamic_snippet_empty');
this._prepareContent();
this.el.classList.remove("o_dynamic_snippet_empty");
this.prepareContent();
} else {
this.$el.addClass('o_dynamic_snippet_empty');
this.renderedContent = '';
this.el.classList.add("o_dynamic_snippet_empty");
this.renderedContentEl = document.createTextNode("");
}
this._renderContent();
this.renderContent();
// TODO Support something like that
/*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.registerCleanup(() => this.services.websiteCore.startInteraction(child));

this.trigger_up('widgets_start_request', {
$target: this.$el.children(),
target: this.el.children(),
options: {parent: this},
editableMode: this.editableMode,
});
},
/**
* @private
*/
_renderContent: function () {
const $templateArea = this.$el.find('.dynamic_snippet_template');
*/
}
renderContent() {
const templateAreaEl = this.el.querySelector(".dynamic_snippet_template");
// TODO Support something like that
/*
this.trigger_up('widgets_stop_request', {
$target: $templateArea,
target: templateAreaEl,
});
const mainPageUrl = this._getMainPageUrl();
*/
const mainPageUrl = this.getMainPageUrl();
const allContentLink = this.el.querySelector(".s_dynamic_snippet_main_page_url");
if (allContentLink && mainPageUrl) {
allContentLink.href = mainPageUrl;
allContentLink.classList.remove("d-none");
}
$templateArea.html(this.renderedContent);
templateAreaEl.replaceChildren(this.renderedContentEl);
// TODO this is probably not the only public widget which creates DOM
// which should be attached to another public widget. Maybe a generic
// method could be added to properly do this operation of DOM addition.
// TODO Support something like that
/*
this.trigger_up('widgets_start_request', {
$target: $templateArea,
target: templateAreaEl,
editableMode: this.editableMode,
});
*/
// Same as above and probably should be done automatically for any
// bootstrap behavior (apparently needed since BS 5.3): start potential
// carousel in new content (according to their data-bs-ride and other
// dataset attributes). Note: done here and not in dynamic carousel
// extension, because: why not?
// (TODO review + See interaction with "slider" public widget).
setTimeout(() => {
$templateArea[0].querySelectorAll('.carousel').forEach(carouselEl => {
templateAreaEl.querySelectorAll('.carousel').forEach(carouselEl => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (this.isDestroyed) {
  return;
}

if (carouselEl.dataset.bsInterval === "0") {
delete carouselEl.dataset.bsRide;
delete carouselEl.dataset.bsInterval;
Expand All @@ -227,61 +201,52 @@ const DynamicSnippet = publicWidget.Widget.extend({
}
});
}, 0);
},
}
/**
*
* @param {Boolean} enable
* @private
*/
_setupSizeChangedManagement: function (enable) {
setupSizeChangedManagement(enable) {
if (enable === true) {
this.removeSizeListener = listenSizeChange(this._onSizeChanged.bind(this));
this.removeSizeListener = listenSizeChange(this.sizeChanged.bind(this));
} else if (this.removeSizeListener) {
this.removeSizeListener();
delete this.removeSizeListener;
}
},
}
/**
*
* @param visible
* @private
*/
_toggleVisibility: function (visible) {
this.$el.toggleClass('o_dynamic_snippet_empty', !visible);
},
toggleVisibility(visible) {
this.el.classList.toggle("o_dynamic_snippet_empty", !visible);
}
/**
* Returns the main URL of the module related to the active filter.
*
* @private
*/
_getMainPageUrl() {
getMainPageUrl() {
return '';
},
}

//------------------------------------- -------------------------------------
// Handlers
//--------------------------------------------------------------------------

/**
* Navigates to the call to action url.
* @private
*/
_onCallToAction: function (ev) {
window.location = $(ev.currentTarget).attr('data-url');
},
callToAction(ev) {
window.location = ev.currentTarget.dataset.url;
}
/**
* Called when the size has reached a new bootstrap breakpoint.
*
* @private
*/
_onSizeChanged: function () {
sizeChanged() {
if (this.isDesplayedAsMobile !== uiUtils.isSmall()) {
this.isDesplayedAsMobile = uiUtils.isSmall();
this._render();
this.render();
}
},
});

publicWidget.registry.dynamic_snippet = DynamicSnippet;
}
}

export default DynamicSnippet;
registry.category("website.active_elements").add("website.dynamic_snippet", DynamicSnippet);
Loading