This commit is contained in:
Claudio Sanches 2015-01-13 10:03:50 -02:00
parent 1bddcf16aa
commit ab5d59b5ae
2 changed files with 83 additions and 0 deletions

82
assets/js/admin/users.js Normal file
View File

@ -0,0 +1,82 @@
/*global wc_users_params */
jQuery( function ( $ ) {
/**
* Users country and state fields
*/
var wc_users_fields = {
states: null,
init: function() {
if ( ! ( typeof wc_users_params.countries === 'undefined' ) ) {
/* State/Country select boxes */
this.states = $.parseJSON( wc_users_params.countries.replace( /"/g, '"' ) );
}
$( '.js_field-country' ).select2().change( this.change_country );
$( '.js_field-country' ).trigger( 'change', [ true ] );
$( 'body' ).on( 'change', 'select.js_field-state', this.change_state );
},
change_country: function( e, stickValue ) {
// Check for stickValue before using it
if ( typeof stickValue === 'undefined' ) {
stickValue = false;
}
// Prevent if we don't have the metabox data
if ( wc_users_fields.states === null ) {
return;
}
var $this = $( this ),
country = $this.val(),
$state = $this.parents( '.form-table' ).find( ':input.js_field-state' ),
$parent = $state.parent(),
input_name = $state.attr( 'name' ),
input_id = $state.attr( 'id' ),
value = $this.data( 'woocommerce.stickState-' + country ) ? $this.data( 'woocommerce.stickState-' + country ) : $state.val(),
placeholder = $state.attr( 'placeholder' );
if ( stickValue ){
$this.data( 'woocommerce.stickState-' + country, value );
}
// Remove the previous DOM element
$parent.show().find( '.select2-container' ).remove();
if ( ! $.isEmptyObject( wc_users_fields.states[ country ] ) ) {
var $states_select = $( '<select name="' + input_name + '" id="' + input_id + '" class="js_field-state" placeholder="' + placeholder + '" style="width: 25em;"></select>' ),
state = wc_users_fields.states[ country ];
$states_select.append( $( '<option value="">' + wc_users_params.i18n_select_state_text + '</option>' ) );
$.each( state, function( index, name ) {
$states_select.append( $( '<option value="' + index + '">' + state[ index ] + '</option>' ) );
} );
$states_select.val( value );
$state.replaceWith( $states_select );
$states_select.show().select2().hide().change();
} else {
$state.replaceWith( '<input type="text" class="js_field-state" name="' + input_name + '" id="' + input_id + '" value="' + value + '" placeholder="' + placeholder + '" />' );
}
$( 'body' ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
},
change_state: function() {
// Here we will find if state value on a select has changed and stick it to the country data
var $this = $( this ),
state = $this.val(),
$country = $this.parents( '.form-table' ).find( ':input.js_field-country' ),
country = $country.val();
$country.data( 'woocommerce.stickState-' + country, state );
},
}
wc_users_fields.init();
});

1
assets/js/admin/users.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(a){var b={states:null,init:function(){"undefined"!=typeof wc_users_params.countries&&(this.states=a.parseJSON(wc_users_params.countries.replace(/&quot;/g,'"'))),a(".js_field-country").select2().change(this.change_country),a(".js_field-country").trigger("change",[!0]),a("body").on("change","select.js_field-state",this.change_state)},change_country:function(c,d){if("undefined"==typeof d&&(d=!1),null!==b.states){var e=a(this),f=e.val(),g=e.parents(".form-table").find(":input.js_field-state"),h=g.parent(),i=g.attr("name"),j=g.attr("id"),k=e.data("woocommerce.stickState-"+f)?e.data("woocommerce.stickState-"+f):g.val(),l=g.attr("placeholder");if(d&&e.data("woocommerce.stickState-"+f,k),h.show().find(".select2-container").remove(),a.isEmptyObject(b.states[f]))g.replaceWith('<input type="text" class="js_field-state" name="'+i+'" id="'+j+'" value="'+k+'" placeholder="'+l+'" />');else{var m=a('<select name="'+i+'" id="'+j+'" class="js_field-state" placeholder="'+l+'" style="width: 25em;"></select>'),n=b.states[f];m.append(a('<option value="">'+wc_users_params.i18n_select_state_text+"</option>")),a.each(n,function(b){m.append(a('<option value="'+b+'">'+n[b]+"</option>"))}),m.val(k),g.replaceWith(m),m.show().select2().hide().change()}a("body").trigger("contry-change.woocommerce",[f,a(this).closest("div")])}},change_state:function(){var b=a(this),c=b.val(),d=b.parents(".form-table").find(":input.js_field-country"),e=d.val();d.data("woocommerce.stickState-"+e,c)}};b.init()});