Skip to content

Commit 8203f6b

Browse files
committed
Make function to rebuild dropdowns reusable
1 parent 596fe5b commit 8203f6b

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

assets/js/amazon-wc-admin.js

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
simple_path_form_id: 'simple_path',
55
payment_region_input: $( '#woocommerce_amazon_payments_advanced_payment_region' ),
66
button_language_input: $( '#woocommerce_amazon_payments_advanced_button_language' ),
7-
button_languages: {},
87
action_url: '#',
98
spId: '',
109
register_now_link: $( 'a.register_now' ),
@@ -23,12 +22,41 @@
2322
poll_timer: false,
2423
poll_interval: 3000,
2524
main_setting_form: $( '#mainform' ),
26-
init_settings: function() {
27-
wc_simple_path_form.button_language_input.children( 'option' ).each( function() {
25+
init_dynamic_options: function( select, parent, combos ) {
26+
var allOptions = select.data( 'wc-apa-all-options' );
27+
if ( typeof allOptions !== 'undefined' ) {
28+
return allOptions;
29+
}
30+
allOptions = {};
31+
select.data( 'wc-apa-all-options', allOptions );
32+
33+
select.children( 'option' ).each( function() {
2834
var key = $( this ).prop( 'value' ).replace( '-', '_' );
29-
wc_simple_path_form.button_languages[ key ] = $( this ).text();
35+
allOptions[ key ] = $( this ).text();
36+
} );
37+
38+
var watch = function() {
39+
var val = parent.val();
40+
wc_simple_path_form.select_rebuild( select, combos, val );
41+
};
42+
43+
watch();
44+
45+
parent.on( 'change', watch );
46+
47+
return allOptions;
48+
},
49+
init_settings: function() {
50+
$.each( amazon_admin_params.language_combinations, function( i, langs ) {
51+
langs.unshift( '' );
52+
langs = $.map( langs, function( item ) {
53+
return item.replace( '-', '_' );
54+
} );
55+
amazon_admin_params.language_combinations[ i ] = langs;
3056
} );
3157

58+
wc_simple_path_form.init_dynamic_options( wc_simple_path_form.button_language_input, wc_simple_path_form.payment_region_input, amazon_admin_params.language_combinations );
59+
3260
// Init values if region is already selected
3361
wc_simple_path_form.payment_region_on_change();
3462

@@ -38,19 +66,20 @@
3866
wc_simple_path_form.delete_settings_link.on( 'click', this.delete_settings_on_click );
3967
$( document ).on( 'click', 'a.wcapa-toggle-section', this.toggle_visibility );
4068
},
41-
payment_button_language_rebuild: function() {
42-
var langs = amazon_admin_params.language_combinations[ wc_simple_path_form.get_region_selected() ].slice();
43-
langs.unshift( '' );
44-
langs = $.map( langs, function( item ) {
45-
return item.replace( '-', '_' );
46-
} );
47-
var selected = wc_simple_path_form.button_language_input.children( 'option:selected' );
48-
var selected_key = selected.prop( 'value' ).replace( '-', '_' );
69+
select_rebuild: function( select, combos, val ) {
70+
var allOptions = select.data( 'wc-apa-all-options' );
71+
var langs = combos[ val ];
72+
if ( typeof combos[ val ] === 'boolean' ) {
73+
langs = combos[ val ] = Object.keys( allOptions );
74+
}
75+
langs = langs.slice();
76+
var selected = select.children( 'option:selected' );
77+
var selected_key = selected.prop( 'value' );
4978
if ( langs.indexOf( selected_key ) === -1 ) {
50-
wc_simple_path_form.button_language_input.children( 'option' ).remove();
79+
select.children( 'option' ).remove();
5180
selected_key = false;
5281
} else {
53-
wc_simple_path_form.button_language_input.children( 'option' ).not( ':selected' ).remove();
82+
select.children( 'option' ).not( ':selected' ).remove();
5483
}
5584
var found = false;
5685
$.each( langs, function( i, key ) {
@@ -62,22 +91,21 @@
6291
var newOpt = $(
6392
'<option/>',
6493
{
65-
value: key.replace( '_', '-' ),
94+
value: key,
6695
}
67-
).html( wc_simple_path_form.button_languages[ key ] );
96+
).html( allOptions[ key ] );
6897

6998
if ( selected_key && ! found ) {
7099
selected.before( newOpt );
71100
} else {
72-
wc_simple_path_form.button_language_input.append( newOpt );
101+
select.append( newOpt );
73102
}
74103
} );
75104
if ( ! selected_key ) {
76-
wc_simple_path_form.button_language_input.children().first().prop( 'selected', true );
105+
select.children().first().prop( 'selected', true );
77106
}
78107
},
79108
payment_region_on_change: function() {
80-
wc_simple_path_form.payment_button_language_rebuild();
81109
if ( 'jp' === wc_simple_path_form.get_region_selected() ) {
82110
// JP does not have Simple Path Registration, we open a new url for it.
83111
wc_simple_path_form.register_now_link.attr( 'href', wc_simple_path_form.get_simple_path_url() );

0 commit comments

Comments
 (0)