From 2e6d37c1c3165ce8ab55b1ff193e2249d8048a55 Mon Sep 17 00:00:00 2001 From: Kevin Killingsworth Date: Mon, 2 May 2016 15:26:14 -0500 Subject: [PATCH] Fix cart ajax submit buttons for Safari. Fixes #10813 This adds a "clicked" attribute to clicked form buttons, which augments Safari specifically since it sets activeElement to the page body when a button is clicked. --- assets/js/frontend/cart.js | 22 +++++++++++++++++++--- assets/js/frontend/cart.min.js | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/assets/js/frontend/cart.js b/assets/js/frontend/cart.js index 71eaee6a362..fa7f8b5c8bb 100644 --- a/assets/js/frontend/cart.js +++ b/assets/js/frontend/cart.js @@ -213,11 +213,16 @@ jQuery( function( $ ) { init: function() { this.update_cart_totals = this.update_cart_totals.bind( this ); this.cart_submit = this.cart_submit.bind( this ); + this.submit_click = this.submit_click.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 ); + $( document ).on( + 'click', + 'div.woocommerce > form input[type=submit]', + this.submit_click ); $( document ).on( 'submit', 'div.woocommerce > form', @@ -239,7 +244,7 @@ jQuery( function( $ ) { }, /** - * After and input is changed, enabled the update cart button. + * After an input is changed, enable the update cart button. */ input_changed: function() { $( 'div.woocommerce > form input[name="update_cart"]' ).prop( 'disabled', false ); @@ -270,6 +275,7 @@ jQuery( function( $ ) { var $form = $( evt.target ); var $submit = $( document.activeElement ); + var $clicked = $( 'input[type=submit][clicked=true]' ); if ( 0 === $form.find( 'table.shop_table.cart' ).length ) { return false; @@ -278,14 +284,24 @@ jQuery( function( $ ) { return false; } - if ( $submit.is( '[name="update_cart"]' ) || $submit.is( 'input.qty' ) ) { + if ( $clicked.is( '[name="update_cart"]' ) || $submit.is( 'input.qty' ) ) { this.quantity_update( $form ); - } else if ( $submit.is( '[name="apply_coupon"]' ) || $submit.is( '#coupon_code' ) ) { + } else if ( $clicked.is( '[name="apply_coupon"]' ) || $submit.is( '#coupon_code' ) ) { this.apply_coupon( $form ); } }, + /** + * Special handling to identify which submit button was clicked. + * + * @param {Object} evt The JQuery event + */ + submit_click: function( evt ) { + $( 'input[type=submit]', $( evt.target ).parents( 'form' ) ).removeAttr( 'clicked' ); + $( evt.target ).attr( 'clicked', 'true' ); + }, + /** * Apply Coupon code * diff --git a/assets/js/frontend/cart.min.js b/assets/js/frontend/cart.min.js index 0a692d711cb..7b6ff04727d 100644 --- a/assets/js/frontend/cart.min.js +++ b/assets/js/frontend/cart.min.js @@ -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("table.shop_table.cart",c).closest("form");if(0===d.length){var e=a(".cart-empty",c).closest(".woocommerce");a("table.shop_table.cart").closest(".woocommerce").replaceWith(e)}else{var f=a(".woocommerce-error",c),h=a(".woocommerce-message",c);f.length>0?g(f):h.length>0?g(h):a(".woocommerce-error, .woocommerce-message").remove(),a("table.shop_table.cart").closest("form").replaceWith(d),a("table.shop_table.cart").closest("form").find('input[name="update_cart"]').prop("disabled",!0)}},g=function(b){var c=a("table.shop_table.cart").closest("form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)},h={init:function(b){this.cart=b,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=this.cart,g=a(b.target);d(g),a("").attr("type","hidden").attr("name","calc_shipping").attr("value","x").appendTo(g),a.ajax({type:g.attr("method"),url:g.attr("action"),data:g.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(g),c.update_cart_totals()}})}},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),a(document).on("change input","div.woocommerce > form .cart_item :input",this.input_changed),a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!0)},input_changed:function(){a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!1)},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 0===d.find("table.shop_table.cart").length?!1:c(d)?!1:void(e.is('[name="update_cart"]')||e.is("input.qty")?this.quantity_update(d):(e.is('[name="apply_coupon"]')||e.is("#coupon_code"))&&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};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){var c=this;a("").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),c.update_cart_totals()}})},item_remove_clicked:function(b){b.preventDefault();var c=this,g=a(b.target),h=g.parents("form");d(h),a.ajax({type:"GET",url:g.attr("href"),dataType:"html",success:f,complete:function(){e(h),c.update_cart_totals()}})}};h.init(i),i.init()}); \ No newline at end of file +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("table.shop_table.cart",c).closest("form");if(0===d.length){var e=a(".cart-empty",c).closest(".woocommerce");a("table.shop_table.cart").closest(".woocommerce").replaceWith(e)}else{var f=a(".woocommerce-error",c),h=a(".woocommerce-message",c);f.length>0?g(f):h.length>0?g(h):a(".woocommerce-error, .woocommerce-message").remove(),a("table.shop_table.cart").closest("form").replaceWith(d),a("table.shop_table.cart").closest("form").find('input[name="update_cart"]').prop("disabled",!0)}},g=function(b){var c=a("table.shop_table.cart").closest("form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)},h={init:function(b){this.cart=b,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=this.cart,g=a(b.target);d(g),a("").attr("type","hidden").attr("name","calc_shipping").attr("value","x").appendTo(g),a.ajax({type:g.attr("method"),url:g.attr("action"),data:g.serialize(),dataType:"html",success:function(a){f(a)},complete:function(){e(g),c.update_cart_totals()}})}},i={init:function(){this.update_cart_totals=this.update_cart_totals.bind(this),this.cart_submit=this.cart_submit.bind(this),this.submit_click=this.submit_click.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("click","div.woocommerce > form input[type=submit]",this.submit_click),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),a(document).on("change input","div.woocommerce > form .cart_item :input",this.input_changed),a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!0)},input_changed:function(){a('div.woocommerce > form input[name="update_cart"]').prop("disabled",!1)},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),f=a("input[type=submit][clicked=true]");return 0===d.find("table.shop_table.cart").length?!1:c(d)?!1:void(f.is('[name="update_cart"]')||e.is("input.qty")?this.quantity_update(d):(f.is('[name="apply_coupon"]')||e.is("#coupon_code"))&&this.apply_coupon(d))},submit_click:function(b){a("input[type=submit]",a(b.target).parents("form")).removeAttr("clicked"),a(b.target).attr("clicked","true")},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};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){var c=this;a("").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),c.update_cart_totals()}})},item_remove_clicked:function(b){b.preventDefault();var c=this,g=a(b.target),h=g.parents("form");d(h),a.ajax({type:"GET",url:g.attr("href"),dataType:"html",success:f,complete:function(){e(h),c.update_cart_totals()}})}};h.init(i),i.init()}); \ No newline at end of file