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