Skip to content

Commit e5881c1

Browse files
committed
Add dynamic content snippet + 3
1 parent c6eaeae commit e5881c1

File tree

5 files changed

+55
-61
lines changed

5 files changed

+55
-61
lines changed

website_dynamic_snippet/__manifest__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
'category': 'Website',
55
'depends': ['website_sale'],
66
'data': [
7-
'views/snippets/snippet_templates.xml',
7+
'views/snippets/snippets.xml',
88
],
99
'assets': {
1010
'web.assets_frontend': [
11-
'/website_dynamic_snippet/static/src/xml/sale_order_snippets.xml',
12-
'/website_dynamic_snippet/static/src/js/dynamic_snippet_sale_orders.js',
13-
'/website_dynamic_snippet/static/src/js/layout_switcher.js',
11+
'website_dynamic_snippet/static/src/snippets/s_dynamic_sale_order_snippet/000.xml',
12+
'website_dynamic_snippet/static/src/snippets/s_dynamic_sale_order_snippet/000.js',
1413
],
1514
'website.assets_wysiwyg': [
1615
'website_dynamic_snippet/static/src/snippets/s_dynamic_sale_order_snippet/options.js',

website_dynamic_snippet/static/src/js/dynamic_snippet_sale_orders.js renamed to website_dynamic_snippet/static/src/snippets/s_dynamic_sale_order_snippet/000.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import publicWidget from "@web/legacy/js/public/public_widget";
44

55
publicWidget.registry.get_product_tab = publicWidget.Widget.extend({
66
selector: '.categories_section',
7-
7+
disabledInEditableMode: false,
88
events: {
99
'click .load-more-button': '_onLoadMoreClick',
1010
},
@@ -21,33 +21,45 @@ publicWidget.registry.get_product_tab = publicWidget.Widget.extend({
2121
},
2222

2323
async loadCategories() {
24+
if(this.$target[0].dataset.setLayout === 'list'){
25+
this.layout = 'list';
26+
} else {
27+
this.layout = 'grid';
28+
this.limit = 9;
29+
}
30+
const domain = this.$target[0].dataset.confirmOrderOnly === 'true' ? [['state', '=', 'sale']] : [];
2431
const result = await this.orm.searchRead(
2532
'sale.order',
26-
[],
33+
domain,
2734
['id', 'name', 'partner_id', 'amount_total', 'state'],
2835
{
2936
offset: this.offset,
3037
limit: this.limit,
3138
order: 'id ASC',
3239
}
3340
);
34-
3541
if (result && result.length) {
3642
const content = await renderToElement(
3743
'website_dynamic_snippet.sale_order_snippet',
38-
{ result: result }
44+
{ result: result, layout: this.layout },
3945
);
4046

47+
const newTableRows = content.querySelectorAll('.list_sale_order > tbody > tr');
48+
const existingTableBody = this.el.querySelector('.list_sale_order > tbody');
49+
50+
const newCardCols = content.querySelectorAll('.card_sale_order > .col-md-4');
51+
const existingCardWrapper = this.el.querySelector('.card_sale_order');
52+
4153
if (this.offset === 0) {
4254
this.el.innerHTML = '';
4355
this.el.appendChild(content);
4456
} else {
45-
const newRows = content.querySelectorAll('tbody > tr');
46-
const existingTbody = this.el.querySelector('tbody');
47-
if (existingTbody) {
48-
newRows.forEach(row => {
49-
existingTbody.appendChild(row);
50-
});
57+
if (existingTableBody && newTableRows.length) {
58+
newTableRows.forEach(row => existingTableBody.appendChild(row));
59+
}
60+
61+
if (existingCardWrapper && newCardCols.length) {
62+
newCardCols.forEach(card => existingCardWrapper.appendChild(card));
5163
}
5264
}
5365

@@ -60,6 +72,7 @@ publicWidget.registry.get_product_tab = publicWidget.Widget.extend({
6072
}
6173
},
6274

75+
6376
async _onLoadMoreClick(ev) {
6477
ev.preventDefault();
6578
await this.loadCategories();

website_dynamic_snippet/static/src/xml/sale_order_snippets.xml renamed to website_dynamic_snippet/static/src/snippets/s_dynamic_sale_order_snippet/000.xml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<h3 class="section_heading">Sale Order Details</h3>
66
<div class="card_list_wrapper">
77
<t t-if="layout == 'list'">
8-
<!-- LIST/TABLE VIEW -->
9-
<table class="table">
8+
<table class="table list_sale_order">
109
<thead>
1110
<tr>
1211
<th scope="col">Number</th>
@@ -26,8 +25,7 @@
2625
</table>
2726
</t>
2827
<t t-else="">
29-
<!-- GRID/CARD VIEW -->
30-
<div class="row">
28+
<div class="row card_sale_order">
3129
<t t-foreach="result" t-as="sale_order" t-key="sale_order.id">
3230
<div class="col-md-4 mb-4">
3331
<div class="card h-100">
@@ -41,13 +39,12 @@
4139
</t>
4240
</div>
4341
</t>
44-
4542
<div class="text-center mt-3">
4643
<button type="button" class="btn btn-primary load-more-button">
4744
Load More
4845
</button>
4946
</div>
5047
</div>
5148
</div>
52-
</t>
53-
</templates>
49+
</t>
50+
</templates>
Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
1-
/** @odoo-module **/
1+
import options from "@web_editor/js/editor/snippets.options";
22

3-
import publicWidget from "@web_editor/js/public/widget";
4-
5-
publicWidget.registry.dynamic_snippet_sale_order = publicWidget.Widget.extend({
6-
selector: ".categories_section",
7-
8-
xmlDependencies: ["/website_dynamic_snippet/static/src/xml/sale_order_snippet.xml"],
9-
10-
start: function () {
11-
this.layout = this.el.dataset.layout || 'list';
12-
this._render();
13-
return this._super(...arguments);
3+
options.registry.DynamicSnippetOptions = options.Class.extend({
4+
confirmOrderOnly(previewMode, widgetValue) {
5+
this.$target[0].dataset.confirmOrderOnly = widgetValue;
146
},
157

16-
_render: function () {
17-
this._rpc({
18-
route: '/your/data/route',
19-
params: { /* your logic */ },
20-
}).then(data => {
21-
this.$el.find(".card_list_wrapper").html(
22-
qweb.render("website_dynamic_snippet.sale_order_snippet", {
23-
result: data.orders,
24-
layout: this.layout,
25-
})
26-
);
27-
});
8+
setLayout(previewMode, widgetValue) {
9+
this.$target[0].setAttribute("data-set-layout", widgetValue);
2810
},
2911

30-
onChangeOption(previewMode, widgetValue, params) {
31-
if (['grid', 'list'].includes(widgetValue)) {
32-
this.layout = widgetValue;
33-
this._render();
12+
//--------------------------------------------------------------------------
13+
// Private
14+
//--------------------------------------------------------------------------
15+
16+
/**
17+
* @override
18+
*/
19+
_computeWidgetState(methodName, params) {
20+
if (methodName === "confirmOrderOnly") {
21+
return this.$target[0].dataset.confirmOrderOnly;
22+
}
23+
if (methodName === "setLayout") {
24+
return this.$target[0].getAttribute("data-set-layout");
3425
}
26+
return this._super(...arguments);
3527
},
3628
});

website_dynamic_snippet/views/snippets/snippet_templates.xml renamed to website_dynamic_snippet/views/snippets/snippets.xml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,14 @@
2222

2323
<template id="s_dynamic_snippet_controller_layout" inherit_id="website.snippet_options">
2424
<xpath expr="." position="inside">
25-
<t t-call="website.s_dynamic_snippet_options_template">
26-
<t t-set="snippet_name" t-value="'dynamic_snippet_sale_order'"/>
27-
<t t-set="snippet_selector" t-value="'.categories_section'"/>
28-
</t>
29-
</xpath>
30-
</template>
31-
32-
<template id="s_dynamic_snippet_sale_order_template_options" inherit_id="website.s_dynamic_snippet_options_template">
33-
<xpath expr="//we-select[@data-name='filter_opt']" position="after">
34-
<t t-if="snippet_name == 'dynamic_snippet_sale_order'">
35-
<we-select data-name="default_listing_layout" string="View" data-no-preview="true" data-reload="/">
25+
<div data-js="DynamicSnippetOptions" data-selector=".categories_section">
26+
<we-select data-name="snippet_data_view" string="View" data-no-preview="true" data-reload="/">
3627
<we-button data-set-layout="grid" data-name="grid_view_opt">Grid</we-button>
3728
<we-button data-set-layout="list" data-name="list_view_opt">List</we-button>
3829
</we-select>
39-
</t>
30+
<we-checkbox data-confirm-order-only="true" class="btn btn-primary" data-no-preview="true">Show Confirm Orders</we-checkbox>
31+
</div>
4032
</xpath>
4133
</template>
34+
4235
</odoo>

0 commit comments

Comments
 (0)