Bind price and decimal input events to body in woocommerce_admin js

The event was bound to the element directly, causing problems when the elements was added after dom ready (through ajax). Now the decimal format check will always take place.
This commit is contained in:
Mike Martel 2014-04-17 12:50:43 +02:00
parent 3d584bc09a
commit e0dc31620b
1 changed files with 40 additions and 43 deletions

View File

@ -4,50 +4,47 @@
jQuery(function(){ jQuery(function(){
// Price input validation // Price input validation
jQuery(".wc_input_decimal[type=text], .wc_input_price[type=text]") jQuery('body').on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text]', function() {
.bind( 'blur', function() {
jQuery('.wc_error_tip').fadeOut('100', function(){ jQuery(this).remove(); } ); jQuery('.wc_error_tip').fadeOut('100', function(){ jQuery(this).remove(); } );
return this; return this;
}); });
jQuery(".wc_input_price[type=text]") jQuery('body').on('keyup change', '.wc_input_price[type=text]', function(){
.bind( 'keyup change', function() { var value = jQuery(this).val();
var value = jQuery(this).val(); var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" );
var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" ); var newvalue = value.replace( regex, '' );
var newvalue = value.replace( regex, '' );
if ( value !== newvalue ) { if ( value !== newvalue ) {
jQuery(this).val( newvalue ); jQuery(this).val( newvalue );
if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) { if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) {
var offset = jQuery(this).position(); var offset = jQuery(this).position();
jQuery(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18n_mon_decimal_error + '</div>' ); jQuery(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18n_mon_decimal_error + '</div>' );
jQuery('.wc_error_tip') jQuery('.wc_error_tip')
.css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) ) .css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) )
.css('top', offset.top + jQuery(this).height() ) .css('top', offset.top + jQuery(this).height() )
.fadeIn('100'); .fadeIn('100');
} }
} }
return this; return this;
}); });
jQuery(".wc_input_decimal[type=text]") jQuery('body').on('keyup change', '.wc_input_price[type=text]', function(){
.bind( 'keyup change', function() { var value = jQuery(this).val();
var value = jQuery(this).val(); var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.decimal_point + "]+", "gi" );
var regex = new RegExp( "[^0-9\%.\\" + woocommerce_admin.decimal_point + "]+", "gi" ); var newvalue = value.replace( regex, '' );
var newvalue = value.replace( regex, '' );
if ( value !== newvalue ) { if ( value !== newvalue ) {
jQuery(this).val( newvalue ); jQuery(this).val( newvalue );
if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) { if ( jQuery(this).parent().find('.wc_error_tip').size() == 0 ) {
var offset = jQuery(this).position(); var offset = jQuery(this).position();
jQuery(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18n_decimal_error + '</div>' ); jQuery(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18n_decimal_error + '</div>' );
jQuery('.wc_error_tip') jQuery('.wc_error_tip')
.css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) ) .css('left', offset.left + jQuery(this).width() - ( jQuery(this).width() / 2 ) - ( jQuery('.wc_error_tip').width() / 2 ) )
.css('top', offset.top + jQuery(this).height() ) .css('top', offset.top + jQuery(this).height() )
.fadeIn('100'); .fadeIn('100');
} }
} }
return this; return this;
}); });
jQuery("body").click(function(){ jQuery("body").click(function(){
@ -56,11 +53,11 @@ jQuery(function(){
// Tooltips // Tooltips
jQuery(".tips, .help_tip").tipTip({ jQuery(".tips, .help_tip").tipTip({
'attribute' : 'data-tip', 'attribute' : 'data-tip',
'fadeIn' : 50, 'fadeIn' : 50,
'fadeOut' : 50, 'fadeOut' : 50,
'delay' : 200 'delay' : 200
}); });
// wc_input_table tables // wc_input_table tables
jQuery('.wc_input_table.sortable tbody').sortable({ jQuery('.wc_input_table.sortable tbody').sortable({
@ -101,7 +98,7 @@ jQuery(function(){
jQuery('.wc_input_table').on( 'focus click', 'input', function( e ) { jQuery('.wc_input_table').on( 'focus click', 'input', function( e ) {
$this_table = jQuery(this).closest('table'); $this_table = jQuery(this).closest('table');
$this_row = jQuery(this).closest('tr'); $this_row = jQuery(this).closest('tr');
if ( ( e.type == 'focus' && hasFocus != $this_row.index() ) || ( e.type == 'click' && jQuery(this).is(':focus') ) ) { if ( ( e.type == 'focus' && hasFocus != $this_row.index() ) || ( e.type == 'click' && jQuery(this).is(':focus') ) ) {
@ -144,7 +141,7 @@ jQuery(function(){
jQuery( '.woocommerce_page_wc-settings .shippingrows tbody tr:even' ).addClass( 'alternate' ); jQuery( '.woocommerce_page_wc-settings .shippingrows tbody tr:even' ).addClass( 'alternate' );
// Availability inputs // Availability inputs
jQuery('select.availability').change(function(){ jQuery('select.availability').change(function(){
if ( jQuery(this).val() == "all" ) { if ( jQuery(this).val() == "all" ) {
jQuery(this).closest('tr').next('tr').hide(); jQuery(this).closest('tr').next('tr').hide();
} else { } else {