|
| 1 | +jQuery( function($) { |
| 2 | + |
| 3 | + // wc_city_select_params is required to continue, ensure the object exists |
| 4 | + // wc_country_select_params is used for select2 texts. This one is added by WC |
| 5 | + if ( typeof wc_country_select_params === 'undefined' || typeof wc_city_select_params === 'undefined' ) { |
| 6 | + return false; |
| 7 | + } |
| 8 | + |
| 9 | + function getEnhancedSelectFormatString() { |
| 10 | + var formatString = { |
| 11 | + formatMatches: function( matches ) { |
| 12 | + if ( 1 === matches ) { |
| 13 | + return wc_country_select_params.i18n_matches_1; |
| 14 | + } |
| 15 | + |
| 16 | + return wc_country_select_params.i18n_matches_n.replace( '%qty%', matches ); |
| 17 | + }, |
| 18 | + formatNoMatches: function() { |
| 19 | + return wc_country_select_params.i18n_no_matches; |
| 20 | + }, |
| 21 | + formatAjaxError: function() { |
| 22 | + return wc_country_select_params.i18n_ajax_error; |
| 23 | + }, |
| 24 | + formatInputTooShort: function( input, min ) { |
| 25 | + var number = min - input.length; |
| 26 | + |
| 27 | + if ( 1 === number ) { |
| 28 | + return wc_country_select_params.i18n_input_too_short_1; |
| 29 | + } |
| 30 | + |
| 31 | + return wc_country_select_params.i18n_input_too_short_n.replace( '%qty%', number ); |
| 32 | + }, |
| 33 | + formatInputTooLong: function( input, max ) { |
| 34 | + var number = input.length - max; |
| 35 | + |
| 36 | + if ( 1 === number ) { |
| 37 | + return wc_country_select_params.i18n_input_too_long_1; |
| 38 | + } |
| 39 | + |
| 40 | + return wc_country_select_params.i18n_input_too_long_n.replace( '%qty%', number ); |
| 41 | + }, |
| 42 | + formatSelectionTooBig: function( limit ) { |
| 43 | + if ( 1 === limit ) { |
| 44 | + return wc_country_select_params.i18n_selection_too_long_1; |
| 45 | + } |
| 46 | + |
| 47 | + return wc_country_select_params.i18n_selection_too_long_n.replace( '%qty%', limit ); |
| 48 | + }, |
| 49 | + formatLoadMore: function() { |
| 50 | + return wc_country_select_params.i18n_load_more; |
| 51 | + }, |
| 52 | + formatSearching: function() { |
| 53 | + return wc_country_select_params.i18n_searching; |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + return formatString; |
| 58 | + } |
| 59 | + |
| 60 | + // Select2 Enhancement if it exists |
| 61 | + if ( $().select2 ) { |
| 62 | + var wc_city_select_select2 = function() { |
| 63 | + $( 'select.city_select:visible' ).each( function() { |
| 64 | + var select2_args = $.extend({ |
| 65 | + placeholderOption: 'first', |
| 66 | + width: '100%' |
| 67 | + }, getEnhancedSelectFormatString() ); |
| 68 | + |
| 69 | + $( this ).select2( select2_args ); |
| 70 | + }); |
| 71 | + }; |
| 72 | + |
| 73 | + wc_city_select_select2(); |
| 74 | + |
| 75 | + $( document.body ).bind( 'city_to_select', function() { |
| 76 | + wc_city_select_select2(); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + /* City select boxes */ |
| 81 | + var cities_json = wc_city_select_params.cities.replace( /"/g, '"' ); |
| 82 | + var cities = $.parseJSON( cities_json ); |
| 83 | + |
| 84 | + $( 'body' ).on( 'country_to_state_changing', function(e, country, $container) { |
| 85 | + var $statebox = $container.find( '#billing_state, #shipping_state, #calc_shipping_state' ); |
| 86 | + var state = $statebox.val(); |
| 87 | + $( document.body ).trigger( 'state_changing', [country, state, $container ] ); |
| 88 | + }); |
| 89 | + |
| 90 | + $( 'body' ).on( 'change', 'select.state_select, #calc_shipping_state', function() { |
| 91 | + var $container = $( this ).closest( 'div' ); |
| 92 | + var country = $container.find( '#billing_country, #shipping_country, #calc_shipping_country' ).val(); |
| 93 | + var state = $( this ).val(); |
| 94 | + |
| 95 | + $( document.body ).trigger( 'state_changing', [country, state, $container ] ); |
| 96 | + }); |
| 97 | + |
| 98 | + $( 'body' ).on( 'state_changing', function(e, country, state, $container) { |
| 99 | + var $citybox = $container.find( '#billing_city, #shipping_city, #calc_shipping_city' ); |
| 100 | + |
| 101 | + if ( cities[ country ] ) { |
| 102 | + /* if the country has no states */ |
| 103 | + if( cities[country] instanceof Array) { |
| 104 | + cityToSelect( $citybox, cities[ country ] ); |
| 105 | + } else if ( state ) { |
| 106 | + if ( cities[ country ][ state ] ) { |
| 107 | + cityToSelect( $citybox, cities[ country ][ state ] ); |
| 108 | + } else { |
| 109 | + cityToInput( $citybox ); |
| 110 | + } |
| 111 | + } else { |
| 112 | + disableCity( $citybox ); |
| 113 | + } |
| 114 | + } else { |
| 115 | + cityToInput( $citybox ); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + /* Ajax replaces .cart_totals (child of .cart-collaterals) on shipping calculator */ |
| 120 | + if ( $( '.cart-collaterals' ).length && $( '#calc_shipping_state' ).length ) { |
| 121 | + var calc_observer = new MutationObserver( function() { |
| 122 | + $( '#calc_shipping_state' ).change(); |
| 123 | + }); |
| 124 | + calc_observer.observe( document.querySelector( '.cart-collaterals' ), { childList: true }); |
| 125 | + } |
| 126 | + |
| 127 | + function cityToInput( $citybox ) { |
| 128 | + if ( $citybox.is('input') ) { |
| 129 | + $citybox.prop( 'disabled', false ); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + var input_name = $citybox.attr( 'name' ); |
| 134 | + var input_id = $citybox.attr( 'id' ); |
| 135 | + var placeholder = $citybox.attr( 'placeholder' ); |
| 136 | + |
| 137 | + $citybox.parent().find( '.select2-container' ).remove(); |
| 138 | + |
| 139 | + $citybox.replaceWith( '<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />' ); |
| 140 | + } |
| 141 | + |
| 142 | + function disableCity( $citybox ) { |
| 143 | + $citybox.val( '' ).change(); |
| 144 | + $citybox.prop( 'disabled', true ); |
| 145 | + } |
| 146 | + |
| 147 | + function cityToSelect( $citybox, current_cities ) { |
| 148 | + var value = $citybox.val(); |
| 149 | + |
| 150 | + if ( $citybox.is('input') ) { |
| 151 | + var input_name = $citybox.attr( 'name' ); |
| 152 | + var input_id = $citybox.attr( 'id' ); |
| 153 | + var placeholder = $citybox.attr( 'placeholder' ); |
| 154 | + |
| 155 | + $citybox.replaceWith( '<select name="' + input_name + '" id="' + input_id + '" class="city_select" placeholder="' + placeholder + '"></select>' ); |
| 156 | + //we have to assign the new object, because of replaceWith |
| 157 | + $citybox = $('#'+input_id); |
| 158 | + } else { |
| 159 | + $citybox.prop( 'disabled', false ); |
| 160 | + } |
| 161 | + |
| 162 | + var options = ''; |
| 163 | + for( var index in current_cities ) { |
| 164 | + if ( current_cities.hasOwnProperty( index ) ) { |
| 165 | + var cityName = current_cities[ index ]; |
| 166 | + options = options + '<option value="' + cityName + '">' + cityName + '</option>'; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + $citybox.html( '<option value="">' + wc_city_select_params.i18n_select_city_text + '</option>' + options ); |
| 171 | + |
| 172 | + if ( $('option[value="'+value+'"]', $citybox).length ) { |
| 173 | + $citybox.val( value ).change(); |
| 174 | + } else { |
| 175 | + $citybox.val( '' ).change(); |
| 176 | + } |
| 177 | + |
| 178 | + $( document.body ).trigger( 'city_to_select' ); |
| 179 | + } |
| 180 | +}); |
0 commit comments