|
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: {}, |
| 7 | + authorization_mode: $( '#woocommerce_amazon_payments_advanced_authorization_mode' ), |
| 8 | + payment_capture: $( '#woocommerce_amazon_payments_advanced_payment_capture' ), |
8 | 9 | action_url: '#',
|
9 | 10 | spId: '',
|
10 | 11 | register_now_link: $( 'a.register_now' ),
|
|
23 | 24 | poll_timer: false,
|
24 | 25 | poll_interval: 3000,
|
25 | 26 | main_setting_form: $( '#mainform' ),
|
26 |
| - init_settings: function() { |
27 |
| - wc_simple_path_form.button_language_input.children( 'option' ).each( function() { |
| 27 | + init_dynamic_options: function( select, parent, combos ) { |
| 28 | + var allOptions = select.data( 'wc-apa-all-options' ); |
| 29 | + if ( typeof allOptions !== 'undefined' ) { |
| 30 | + return allOptions; |
| 31 | + } |
| 32 | + allOptions = {}; |
| 33 | + select.data( 'wc-apa-all-options', allOptions ); |
| 34 | + |
| 35 | + select.children( 'option' ).each( function() { |
28 | 36 | var key = $( this ).prop( 'value' ).replace( '-', '_' );
|
29 |
| - wc_simple_path_form.button_languages[ key ] = $( this ).text(); |
| 37 | + allOptions[ key ] = $( this ).text(); |
| 38 | + } ); |
| 39 | + |
| 40 | + var watch = function() { |
| 41 | + var val = parent.val(); |
| 42 | + wc_simple_path_form.select_rebuild( select, combos, val ); |
| 43 | + }; |
| 44 | + |
| 45 | + watch(); |
| 46 | + |
| 47 | + parent.on( 'change', watch ); |
| 48 | + |
| 49 | + return allOptions; |
| 50 | + }, |
| 51 | + init_settings: function() { |
| 52 | + $.each( amazon_admin_params.language_combinations, function( i, langs ) { |
| 53 | + langs.unshift( '' ); |
| 54 | + langs = $.map( langs, function( item ) { |
| 55 | + return item.replace( '-', '_' ); |
| 56 | + } ); |
| 57 | + amazon_admin_params.language_combinations[ i ] = langs; |
| 58 | + } ); |
| 59 | + |
| 60 | + 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 ); |
| 61 | + wc_simple_path_form.init_dynamic_options( wc_simple_path_form.authorization_mode, wc_simple_path_form.payment_capture, { |
| 62 | + '': [ 'sync' ], |
| 63 | + authorize: true, |
| 64 | + manual: true, |
30 | 65 | } );
|
31 | 66 |
|
32 | 67 | // Init values if region is already selected
|
|
38 | 73 | wc_simple_path_form.delete_settings_link.on( 'click', this.delete_settings_on_click );
|
39 | 74 | $( document ).on( 'click', 'a.wcapa-toggle-section', this.toggle_visibility );
|
40 | 75 | },
|
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( '-', '_' ); |
| 76 | + select_rebuild: function( select, combos, val ) { |
| 77 | + var allOptions = select.data( 'wc-apa-all-options' ); |
| 78 | + var langs = combos[ val ]; |
| 79 | + if ( typeof combos[ val ] === 'boolean' ) { |
| 80 | + langs = combos[ val ] = Object.keys( allOptions ); |
| 81 | + } |
| 82 | + langs = langs.slice(); |
| 83 | + var selected = select.children( 'option:selected' ); |
| 84 | + var selected_key = selected.prop( 'value' ); |
49 | 85 | if ( langs.indexOf( selected_key ) === -1 ) {
|
50 |
| - wc_simple_path_form.button_language_input.children( 'option' ).remove(); |
| 86 | + select.children( 'option' ).remove(); |
51 | 87 | selected_key = false;
|
52 | 88 | } else {
|
53 |
| - wc_simple_path_form.button_language_input.children( 'option' ).not( ':selected' ).remove(); |
| 89 | + select.children( 'option' ).not( ':selected' ).remove(); |
54 | 90 | }
|
55 | 91 | var found = false;
|
56 | 92 | $.each( langs, function( i, key ) {
|
|
62 | 98 | var newOpt = $(
|
63 | 99 | '<option/>',
|
64 | 100 | {
|
65 |
| - value: key.replace( '_', '-' ), |
| 101 | + value: key, |
66 | 102 | }
|
67 |
| - ).html( wc_simple_path_form.button_languages[ key ] ); |
| 103 | + ).html( allOptions[ key ] ); |
68 | 104 |
|
69 | 105 | if ( selected_key && ! found ) {
|
70 | 106 | selected.before( newOpt );
|
71 | 107 | } else {
|
72 |
| - wc_simple_path_form.button_language_input.append( newOpt ); |
| 108 | + select.append( newOpt ); |
73 | 109 | }
|
74 | 110 | } );
|
75 | 111 | if ( ! selected_key ) {
|
76 |
| - wc_simple_path_form.button_language_input.children().first().prop( 'selected', true ); |
| 112 | + select.children().first().prop( 'selected', true ); |
77 | 113 | }
|
78 | 114 | },
|
79 | 115 | payment_region_on_change: function() {
|
80 |
| - wc_simple_path_form.payment_button_language_rebuild(); |
81 | 116 | if ( 'jp' === wc_simple_path_form.get_region_selected() ) {
|
82 | 117 | // JP does not have Simple Path Registration, we open a new url for it.
|
83 | 118 | wc_simple_path_form.register_now_link.attr( 'href', wc_simple_path_form.get_simple_path_url() );
|
|
0 commit comments