woocommerce/assets/js/frontend/country-select.min.js

1 line
3.0 KiB
JavaScript

jQuery(function(u){if("undefined"==typeof wc_country_select_params)return!1;if(u().selectWoo){var t=function(){u("select.country_select:visible, select.state_select:visible").each(function(){var t=u.extend({placeholderOption:"first",width:"100%"},{language:{errorLoading:function(){return wc_country_select_params.i18n_searching},inputTooLong:function(t){var e=t.input.length-t.maximum;return 1===e?wc_country_select_params.i18n_input_too_long_1:wc_country_select_params.i18n_input_too_long_n.replace("%qty%",e)},inputTooShort:function(t){var e=t.minimum-t.input.length;return 1===e?wc_country_select_params.i18n_input_too_short_1:wc_country_select_params.i18n_input_too_short_n.replace("%qty%",e)},loadingMore:function(){return wc_country_select_params.i18n_load_more},maximumSelected:function(t){return 1===t.maximum?wc_country_select_params.i18n_selection_too_long_1:wc_country_select_params.i18n_selection_too_long_n.replace("%qty%",t.maximum)},noResults:function(){return wc_country_select_params.i18n_no_matches},searching:function(){return wc_country_select_params.i18n_searching}}});u(this).selectWoo(t),u(this).on("select2:select",function(){u(this).focus()})})};t(),u(document.body).bind("country_to_state_changed",function(){t()})}var e=wc_country_select_params.countries.replace(/&quot;/g,'"'),p=u.parseJSON(e);u(document.body).on("change","select.country_to_state, input.country_to_state",function(){var t=u(this).closest(".woocommerce-billing-fields, .woocommerce-shipping-fields, .woocommerce-shipping-calculator");t.length||(t=u(this).closest(".form-row").parent());var e=u(this).val(),n=t.find("#billing_state, #shipping_state, #calc_shipping_state"),o=n.closest("p.form-row"),c=n.attr("name"),i=n.attr("id"),r=n.val(),a=n.attr("placeholder")||n.attr("data-placeholder")||"";if(p[e])if(u.isEmptyObject(p[e]))n.closest("p.form-row").hide().find(".select2-container").remove(),n.replaceWith('<input type="hidden" class="hidden" name="'+c+'" id="'+i+'" value="" placeholder="'+a+'" />'),u(document.body).trigger("country_to_state_changed",[e,t]);else{var s="",_=p[e];for(var l in _)_.hasOwnProperty(l)&&(s=s+'<option value="'+l+'">'+_[l]+"</option>");n.closest("p.form-row").show(),n.is("input")&&(n.replaceWith('<select name="'+c+'" id="'+i+'" class="state_select" data-placeholder="'+a+'"></select>'),n=t.find("#billing_state, #shipping_state, #calc_shipping_state")),n.html('<option value="">'+wc_country_select_params.i18n_select_state_text+"</option>"+s),n.val(r).change(),u(document.body).trigger("country_to_state_changed",[e,t])}else n.is("select")?(o.show().find(".select2-container").remove(),n.replaceWith('<input type="text" class="input-text" name="'+c+'" id="'+i+'" placeholder="'+a+'" />'),u(document.body).trigger("country_to_state_changed",[e,t])):n.is('input[type="hidden"]')&&(o.show().find(".select2-container").remove(),n.replaceWith('<input type="text" class="input-text" name="'+c+'" id="'+i+'" placeholder="'+a+'" />'),u(document.body).trigger("country_to_state_changed",[e,t]));u(document.body).trigger("country_to_state_changing",[e,t])})});