cart objects: refactor into objects

This update actually takes the code and refactors it into
two object handlers, cart and cart_shipping.

Also, more block comments are added to each function.
This commit is contained in:
Kevin Killingsworth 2016-02-22 10:15:47 -06:00
parent 02b7c1edc4
commit b7f365982e
2 changed files with 197 additions and 161 deletions

View File

@ -66,6 +66,18 @@ jQuery( function( $ ) {
$( 'div.woocommerce' ).replaceWith( $new_div );
};
/**
* Clear previous notices and shows new one above form.
*
* @param {Object} The Notice HTML Element in string or object form.
*/
var show_notice = function( html_element ) {
var $form = $( 'div.woocommerce > form' );
$( '.woocommerce-error, .woocommerce-message' ).remove();
$form.before( html_element );
};
/**
* Object to handle AJAX calls for cart shipping changes.
@ -76,6 +88,10 @@ jQuery( function( $ ) {
* Initialize event handlers and UI state.
*/
init: function() {
this.toggle_shipping = this.toggle_shipping.bind( this );
this.shipping_method_selected = this.shipping_method_selected.bind( this );
this.shipping_calculator_submit = this.shipping_calculator_submit.bind( this );
$( document ).on(
'click',
'.shipping-calculator-button',
@ -165,179 +181,199 @@ jQuery( function( $ ) {
};
/**
* Update the cart after something has changed.
* Object to handle cart UI.
*/
var update_cart_totals = function() {
block( $( 'div.cart_totals' ) );
var cart = {
/**
* Initialize cart UI events.
*/
init: function() {
this.update_cart_totals = this.update_cart_totals.bind( this );
this.cart_submit = this.cart_submit.bind( this );
this.apply_coupon = this.apply_coupon.bind( this );
this.remove_coupon_clicked = this.remove_coupon_clicked.bind( this );
this.quantity_update = this.quantity_update.bind( this );
this.item_remove_clicked = this.item_remove_clicked.bind( this );
$.ajax( {
url: get_url( 'get_cart_totals' ),
dataType: 'html',
success: function( response ) {
$( 'div.cart_totals' ).replaceWith( response );
$( document ).on(
'submit',
'div.woocommerce > form',
this.cart_submit );
$( document ).on(
'click',
'a.woocommerce-remove-coupon',
this.remove_coupon_clicked );
$( document ).on(
'click',
'td.product-remove > a',
this.item_remove_clicked );
},
/**
* Update the cart after something has changed.
*/
update_cart_totals: function() {
block( $( 'div.cart_totals' ) );
$.ajax( {
url: get_url( 'get_cart_totals' ),
dataType: 'html',
success: function( response ) {
$( 'div.cart_totals' ).replaceWith( response );
}
} );
},
/**
* Handle cart form submit and route to correct logic.
*
* @param {Object} evt The JQuery event
*/
cart_submit: function( evt ) {
evt.preventDefault();
var $form = $( evt.target );
var $submit = $( document.activeElement );
window.console.log( $submit );
if ( is_blocked( $form ) ) {
return false;
}
} );
};
/**
* Clear previous notices and shows new one above form.
*
* @param {Object} The Notice HTML Element in string or object form.
*/
var show_notice = function( html_element ) {
var $form = $( 'div.woocommerce > form' );
if ( $submit.is( '[name="update_cart"]' ) || $submit.is( 'input.qty' ) ) {
window.console.log( 'update cart' );
this.quantity_update( $form );
$( '.woocommerce-error, .woocommerce-message' ).remove();
$form.before( html_element );
};
} else if ( $submit.is( '[name="apply_coupon"]' ) || $submit.is( '#coupon_code' ) ) {
window.console.log( 'apply coupon' );
this.apply_coupon( $form );
}
},
/**
* Handle cart form submit and route to correct logic.
*
* @param {Object} evt The JQuery event
*/
var cart_submit = function( evt ) {
evt.preventDefault();
/**
* Apply Coupon code
*
* @param {JQuery Object} $form The cart form.
*/
apply_coupon: function( $form ) {
block( $form );
var $form = $( evt.target );
var $submit = $( document.activeElement );
var cart = this;
var $text_field = $( '#coupon_code' );
var coupon_code = $text_field.val();
window.console.log( $submit );
var data = {
security: wc_cart_params.apply_coupon_nonce,
coupon_code: coupon_code
};
if ( is_blocked( $form ) ) {
return false;
}
$.ajax( {
type: 'POST',
url: get_url( 'apply_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
},
complete: function() {
unblock( $form );
$text_field.val( '' );
cart.update_cart_totals();
}
} );
},
if ( $submit.is( '[name="update_cart"]' ) || $submit.is( 'input.qty' ) ) {
window.console.log( 'update cart' );
quantity_update( $form );
/**
* Handle when a remove coupon link is clicked.
*
* @param {Object} evt The JQuery event
*/
remove_coupon_clicked: function( evt ) {
evt.preventDefault();
} else if ( $submit.is( '[name="apply_coupon"]' ) || $submit.is( '#coupon_code' ) ) {
window.console.log( 'apply coupon' );
apply_coupon( $form );
var cart = this;
var $tr = $( evt.target ).parents( 'tr' );
var coupon = $( evt.target ).attr( 'data-coupon' );
block( $tr.parents( 'table' ) );
var data = {
security: wc_cart_params.remove_coupon_nonce,
coupon: coupon
};
window.console.log( 'data' );
window.console.log( data );
$.ajax( {
type: 'POST',
url: get_url( 'remove_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
unblock( $tr.parents( 'table' ) );
},
complete: function() {
cart.update_cart_totals();
}
} );
},
/**
* Handle a cart Quantity Update
*
* @param {JQuery Object} $form The cart form.
*/
quantity_update: function( $form ) {
// Provide the submit button value because wc-form-handler expects it.
$( '<input />' ).attr( 'type', 'hidden' )
.attr( 'name', 'update_cart' )
.attr( 'value', 'Update Cart' )
.appendTo( $form );
block( $form );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: update_wc_div,
complete: function() {
unblock( $form );
}
} );
},
/**
* Handle when a remove item link is clicked.
*
* @param {Object} evt The JQuery event
*/
item_remove_clicked: function( evt ) {
evt.preventDefault();
var $a = $( evt.target );
var $form = $a.parents( 'form' );
block( $form );
$.ajax( {
type: 'GET',
url: $a.attr( 'href' ),
dataType: 'html',
success: update_wc_div,
complete: function() {
unblock( $form );
}
} );
}
};
/**
* Apply Coupon code
*
* @param {JQuery Object} $form The cart form.
*/
var apply_coupon = function( $form ) {
block( $form );
var $text_field = $( '#coupon_code' );
var coupon_code = $text_field.val();
var data = {
security: wc_cart_params.apply_coupon_nonce,
coupon_code: coupon_code
};
$.ajax( {
type: 'POST',
url: get_url( 'apply_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
},
complete: function() {
unblock( $form );
$text_field.val( '' );
update_cart_totals();
}
} );
};
/**
* Handle when a remove coupon link is clicked.
*
* @param {Object} evt The JQuery event
*/
var remove_coupon_clicked = function( evt ) {
evt.preventDefault();
var $tr = $( this ).parents( 'tr' );
var coupon = $( this ).attr( 'data-coupon' );
block( $tr.parents( 'table' ) );
var data = {
security: wc_cart_params.remove_coupon_nonce,
coupon: coupon
};
$.ajax( {
type: 'POST',
url: get_url( 'remove_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
show_notice( response );
unblock( $tr.parents( 'table' ) );
},
complete: function() {
update_cart_totals();
}
} );
};
/**
* Handle a cart Quantity Update
*
* @param {JQuery Object} $form The cart form.
*/
var quantity_update = function( $form ) {
// Provide the submit button value because wc-form-handler expects it.
$( '<input />' ).attr( 'type', 'hidden' )
.attr( 'name', 'update_cart' )
.attr( 'value', 'Update Cart' )
.appendTo( $form );
block( $form );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: update_wc_div,
complete: function() {
unblock( $form );
}
} );
};
/**
* Handle when a remvoe item link is clicked.
*
* @param {Object} evt The JQuery event
*/
var item_remove_clicked = function( evt ) {
evt.preventDefault();
var $a = $( evt.target );
var $form = $a.parents( 'form' );
block( $form );
$.ajax( {
type: 'GET',
url: $a.attr( 'href' ),
dataType: 'html',
success: update_wc_div,
complete: function() {
unblock( $form );
}
} );
};
$( document ).on( 'submit', 'div.woocommerce > form', cart_submit );
$( document ).on( 'click', 'a.woocommerce-remove-coupon', remove_coupon_clicked );
$( document ).on( 'click', 'td.product-remove > a', item_remove_clicked );
cart_shipping.init();
cart.init();
} );

View File

@ -1 +1 @@
jQuery(function(a){if("undefined"==typeof wc_cart_params)return!1;var b=function(a){return wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%",a)},c=function(a){return a.is(".processing")},d=function(a){a.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},e=function(a){a.removeClass("processing").unblock()},f=function(b){var c=a.parseHTML(b),d=a("div.woocommerce",c);a("div.woocommerce").replaceWith(d)},g={init:function(){a(document).on("click",".shipping-calculator-button",this.toggle_shipping),a(document).on("change","select.shipping_method, input[name^=shipping_method]",this.shipping_method_selected),a(document).on("submit","form.woocommerce-shipping-calculator",this.shipping_calculator_submit),a(".shipping-calculator-form").hide()},toggle_shipping:function(){return a(".shipping-calculator-form").slideToggle("slow"),!1},shipping_method_selected:function(c){var e=c.target,f=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){f[a(e).data("index")]=a(e).val()}),d(a("div.cart_totals"));var g={security:wc_cart_params.update_shipping_method_nonce,shipping_method:f};a.post(b("update_shipping_method"),g,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})},shipping_calculator_submit:function(b){b.preventDefault();var c=a(b.target);d(c),a("<input />").attr("type","hidden").attr("name","calc_shipping").attr("value","x").appendTo(c),a.ajax({type:c.attr("method"),url:c.attr("action"),data:c.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(c)}})}},h=function(){d(a("div.cart_totals")),a.ajax({url:b("get_cart_totals"),dataType:"html",success:function(b){a("div.cart_totals").replaceWith(b)}})},i=function(b){var c=a("div.woocommerce > form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)},j=function(b){b.preventDefault();var d=a(b.target),e=a(document.activeElement);return window.console.log(e),c(d)?!1:void(e.is('[name="update_cart"]')||e.is("input.qty")?(window.console.log("update cart"),m(d)):(e.is('[name="apply_coupon"]')||e.is("#coupon_code"))&&(window.console.log("apply coupon"),k(d)))},k=function(c){d(c);var f=a("#coupon_code"),g=f.val(),j={security:wc_cart_params.apply_coupon_nonce,coupon_code:g};a.ajax({type:"POST",url:b("apply_coupon"),data:j,dataType:"html",success:function(a){i(a)},complete:function(){e(c),f.val(""),h()}})},l=function(c){c.preventDefault();var f=a(this).parents("tr"),g=a(this).attr("data-coupon");d(f.parents("table"));var j={security:wc_cart_params.remove_coupon_nonce,coupon:g};a.ajax({type:"POST",url:b("remove_coupon"),data:j,dataType:"html",success:function(a){i(a),e(f.parents("table"))},complete:function(){h()}})},m=function(b){a("<input />").attr("type","hidden").attr("name","update_cart").attr("value","Update Cart").appendTo(b),d(b),a.ajax({type:b.attr("method"),url:b.attr("action"),data:b.serialize(),dataType:"html",success:f,complete:function(){e(b)}})},n=function(b){b.preventDefault();var c=a(b.target),g=c.parents("form");d(g),a.ajax({type:"GET",url:c.attr("href"),dataType:"html",success:f,complete:function(){e(g)}})};a(document).on("submit","div.woocommerce > form",j),a(document).on("click","a.woocommerce-remove-coupon",l),a(document).on("click","td.product-remove > a",n),g.init()});
jQuery(function(a){if("undefined"==typeof wc_cart_params)return!1;var b=function(a){return wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%",a)},c=function(a){return a.is(".processing")},d=function(a){a.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},e=function(a){a.removeClass("processing").unblock()},f=function(b){var c=a.parseHTML(b),d=a("div.woocommerce",c);a("div.woocommerce").replaceWith(d)},g=function(b){var c=a("div.woocommerce > form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)},h={init:function(){this.toggle_shipping=this.toggle_shipping.bind(this),this.shipping_method_selected=this.shipping_method_selected.bind(this),this.shipping_calculator_submit=this.shipping_calculator_submit.bind(this),a(document).on("click",".shipping-calculator-button",this.toggle_shipping),a(document).on("change","select.shipping_method, input[name^=shipping_method]",this.shipping_method_selected),a(document).on("submit","form.woocommerce-shipping-calculator",this.shipping_calculator_submit),a(".shipping-calculator-form").hide()},toggle_shipping:function(){return a(".shipping-calculator-form").slideToggle("slow"),!1},shipping_method_selected:function(c){var e=c.target,f=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){f[a(e).data("index")]=a(e).val()}),d(a("div.cart_totals"));var g={security:wc_cart_params.update_shipping_method_nonce,shipping_method:f};a.post(b("update_shipping_method"),g,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})},shipping_calculator_submit:function(b){b.preventDefault();var c=a(b.target);d(c),a("<input />").attr("type","hidden").attr("name","calc_shipping").attr("value","x").appendTo(c),a.ajax({type:c.attr("method"),url:c.attr("action"),data:c.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(c)}})}},i={init:function(){this.update_cart_totals=this.update_cart_totals.bind(this),this.cart_submit=this.cart_submit.bind(this),this.apply_coupon=this.apply_coupon.bind(this),this.remove_coupon_clicked=this.remove_coupon_clicked.bind(this),this.quantity_update=this.quantity_update.bind(this),this.item_remove_clicked=this.item_remove_clicked.bind(this),a(document).on("submit","div.woocommerce > form",this.cart_submit),a(document).on("click","a.woocommerce-remove-coupon",this.remove_coupon_clicked),a(document).on("click","td.product-remove > a",this.item_remove_clicked)},update_cart_totals:function(){d(a("div.cart_totals")),a.ajax({url:b("get_cart_totals"),dataType:"html",success:function(b){a("div.cart_totals").replaceWith(b)}})},cart_submit:function(b){b.preventDefault();var d=a(b.target),e=a(document.activeElement);return window.console.log(e),c(d)?!1:void(e.is('[name="update_cart"]')||e.is("input.qty")?(window.console.log("update cart"),this.quantity_update(d)):(e.is('[name="apply_coupon"]')||e.is("#coupon_code"))&&(window.console.log("apply coupon"),this.apply_coupon(d)))},apply_coupon:function(c){d(c);var f=this,h=a("#coupon_code"),i=h.val(),j={security:wc_cart_params.apply_coupon_nonce,coupon_code:i};a.ajax({type:"POST",url:b("apply_coupon"),data:j,dataType:"html",success:function(a){g(a)},complete:function(){e(c),h.val(""),f.update_cart_totals()}})},remove_coupon_clicked:function(c){c.preventDefault();var f=this,h=a(c.target).parents("tr"),i=a(c.target).attr("data-coupon");d(h.parents("table"));var j={security:wc_cart_params.remove_coupon_nonce,coupon:i};window.console.log("data"),window.console.log(j),a.ajax({type:"POST",url:b("remove_coupon"),data:j,dataType:"html",success:function(a){g(a),e(h.parents("table"))},complete:function(){f.update_cart_totals()}})},quantity_update:function(b){a("<input />").attr("type","hidden").attr("name","update_cart").attr("value","Update Cart").appendTo(b),d(b),a.ajax({type:b.attr("method"),url:b.attr("action"),data:b.serialize(),dataType:"html",success:f,complete:function(){e(b)}})},item_remove_clicked:function(b){b.preventDefault();var c=a(b.target),g=c.parents("form");d(g),a.ajax({type:"GET",url:c.attr("href"),dataType:"html",success:f,complete:function(){e(g)}})}};h.init(),i.init()});