plugin updates
This commit is contained in:
@@ -9,16 +9,17 @@ jQuery( function( $ ) {
|
||||
* AddToCartHandler class.
|
||||
*/
|
||||
var AddToCartHandler = function() {
|
||||
this.requests = [];
|
||||
this.addRequest = this.addRequest.bind( this );
|
||||
this.run = this.run.bind( this );
|
||||
this.requests = [];
|
||||
this.addRequest = this.addRequest.bind( this );
|
||||
this.run = this.run.bind( this );
|
||||
this.$liveRegion = this.createLiveRegion();
|
||||
|
||||
$( document.body )
|
||||
.on( 'click', '.add_to_cart_button:not(.wc-interactive)', { addToCartHandler: this }, this.onAddToCart )
|
||||
.on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart )
|
||||
.on( 'added_to_cart', this.updateButton )
|
||||
.on( 'ajax_request_not_sent.adding_to_cart', this.updateButton )
|
||||
.on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments );
|
||||
.on( 'added_to_cart', { addToCartHandler: this }, this.onAddedToCart )
|
||||
.on( 'removed_from_cart', { addToCartHandler: this }, this.onRemovedFromCart )
|
||||
.on( 'ajax_request_not_sent.adding_to_cart', this.updateButton );
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -65,6 +66,12 @@ jQuery( function( $ ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Clean existing text in mini cart live region and update aria-relevant attribute
|
||||
// so screen readers can identify the next update if it's the same as the previous one.
|
||||
e.data.addToCartHandler.$liveRegion
|
||||
.text( '' )
|
||||
.removeAttr( 'aria-relevant' );
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
$thisbutton.removeClass( 'added' );
|
||||
@@ -127,6 +134,10 @@ jQuery( function( $ ) {
|
||||
var $thisbutton = $( this ),
|
||||
$row = $thisbutton.closest( '.woocommerce-mini-cart-item' );
|
||||
|
||||
e.data.addToCartHandler.$liveRegion
|
||||
.text( '' )
|
||||
.removeAttr( 'aria-relevant' );
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
$row.block({
|
||||
@@ -207,6 +218,55 @@ jQuery( function( $ ) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Update cart live region message after add/remove cart events.
|
||||
*/
|
||||
AddToCartHandler.prototype.alertCartUpdated = function( e, fragments, cart_hash, $button ) {
|
||||
var message = $button.data( 'success_message' );
|
||||
|
||||
if ( !message ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the response after adding/removing an item to/from the cart is really fast,
|
||||
// screen readers may not have time to identify the changes in the live region element.
|
||||
// So, we add a delay to ensure an interval between messages.
|
||||
e.data.addToCartHandler.$liveRegion
|
||||
.delay(1000)
|
||||
.text( message )
|
||||
.attr( 'aria-relevant', 'all' );
|
||||
};
|
||||
|
||||
/**
|
||||
* Add live region into the body element.
|
||||
*/
|
||||
AddToCartHandler.prototype.createLiveRegion = function() {
|
||||
var existingLiveRegion = $( '.widget_shopping_cart_live_region' );
|
||||
|
||||
if ( existingLiveRegion.length ) {
|
||||
return existingLiveRegion;
|
||||
}
|
||||
|
||||
return $( '<div class="widget_shopping_cart_live_region screen-reader-text" role="status"></div>' ).appendTo( 'body' );
|
||||
};
|
||||
|
||||
/**
|
||||
* Callbacks after added to cart event.
|
||||
*/
|
||||
AddToCartHandler.prototype.onAddedToCart = function( e, fragments, cart_hash, $button ) {
|
||||
e.data.addToCartHandler.updateButton( e, fragments, cart_hash, $button );
|
||||
e.data.addToCartHandler.updateFragments( e, fragments );
|
||||
e.data.addToCartHandler.alertCartUpdated( e, fragments, cart_hash, $button );
|
||||
};
|
||||
|
||||
/**
|
||||
* Callbacks after removed from cart event.
|
||||
*/
|
||||
AddToCartHandler.prototype.onRemovedFromCart = function( e, fragments, cart_hash, $button ) {
|
||||
e.data.addToCartHandler.updateFragments( e, fragments );
|
||||
e.data.addToCartHandler.alertCartUpdated( e, fragments, cart_hash, $button );
|
||||
};
|
||||
|
||||
/**
|
||||
* Init AddToCartHandler.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user