1
1
/** @odoo -module **/
2
2
3
3
import { renderToElement } from "@web/core/utils/render" ;
4
- import options from '@web_editor/js/editor/snippets.options.legacy' ;
5
4
import { _t } from "@web/core/l10n/translation" ;
5
+ import { SnippetOption } from "@web_editor/js/editor/snippets.options" ;
6
+ import { registerWebsiteOption } from "@website/js/editor/snippets.registry" ;
6
7
7
- options . registry . Donation = options . Class . extend ( {
8
+ export class Donation extends SnippetOption {
8
9
/**
9
10
* @override
10
11
*/
11
- start ( ) {
12
+ constructor ( ) {
13
+ super ( ...arguments ) ;
12
14
this . defaultDescription = _t ( "Add a description here" ) ;
13
- return this . _super ( ...arguments ) ;
14
- } ,
15
+ this . descriptions = [ ] ;
16
+ if ( this . $target [ 0 ] . dataset . descriptions ) {
17
+ const descriptionEls = this . $target [ 0 ] . querySelectorAll ( '#s_donation_description_inputs > input' ) ;
18
+ for ( const descriptionEl of descriptionEls ) {
19
+ this . descriptions . push ( descriptionEl . value ) ;
20
+ }
21
+ }
22
+ }
23
+ /**
24
+ * @override
25
+ */
26
+ async willStart ( ) {
27
+ await super . willStart ( ...arguments ) ;
28
+ this . renderContext . showOptionDescriptions = this . $target [ 0 ] . dataset . descriptions ;
29
+ }
15
30
/**
16
31
* @override
17
32
*/
18
33
onBuilt ( ) {
19
34
this . _rebuildPrefilledOptions ( ) ;
20
- return this . _super ( ...arguments ) ;
21
- } ,
35
+ return super . onBuilt ( ...arguments ) ;
36
+ }
22
37
/**
23
38
* @override
24
39
*/
25
40
cleanForSave ( ) {
26
41
if ( ! this . $target [ 0 ] . dataset . descriptions ) {
27
42
this . _updateDescriptions ( ) ;
28
43
}
29
- } ,
44
+ }
30
45
31
46
//--------------------------------------------------------------------------
32
47
// Public
@@ -36,9 +51,9 @@ options.registry.Donation = options.Class.extend({
36
51
* @override
37
52
*/
38
53
async updateUI ( ) {
39
- await this . _super ( ...arguments ) ;
40
- this . _buildDescriptionsList ( ) ;
41
- } ,
54
+ await super . updateUI ( ...arguments ) ;
55
+ this . _updateDescriptions ( ) ;
56
+ }
42
57
43
58
//--------------------------------------------------------------------------
44
59
// Options
@@ -57,29 +72,29 @@ options.registry.Donation = options.Class.extend({
57
72
this . $target [ 0 ] . dataset . customAmount = "slider" ;
58
73
}
59
74
this . _rebuildPrefilledOptions ( ) ;
60
- } ,
75
+ }
61
76
/**
62
77
* Add/remove prefilled buttons.
63
78
*
64
79
* @see this.selectClass for parameters
65
80
*/
66
81
togglePrefilledOptions ( previewMode , widgetValue , params ) {
67
82
this . $target [ 0 ] . dataset . prefilledOptions = widgetValue ;
68
- this . $el . find ( '.o_we_prefilled_options_list' ) . toggleClass ( 'd-none' , ! widgetValue ) ;
69
83
if ( ! widgetValue && this . $target [ 0 ] . dataset . displayOptions ) {
70
84
this . $target [ 0 ] . dataset . customAmount = "slider" ;
71
85
}
72
86
this . _rebuildPrefilledOptions ( ) ;
73
- } ,
87
+ }
74
88
/**
75
89
* Add/remove description of prefilled buttons.
76
90
*
77
91
* @see this.selectClass for parameters
78
92
*/
79
93
toggleOptionDescription ( previewMode , widgetValue , params ) {
80
94
this . $target [ 0 ] . dataset . descriptions = widgetValue ;
95
+ this . renderContext . showOptionDescriptions = widgetValue ;
81
96
this . renderListItems ( false , this . _buildPrefilledOptionsList ( ) ) ;
82
- } ,
97
+ }
83
98
/**
84
99
* Select an amount input
85
100
*
@@ -88,7 +103,7 @@ options.registry.Donation = options.Class.extend({
88
103
selectAmountInput ( previewMode , widgetValue , params ) {
89
104
this . $target [ 0 ] . dataset . customAmount = widgetValue ;
90
105
this . _rebuildPrefilledOptions ( ) ;
91
- } ,
106
+ }
92
107
/**
93
108
* Apply the we-list on the target and rebuild the input(s)
94
109
*
@@ -97,13 +112,17 @@ options.registry.Donation = options.Class.extend({
97
112
renderListItems ( previewMode , value , params ) {
98
113
const valueList = JSON . parse ( value ) ;
99
114
const donationAmounts = [ ] ;
115
+ this . descriptions = [ ] ;
100
116
delete this . $target [ 0 ] . dataset . donationAmounts ;
101
117
valueList . forEach ( ( value ) => {
102
118
donationAmounts . push ( value . display_name ) ;
119
+ if ( value . secondInputText ) {
120
+ this . descriptions . push ( value . secondInputText ) ;
121
+ }
103
122
} ) ;
104
123
this . $target [ 0 ] . dataset . donationAmounts = JSON . stringify ( donationAmounts ) ;
105
124
this . _rebuildPrefilledOptions ( ) ;
106
- } ,
125
+ }
107
126
/**
108
127
* Redraws the target whenever the list changes
109
128
*
@@ -112,7 +131,7 @@ options.registry.Donation = options.Class.extend({
112
131
listChanged ( previewMode , value , params ) {
113
132
this . _updateDescriptions ( ) ;
114
133
this . _rebuildPrefilledOptions ( ) ;
115
- } ,
134
+ }
116
135
/**
117
136
* @see this.selectClass for parameters
118
137
*/
@@ -125,7 +144,7 @@ options.registry.Donation = options.Class.extend({
125
144
} else if ( $amountInput . length ) {
126
145
$amountInput [ 0 ] . min = widgetValue ;
127
146
}
128
- } ,
147
+ }
129
148
/**
130
149
* @see this.selectClass for parameters
131
150
*/
@@ -138,7 +157,7 @@ options.registry.Donation = options.Class.extend({
138
157
} else if ( $amountInput . length ) {
139
158
$amountInput [ 0 ] . max = widgetValue ;
140
159
}
141
- } ,
160
+ }
142
161
/**
143
162
* @see this.selectClass for parameters
144
163
*/
@@ -148,7 +167,7 @@ options.registry.Donation = options.Class.extend({
148
167
if ( $rangeSlider . length ) {
149
168
$rangeSlider [ 0 ] . step = widgetValue ;
150
169
}
151
- } ,
170
+ }
152
171
153
172
//--------------------------------------------------------------------------
154
173
// Private
@@ -184,70 +203,41 @@ options.registry.Donation = options.Class.extend({
184
203
return this . $target [ 0 ] . dataset . sliderStep ;
185
204
}
186
205
}
187
- return this . _super ( ...arguments ) ;
188
- } ,
206
+ return super . _computeWidgetState ( ...arguments ) ;
207
+ }
189
208
/**
190
209
* @override
191
210
*/
192
211
async _computeWidgetVisibility ( widgetName , params ) {
193
212
if ( widgetName === 'free_amount_opt' ) {
194
213
return ! ( this . $target [ 0 ] . dataset . displayOptions && ! this . $target [ 0 ] . dataset . prefilledOptions ) ;
195
214
}
196
- return this . _super ( ...arguments ) ;
197
- } ,
198
- /**
199
- * @override
200
- */
201
- _renderCustomXML ( uiFragment ) {
202
- const list = document . createElement ( 'we-list' ) ;
203
- list . dataset . dependencies = "pre_filled_opt" ;
204
- list . dataset . addItemTitle = _t ( "Add new pre-filled option" ) ;
205
- list . dataset . renderListItems = '' ;
206
- list . dataset . unsortable = 'true' ;
207
- list . dataset . inputType = 'number' ;
208
- list . dataset . defaultValue = 50 ;
209
- list . dataset . listChanged = '' ;
210
- $ ( uiFragment ) . find ( 'we-checkbox[data-name="pre_filled_opt"]' ) . after ( list ) ;
211
- } ,
215
+ return super . _computeWidgetVisibility ( ...arguments ) ;
216
+ }
212
217
/**
213
218
* Build the prefilled options list in the editor panel
214
219
*
215
220
* @private
216
221
*/
217
222
_buildPrefilledOptionsList ( ) {
218
223
const amounts = JSON . parse ( this . $target [ 0 ] . dataset . donationAmounts ) ;
219
- let valueList = amounts . map ( amount => {
224
+ let valueList = amounts . map ( ( amount , i ) => {
225
+ let doubleInput = { } ;
226
+ if ( this . $target [ 0 ] . dataset . descriptions ) {
227
+ doubleInput = {
228
+ firstInputClass : "w-25" ,
229
+ secondInputClass : "w-auto" ,
230
+ secondInputText : this . descriptions [ i ] || this . defaultDescription ,
231
+ }
232
+ }
220
233
return {
221
234
id : amount ,
222
235
display_name : amount ,
236
+ ...doubleInput ,
223
237
} ;
224
238
} ) ;
225
239
return JSON . stringify ( valueList ) ;
226
- } ,
227
- /**
228
- * Add descriptions in the prefilled options list of the
229
- * editor panel.
230
- *
231
- * @private
232
- */
233
- _buildDescriptionsList ( ) {
234
- if ( this . $target [ 0 ] . dataset . descriptions ) {
235
- const $descriptions = this . $target . find ( '#s_donation_description_inputs > input' ) ;
236
- const $tableEl = this . $el . find ( 'we-list table' ) ;
237
- $tableEl . find ( "tr" ) . toArray ( ) . forEach ( ( trEl , i ) => {
238
- const $inputAmount = $ ( trEl ) . find ( 'td' ) . first ( ) ;
239
- $inputAmount . addClass ( 'w-25' ) ;
240
- const tdEl = document . createElement ( 'td' ) ;
241
- const inputEl = document . createElement ( 'input' ) ;
242
- inputEl . type = 'text' ;
243
- inputEl . value = $descriptions [ i ] ? $descriptions [ i ] . value : this . defaultDescription ;
244
- tdEl . classList . add ( 'w-auto' ) ;
245
- tdEl . appendChild ( inputEl ) ;
246
- $ ( tdEl ) . insertAfter ( $inputAmount ) ;
247
- } ) ;
248
- this . _updateDescriptions ( ) ;
249
- }
250
- } ,
240
+ }
251
241
/**
252
242
* Update descriptions in the input hidden.
253
243
*
@@ -256,16 +246,15 @@ options.registry.Donation = options.Class.extend({
256
246
_updateDescriptions ( ) {
257
247
const descriptionInputs = this . $target . find ( '#s_donation_description_inputs' ) ;
258
248
descriptionInputs . empty ( ) ;
259
- const descriptions = this . $el . find ( 'we-list input[type=text]' ) ;
260
- descriptions . toArray ( ) . forEach ( ( description ) => {
249
+ this . descriptions . forEach ( ( description ) => {
261
250
const inputEl = document . createElement ( 'input' ) ;
262
251
inputEl . type = 'hidden' ;
263
252
inputEl . classList . add ( 'o_translatable_input_hidden' , 'd-block' , 'mb-1' , 'w-100' ) ;
264
253
inputEl . name = 'donation_descriptions' ;
265
- inputEl . value = description . value ;
254
+ inputEl . value = description ;
266
255
descriptionInputs [ 0 ] . appendChild ( inputEl ) ;
267
256
} ) ;
268
- } ,
257
+ }
269
258
/**
270
259
* Rebuild options in the DOM.
271
260
*
@@ -311,9 +300,10 @@ options.registry.Donation = options.Class.extend({
311
300
} ) ) ;
312
301
this . $target . find ( '#s_donation_description_inputs' ) . after ( $prefilledButtons ) ;
313
302
}
314
- } ,
303
+ }
304
+ }
305
+ registerWebsiteOption ( "Donation" , {
306
+ Class : Donation ,
307
+ template : "website_payment.s_donation_options" ,
308
+ selector : ".s_donation" ,
315
309
} ) ;
316
-
317
- export default {
318
- Donation : options . registry . Donation ,
319
- } ;
0 commit comments