2015-08-07 16:10:01 +00:00
|
|
|
/* global htmlSettingsTaxLocalizeScript */
|
2015-08-07 15:04:55 +00:00
|
|
|
/**
|
|
|
|
* Used by woocommerce/includes/admin/settings/views/html-settings-tax.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function($, data, wp){
|
|
|
|
$(function() {
|
2015-08-07 19:36:12 +00:00
|
|
|
|
2015-08-08 14:57:22 +00:00
|
|
|
var rowTemplate = wp.template( 'wc-tax-table-row' ),
|
2015-08-07 20:36:50 +00:00
|
|
|
paginationTemplate = wp.template( 'wc-tax-table-pagination' ),
|
2015-08-08 18:45:37 +00:00
|
|
|
$table = $( '.wc_tax_rates' ),
|
2015-08-08 14:57:22 +00:00
|
|
|
$tbody = $( '#rates' ),
|
|
|
|
$pagination = $( '#rates-pagination' );
|
2015-08-07 19:36:12 +00:00
|
|
|
|
2015-08-07 19:44:10 +00:00
|
|
|
/**
|
|
|
|
* Build the table contents.
|
|
|
|
* @param rates
|
|
|
|
*/
|
|
|
|
function renderTableContents( rates ) {
|
|
|
|
// Blank out the contents.
|
|
|
|
$tbody.empty();
|
|
|
|
|
|
|
|
// Populate $tbody with the current page of results.
|
|
|
|
$.each( rates, function ( id, rowData ) {
|
|
|
|
$tbody.append( rowTemplate( rowData ) );
|
2015-08-08 14:57:22 +00:00
|
|
|
});
|
2015-08-07 19:44:10 +00:00
|
|
|
|
|
|
|
// Initialize autocomplete for countries.
|
|
|
|
$tbody.find( 'td.country input' ).autocomplete({
|
|
|
|
source: data.countries,
|
|
|
|
minLength: 3
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize autocomplete for states.
|
|
|
|
$tbody.find( 'td.state input' ).autocomplete({
|
|
|
|
source: data.states,
|
|
|
|
minLength: 3
|
|
|
|
});
|
2015-08-07 19:47:26 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
$tbody.find( 'td.postcode input, td.city input').change(function() {
|
|
|
|
$(this).attr( 'name', $(this).data( 'name' ) );
|
|
|
|
});
|
2015-08-07 19:44:10 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 20:02:45 +00:00
|
|
|
/**
|
2015-08-07 20:58:42 +00:00
|
|
|
* Renders table contents by page.
|
2015-08-07 20:02:45 +00:00
|
|
|
*/
|
2015-08-07 20:58:42 +00:00
|
|
|
function renderPage( page_num ) {
|
|
|
|
var qty_pages = Math.ceil( data.rates.length / data.limit );
|
|
|
|
|
|
|
|
page_num = parseInt( page_num, 10 );
|
|
|
|
if ( page_num < 1 ) {
|
|
|
|
page_num = 1;
|
|
|
|
} else if ( page_num > qty_pages ) {
|
|
|
|
page_num = qty_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
var first_index = data.limit * ( page_num - 1),
|
|
|
|
last_index = data.limit * page_num;
|
2015-08-07 19:44:10 +00:00
|
|
|
|
2015-08-07 20:02:45 +00:00
|
|
|
renderTableContents( data.rates.slice( first_index, last_index ) );
|
|
|
|
|
2015-08-07 20:58:42 +00:00
|
|
|
if ( data.rates.length > data.limit ) {
|
|
|
|
// We've now displayed our initial page, time to render the pagination box.
|
|
|
|
$pagination.html( paginationTemplate( {
|
|
|
|
qty_rates : data.rates.length,
|
|
|
|
current_page : page_num,
|
|
|
|
qty_pages : qty_pages
|
|
|
|
} ) );
|
|
|
|
}
|
2015-08-07 20:02:45 +00:00
|
|
|
}
|
2015-08-07 15:42:49 +00:00
|
|
|
|
2015-08-07 20:58:42 +00:00
|
|
|
/**
|
|
|
|
* Handle the initial display.
|
|
|
|
*/
|
|
|
|
renderPage( data.page );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle clicks on the pagination links.
|
|
|
|
*
|
|
|
|
* Abstracting it out here instead of re-running it after each render.
|
|
|
|
*/
|
|
|
|
$pagination.on( 'click', 'a', function(event){
|
|
|
|
event.preventDefault();
|
|
|
|
renderPage( $( event.currentTarget ).data('goto') );
|
|
|
|
} );
|
2015-08-07 21:21:44 +00:00
|
|
|
$pagination.on( 'change', 'input', function(event) {
|
|
|
|
renderPage( $( event.currentTarget ).val() );
|
|
|
|
} );
|
2015-08-07 20:58:42 +00:00
|
|
|
|
2015-08-08 14:57:46 +00:00
|
|
|
$table.find('.remove_tax_rates').click(function() {
|
2015-08-07 19:36:12 +00:00
|
|
|
if ( $tbody.find('tr.current').length > 0 ) {
|
|
|
|
var $current = $tbody.find('tr.current');
|
|
|
|
$current.find('input').val('');
|
|
|
|
$current.find('input.remove_tax_rate').val('1');
|
2015-08-07 15:42:49 +00:00
|
|
|
|
2015-08-07 19:36:12 +00:00
|
|
|
$current.each(function(){
|
|
|
|
if ( $(this).is('.new') ) {
|
|
|
|
$( this ).remove();
|
2015-08-07 15:42:49 +00:00
|
|
|
} else {
|
2015-08-07 19:36:12 +00:00
|
|
|
$( this ).hide();
|
2015-08-07 15:42:49 +00:00
|
|
|
}
|
2015-08-07 19:36:12 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
window.alert( data.strings.no_rows_selected );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2015-08-08 14:57:03 +00:00
|
|
|
* Handle the exporting of tax rates, and build it off the global `data.rates` object.
|
2015-08-07 19:36:12 +00:00
|
|
|
*
|
2015-08-08 14:57:03 +00:00
|
|
|
* @todo: Have the `export` button save the current form and generate this from php, so there's no chance the current page is out of date.
|
2015-08-07 19:36:12 +00:00
|
|
|
*/
|
2015-08-08 14:57:46 +00:00
|
|
|
$table.find('.export').click(function() {
|
2015-08-07 19:36:12 +00:00
|
|
|
var csv_data = 'data:application/csv;charset=utf-8,' + data.strings.csv_data_cols.join(',') + '\n';
|
|
|
|
|
2015-08-08 14:57:03 +00:00
|
|
|
$.each( data.rates, function( id, rowData ) {
|
2015-08-07 19:36:12 +00:00
|
|
|
var row = '';
|
2015-08-08 14:57:03 +00:00
|
|
|
|
|
|
|
row += rowData.tax_rate_country + ',';
|
|
|
|
row += rowData.tax_rate_state + ',';
|
|
|
|
row += rowData.tax_rate_postcode ? rowData.tax_rate_postcode.join( '; ' ) : '' + ',';
|
|
|
|
row += rowData.tax_rate_city ? rowData.tax_rate_city.join( '; ' ) : '' + ',';
|
|
|
|
row += rowData.tax_rate + ',';
|
|
|
|
row += rowData.tax_rate_name + ',';
|
|
|
|
row += rowData.tax_rate_priority + ',';
|
|
|
|
row += rowData.tax_rate_compound + ',';
|
|
|
|
row += rowData.tax_rate_shipping + ',';
|
|
|
|
row += data.current_class;
|
|
|
|
|
|
|
|
csv_data += row + '\n';
|
2015-08-07 15:42:49 +00:00
|
|
|
});
|
|
|
|
|
2015-08-07 19:36:12 +00:00
|
|
|
$(this).attr( 'href', encodeURI( csv_data ) );
|
2015-08-07 15:42:49 +00:00
|
|
|
|
2015-08-07 19:36:12 +00:00
|
|
|
return true;
|
|
|
|
});
|
2015-08-07 15:42:49 +00:00
|
|
|
|
2015-08-07 19:36:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new blank row to the table for the user to fill out and save.
|
|
|
|
*/
|
2015-08-08 14:57:46 +00:00
|
|
|
$table.find('.insert').click(function() {
|
2015-08-07 19:36:12 +00:00
|
|
|
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 ) {
|
|
|
|
$tbody.find('tr.current').after( code );
|
|
|
|
} else {
|
|
|
|
$tbody.append( code );
|
|
|
|
}
|
|
|
|
|
|
|
|
$( 'td.country input' ).autocomplete({
|
|
|
|
source: data.countries,
|
|
|
|
minLength: 3
|
|
|
|
});
|
2015-08-07 15:42:49 +00:00
|
|
|
|
2015-08-07 19:36:12 +00:00
|
|
|
$( 'td.state input' ).autocomplete({
|
|
|
|
source: data.states,
|
|
|
|
minLength: 3
|
|
|
|
});
|
2015-08-07 15:42:49 +00:00
|
|
|
|
2015-08-07 19:36:12 +00:00
|
|
|
return false;
|
2015-08-07 15:42:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2015-08-07 15:04:55 +00:00
|
|
|
})(jQuery, htmlSettingsTaxLocalizeScript, wp);
|