city-select.js
182 lines
| 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 | console.log([country, state, $container ]); |
| 96 | $( document.body ).trigger( 'state_changing', [country, state, $container ] ); |
| 97 | }); |
| 98 | |
| 99 | $( 'body' ).on( 'state_changing', function(e, country, state, $container) { |
| 100 | var $citybox = $container.find( '#billing_city, #shipping_city, #calc_shipping_city' ); |
| 101 | |
| 102 | if ( cities[ country ] ) { |
| 103 | /* if the country has no states */ |
| 104 | if( cities[country] instanceof Array) { |
| 105 | cityToSelect( $citybox, cities[ country ] ); |
| 106 | } else if ( state ) { |
| 107 | if ( cities[ country ][ state ] ) { |
| 108 | cityToSelect( $citybox, cities[ country ][ state ] ); |
| 109 | } else { |
| 110 | cityToInput( $citybox ); |
| 111 | } |
| 112 | } else { |
| 113 | disableCity( $citybox ); |
| 114 | } |
| 115 | } else { |
| 116 | cityToInput( $citybox ); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | /* Ajax replaces .cart_totals (child of .cart-collaterals) on shipping calculator */ |
| 121 | if ( $( '.cart-collaterals' ).length && $( '#calc_shipping_state' ).length ) { |
| 122 | var calc_observer = new MutationObserver( function() { |
| 123 | $( '#calc_shipping_state' ).change(); |
| 124 | }); |
| 125 | calc_observer.observe( document.querySelector( '.cart-collaterals' ), { childList: true }); |
| 126 | } |
| 127 | |
| 128 | function cityToInput( $citybox ) { |
| 129 | if ( $citybox.is('input') ) { |
| 130 | $citybox.prop( 'disabled', false ); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | var input_name = $citybox.attr( 'name' ); |
| 135 | var input_id = $citybox.attr( 'id' ); |
| 136 | var placeholder = $citybox.attr( 'placeholder' ); |
| 137 | |
| 138 | $citybox.parent().find( '.select2-container' ).remove(); |
| 139 | |
| 140 | $citybox.replaceWith( '<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />' ); |
| 141 | } |
| 142 | |
| 143 | function disableCity( $citybox ) { |
| 144 | $citybox.val( '' ).change(); |
| 145 | $citybox.prop( 'disabled', true ); |
| 146 | } |
| 147 | |
| 148 | function cityToSelect( $citybox, current_cities ) { |
| 149 | var value = $citybox.val(); |
| 150 | |
| 151 | if ( $citybox.is('input') ) { |
| 152 | var input_name = $citybox.attr( 'name' ); |
| 153 | var input_id = $citybox.attr( 'id' ); |
| 154 | var placeholder = $citybox.attr( 'placeholder' ); |
| 155 | |
| 156 | $citybox.replaceWith( '<select name="' + input_name + '" id="' + input_id + '" class="city_select" placeholder="' + placeholder + '"></select>' ); |
| 157 | //we have to assign the new object, because of replaceWith |
| 158 | $citybox = $('#'+input_id); |
| 159 | } else { |
| 160 | $citybox.prop( 'disabled', false ); |
| 161 | } |
| 162 | |
| 163 | var options = ''; |
| 164 | for( var index in current_cities ) { |
| 165 | if ( current_cities.hasOwnProperty( index ) ) { |
| 166 | var cityName = current_cities[ index ]; |
| 167 | options = options + '<option value="' + cityName + '">' + cityName + '</option>'; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | $citybox.html( '<option value="">' + wc_city_select_params.i18n_select_city_text + '</option>' + options ); |
| 172 | |
| 173 | if ( $('option[value="'+value+'"]', $citybox).length ) { |
| 174 | $citybox.val( value ).change(); |
| 175 | } else { |
| 176 | $citybox.val( '' ).change(); |
| 177 | } |
| 178 | |
| 179 | $( document.body ).trigger( 'city_to_select' ); |
| 180 | } |
| 181 | }); |
| 182 |