Ernest the JS so none of it evaluates before dom ready.

This commit is contained in:
George Stephanis 2015-08-07 15:36:12 -04:00
parent 846dcb95c6
commit d297a1ac62
1 changed files with 83 additions and 82 deletions

View File

@ -4,10 +4,11 @@
*/ */
(function($, data, wp){ (function($, data, wp){
var rowTemplate = wp.template( 'wc-tax-table-row' ),
$tbody = $('#rates');
$(function() { $(function() {
var rowTemplate = wp.template( 'wc-tax-table-row' ),
$tbody = $('#rates');
// Blank out the contents. // Blank out the contents.
$tbody.innerHTML = ''; $tbody.innerHTML = '';
@ -27,100 +28,100 @@
source: data.states, source: data.states,
minLength: 3 minLength: 3
}); });
});
$('.wc_tax_rates .remove_tax_rates').click(function() { $('.wc_tax_rates .remove_tax_rates').click(function() {
if ( $tbody.find('tr.current').length > 0 ) { if ( $tbody.find('tr.current').length > 0 ) {
var $current = $tbody.find('tr.current'); var $current = $tbody.find('tr.current');
$current.find('input').val(''); $current.find('input').val('');
$current.find('input.remove_tax_rate').val('1'); $current.find('input.remove_tax_rate').val('1');
$current.each(function(){ $current.each(function(){
if ( $(this).is('.new') ) { if ( $(this).is('.new') ) {
$( this ).remove(); $( this ).remove();
} else {
$( this ).hide();
}
});
} else {
window.alert( data.strings.no_rows_selected );
}
return false;
});
/**
* Handle the exporting of tax rates.
*
* As an aside: Why is this being handled in Javascript instead of being built by php? -George
*/
$('.wc_tax_rates .export').click(function() {
var csv_data = 'data:application/csv;charset=utf-8,' + data.strings.csv_data_cols.join(',') + '\n';
$('#rates tr:visible').each(function() {
var row = '';
$(this).find('td:not(.sort) input').each(function() {
var val = '';
if ( $(this).is('.checkbox') ) {
if ( $(this).is(':checked') ) {
val = 1;
} else { } else {
val = 0; $( this ).hide();
} }
} else { });
val = $(this).val(); } else {
if ( ! val ) { window.alert( data.strings.no_rows_selected );
val = $( this ).attr( 'placeholder' ); }
return false;
});
/**
* Handle the exporting of tax rates.
*
* As an aside: Why is this being handled in Javascript instead of being built by php? -George
*/
$('.wc_tax_rates .export').click(function() {
var csv_data = 'data:application/csv;charset=utf-8,' + data.strings.csv_data_cols.join(',') + '\n';
$('#rates tr:visible').each(function() {
var row = '';
$(this).find('td:not(.sort) input').each(function() {
var val = '';
if ( $(this).is('.checkbox') ) {
if ( $(this).is(':checked') ) {
val = 1;
} else {
val = 0;
}
} else {
val = $(this).val();
if ( ! val ) {
val = $( this ).attr( 'placeholder' );
}
} }
} row = row + val + ',';
row = row + val + ','; });
row = row + data.current_class;
//row.substring( 0, row.length - 1 );
csv_data = csv_data + row + '\n';
}); });
row = row + data.current_class;
//row.substring( 0, row.length - 1 ); $(this).attr( 'href', encodeURI( csv_data ) );
csv_data = csv_data + row + '\n';
return true;
}); });
$(this).attr( 'href', encodeURI( csv_data ) ); /**
* Add a new blank row to the table for the user to fill out and save.
*/
$('.wc_tax_rates .insert').click(function() {
var size = $tbody.find('tr').length;
var code = wp.template( 'wc-tax-table-row' )( {
tax_rate_id : 'new-' + size,
tax_rate_priority : 1,
tax_rate_shipping : 1,
newRow : true
} );
return true; if ( $tbody.find('tr.current').length > 0 ) {
}); $tbody.find('tr.current').after( code );
} else {
$tbody.append( code );
}
/** $( 'td.country input' ).autocomplete({
* Add a new blank row to the table for the user to fill out and save. source: data.countries,
*/ minLength: 3
$('.wc_tax_rates .insert').click(function() { });
var size = $tbody.find('tr').length;
var code = wp.template( 'wc-tax-table-row' )( {
tax_rate_id : 'new-' + size,
tax_rate_priority : 1,
tax_rate_shipping : 1,
newRow : true
} );
if ( $tbody.find('tr.current').length > 0 ) { $( 'td.state input' ).autocomplete({
$tbody.find('tr.current').after( code ); source: data.states,
} else { minLength: 3
$tbody.append( code ); });
}
$( 'td.country input' ).autocomplete({ return false;
source: data.countries,
minLength: 3
}); });
$( 'td.state input' ).autocomplete({ /**
source: data.states, * Postcode and city don't have `name` values by default. They're only created if the contents changes, to save on database queries (I think)
minLength: 3 */
$('.wc_tax_rates td.postcode, .wc_tax_rates td.city').find('input').change(function() {
$(this).attr( 'name', $(this).attr( 'data-name' ) );
}); });
return false;
}); });
/**
* Postcode and city don't have `name` values by default. They're only created if the contents changes, to save on database queries (I think)
*/
$('.wc_tax_rates td.postcode, .wc_tax_rates td.city').find('input').change(function() {
$(this).attr( 'name', $(this).attr( 'data-name' ) );
});
})(jQuery, htmlSettingsTaxLocalizeScript, wp); })(jQuery, htmlSettingsTaxLocalizeScript, wp);