|
4 | 4 | simple_path_form_id: 'simple_path',
|
5 | 5 | payment_region_input: $( '#woocommerce_amazon_payments_advanced_payment_region' ),
|
6 | 6 | button_language_input: $( '#woocommerce_amazon_payments_advanced_button_language' ),
|
7 |
| - button_languages: {}, |
8 | 7 | action_url: '#',
|
9 | 8 | spId: '',
|
10 | 9 | register_now_link: $( 'a.register_now' ),
|
|
23 | 22 | poll_timer: false,
|
24 | 23 | poll_interval: 3000,
|
25 | 24 | 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() { |
28 | 34 | 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; |
30 | 56 | } );
|
31 | 57 |
|
| 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 | + |
32 | 60 | // Init values if region is already selected
|
33 | 61 | wc_simple_path_form.payment_region_on_change();
|
34 | 62 |
|
|
38 | 66 | wc_simple_path_form.delete_settings_link.on( 'click', this.delete_settings_on_click );
|
39 | 67 | $( document ).on( 'click', 'a.wcapa-toggle-section', this.toggle_visibility );
|
40 | 68 | },
|
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' ); |
49 | 78 | if ( langs.indexOf( selected_key ) === -1 ) {
|
50 |
| - wc_simple_path_form.button_language_input.children( 'option' ).remove(); |
| 79 | + select.children( 'option' ).remove(); |
51 | 80 | selected_key = false;
|
52 | 81 | } else {
|
53 |
| - wc_simple_path_form.button_language_input.children( 'option' ).not( ':selected' ).remove(); |
| 82 | + select.children( 'option' ).not( ':selected' ).remove(); |
54 | 83 | }
|
55 | 84 | var found = false;
|
56 | 85 | $.each( langs, function( i, key ) {
|
|
62 | 91 | var newOpt = $(
|
63 | 92 | '<option/>',
|
64 | 93 | {
|
65 |
| - value: key.replace( '_', '-' ), |
| 94 | + value: key, |
66 | 95 | }
|
67 |
| - ).html( wc_simple_path_form.button_languages[ key ] ); |
| 96 | + ).html( allOptions[ key ] ); |
68 | 97 |
|
69 | 98 | if ( selected_key && ! found ) {
|
70 | 99 | selected.before( newOpt );
|
71 | 100 | } else {
|
72 |
| - wc_simple_path_form.button_language_input.append( newOpt ); |
| 101 | + select.append( newOpt ); |
73 | 102 | }
|
74 | 103 | } );
|
75 | 104 | if ( ! selected_key ) {
|
76 |
| - wc_simple_path_form.button_language_input.children().first().prop( 'selected', true ); |
| 105 | + select.children().first().prop( 'selected', true ); |
77 | 106 | }
|
78 | 107 | },
|
79 | 108 | payment_region_on_change: function() {
|
80 |
| - wc_simple_path_form.payment_button_language_rebuild(); |
81 | 109 | if ( 'jp' === wc_simple_path_form.get_region_selected() ) {
|
82 | 110 | // JP does not have Simple Path Registration, we open a new url for it.
|
83 | 111 | wc_simple_path_form.register_now_link.attr( 'href', wc_simple_path_form.get_simple_path_url() );
|
|
0 commit comments