2015-08-14 15:10:55 +00:00
|
|
|
/* global htmlSettingsTaxLocalizeScript, ajaxurl */
|
2016-01-23 16:51:43 +00:00
|
|
|
|
2015-08-07 15:04:55 +00:00
|
|
|
/**
|
|
|
|
* Used by woocommerce/includes/admin/settings/views/html-settings-tax.php
|
|
|
|
*/
|
2015-10-27 20:19:30 +00:00
|
|
|
( function( $, data, wp, ajaxurl ) {
|
|
|
|
$( function() {
|
2015-08-07 19:36:12 +00:00
|
|
|
|
2015-08-10 21:22:03 +00:00
|
|
|
if ( ! String.prototype.trim ) {
|
|
|
|
String.prototype.trim = function () {
|
|
|
|
return this.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '' );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-08-08 14:57:22 +00:00
|
|
|
var rowTemplate = wp.template( 'wc-tax-table-row' ),
|
2015-08-12 19:29:17 +00:00
|
|
|
rowTemplateEmpty = wp.template( 'wc-tax-table-row-empty' ),
|
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' ),
|
2015-10-08 12:18:00 +00:00
|
|
|
$save_button = $( 'input[name="save"]' ),
|
2015-08-08 18:46:21 +00:00
|
|
|
$pagination = $( '#rates-pagination' ),
|
2015-08-12 18:30:26 +00:00
|
|
|
$search_field = $( '#rates-search .wc-tax-rates-search-field' ),
|
2015-08-13 20:57:54 +00:00
|
|
|
$submit = $( '.submit .button-primary[type=submit]' ),
|
2015-08-10 21:24:06 +00:00
|
|
|
WCTaxTableModelConstructor = Backbone.Model.extend({
|
2015-10-27 20:19:30 +00:00
|
|
|
changes: {},
|
|
|
|
setRateAttribute: function( rateID, attribute, value ) {
|
2015-08-13 15:59:34 +00:00
|
|
|
var rates = _.indexBy( this.get( 'rates' ), 'tax_rate_id' ),
|
|
|
|
changes = {};
|
2015-08-10 21:24:06 +00:00
|
|
|
|
|
|
|
if ( rates[ rateID ][ attribute ] !== value ) {
|
2015-08-13 15:59:34 +00:00
|
|
|
changes[ rateID ] = {};
|
|
|
|
changes[ rateID ][ attribute ] = value;
|
|
|
|
rates[ rateID ][ attribute ] = value;
|
2015-08-10 21:24:06 +00:00
|
|
|
}
|
2015-08-13 15:59:34 +00:00
|
|
|
|
|
|
|
this.logChanges( changes );
|
2015-08-12 18:31:07 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
logChanges: function( changedRows ) {
|
2015-08-13 15:59:12 +00:00
|
|
|
var changes = this.changes || {};
|
|
|
|
|
|
|
|
_.each( changedRows, function( row, id ) {
|
2016-01-23 16:51:43 +00:00
|
|
|
changes[ id ] = _.extend( changes[ id ] || {
|
|
|
|
tax_rate_id : id
|
|
|
|
}, row );
|
2015-08-12 21:18:32 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-13 15:59:12 +00:00
|
|
|
this.changes = changes;
|
|
|
|
this.trigger( 'change:rates' );
|
2015-08-12 21:18:32 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
getFilteredRates: function() {
|
2015-08-12 18:31:07 +00:00
|
|
|
var rates = this.get( 'rates' ),
|
|
|
|
search = $search_field.val().toLowerCase();
|
|
|
|
|
|
|
|
if ( search.length ) {
|
|
|
|
rates = _.filter( rates, function( rate ) {
|
2015-08-12 18:41:52 +00:00
|
|
|
var search_text = _.toArray( rate ).join( ' ' ).toLowerCase();
|
2015-08-12 18:31:07 +00:00
|
|
|
return ( -1 !== search_text.indexOf( search ) );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2015-08-12 21:18:46 +00:00
|
|
|
rates = _.sortBy( rates, function( rate ) {
|
|
|
|
return parseInt( rate.tax_rate_order, 10 );
|
|
|
|
} );
|
2015-08-12 19:29:33 +00:00
|
|
|
|
2015-08-12 18:31:07 +00:00
|
|
|
return rates;
|
2015-08-13 20:57:54 +00:00
|
|
|
},
|
2015-12-19 14:12:31 +00:00
|
|
|
block: function() {
|
|
|
|
$( '.wc_tax_rates' ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
unblock: function() {
|
|
|
|
$( '.wc_tax_rates' ).unblock();
|
2015-08-13 20:57:54 +00:00
|
|
|
},
|
2015-12-19 14:12:31 +00:00
|
|
|
save: function() {
|
|
|
|
var self = this;
|
2015-08-13 21:54:31 +00:00
|
|
|
|
2015-12-19 14:12:31 +00:00
|
|
|
self.block();
|
|
|
|
|
|
|
|
Backbone.ajax({
|
|
|
|
method: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
url: ajaxurl + '?action=woocommerce_tax_rates_save_changes',
|
|
|
|
data: {
|
|
|
|
current_class: data.current_class,
|
|
|
|
wc_tax_nonce: data.wc_tax_nonce,
|
|
|
|
changes: self.changes
|
|
|
|
},
|
|
|
|
success: function( response, textStatus ) {
|
|
|
|
if ( 'success' === textStatus ) {
|
|
|
|
WCTaxTableModelInstance.set( 'rates', response.data.rates );
|
|
|
|
WCTaxTableModelInstance.trigger( 'change:rates' );
|
|
|
|
|
|
|
|
WCTaxTableModelInstance.changes = {};
|
|
|
|
WCTaxTableModelInstance.trigger( 'saved:rates' );
|
2016-01-23 16:51:43 +00:00
|
|
|
|
|
|
|
// Reload view.
|
|
|
|
WCTaxTableInstance.render();
|
2015-12-19 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.unblock();
|
|
|
|
}
|
|
|
|
});
|
2015-08-10 21:24:06 +00:00
|
|
|
}
|
|
|
|
} ),
|
2015-10-27 20:19:30 +00:00
|
|
|
WCTaxTableViewConstructor = Backbone.View.extend({
|
|
|
|
rowTemplate: rowTemplate,
|
|
|
|
per_page: data.limit,
|
|
|
|
page: data.page,
|
|
|
|
initialize: function() {
|
2015-10-27 21:33:12 +00:00
|
|
|
var qty_pages = Math.ceil( _.toArray( this.model.get( 'rates' ) ).length / this.per_page );
|
|
|
|
|
|
|
|
this.qty_pages = 0 === qty_pages ? 1 : qty_pages;
|
2015-08-13 17:01:42 +00:00
|
|
|
this.page = this.sanitizePage( data.page );
|
|
|
|
|
|
|
|
this.listenTo( this.model, 'change:rates', this.setUnloadConfirmation );
|
2015-08-13 20:57:54 +00:00
|
|
|
this.listenTo( this.model, 'saved:rates', this.clearUnloadConfirmation );
|
2016-01-27 12:14:21 +00:00
|
|
|
$tbody.on( 'change autocompletechange', ':input', { view: this }, this.updateModelOnChange );
|
2015-10-27 21:33:12 +00:00
|
|
|
$tbody.on( 'sortupdate', { view: this }, this.updateModelOnSort );
|
|
|
|
$search_field.on( 'keyup search', { view: this }, this.onSearchField );
|
|
|
|
$pagination.on( 'click', 'a', { view: this }, this.onPageChange );
|
|
|
|
$pagination.on( 'change', 'input', { view: this }, this.onPageChange );
|
|
|
|
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
|
|
|
|
$submit.on( 'click', { view: this }, this.onSubmit );
|
2015-10-27 20:19:30 +00:00
|
|
|
$save_button.attr( 'disabled','disabled' );
|
2015-08-13 17:07:20 +00:00
|
|
|
|
|
|
|
// Can bind these directly to the buttons, as they won't get overwritten.
|
2015-10-27 21:33:12 +00:00
|
|
|
$table.find( '.insert' ).on( 'click', { view: this }, this.onAddNewRow );
|
|
|
|
$table.find( '.remove_tax_rates' ).on( 'click', { view: this }, this.onDeleteRow );
|
|
|
|
$table.find( '.export' ).on( 'click', { view: this }, this.onExport );
|
2015-08-13 17:01:42 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
render: function() {
|
2015-08-12 18:43:48 +00:00
|
|
|
var rates = this.model.getFilteredRates(),
|
2015-08-12 19:03:50 +00:00
|
|
|
qty_rates = _.size( rates ),
|
2015-08-08 18:46:21 +00:00
|
|
|
qty_pages = Math.ceil( qty_rates / this.per_page ),
|
2015-10-27 21:33:12 +00:00
|
|
|
first_index = 0 === qty_rates ? 0 : this.per_page * ( this.page - 1 ),
|
2015-08-08 18:46:21 +00:00
|
|
|
last_index = this.per_page * this.page,
|
2015-08-12 18:43:48 +00:00
|
|
|
paged_rates = _.toArray( rates ).slice( first_index, last_index ),
|
2015-08-08 18:46:21 +00:00
|
|
|
view = this;
|
|
|
|
|
|
|
|
// Blank out the contents.
|
|
|
|
this.$el.empty();
|
|
|
|
|
2015-08-12 19:29:17 +00:00
|
|
|
if ( paged_rates.length ) {
|
|
|
|
// Populate $tbody with the current page of results.
|
2015-10-27 20:19:30 +00:00
|
|
|
$.each( paged_rates, function( id, rowData ) {
|
2015-08-12 19:29:17 +00:00
|
|
|
view.$el.append( view.rowTemplate( rowData ) );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
view.$el.append( rowTemplateEmpty() );
|
|
|
|
}
|
2015-08-08 18:46:21 +00:00
|
|
|
|
|
|
|
// Initialize autocomplete for countries.
|
|
|
|
this.$el.find( 'td.country input' ).autocomplete({
|
|
|
|
source: data.countries,
|
2015-08-10 22:37:53 +00:00
|
|
|
minLength: 2
|
2015-08-08 18:46:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize autocomplete for states.
|
|
|
|
this.$el.find( 'td.state input' ).autocomplete({
|
|
|
|
source: data.states,
|
|
|
|
minLength: 3
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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)
|
2015-10-27 20:19:30 +00:00
|
|
|
this.$el.find( 'td.postcode input, td.city input' ).change( function() {
|
|
|
|
$( this ).attr( 'name', $( this ).data( 'name' ) );
|
2015-08-08 18:46:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if ( qty_pages > 1 ) {
|
|
|
|
// We've now displayed our initial page, time to render the pagination box.
|
|
|
|
$pagination.html( paginationTemplate( {
|
2015-10-27 20:19:30 +00:00
|
|
|
qty_rates: qty_rates,
|
|
|
|
current_page: this.page,
|
|
|
|
qty_pages: qty_pages
|
2015-08-08 18:46:21 +00:00
|
|
|
} ) );
|
2016-01-12 12:01:10 +00:00
|
|
|
} else {
|
|
|
|
$pagination.empty();
|
|
|
|
view.page = 1;
|
2015-08-08 18:46:21 +00:00
|
|
|
}
|
2015-08-13 16:13:05 +00:00
|
|
|
|
|
|
|
// Disable sorting if there is a search term filtering the items.
|
|
|
|
if ( $search_field.val() ) {
|
|
|
|
$tbody.sortable( 'disable' );
|
|
|
|
} else {
|
|
|
|
$tbody.sortable( 'enable' );
|
|
|
|
}
|
2015-08-08 18:46:21 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
updateUrl: function() {
|
2015-08-12 18:19:45 +00:00
|
|
|
if ( ! window.history.replaceState ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = data.base_url,
|
|
|
|
search = $search_field.val();
|
|
|
|
|
|
|
|
if ( 1 < this.page ) {
|
|
|
|
url += '&p=' + encodeURIComponent( this.page );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( search.length ) {
|
|
|
|
url += '&s=' + encodeURIComponent( search );
|
|
|
|
}
|
|
|
|
|
|
|
|
window.history.replaceState( {}, '', url );
|
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
onSubmit: function( event ) {
|
2015-08-13 20:57:54 +00:00
|
|
|
event.data.view.model.save();
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
onAddNewRow: function( event ) {
|
2015-08-13 18:10:47 +00:00
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
2015-10-27 21:33:12 +00:00
|
|
|
rates = _.indexBy( model.get( 'rates' ), 'tax_rate_id' ),
|
2015-08-13 18:10:47 +00:00
|
|
|
changes = {},
|
|
|
|
size = _.size( rates ),
|
|
|
|
newRow = _.extend( {}, data.default_rate, {
|
2015-10-27 20:19:30 +00:00
|
|
|
tax_rate_id: 'new-' + size + '-' + Date.now(),
|
|
|
|
newRow: true
|
2015-08-13 18:10:47 +00:00
|
|
|
} ),
|
2015-08-13 18:27:50 +00:00
|
|
|
$current, current_id, current_order, rates_to_reorder, reordered_rates;
|
2015-08-13 17:07:20 +00:00
|
|
|
|
2015-10-08 11:57:42 +00:00
|
|
|
$current = $tbody.children( '.current' );
|
2015-08-13 18:10:47 +00:00
|
|
|
|
2015-10-08 11:57:42 +00:00
|
|
|
if ( $current.length ) {
|
|
|
|
current_id = $current.last().data( 'id' );
|
|
|
|
current_order = parseInt( rates[ current_id ].tax_rate_order, 10 );
|
2015-08-13 18:10:47 +00:00
|
|
|
newRow.tax_rate_order = 1 + current_order;
|
|
|
|
|
|
|
|
rates_to_reorder = _.filter( rates, function( rate ) {
|
|
|
|
if ( parseInt( rate.tax_rate_order, 10 ) > current_order ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
|
|
|
|
reordered_rates = _.map( rates_to_reorder, function( rate ) {
|
|
|
|
rate.tax_rate_order++;
|
|
|
|
changes[ rate.tax_rate_id ] = _.extend( changes[ rate.tax_rate_id ] || {}, { tax_rate_order : rate.tax_rate_order } );
|
|
|
|
return rate;
|
|
|
|
} );
|
2015-08-13 17:07:20 +00:00
|
|
|
} else {
|
2015-08-13 18:10:47 +00:00
|
|
|
newRow.tax_rate_order = 1 + _.max(
|
|
|
|
_.pluck( rates, 'tax_rate_order' ),
|
|
|
|
function ( val ) {
|
|
|
|
// Cast them all to integers, because strings compare funky. Sighhh.
|
|
|
|
return parseInt( val, 10 );
|
|
|
|
}
|
|
|
|
);
|
2016-01-12 12:01:10 +00:00
|
|
|
// Move the last page
|
|
|
|
view.page = view.qty_pages;
|
2015-08-13 17:07:20 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 18:10:47 +00:00
|
|
|
rates[ newRow.tax_rate_id ] = newRow;
|
|
|
|
changes[ newRow.tax_rate_id ] = newRow;
|
2015-08-13 17:07:20 +00:00
|
|
|
|
2015-08-13 18:10:47 +00:00
|
|
|
model.set( 'rates', rates );
|
|
|
|
model.logChanges( changes );
|
2015-08-13 17:07:20 +00:00
|
|
|
|
2015-08-13 18:10:47 +00:00
|
|
|
view.render();
|
2015-08-13 17:07:20 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
onDeleteRow: function( event ) {
|
2015-08-13 18:25:38 +00:00
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
2015-10-27 21:33:12 +00:00
|
|
|
rates = _.indexBy( model.get( 'rates' ), 'tax_rate_id' ),
|
2015-08-13 18:25:38 +00:00
|
|
|
changes = {},
|
2016-01-12 12:01:10 +00:00
|
|
|
$current, current_id;
|
2015-08-13 18:25:38 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
2015-08-13 17:07:20 +00:00
|
|
|
|
2015-08-13 18:25:38 +00:00
|
|
|
if ( $current = $tbody.children( '.current' ) ) {
|
2015-08-13 17:07:20 +00:00
|
|
|
$current.each(function(){
|
2015-08-13 18:25:38 +00:00
|
|
|
current_id = $( this ).data('id');
|
|
|
|
|
|
|
|
delete rates[ current_id ];
|
|
|
|
|
|
|
|
changes[ current_id ] = _.extend( changes[ current_id ] || {}, { deleted : 'deleted' } );
|
2015-08-13 17:07:20 +00:00
|
|
|
});
|
2015-08-13 18:25:38 +00:00
|
|
|
|
|
|
|
model.set( 'rates', rates );
|
|
|
|
model.logChanges( changes );
|
|
|
|
|
|
|
|
view.render();
|
2015-08-13 17:07:20 +00:00
|
|
|
} else {
|
|
|
|
window.alert( data.strings.no_rows_selected );
|
|
|
|
}
|
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
onSearchField: function( event ){
|
2015-08-12 19:01:27 +00:00
|
|
|
event.data.view.updateUrl();
|
|
|
|
event.data.view.render();
|
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
onPageChange: function( event ) {
|
2015-08-12 19:01:27 +00:00
|
|
|
var $target = $( event.currentTarget );
|
|
|
|
|
|
|
|
event.preventDefault();
|
2015-10-27 21:33:12 +00:00
|
|
|
event.data.view.page = $target.data( 'goto' ) ? $target.data( 'goto' ) : $target.val();
|
2015-08-12 19:05:00 +00:00
|
|
|
event.data.view.render();
|
|
|
|
event.data.view.updateUrl();
|
2015-08-12 19:01:27 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
onExport: function( event ) {
|
2015-08-13 18:58:19 +00:00
|
|
|
var csv_data = 'data:application/csv;charset=utf-8,' + data.strings.csv_data_cols.join(',') + '\n';
|
|
|
|
|
2015-10-08 12:18:00 +00:00
|
|
|
$.each( event.data.view.model.getFilteredRates(), function( id, rowData ) {
|
2015-08-13 18:58:19 +00:00
|
|
|
var row = '';
|
|
|
|
|
|
|
|
row += rowData.tax_rate_country + ',';
|
|
|
|
row += rowData.tax_rate_state + ',';
|
|
|
|
row += ( rowData.postcode ? rowData.postcode.join( '; ' ) : '' ) + ',';
|
|
|
|
row += ( rowData.city ? rowData.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-10-27 20:19:30 +00:00
|
|
|
$( this ).attr( 'href', encodeURI( csv_data ) );
|
2015-08-13 18:58:19 +00:00
|
|
|
|
|
|
|
return true;
|
2015-08-13 19:22:05 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
setUnloadConfirmation: function() {
|
2015-08-10 14:29:11 +00:00
|
|
|
this.needsUnloadConfirm = true;
|
2015-10-27 20:19:30 +00:00
|
|
|
$save_button.removeAttr( 'disabled' );
|
2015-08-10 14:29:11 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
clearUnloadConfirmation: function() {
|
2015-08-10 14:29:11 +00:00
|
|
|
this.needsUnloadConfirm = false;
|
2015-10-27 20:19:30 +00:00
|
|
|
$save_button.attr( 'disabled', 'disabled' );
|
2015-08-10 14:29:11 +00:00
|
|
|
},
|
2015-10-27 21:33:12 +00:00
|
|
|
unloadConfirmation: function( event ) {
|
2015-08-10 20:27:04 +00:00
|
|
|
if ( event.data.view.needsUnloadConfirm ) {
|
|
|
|
event.returnValue = data.strings.unload_confirmation_msg;
|
2015-08-10 14:29:11 +00:00
|
|
|
window.event.returnValue = data.strings.unload_confirmation_msg;
|
|
|
|
return data.strings.unload_confirmation_msg;
|
|
|
|
}
|
2015-08-08 18:46:21 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
updateModelOnChange: function( event ) {
|
2015-08-10 21:24:37 +00:00
|
|
|
var model = event.data.view.model,
|
|
|
|
$target = $( event.target ),
|
2015-10-27 20:19:30 +00:00
|
|
|
id = $target.closest( 'tr' ).data( 'id' ),
|
|
|
|
attribute = $target.data( 'attribute' ),
|
2015-08-10 21:24:37 +00:00
|
|
|
val = $target.val();
|
|
|
|
|
|
|
|
if ( 'city' === attribute || 'postcode' === attribute ) {
|
2015-10-27 21:33:12 +00:00
|
|
|
val = val.split( ';' );
|
2015-08-10 21:24:37 +00:00
|
|
|
val = $.map( val, function( thing ) {
|
|
|
|
return thing.trim();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'tax_rate_compound' === attribute || 'tax_rate_shipping' === attribute ) {
|
2015-10-27 21:33:12 +00:00
|
|
|
if ( $target.is( ':checked' ) ) {
|
2015-08-10 21:24:37 +00:00
|
|
|
val = 1;
|
|
|
|
} else {
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
model.setRateAttribute( id, attribute, val );
|
|
|
|
},
|
2016-01-12 12:01:10 +00:00
|
|
|
updateModelOnSort: function( event ) {
|
2015-08-12 21:18:32 +00:00
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
2015-10-27 20:19:30 +00:00
|
|
|
rates = _.indexBy( model.get( 'rates' ), 'tax_rate_id' ),
|
2016-01-12 12:01:10 +00:00
|
|
|
changes = {};
|
2015-08-12 21:18:32 +00:00
|
|
|
|
2016-01-12 12:01:10 +00:00
|
|
|
_.each( rates, function( rate ) {
|
|
|
|
var new_position = 0;
|
|
|
|
var old_position = parseInt( rate.tax_rate_order, 10 );
|
2015-08-12 21:18:32 +00:00
|
|
|
|
2016-01-12 12:01:10 +00:00
|
|
|
if ( $table.find( 'tr[data-id="' + rate.tax_rate_id + '"]').size() ) {
|
|
|
|
new_position = parseInt( $table.find( 'tr[data-id="' + rate.tax_rate_id + '"]').index(), 10 ) + parseInt( ( view.page - 1 ) * view.per_page, 10 );
|
|
|
|
} else {
|
|
|
|
new_position = old_position;
|
2015-08-12 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 12:01:10 +00:00
|
|
|
if ( old_position !== new_position ) {
|
|
|
|
changes[ rate.tax_rate_id ] = _.extend( changes[ rate.tax_rate_id ] || {}, { tax_rate_order : new_position } );
|
|
|
|
}
|
2015-08-12 21:18:32 +00:00
|
|
|
} );
|
|
|
|
|
2016-01-12 12:01:10 +00:00
|
|
|
if ( _.size( changes ) ) {
|
2015-08-13 15:59:12 +00:00
|
|
|
model.logChanges( changes );
|
2015-08-12 21:26:10 +00:00
|
|
|
}
|
2015-08-12 21:18:32 +00:00
|
|
|
},
|
2015-10-27 20:19:30 +00:00
|
|
|
sanitizePage: function( page_num ) {
|
2015-08-08 18:46:21 +00:00
|
|
|
page_num = parseInt( page_num, 10 );
|
|
|
|
if ( page_num < 1 ) {
|
|
|
|
page_num = 1;
|
|
|
|
} else if ( page_num > this.qty_pages ) {
|
|
|
|
page_num = this.qty_pages;
|
|
|
|
}
|
|
|
|
return page_num;
|
|
|
|
}
|
|
|
|
} ),
|
|
|
|
WCTaxTableModelInstance = new WCTaxTableModelConstructor({
|
2015-10-27 20:19:30 +00:00
|
|
|
rates: data.rates
|
2015-08-08 18:46:21 +00:00
|
|
|
} ),
|
|
|
|
WCTaxTableInstance = new WCTaxTableViewConstructor({
|
2015-10-27 20:19:30 +00:00
|
|
|
model: WCTaxTableModelInstance,
|
|
|
|
el: '#rates'
|
2015-08-08 18:46:21 +00:00
|
|
|
} );
|
2015-08-07 15:42:49 +00:00
|
|
|
|
2015-08-08 18:46:37 +00:00
|
|
|
WCTaxTableInstance.render();
|
2015-08-07 20:58:42 +00:00
|
|
|
|
2015-08-07 15:42:49 +00:00
|
|
|
});
|
2015-10-27 20:19:30 +00:00
|
|
|
})( jQuery, htmlSettingsTaxLocalizeScript, wp, ajaxurl );
|