1/*global wc_country_select_params */ 2jQuery( 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 $this = $( this ); 59 60 var select2_args = $.extend({ 61 placeholder: $this.attr( 'data-placeholder' ) || $this.attr( 'placeholder' ) || '', 62 label: $this.attr( 'data-label' ) || null, 63 required: $this.attr( 'aria-required' ) === 'true' || null, 64 width: '100%' 65 }, getEnhancedSelectFormatString() ); 66 67 $( this ) 68 .on( 'select2:select', function() { 69 $( this ).trigger( 'focus' ); // Maintain focus after select https://github.com/select2/select2/issues/4384 70 } ) 71 .selectWoo( select2_args ); 72 }); 73 }; 74 75 wc_country_select_select2(); 76 77 $( document.body ).on( 'country_to_state_changed', function() { 78 wc_country_select_select2(); 79 }); 80 } 81 82 /* State/Country select boxes */ 83 var states_json = wc_country_select_params.countries.replace( /"/g, '"' ), 84 states = JSON.parse( states_json ), 85 wrapper_selectors = '.woocommerce-billing-fields,' + 86 '.woocommerce-shipping-fields,' +
87 '.woocommerce-address-fields,' + 88 '.woocommerce-shipping-calculator'; 89 90 $( document.body ).on( 'change refresh', 'select.country_to_state, input.country_to_state', function() { 91 // Grab wrapping element to target only stateboxes in same 'group' 92 var $wrapper = $( this ).closest( wrapper_selectors ); 93 94 if ( ! $wrapper.length ) { 95 $wrapper = $( this ).closest('.form-row').parent(); 96 } 97 98 var country = $( this ).val(), 99 $statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' ), 100 $parent = $statebox.closest( '.form-row' ), 101 input_name = $statebox.attr( 'name' ), 102 input_id = $statebox.attr('id'), 103 input_classes = $statebox.attr('data-input-classes'), 104 value = $statebox.val(), 105 placeholder = $statebox.attr( 'placeholder' ) || $statebox.attr( 'data-placeholder' ) || '', 106 $newstate; 107 108 if ( placeholder === wc_country_select_params.i18n_select_state_text ) { 109 placeholder = ''; 110 } 111 112 if ( states[ country ] ) { 113 if ( $.isEmptyObject( states[ country ] ) ) { 114 $newstate = $( '<input type="hidden" />' ) 115 .prop( 'id', input_id ) 116 .prop( 'name', input_name ) 117 .attr( 'data-input-classes', input_classes ) 118 .addClass( 'hidden ' + input_classes ); 119 $parent.hide().find( '.select2-container' ).remove(); 120 $statebox.replaceWith( $newstate ); 121 $( document.body ).trigger( 'country_to_state_changed', [ country, $wrapper ] ); 122 } else { 123 var state = states[ country ], 124 $defaultOption = $( '<option value=""></option>' ).text( wc_country_select_params.i18n_select_state_text ); 125 126 if ( ! placeholder ) { 127 placeholder = wc_country_select_params.i18n_select_state_text; 128 } 129 130 $parent.show(); 131 132 if ( $statebox.is( 'input' ) ) { 133 $newstate = $( '<select></select>' ) 134 .prop( 'id', input_id ) 135 .prop( 'name', input_name ) 136 .data( 'placeholder', placeholder ) 137 .attr( 'data-input-classes', input_classes ) 138 .addClass( 'state_select ' + input_classes ); 139 $statebox.replaceWith( $newstate ); 140 $statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' ); 141 } 142 143 $statebox.empty().append( $defaultOption ); 144 145 $.each( state, function( index ) { 146 var $option = $( '<option></option>' ) 147 .prop( 'value', index ) 148 .text( state[ index ] ); 149 $statebox.append( $option ); 150 } ); 151 152 $statebox.val( value ).trigger( 'change' ); 153 154 $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); 155 } 156 } else { 157 if ( $statebox.is( 'select, input[type="hidden"]' ) ) { 158 $newstate = $( '<input type="text" />' ) 159 .prop( 'id', input_id ) 160 .prop( 'name', input_name ) 161 .prop( 'placeholder', placeholder ) 162 .attr( 'data-input-classes', input_classes ) 163 .addClass( 'input-text ' + input_classes ); 164 $parent.show().find( '.select2-container' ).remove(); 165 $statebox.replaceWith( $newstate ); 166 $( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] ); 167 } 168 } 169 170 $( document.body ).trigger( 'country_to_state_changing', [country, $wrapper ] ); 171 }); 172 173 $( document.body ).on( 'wc_address_i18n_ready', function() { 174 // Init country selects with their default value once the page loads. 175 $( wrapper_selectors ).each( function() { 176 var $country_input = $( this ).find( '#billing_country, #shipping_country, #calc_shipping_country' ); 177 178 if ( 0 === $country_input.length || 0 === $country_input.val().length ) { 179 return; 180 } 181 182 $country_input.trigger( 'refresh' ); 183 }); 184 }); 185});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.