Select2 frontend

This commit is contained in:
Mike Jolley 2015-01-12 16:16:10 +00:00
parent 60b23e6f6f
commit 0fdd24c526
5 changed files with 38 additions and 25 deletions

File diff suppressed because one or more lines are too long

View File

@ -1286,9 +1286,12 @@ p.demo_store {
box-shadow: none;
}
.select2-container {
width: 100%;
}
&.woocommerce-invalid {
.chosen-single,
.chosen-drop,
.select2-container,
input.input-text,
select {
border-color: $red;
@ -1296,8 +1299,7 @@ p.demo_store {
}
&.woocommerce-validated {
.chosen-single,
.chosen-drop,
.select2-container,
input.input-text,
select {
border-color: $green - #111;
@ -1516,6 +1518,10 @@ p.demo_store {
}
}
.select2-results {
line-height: 1.5em;
}
/**
* Account page
*/

View File

@ -5,18 +5,22 @@ jQuery( function( $ ) {
return false;
}
/*
// Frontend Chosen selects
$( 'select.country_select, select.state_select' ).chosen( { search_contains: true } );
$( 'body' ).bind( 'country_to_state_changed', function() {
$( 'select.state_select' ).chosen().trigger( 'chosen:updated' );
});
*/
// Select2 Enhancement if it exists
if( $().select2 ) {
var wc_country_select_select2 = function() {
$( 'select.country_select, select.state_select' ).each(function(){
$(this).select2({
minimumResultsForSearch: 10,
placeholder: $( this ).attr( 'placeholder' ),
placeholderOption: 'first'
});
});
};
wc_country_select_select2();
$( 'body' ).bind( 'country_to_state_changed', function() {
wc_country_select_select2();
});
}
/* State/Country select boxes */
var states_json = wc_country_select_params.countries.replace( /"/g, '"' ),
@ -35,7 +39,7 @@ jQuery( function( $ ) {
if ( states[ country ] ) {
if ( $.isEmptyObject( states[ country ] ) ) {
$statebox.parent().hide().find( '.chosen-container' ).remove();
$statebox.parent().hide().find( '.select2-container' ).remove();
$statebox.replaceWith( '<input type="hidden" class="hidden" name="' + input_name + '" id="' + input_id + '" value="" placeholder="' + placeholder + '" />' );
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
@ -69,14 +73,14 @@ jQuery( function( $ ) {
} else {
if ( $statebox.is( 'select' ) ) {
$parent.show().find( '.chosen-container' ).remove();
$parent.show().find( '.select2-container' ).remove();
$statebox.replaceWith( '<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />' );
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
} else if ( $statebox.is( '.hidden' ) ) {
$parent.show().find( '.chosen-container' ).remove();
$parent.show().find( '.select2-container' ).remove();
$statebox.replaceWith( '<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />' );
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );

View File

@ -1 +1 @@
jQuery(function(a){if("undefined"==typeof wc_country_select_params)return!1;var b=wc_country_select_params.countries.replace(/&quot;/g,'"'),c=a.parseJSON(b);a("select.country_to_state, input.country_to_state").change(function(){var b=a(this).val(),d=a(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state"),e=d.parent(),f=d.attr("name"),g=d.attr("id"),h=d.val(),i=d.attr("placeholder");if(c[b])if(a.isEmptyObject(c[b]))d.parent().hide().find(".chosen-container").remove(),d.replaceWith('<input type="hidden" class="hidden" name="'+f+'" id="'+g+'" value="" placeholder="'+i+'" />'),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")]);else{var j="",k=c[b];for(var l in k)k.hasOwnProperty(l)&&(j=j+'<option value="'+l+'">'+k[l]+"</option>");d.parent().show(),d.is("input")&&(d.replaceWith('<select name="'+f+'" id="'+g+'" class="state_select" placeholder="'+i+'"></select>'),d=a(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state")),d.html('<option value="">'+wc_country_select_params.i18n_select_state_text+"</option>"+j),d.val(h),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")])}else d.is("select")?(e.show().find(".chosen-container").remove(),d.replaceWith('<input type="text" class="input-text" name="'+f+'" id="'+g+'" placeholder="'+i+'" />'),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")])):d.is(".hidden")&&(e.show().find(".chosen-container").remove(),d.replaceWith('<input type="text" class="input-text" name="'+f+'" id="'+g+'" placeholder="'+i+'" />'),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")]));a("body").trigger("country_to_state_changing",[b,a(this).closest("div")])}),a(function(){a("select.country_to_state, input.country_to_state").change()})});
jQuery(function(a){if("undefined"==typeof wc_country_select_params)return!1;if(a().select2){var b=function(){a("select.country_select, select.state_select").each(function(){a(this).select2({minimumResultsForSearch:10,placeholder:a(this).attr("placeholder"),placeholderOption:"first"})})};b(),a("body").bind("country_to_state_changed",function(){b()})}var c=wc_country_select_params.countries.replace(/&quot;/g,'"'),d=a.parseJSON(c);a("select.country_to_state, input.country_to_state").change(function(){var b=a(this).val(),c=a(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state"),e=c.parent(),f=c.attr("name"),g=c.attr("id"),h=c.val(),i=c.attr("placeholder");if(d[b])if(a.isEmptyObject(d[b]))c.parent().hide().find(".select2-container").remove(),c.replaceWith('<input type="hidden" class="hidden" name="'+f+'" id="'+g+'" value="" placeholder="'+i+'" />'),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")]);else{var j="",k=d[b];for(var l in k)k.hasOwnProperty(l)&&(j=j+'<option value="'+l+'">'+k[l]+"</option>");c.parent().show(),c.is("input")&&(c.replaceWith('<select name="'+f+'" id="'+g+'" class="state_select" placeholder="'+i+'"></select>'),c=a(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state")),c.html('<option value="">'+wc_country_select_params.i18n_select_state_text+"</option>"+j),c.val(h),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")])}else c.is("select")?(e.show().find(".select2-container").remove(),c.replaceWith('<input type="text" class="input-text" name="'+f+'" id="'+g+'" placeholder="'+i+'" />'),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")])):c.is(".hidden")&&(e.show().find(".select2-container").remove(),c.replaceWith('<input type="text" class="input-text" name="'+f+'" id="'+g+'" placeholder="'+i+'" />'),a("body").trigger("country_to_state_changed",[b,a(this).closest("div")]));a("body").trigger("country_to_state_changing",[b,a(this).closest("div")])}),a(function(){a("select.country_to_state, input.country_to_state").change()})});

View File

@ -112,8 +112,11 @@ class WC_Frontend_Scripts {
$assets_path = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';
$frontend_script_path = $assets_path . 'js/frontend/';
// Register any scripts for later use, or used as dependencies
// Chosen is @deprecated as of 2.3 in favour of 2.3. Here for backwards compatibility.
self::register_script( 'chosen', $assets_path . 'js/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), '1.0.0' );
self::register_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js', array( 'jquery' ), '3.5.2' );
// Register any scripts for later use, or used as dependencies
self::register_script( 'jquery-blockui', $assets_path . 'js/jquery-blockui/jquery.blockUI' . $suffix . '.js', array( 'jquery' ), '2.60' );
self::register_script( 'jquery-payment', $assets_path . 'js/jquery-payment/jquery.payment' . $suffix . '.js', array( 'jquery' ), '1.0.2' );
self::register_script( 'jquery-cookie', $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js', array( 'jquery' ), '1.3.1' );
@ -130,9 +133,9 @@ class WC_Frontend_Scripts {
if ( is_cart() ) {
self::enqueue_script( 'wc-cart', $frontend_script_path . 'cart' . $suffix . '.js', array( 'jquery', 'wc-country-select' ) );
}
if ( apply_filters( 'woocommerce_chosen_country_select', true ) && ( is_checkout() || is_page( get_option( 'woocommerce_myaccount_page_id' ) ) ) ) {
self::enqueue_script( 'wc-chosen', $frontend_script_path . 'chosen-frontend' . $suffix . '.js', array( 'chosen' ) );
wp_enqueue_style( 'woocommerce_chosen_styles', $assets_path . 'css/chosen.css' );
if ( is_checkout() || is_page( get_option( 'woocommerce_myaccount_page_id' ) ) ) {
self::enqueue_script( 'select2' );
wp_enqueue_style( 'select2', $assets_path . 'css/select2.css' );
}
if ( is_checkout() ) {
self::enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ) );