Move js into script file

This commit is contained in:
Mike Jolley 2017-05-12 15:03:00 +01:00
parent 8fe0f03a95
commit 0bf749aeac
4 changed files with 97 additions and 86 deletions

View File

@ -0,0 +1,83 @@
/*global ajaxurl */
;(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();
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 );
event.data.productExportForm.processStep( 1, $( this ).serialize(), '' );
};
/**
* Process the current export step.
*/
productExportForm.prototype.processStep = function( step, data, columns ) {
var $this = this,
selected_columns = $( '.woocommerce-exporter-columns' ).val(),
export_meta = $( '#woocommerce-exporter-meta:checked' ).length ? 1 : 0,
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,
export_types : export_types
},
dataType: 'json',
success: function( response ) {
if ( response.success ) {
if ( 'done' === response.data.step ) {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
$this.$form.removeClass( 'woocommerce-exporter__exporting' );
$this.$form.find('.woocommerce-exporter-button').prop( 'disabled', false );
window.location = response.data.url;
} else {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
$this.processStep( parseInt( response.data.step, 10 ), data, response.data.columns );
}
}
}
} ).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();
})( jQuery, window, document );

View File

@ -0,0 +1 @@
!function(a,b){var c=function(a){this.$form=a,this.xhr=!1,this.$form.find(".woocommerce-exporter-progress").val(0),this.processStep=this.processStep.bind(this),a.on("submit",{productExportForm:this},this.onSubmit)};c.prototype.onSubmit=function(b){b.preventDefault(),b.data.productExportForm.$form.addClass("woocommerce-exporter__exporting"),b.data.productExportForm.$form.find(".woocommerce-exporter-progress").val(0),b.data.productExportForm.$form.find(".woocommerce-exporter-button").prop("disabled",!0),b.data.productExportForm.processStep(1,a(this).serialize(),"")},c.prototype.processStep=function(c,d,e){var f=this,g=a(".woocommerce-exporter-columns").val(),h=a("#woocommerce-exporter-meta:checked").length?1:0,i=a(".woocommerce-exporter-types").val();a.ajax({type:"POST",url:ajaxurl,data:{form:d,action:"woocommerce_do_ajax_product_export",step:c,columns:e,selected_columns:g,export_meta:h,export_types:i},dataType:"json",success:function(a){a.success&&("done"===a.data.step?(f.$form.find(".woocommerce-exporter-progress").val(a.data.percentage),f.$form.removeClass("woocommerce-exporter__exporting"),f.$form.find(".woocommerce-exporter-button").prop("disabled",!1),b.location=a.data.url):(f.$form.find(".woocommerce-exporter-progress").val(a.data.percentage),f.processStep(parseInt(a.data.step,10),d,a.data.columns)))}}).fail(function(a){b.console.log(a)})},a.fn.wc_product_export_form=function(){return new c(this),this},a(".woocommerce-exporter").wc_product_export_form()}(jQuery,window,document);

View File

@ -21,6 +21,7 @@ class WC_Admin_Product_Export {
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ), 55 );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
add_action( 'admin_init', array( $this, 'download_export_file' ) );
add_action( 'wp_ajax_woocommerce_do_ajax_product_export', array( $this, 'do_ajax_product_export' ) );
}
@ -32,6 +33,14 @@ class WC_Admin_Product_Export {
add_submenu_page( 'edit.php?post_type=product', __( 'Import / Export', 'woocommerce' ), __( 'Import / Export', 'woocommerce' ), 'edit_products', 'woocommerce_importer', array( $this, 'admin_screen' ) );
}
/**
* Enqueue scripts.
*/
public function admin_scripts() {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script( 'wc-product-export', WC()->plugin_url() . '/assets/js/admin/wc-product-export' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
}
/**
* Export page UI.
*/

View File

@ -5,6 +5,8 @@
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
wp_enqueue_script( 'wc-product-export' );
?>
<div class="wrap woocommerce">
<h1><?php esc_html_e( 'Import / Export Data', 'woocommerce' ); ?></h1>
@ -33,7 +35,7 @@ if ( ! defined( 'ABSPATH' ) ) {
</tr>
<tr>
<th scope="row">
<label for="woocommerce-exporter-columns"><?php esc_html_e( 'Which columns should be exported?', 'woocommerce' ); ?></label>
<label for="woocommerce-exporter-columns"><?php esc_html_e( 'What product data should be exported?', 'woocommerce' ); ?></label>
</th>
<td>
<select id="woocommerce-exporter-columns" class="woocommerce-exporter-columns wc-enhanced-select" style="width:100%;" multiple data-placeholder="<?php esc_attr_e( 'Export all data', 'woocommerce' ); ?>">
@ -49,7 +51,7 @@ if ( ! defined( 'ABSPATH' ) ) {
</tr>
<tr>
<th scope="row">
<label for="woocommerce-exporter-meta"><?php esc_html_e( 'Export meta data?', 'woocommerce' ); ?></label>
<label for="woocommerce-exporter-meta"><?php esc_html_e( 'Export custom meta data?', 'woocommerce' ); ?></label>
</th>
<td>
<input type="checkbox" id="woocommerce-exporter-meta" value="1" />
@ -65,88 +67,4 @@ if ( ! defined( 'ABSPATH' ) ) {
<progress class="woocommerce-exporter-progress" max="100" value="0"></progress>
</div>
</form>
<script type="text/javascript">
;(function ( $, window, document ) {
/**
* 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();
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 );
event.data.productExportForm.processStep( 1, $( this ).serialize(), '' );
}
/**
* Process the current export step.
*/
productExportForm.prototype.processStep = function( step, data, columns ) {
var $this = this,
selected_columns = $( '.woocommerce-exporter-columns' ).val(),
export_meta = $( '#woocommerce-exporter-meta:checked' ).length ? 1 : 0,
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,
export_types : export_types
},
dataType: "json",
success: function( response ) {
if ( response.success ) {
if ( 'done' === response.data.step ) {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
$this.$form.removeClass( 'woocommerce-exporter__exporting' );
$this.$form.find('.woocommerce-exporter-button').prop( 'disabled', false );
window.location = response.data.url;
} else {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
$this.processStep( parseInt( response.data.step ), data, response.data.columns );
}
}
}
} ).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();
})( jQuery, window, document );
</script>
</div>