-
Notifications
You must be signed in to change notification settings - Fork 116
dynamic snippet #3492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
dynamic snippet #3492
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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; | ||
|
@@ -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 | ||
/* | ||
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 => { | ||
|
||
if (carouselEl.dataset.bsInterval === "0") { | ||
delete carouselEl.dataset.bsRide; | ||
delete carouselEl.dataset.bsInterval; | ||
|
@@ -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); |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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));