1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
odoo.define('website.s_dynamic_snippet_options', function (require) {
'use strict';
const options = require('web_editor.snippets.options');
const dynamicSnippetOptions = options.Class.extend({
/**
*
* @override
*/
init: function () {
this._super.apply(this, arguments);
this.dynamicFilters = {};
this.dynamicFilterTemplates = {};
},
/**
*
* @override
*/
onBuilt: function () {
this._setOptionsDefaultValues();
},
//--------------------------------------------------------------------------
// Options
//--------------------------------------------------------------------------
/**
*
* @see this.selectClass for parameters
*/
selectDataAttribute: function (previewMode, widgetValue, params) {
this._super.apply(this, arguments);
if (params.attributeName === 'filterId' && previewMode === false) {
this.$target.get(0).dataset.numberOfRecords = this.dynamicFilters[parseInt(widgetValue)].limit;
}
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Fetches dynamic filters.
* @private
* @returns {Promise}
*/
_fetchDynamicFilters: function () {
return this._rpc({route: '/website/snippet/options_filters'});
},
/**
* Fetch dynamic filters templates.
* @private
* @returns {Promise}
*/
_fetchDynamicFilterTemplates: function () {
return this._rpc({route: '/website/snippet/filter_templates'});
},
/**
*
* @override
* @private
*/
_renderCustomXML: async function (uiFragment) {
await this._renderDynamicFiltersSelector(uiFragment);
await this._renderDynamicFilterTemplatesSelector(uiFragment);
},
/**
* Renders the dynamic filter option selector content into the provided uiFragment.
* @param {HTMLElement} uiFragment
* @private
*/
_renderDynamicFiltersSelector: async function (uiFragment) {
const dynamicFilters = await this._fetchDynamicFilters();
for (let index in dynamicFilters) {
this.dynamicFilters[dynamicFilters[index].id] = dynamicFilters[index];
}
const filtersSelectorEl = uiFragment.querySelector('[data-name="filter_opt"]');
return this._renderSelectUserValueWidgetButtons(filtersSelectorEl, this.dynamicFilters);
},
/**
* Renders we-buttons into a SelectUserValueWidget element according to provided data.
* @param {HTMLElement} selectUserValueWidgetElement the SelectUserValueWidget buttons
* have to be created into.
* @param {JSON} data
* @private
*/
_renderSelectUserValueWidgetButtons: async function (selectUserValueWidgetElement, data) {
for (let id in data) {
const button = document.createElement('we-button');
button.dataset.selectDataAttribute = id;
button.innerHTML = data[id].name;
selectUserValueWidgetElement.appendChild(button);
}
},
/**
* Renders the template option selector content into the provided uiFragment.
* @param {HTMLElement} uiFragment
* @private
*/
_renderDynamicFilterTemplatesSelector: async function (uiFragment) {
const dynamicFilterTemplates = await this._fetchDynamicFilterTemplates();
for (let index in dynamicFilterTemplates) {
this.dynamicFilterTemplates[dynamicFilterTemplates[index].key] = dynamicFilterTemplates[index];
}
const templatesSelectorEl = uiFragment.querySelector('[data-name="template_opt"]');
return this._renderSelectUserValueWidgetButtons(templatesSelectorEl, this.dynamicFilterTemplates);
},
/**
* Sets default options values.
* Method to be overridden in child components in order to set additional
* options default values.
* @private
*/
_setOptionsDefaultValues: function () {
this._setOptionValue('numberOfElements', 4);
this._setOptionValue('numberOfElementsSmallDevices', 1);
},
/**
* Sets the option value.
* @param optionName
* @param value
* @private
*/
_setOptionValue: function (optionName, value) {
if (this.$target.get(0).dataset[optionName] === undefined) {
this.$target.get(0).dataset[optionName] = value;
}
},
});
options.registry.dynamic_snippet = dynamicSnippetOptions;
return dynamicSnippetOptions;
});
|