11/** @odoo -module **/
22
33import { renderToElement } from "@web/core/utils/render" ;
4- import options from '@web_editor/js/editor/snippets.options.legacy' ;
54import { _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" ;
67
7- options . registry . Donation = options . Class . extend ( {
8+ export class Donation extends SnippetOption {
89 /**
910 * @override
1011 */
11- start ( ) {
12+ constructor ( ) {
13+ super ( ...arguments ) ;
1214 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+ }
1530 /**
1631 * @override
1732 */
1833 onBuilt ( ) {
1934 this . _rebuildPrefilledOptions ( ) ;
20- return this . _super ( ...arguments ) ;
21- } ,
35+ return super . onBuilt ( ...arguments ) ;
36+ }
2237 /**
2338 * @override
2439 */
2540 cleanForSave ( ) {
2641 if ( ! this . $target [ 0 ] . dataset . descriptions ) {
2742 this . _updateDescriptions ( ) ;
2843 }
29- } ,
44+ }
3045
3146 //--------------------------------------------------------------------------
3247 // Public
@@ -36,9 +51,9 @@ options.registry.Donation = options.Class.extend({
3651 * @override
3752 */
3853 async updateUI ( ) {
39- await this . _super ( ...arguments ) ;
40- this . _buildDescriptionsList ( ) ;
41- } ,
54+ await super . updateUI ( ...arguments ) ;
55+ this . _updateDescriptions ( ) ;
56+ }
4257
4358 //--------------------------------------------------------------------------
4459 // Options
@@ -57,29 +72,29 @@ options.registry.Donation = options.Class.extend({
5772 this . $target [ 0 ] . dataset . customAmount = "slider" ;
5873 }
5974 this . _rebuildPrefilledOptions ( ) ;
60- } ,
75+ }
6176 /**
6277 * Add/remove prefilled buttons.
6378 *
6479 * @see this.selectClass for parameters
6580 */
6681 togglePrefilledOptions ( previewMode , widgetValue , params ) {
6782 this . $target [ 0 ] . dataset . prefilledOptions = widgetValue ;
68- this . $el . find ( '.o_we_prefilled_options_list' ) . toggleClass ( 'd-none' , ! widgetValue ) ;
6983 if ( ! widgetValue && this . $target [ 0 ] . dataset . displayOptions ) {
7084 this . $target [ 0 ] . dataset . customAmount = "slider" ;
7185 }
7286 this . _rebuildPrefilledOptions ( ) ;
73- } ,
87+ }
7488 /**
7589 * Add/remove description of prefilled buttons.
7690 *
7791 * @see this.selectClass for parameters
7892 */
7993 toggleOptionDescription ( previewMode , widgetValue , params ) {
8094 this . $target [ 0 ] . dataset . descriptions = widgetValue ;
95+ this . renderContext . showOptionDescriptions = widgetValue ;
8196 this . renderListItems ( false , this . _buildPrefilledOptionsList ( ) ) ;
82- } ,
97+ }
8398 /**
8499 * Select an amount input
85100 *
@@ -88,7 +103,7 @@ options.registry.Donation = options.Class.extend({
88103 selectAmountInput ( previewMode , widgetValue , params ) {
89104 this . $target [ 0 ] . dataset . customAmount = widgetValue ;
90105 this . _rebuildPrefilledOptions ( ) ;
91- } ,
106+ }
92107 /**
93108 * Apply the we-list on the target and rebuild the input(s)
94109 *
@@ -97,13 +112,17 @@ options.registry.Donation = options.Class.extend({
97112 renderListItems ( previewMode , value , params ) {
98113 const valueList = JSON . parse ( value ) ;
99114 const donationAmounts = [ ] ;
115+ this . descriptions = [ ] ;
100116 delete this . $target [ 0 ] . dataset . donationAmounts ;
101117 valueList . forEach ( ( value ) => {
102118 donationAmounts . push ( value . display_name ) ;
119+ if ( value . secondInputText ) {
120+ this . descriptions . push ( value . secondInputText ) ;
121+ }
103122 } ) ;
104123 this . $target [ 0 ] . dataset . donationAmounts = JSON . stringify ( donationAmounts ) ;
105124 this . _rebuildPrefilledOptions ( ) ;
106- } ,
125+ }
107126 /**
108127 * Redraws the target whenever the list changes
109128 *
@@ -112,7 +131,7 @@ options.registry.Donation = options.Class.extend({
112131 listChanged ( previewMode , value , params ) {
113132 this . _updateDescriptions ( ) ;
114133 this . _rebuildPrefilledOptions ( ) ;
115- } ,
134+ }
116135 /**
117136 * @see this.selectClass for parameters
118137 */
@@ -125,7 +144,7 @@ options.registry.Donation = options.Class.extend({
125144 } else if ( $amountInput . length ) {
126145 $amountInput [ 0 ] . min = widgetValue ;
127146 }
128- } ,
147+ }
129148 /**
130149 * @see this.selectClass for parameters
131150 */
@@ -138,7 +157,7 @@ options.registry.Donation = options.Class.extend({
138157 } else if ( $amountInput . length ) {
139158 $amountInput [ 0 ] . max = widgetValue ;
140159 }
141- } ,
160+ }
142161 /**
143162 * @see this.selectClass for parameters
144163 */
@@ -148,7 +167,7 @@ options.registry.Donation = options.Class.extend({
148167 if ( $rangeSlider . length ) {
149168 $rangeSlider [ 0 ] . step = widgetValue ;
150169 }
151- } ,
170+ }
152171
153172 //--------------------------------------------------------------------------
154173 // Private
@@ -184,70 +203,41 @@ options.registry.Donation = options.Class.extend({
184203 return this . $target [ 0 ] . dataset . sliderStep ;
185204 }
186205 }
187- return this . _super ( ...arguments ) ;
188- } ,
206+ return super . _computeWidgetState ( ...arguments ) ;
207+ }
189208 /**
190209 * @override
191210 */
192211 async _computeWidgetVisibility ( widgetName , params ) {
193212 if ( widgetName === 'free_amount_opt' ) {
194213 return ! ( this . $target [ 0 ] . dataset . displayOptions && ! this . $target [ 0 ] . dataset . prefilledOptions ) ;
195214 }
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+ }
212217 /**
213218 * Build the prefilled options list in the editor panel
214219 *
215220 * @private
216221 */
217222 _buildPrefilledOptionsList ( ) {
218223 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+ }
220233 return {
221234 id : amount ,
222235 display_name : amount ,
236+ ...doubleInput ,
223237 } ;
224238 } ) ;
225239 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+ }
251241 /**
252242 * Update descriptions in the input hidden.
253243 *
@@ -256,16 +246,15 @@ options.registry.Donation = options.Class.extend({
256246 _updateDescriptions ( ) {
257247 const descriptionInputs = this . $target . find ( '#s_donation_description_inputs' ) ;
258248 descriptionInputs . empty ( ) ;
259- const descriptions = this . $el . find ( 'we-list input[type=text]' ) ;
260- descriptions . toArray ( ) . forEach ( ( description ) => {
249+ this . descriptions . forEach ( ( description ) => {
261250 const inputEl = document . createElement ( 'input' ) ;
262251 inputEl . type = 'hidden' ;
263252 inputEl . classList . add ( 'o_translatable_input_hidden' , 'd-block' , 'mb-1' , 'w-100' ) ;
264253 inputEl . name = 'donation_descriptions' ;
265- inputEl . value = description . value ;
254+ inputEl . value = description ;
266255 descriptionInputs [ 0 ] . appendChild ( inputEl ) ;
267256 } ) ;
268- } ,
257+ }
269258 /**
270259 * Rebuild options in the DOM.
271260 *
@@ -311,9 +300,10 @@ options.registry.Donation = options.Class.extend({
311300 } ) ) ;
312301 this . $target . find ( '#s_donation_description_inputs' ) . after ( $prefilledButtons ) ;
313302 }
314- } ,
303+ }
304+ }
305+ registerWebsiteOption ( "Donation" , {
306+ Class : Donation ,
307+ template : "website_payment.s_donation_options" ,
308+ selector : ".s_donation" ,
315309} ) ;
316-
317- export default {
318- Donation : options . registry . Donation ,
319- } ;
0 commit comments