Made checkout JS more modular

This commit is contained in:
Mike Jolley 2015-01-06 17:39:09 +00:00
parent 8a28054130
commit 054d499636
3 changed files with 419 additions and 425 deletions

View File

@ -1,18 +1,170 @@
jQuery( function( $ ) {
// wc_checkout_params is required to continue, ensure the object exists
if ( typeof wc_checkout_params === 'undefined' ) {
return false;
}
$.blockUI.defaults.overlayCSS.cursor = 'default';
// wc_checkout_params is required to continue, ensure the object exists
if ( typeof wc_checkout_params === 'undefined' )
return false;
var wc_checkout_form = {
updateTimer: false,
dirtyInput: false,
xhr: false,
$order_review: $( '#order_review' ),
$checkout_form: $( 'form.checkout' ),
init: function() {
$( 'body' ).bind( 'update_checkout', this.reset_update_checkout_timer );
$( 'body' ).bind( 'update_checkout', this.update_checkout );
$( 'body' ).bind( 'init_checkout', this.init_checkout );
var updateTimer,
dirtyInput = false,
xhr;
// Payment methods
this.$order_review.on( 'click', '.payment_methods input.input-radio', this.payment_method_selected );
function update_checkout() {
// Form submission
this.$checkout_form.on( 'submit', this.submit );
if ( xhr ) xhr.abort();
// Inline validation
this.$checkout_form.on( 'blur input change', '.input-text, select', this.validate_field );
// Inputs/selects which update totals
this.$checkout_form.on( 'input change', 'select.shipping_method, input[name^=shipping_method], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type=radio]', this.trigger_update_checkout );
this.$checkout_form.on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', this.maybe_input_changed );
this.$checkout_form.on( 'input change', '.address-field select', this.input_changed );
this.$checkout_form.on( 'input keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', this.queue_update_checkout );
// Address fields
this.$checkout_form.on( 'change', '#ship-to-different-address input', this.ship_to_different_address );
// Trigger events
this.$order_review.find( 'input[name=payment_method]:checked' ).triggerHandler( 'click' );
this.$checkout_form.find( '#ship-to-different-address input' ).change();
// Update on page load
if ( wc_checkout_params.is_checkout === '1' ) {
$( 'body' ).trigger( 'init_checkout' );
}
if ( wc_checkout_params.option_guest_checkout === 'yes' ) {
$( 'input#createaccount' ).change( this.toggle_create_account ).change();
}
},
toggle_create_account: function( e ) {
$( 'div.create-account' ).hide();
if ( $( this ).is( ':checked' ) ) {
$( 'div.create-account' ).slideDown();
}
},
init_checkout: function( e ) {
$( '#billing_country, #shipping_country, .country_to_state' ).change();
$( 'body' ).trigger( 'update_checkout' );
},
maybe_input_changed: function( e ) {
if ( wc_checkout_form.dirtyInput ) {
wc_checkout_form.input_changed();
}
},
input_changed: function( e ) {
wc_checkout_form.dirtyInput = this;
wc_checkout_form.maybe_update_checkout();
},
queue_update_checkout: function( e ) {
var code = e.keyCode || e.which || 0;
if ( code === 9 ) {
return true;
}
wc_checkout_form.dirtyInput = this;
wc_checkout_form.reset_update_checkout_timer();
wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.maybe_update_checkout, '1000' );
},
trigger_update_checkout: function( e ) {
wc_checkout_form.reset_update_checkout_timer();
wc_checkout_form.dirtyInput = false;
$( 'body' ).trigger( 'update_checkout' );
},
maybe_update_checkout: function() {
var update_totals = true;
if ( $( wc_checkout_form.dirtyInput ).size() ) {
$required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
if ( $required_inputs.size() ) {
$required_inputs.each( function( e ) {
if ( $( this ).find( 'input.input-text' ).val() === '' ) {
update_totals = false;
}
});
}
}
if ( update_totals ) {
wc_checkout_form.trigger_update_checkout();
}
},
ship_to_different_address: function( e ) {
$( 'div.shipping_address' ).hide();
if ( $( this ).is( ':checked' ) ) {
$( 'div.shipping_address' ).slideDown();
}
},
payment_method_selected: function( e ) {
if ( $( '.payment_methods input.input-radio' ).length > 1 ) {
var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) );
if ( $( this ).is( ':checked' ) && ! target_payment_box.is( ':visible' ) ) {
$( 'div.payment_box' ).filter( ':visible' ).slideUp( 250 );
if ( $( this ).is( ':checked' ) ) {
$( 'div.payment_box.' + $( this ).attr( 'ID' ) ).slideDown( 250 );
}
}
} else {
$( 'div.payment_box' ).show();
}
if ( $( this ).data( 'order_button_text' ) ) {
$( '#place_order' ).val( $( this ).data( 'order_button_text' ) );
} else {
$( '#place_order' ).val( $( '#place_order' ).data( 'value' ) );
}
},
reset_update_checkout_timer: function() {
clearTimeout( wc_checkout_form.updateTimer );
},
validate_field: function( e ) {
var $this = $( this ),
$parent = $this.closest( '.form-row' ),
validated = true;
if ( $parent.is( '.validate-required' ) ) {
if ( $this.val() === '' ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
validated = false;
}
}
if ( $parent.is( '.validate-email' ) ) {
if ( $this.val() ) {
/* http://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
if ( ! pattern.test( $this.val() ) ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' );
validated = false;
}
}
}
if ( validated ) {
$parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field' ).addClass( 'woocommerce-validated' );
}
},
update_checkout: function( e ) {
if ( wc_checkout_form.xhr ) {
wc_checkout_form.xhr.abort();
}
var shipping_methods = [];
@ -78,27 +230,20 @@ jQuery( function( $ ) {
post_data: $( 'form.checkout' ).serialize()
};
xhr = $.ajax({
wc_checkout_form.xhr = $.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: data,
success: function( data ) {
// Remove old errors
$( '.woocommerce-error, .woocommerce-message' ).remove();
// Always update the fragements
if ( data ) {
if ( data.fragments ) {
if ( data && data.fragments ) {
$.each( data.fragments, function ( key, value ) {
$( key ).replaceWith( value );
$( key ).unblock();
} );
}
}
// Check for error
@ -128,182 +273,17 @@ jQuery( function( $ ) {
}
// Trigger click event on selected payment method
$( '.woocommerce-checkout' ).find( 'input[name=payment_method]:checked' ).trigger( 'click' );
// Trigger click e on selected payment method
$( '.woocommerce-checkout' ).find( 'input[name=payment_method]:checked' ).triggerHandler( 'click' );
// Fire updated_checkout event
// Fire updated_checkout e
$( 'body' ).trigger( 'updated_checkout' );
}
});
}
// Event for updating the checkout
$( 'body' ).bind( 'update_checkout', function() {
clearTimeout( updateTimer );
update_checkout();
});
$( '.checkout_coupon, div.shipping_address' ).hide();
$( 'a.showlogin' ).click( function() {
$( 'form.login' ).slideToggle();
return false;
});
$( 'a.showcoupon' ).click( function() {
$( '.checkout_coupon' ).slideToggle( 400, function() {
$( '#coupon_code' ).focus();
});
return false;
});
$( '#ship-to-different-address input' ).change( function() {
$( 'div.shipping_address' ).hide();
if ( $( this ).is( ':checked' ) ) {
$( 'div.shipping_address' ).slideDown();
}
}).change();
if ( wc_checkout_params.option_guest_checkout === 'yes' ) {
$( 'div.create-account' ).hide();
$( 'input#createaccount' ).change( function() {
$( 'div.create-account' ).hide();
if ( $( this ).is( ':checked' ) ) {
$( 'div.create-account' ).slideDown();
}
}).change();
}
$( '#order_review' )
/* Payment option selection */
.on( 'click', '.payment_methods input.input-radio', function() {
if ( $( '.payment_methods input.input-radio' ).length > 1 ) {
var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) );
if ( $( this ).is( ':checked' ) && ! target_payment_box.is( ':visible' ) ) {
$( 'div.payment_box' ).filter( ':visible' ).slideUp( 250 );
if ( $( this ).is( ':checked' ) ) {
$( 'div.payment_box.' + $( this ).attr( 'ID' ) ).slideDown( 250 );
}
}
} else {
$( 'div.payment_box' ).show();
}
if ( $( this ).data( 'order_button_text' ) ) {
$( '#place_order' ).val( $( this ).data( 'order_button_text' ) );
} else {
$( '#place_order' ).val( $( '#place_order' ).data( 'value' ) );
}
})
// Trigger initial click
.find( 'input[name=payment_method]:checked' ).click();
// Used for input change events below
function input_changed() {
var update_totals = true;
if ( $( dirtyInput ).size() ) {
$required_inputs = $( dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
if ( $required_inputs.size() ) {
$required_inputs.each( function() {
if ( $( this ).find( 'input.input-text' ).val() === '' ) {
update_totals = false;
}
});
}
}
if ( update_totals ) {
dirtyInput = false;
$( 'body' ).trigger( 'update_checkout' );
}
}
$( 'form.checkout' )
/* Update totals/taxes/shipping */
// Inputs/selects which update totals instantly
.on( 'input change', 'select.shipping_method, input[name^=shipping_method], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type=radio]', function() {
clearTimeout( updateTimer );
dirtyInput = false;
$( 'body' ).trigger( 'update_checkout' );
})
// Address-fields which refresh totals when all required fields are filled
.on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', function() {
if ( dirtyInput ) {
input_changed();
}
})
.on( 'input change', '.address-field select', function() {
dirtyInput = this;
input_changed();
})
.on( 'input keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', function( e ){
var code = e.keyCode || e.which || 0;
if ( code === 9 ) {
return true;
}
dirtyInput = this;
clearTimeout( updateTimer );
updateTimer = setTimeout( input_changed, '1000' );
})
/* Inline validation */
.on( 'blur input change', '.input-text, select', function() {
var $this = $( this ),
$parent = $this.closest( '.form-row' ),
validated = true;
if ( $parent.is( '.validate-required' ) ) {
if ( $this.val() === '' ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
validated = false;
}
}
if ( $parent.is( '.validate-email' ) ) {
if ( $this.val() ) {
/* http://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
if ( ! pattern.test( $this.val() ) ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email' );
validated = false;
}
}
}
if ( validated ) {
$parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field' ).addClass( 'woocommerce-validated' );
}
} )
/* AJAX Form Submission */
.submit( function() {
clearTimeout( updateTimer );
},
submit: function( e ) {
wc_checkout_form.reset_update_checkout_timer();
var $form = $( this );
if ( $form.is( '.processing' ) ) {
@ -387,8 +367,9 @@ jQuery( function( $ ) {
}, 1000 );
// Trigger update in case we need a fresh nonce
if ( result.refresh === 'true' )
if ( result.refresh === 'true' ) {
$( 'body' ).trigger( 'update_checkout' );
}
$( 'body' ).trigger( 'checkout_error' );
}
@ -399,10 +380,22 @@ jQuery( function( $ ) {
}
return false;
});
}
};
/* AJAX Coupon Form Submission */
$( 'form.checkout_coupon' ).submit( function() {
var wc_checkout_coupons = {
init: function() {
$( 'body' ).on( 'click', 'a.showcoupon', this.show_coupon_form );
$( 'body' ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon );
$( 'form.checkout_coupon' ).hide().submit( this.submit );
},
show_coupon_form: function( e ) {
$( '.checkout_coupon' ).slideToggle( 400, function( e ) {
$( '.checkout_coupon' ).find(':input:eq(0)').focus()
});
return false;
},
submit: function( e ) {
var $form = $( this );
if ( $form.is( '.processing' ) ) return false;
@ -440,10 +433,9 @@ jQuery( function( $ ) {
});
return false;
});
$( '.woocommerce-checkout-review-order' ).on( 'click', '.woocommerce-remove-coupon', function( e ) {
e.preventDefault();
},
remove_coupon: function( e ) {
e.preDefault();
var container = $( this ).parents( '.woocommerce-checkout-review-order' ),
coupon = $( this ).data( 'coupon' );
@ -481,18 +473,20 @@ jQuery( function( $ ) {
},
dataType: 'html'
});
});
$( 'body' )
// Init trigger
.bind( 'init_checkout', function() {
$( '#billing_country, #shipping_country, .country_to_state' ).change();
$( 'body' ).trigger( 'update_checkout' );
});
// Update on page load
if ( wc_checkout_params.is_checkout === '1' ) {
$( 'body' ).trigger( 'init_checkout' );
}
}
var wc_checkout_login_form = {
init: function() {
$( 'body' ).on( 'click', 'a.showlogin', this.show_login_form );
},
show_login_form: function( e ) {
$( 'form.login' ).slideToggle();
return false;
}
}
wc_checkout_form.init();
wc_checkout_coupons.init();
wc_checkout_login_form.init();
});

File diff suppressed because one or more lines are too long

View File

@ -115,7 +115,7 @@ class WC_Settings_Shipping extends WC_Settings_Page {
'title' => __( 'Shipping Destination', 'woocommerce' ),
'desc' => __( 'This controls which shipping address is used by default.', 'woocommerce' ),
'id' => 'woocommerce_ship_to_destination',
'default' => 'shipping',
'default' => 'billing',
'type' => 'radio',
'options' => array(
'shipping' => __( 'Default to shipping address', 'woocommerce' ),