commerce-8.x-2.8/modules/cart/js/commerce_cart.js
modules/cart/js/commerce_cart.js
/**
* @file
* Defines Javascript behaviors for the commerce cart module.
*/
(function ($, Drupal, drupalSettings) {
'use strict';
Drupal.behaviors.commerceCartBlock = {
attach: function (context) {
var $context = $(context);
var $cart = $context.find('.cart--cart-block');
var $cartButton = $context.find('.cart-block--link__expand');
var $cartContents = $cart.find('.cart-block--contents');
if ($cartContents.length > 0) {
// Expand the block when the link is clicked.
$cartButton.once('cart-button-processed').on('click', function (e) {
// Prevent it from going to the cart.
e.preventDefault();
// Get the shopping cart width + the offset to the left.
var windowWidth = $(window).width();
var cartWidth = $cartContents.width() + $cart.offset().left;
// If the cart goes out of the viewport we should align it right.
if (cartWidth > windowWidth) {
$cartContents.addClass('is-outside-horizontal');
}
// Toggle the expanded class.
$cartContents
.toggleClass('cart-block--contents__expanded')
.slideToggle();
});
}
}
};
})(jQuery, Drupal, drupalSettings);
