add-payment-method.js
8 years ago
add-payment-method.min.js
7 years ago
add-to-cart-variation.js
6 years ago
add-to-cart-variation.min.js
6 years ago
add-to-cart.js
6 years ago
add-to-cart.min.js
6 years ago
address-i18n.js
7 years ago
address-i18n.min.js
7 years ago
cart-fragments.js
7 years ago
cart-fragments.min.js
7 years ago
cart.js
6 years ago
cart.min.js
6 years ago
checkout.js
6 years ago
checkout.min.js
6 years ago
country-select.js
6 years ago
country-select.min.js
6 years ago
credit-card-form.js
8 years ago
credit-card-form.min.js
8 years ago
geolocation.js
6 years ago
geolocation.min.js
6 years ago
lost-password.js
8 years ago
lost-password.min.js
8 years ago
password-strength-meter.js
6 years ago
password-strength-meter.min.js
6 years ago
price-slider.js
7 years ago
price-slider.min.js
7 years ago
single-product.js
6 years ago
single-product.min.js
6 years ago
tokenization-form.js
6 years ago
tokenization-form.min.js
6 years ago
woocommerce.js
6 years ago
woocommerce.min.js
6 years ago
country-select.js
179 lines
| 1 | /*global wc_country_select_params */ |
| 2 | jQuery( function( $ ) { |
| 3 | |
| 4 | // wc_country_select_params is required to continue, ensure the object exists |
| 5 | if ( typeof wc_country_select_params === 'undefined' ) { |
| 6 | return false; |
| 7 | } |
| 8 | |
| 9 | // Select2 Enhancement if it exists |
| 10 | if ( $().selectWoo ) { |
| 11 | var getEnhancedSelectFormatString = function() { |
| 12 | return { |
| 13 | 'language': { |
| 14 | errorLoading: function() { |
| 15 | // Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error. |
| 16 | return wc_country_select_params.i18n_searching; |
| 17 | }, |
| 18 | inputTooLong: function( args ) { |
| 19 | var overChars = args.input.length - args.maximum; |
| 20 | |
| 21 | if ( 1 === overChars ) { |
| 22 | return wc_country_select_params.i18n_input_too_long_1; |
| 23 | } |
| 24 | |
| 25 | return wc_country_select_params.i18n_input_too_long_n.replace( '%qty%', overChars ); |
| 26 | }, |
| 27 | inputTooShort: function( args ) { |
| 28 | var remainingChars = args.minimum - args.input.length; |
| 29 | |
| 30 | if ( 1 === remainingChars ) { |
| 31 | return wc_country_select_params.i18n_input_too_short_1; |
| 32 | } |
| 33 | |
| 34 | return wc_country_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars ); |
| 35 | }, |
| 36 | loadingMore: function() { |
| 37 | return wc_country_select_params.i18n_load_more; |
| 38 | }, |
| 39 | maximumSelected: function( args ) { |
| 40 | if ( args.maximum === 1 ) { |
| 41 | return wc_country_select_params.i18n_selection_too_long_1; |
| 42 | } |
| 43 | |
| 44 | return wc_country_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum ); |
| 45 | }, |
| 46 | noResults: function() { |
| 47 | return wc_country_select_params.i18n_no_matches; |
| 48 | }, |
| 49 | searching: function() { |
| 50 | return wc_country_select_params.i18n_searching; |
| 51 | } |
| 52 | } |
| 53 | }; |
| 54 | }; |
| 55 | |
| 56 | var wc_country_select_select2 = function() { |
| 57 | $( 'select.country_select:visible, select.state_select:visible' ).each( function() { |
| 58 | var select2_args = $.extend({ |
| 59 | placeholder: $( this ).attr( 'data-placeholder' ) || $( this ).attr( 'placeholder' ) || '', |
| 60 | width: '100%' |
| 61 | }, getEnhancedSelectFormatString() ); |
| 62 | |
| 63 | $( this ) |
| 64 | .on( 'select2:select', function() { |
| 65 | $( this ).focus(); // Maintain focus after select https://github.com/select2/select2/issues/4384 |
| 66 | } ) |
| 67 | .selectWoo( select2_args ); |
| 68 | }); |
| 69 | }; |
| 70 | |
| 71 | wc_country_select_select2(); |
| 72 | |
| 73 | $( document.body ).bind( 'country_to_state_changed', function() { |
| 74 | wc_country_select_select2(); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | /* State/Country select boxes */ |
| 79 | var states_json = wc_country_select_params.countries.replace( /"/g, '"' ), |
| 80 | states = $.parseJSON( states_json ), |
| 81 | wrapper_selectors = '.woocommerce-billing-fields,' + |
| 82 | '.woocommerce-shipping-fields,' + |
| 83 | '.woocommerce-address-fields,' + |
| 84 | '.woocommerce-shipping-calculator'; |
| 85 | |
| 86 | $( document.body ).on( 'change refresh', 'select.country_to_state, input.country_to_state', function() { |
| 87 | // Grab wrapping element to target only stateboxes in same 'group' |
| 88 | var $wrapper = $( this ).closest( wrapper_selectors ); |
| 89 | |
| 90 | if ( ! $wrapper.length ) { |
| 91 | $wrapper = $( this ).closest('.form-row').parent(); |
| 92 | } |
| 93 | |
| 94 | var country = $( this ).val(), |
| 95 | $statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' ), |
| 96 | $parent = $statebox.closest( '.form-row' ), |
| 97 | input_name = $statebox.attr( 'name' ), |
| 98 | input_id = $statebox.attr('id'), |
| 99 | input_classes = $statebox.attr('data-input-classes'), |
| 100 | value = $statebox.val(), |
| 101 | placeholder = $statebox.attr( 'placeholder' ) || $statebox.attr( 'data-placeholder' ) || '', |
| 102 | $newstate; |
| 103 | |
| 104 | if ( states[ country ] ) { |
| 105 | if ( $.isEmptyObject( states[ country ] ) ) { |
| 106 | $newstate = $( '<input type="hidden" />' ) |
| 107 | .prop( 'id', input_id ) |
| 108 | .prop( 'name', input_name ) |
| 109 | .prop( 'placeholder', placeholder ) |
| 110 | .attr( 'data-input-classes', input_classes ) |
| 111 | .addClass( 'hidden ' + input_classes ); |
| 112 | $parent.hide().find( '.select2-container' ).remove(); |
| 113 | $statebox.replaceWith( $newstate ); |
| 114 | $( document.body ).trigger( 'country_to_state_changed', [ country, $wrapper ] ); |
| 115 | } else { |
| 116 | var state = states[ country ], |
| 117 | $defaultOption = $( '<option value=""></option>' ).text( wc_country_select_params.i18n_select_state_text ); |
| 118 | |
| 119 | if ( ! placeholder ) { |
| 120 | placeholder = wc_country_select_params.i18n_select_state_text; |
| 121 | } |
| 122 | |
| 123 | $parent.show(); |
| 124 | |
| 125 | if ( $statebox.is( 'input' ) ) { |
| 126 | $newstate = $( '<select></select>' ) |
| 127 | .prop( 'id', input_id ) |
| 128 | .prop( 'name', input_name ) |
| 129 | .data( 'placeholder', placeholder ) |
| 130 | .attr( 'data-input-classes', input_classes ) |
| 131 | .addClass( 'state_select ' + input_classes ); |
| 132 | $statebox.replaceWith( $newstate ); |
| 133 | $statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' ); |
| 134 | } |
| 135 | |
| 136 | $statebox.empty().append( $defaultOption ); |
| 137 | |
| 138 | $.each( state, function( index ) { |
| 139 | var $option = $( '<option></option>' ) |
| 140 | .prop( 'value', index ) |
| 141 | .text( state[ index ] ); |
| 142 | $statebox.append( $option ); |
| 143 | } ); |
| 144 | |
| 145 | $statebox.val( value ).change(); |
| 146 | |
| 147 | $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); |
| 148 | } |
| 149 | } else { |
| 150 | if ( $statebox.is( 'select, input[type="hidden"]' ) ) { |
| 151 | $newstate = $( '<input type="text" />' ) |
| 152 | .prop( 'id', input_id ) |
| 153 | .prop( 'name', input_name ) |
| 154 | .prop('placeholder', placeholder) |
| 155 | .attr('data-input-classes', input_classes ) |
| 156 | .addClass( 'input-text ' + input_classes ); |
| 157 | $parent.show().find( '.select2-container' ).remove(); |
| 158 | $statebox.replaceWith( $newstate ); |
| 159 | $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $( document.body ).trigger( 'country_to_state_changing', [country, $wrapper ] ); |
| 164 | }); |
| 165 | |
| 166 | $( document.body ).on( 'wc_address_i18n_ready', function() { |
| 167 | // Init country selects with their default value once the page loads. |
| 168 | $( wrapper_selectors ).each( function() { |
| 169 | var $country_input = $( this ).find( '#billing_country, #shipping_country, #calc_shipping_country' ); |
| 170 | |
| 171 | if ( 0 === $country_input.length || 0 === $country_input.val().length ) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | $country_input.trigger( 'refresh' ); |
| 176 | }); |
| 177 | }); |
| 178 | }); |
| 179 |