Created
October 1, 2018 19:45
-
-
Save apoleshchuk/6518d29bba879b8452ca6a228de873da to your computer and use it in GitHub Desktop.
InSales cart widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selectors = { | |
cartCount: "[data-cart-widget-count]" | |
}; | |
const classNames = { | |
animate: "cart-widget__count_animate" | |
}; | |
let eventCounter = 0; | |
const updateWidget = ({ items_count }) => { | |
const { animate } = classNames; | |
const $count = $(selectors.cartCount); | |
const oldValue = $count.text(); | |
const newValue = items_count || ""; | |
// елемент скрывается стилями через :empty | |
$count.text(newValue); | |
/** | |
* не анимируем в первом событии т.к. оно происходит после загзрузки страницы, | |
* анимируем только количество > 0 | |
*/ | |
if (eventCounter && items_count && oldValue != newValue) { | |
$count.removeClass(animate); | |
/* для корректного сброса состояния нужна задержка */ | |
setTimeout(() => { | |
$count.addClass(animate); | |
}, 100); | |
} | |
eventCounter++; | |
} | |
// подписка на событие в commonjs v2 | |
EventBus.subscribe("update_items:insales:cart", updateCartWidget); | |
// обновление после ручного добавления | |
$.post('/cart_items.json', { variant_id: id, quantity: count }).done(updateCartWidget) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment