woocommerce/assets/js/admin/wc-product-export.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-05-12 19:15:08 +00:00
/*global ajaxurl, wc_product_export_params */
2017-05-12 14:03:00 +00:00
;(function ( $, window ) {
/**
* productExportForm handles the export process.
*/
var productExportForm = function( $form ) {
this.$form = $form;
this.xhr = false;
// Initial state.
this.$form.find('.woocommerce-exporter-progress').val( 0 );
// Methods.
this.processStep = this.processStep.bind( this );
// Events.
$form.on( 'submit', { productExportForm: this }, this.onSubmit );
};
/**
* Handle export form submission.
*/
productExportForm.prototype.onSubmit = function( event ) {
event.preventDefault();
2018-01-02 13:29:23 +00:00
var currentDate = new Date(),
day = currentDate.getDate(),
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear(),
timestamp = currentDate.getTime(),
filename = 'wc-product-export-' + day + '-' + month + '-' + year + '-' + timestamp + '.csv';
2017-05-12 14:03:00 +00:00
event.data.productExportForm.$form.addClass( 'woocommerce-exporter__exporting' );
event.data.productExportForm.$form.find('.woocommerce-exporter-progress').val( 0 );
event.data.productExportForm.$form.find('.woocommerce-exporter-button').prop( 'disabled', true );
2018-01-02 13:29:23 +00:00
event.data.productExportForm.processStep( 1, $( this ).serialize(), '', filename );
2017-05-12 14:03:00 +00:00
};
/**
* Process the current export step.
*/
2018-01-02 13:29:23 +00:00
productExportForm.prototype.processStep = function( step, data, columns, filename ) {
var $this = this,
2017-05-12 14:03:00 +00:00
selected_columns = $( '.woocommerce-exporter-columns' ).val(),
export_meta = $( '#woocommerce-exporter-meta:checked' ).length ? 1: 0,
2017-05-12 14:03:00 +00:00
export_types = $( '.woocommerce-exporter-types' ).val();
$.ajax( {
type: 'POST',
url: ajaxurl,
data: {
form : data,
action : 'woocommerce_do_ajax_product_export',
step : step,
columns : columns,
selected_columns : selected_columns,
export_meta : export_meta,
2017-05-12 19:15:08 +00:00
export_types : export_types,
2018-01-02 13:29:23 +00:00
filename : filename,
2017-05-12 19:15:08 +00:00
security : wc_product_export_params.export_nonce
2017-05-12 14:03:00 +00:00
},
dataType: 'json',
success: function( response ) {
if ( response.success ) {
if ( 'done' === response.data.step ) {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
window.location = response.data.url;
2017-05-12 14:26:53 +00:00
setTimeout( function() {
$this.$form.removeClass( 'woocommerce-exporter__exporting' );
$this.$form.find('.woocommerce-exporter-button').prop( 'disabled', false );
}, 2000 );
2017-05-12 14:03:00 +00:00
} else {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
2018-01-02 13:29:23 +00:00
$this.processStep( parseInt( response.data.step, 10 ), data, response.data.columns, filename );
2017-05-12 14:03:00 +00:00
}
}
}
} ).fail( function( response ) {
window.console.log( response );
} );
};
/**
* Function to call productExportForm on jquery selector.
*/
$.fn.wc_product_export_form = function() {
new productExportForm( this );
return this;
};
$( '.woocommerce-exporter' ).wc_product_export_form();
2017-05-15 09:42:10 +00:00
})( jQuery, window );