frontend country-select jshinted and coding standards

This commit is contained in:
splashingpixels 2014-03-18 20:51:37 -07:00
parent f9f8b11d3a
commit 880d655539
2 changed files with 44 additions and 39 deletions

View File

@ -1,72 +1,77 @@
jQuery(function($) { jQuery( function( $ ) {
// wc_country_select_params is required to continue, ensure the object exists // wc_country_select_params is required to continue, ensure the object exists
if (typeof wc_country_select_params === "undefined") if ( typeof wc_country_select_params === 'undefined' ) {
return false; return false;
}
/* State/Country select boxes */ /* State/Country select boxes */
var states_json = wc_country_select_params.countries.replace(/"/g, '"'); var states_json = wc_country_select_params.countries.replace( /"/g, '"' ),
var states = $.parseJSON( states_json ); states = $.parseJSON( states_json );
$('select.country_to_state, input.country_to_state').change(function(){ $( 'select.country_to_state, input.country_to_state' ).change( function() {
var country = $(this).val(); var country = $( this ).val(),
$statebox = $( this ).closest( 'div' ).find( '#billing_state, #shipping_state, #calc_shipping_state' ),
$parent = $statebox.parent(),
input_name = $statebox.attr( 'name' ),
input_id = $statebox.attr( 'id' ),
value = $statebox.val(),
placeholder = $statebox.attr( 'placeholder' );
var $statebox = $(this).closest('div').find('#billing_state, #shipping_state, #calc_shipping_state'); if ( states[ country ] ) {
var $parent = $statebox.parent(); if ( states[ country ].length === 0 ) {
var input_name = $statebox.attr('name'); $statebox.parent().hide().find( '.chosen-container' ).remove();
var input_id = $statebox.attr('id'); $statebox.replaceWith( '<input type="hidden" class="hidden" name="' + input_name + '" id="' + input_id + '" value="" placeholder="' + placeholder + '" />' );
var value = $statebox.val();
var placeholder = $statebox.attr('placeholder');
if (states[country]) { $( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
if (states[country].length == 0) {
$statebox.parent().hide().find('.chosen-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')]);
} else { } else {
var options = ''; var options = '',
var state = states[country]; state = states[ country ];
for(var index in state) {
options = options + '<option value="' + index + '">' + state[index] + '</option>'; for( var index in state ) {
if ( state.hasOwnProperty( index ) ) {
options = options + '<option value="' + index + '">' + state[ index ] + '</option>';
}
} }
$statebox.parent().show(); $statebox.parent().show();
if ($statebox.is('input')) {
if ( $statebox.is( 'input' ) ) {
// Change for select // Change for select
$statebox.replaceWith('<select name="' + input_name + '" id="' + input_id + '" class="state_select" placeholder="' + placeholder + '"></select>'); $statebox.replaceWith( '<select name="' + input_name + '" id="' + input_id + '" class="state_select" placeholder="' + placeholder + '"></select>' );
$statebox = $(this).closest('div').find('#billing_state, #shipping_state, #calc_shipping_state'); $statebox = $( this ).closest( 'div' ).find( '#billing_state, #shipping_state, #calc_shipping_state' );
} }
$statebox.html( '<option value="">' + wc_country_select_params.i18n_select_state_text + '</option>' + options);
$statebox.val(value); $statebox.html( '<option value="">' + wc_country_select_params.i18n_select_state_text + '</option>' + options );
$('body').trigger('country_to_state_changed', [country, $(this).closest('div')]); $statebox.val( value );
$( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
} }
} else { } else {
if ($statebox.is('select')) { if ( $statebox.is( 'select' ) ) {
$parent.show().find('.chosen-container').remove(); $parent.show().find( '.chosen-container' ).remove();
$statebox.replaceWith('<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />'); $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')]); $( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
} else if ($statebox.is('.hidden')) { } else if ( $statebox.is( '.hidden' ) ) {
$parent.show().find('.chosen-container').remove(); $parent.show().find( '.chosen-container' ).remove();
$statebox.replaceWith('<input type="text" class="input-text" name="' + input_name + '" id="' + input_id + '" placeholder="' + placeholder + '" />'); $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')]); $( 'body' ).trigger( 'country_to_state_changed', [country, $( this ).closest( 'div' )] );
} }
} }
$('body').trigger('country_to_state_changing', [country, $(this).closest('div')]); $( 'body' ).trigger( 'country_to_state_changing', [country, $( this ).closest( 'div' )] );
}).change(); }).change();

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(0==c[b].length)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)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")])}).change()}); jQuery(function(e){if(typeof wc_country_select_params==="undefined"){return false}var t=wc_country_select_params.countries.replace(/&quot;/g,'"'),n=e.parseJSON(t);e("select.country_to_state, input.country_to_state").change(function(){var t=e(this).val(),r=e(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state"),i=r.parent(),s=r.attr("name"),o=r.attr("id"),u=r.val(),a=r.attr("placeholder");if(n[t]){if(n[t].length===0){r.parent().hide().find(".chosen-container").remove();r.replaceWith('<input type="hidden" class="hidden" name="'+s+'" id="'+o+'" value="" placeholder="'+a+'" />');e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}else{var f="",l=n[t];for(var c in l){if(l.hasOwnProperty(c)){f=f+'<option value="'+c+'">'+l[c]+"</option>"}}r.parent().show();if(r.is("input")){r.replaceWith('<select name="'+s+'" id="'+o+'" class="state_select" placeholder="'+a+'"></select>');r=e(this).closest("div").find("#billing_state, #shipping_state, #calc_shipping_state")}r.html('<option value="">'+wc_country_select_params.i18n_select_state_text+"</option>"+f);r.val(u);e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}}else{if(r.is("select")){i.show().find(".chosen-container").remove();r.replaceWith('<input type="text" class="input-text" name="'+s+'" id="'+o+'" placeholder="'+a+'" />');e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}else if(r.is(".hidden")){i.show().find(".chosen-container").remove();r.replaceWith('<input type="text" class="input-text" name="'+s+'" id="'+o+'" placeholder="'+a+'" />');e("body").trigger("country_to_state_changed",[t,e(this).closest("div")])}}e("body").trigger("country_to_state_changing",[t,e(this).closest("div")])}).change()})