34 lines
1.5 KiB
JavaScript
34 lines
1.5 KiB
JavaScript
(function (run) {
|
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
|
else run();
|
|
})(function () {
|
|
var root = document.querySelector('[data-favorites]');
|
|
if (!root || !window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
|
var status = root.querySelector('[data-favorites-status]');
|
|
document.addEventListener('click', function (event) {
|
|
var button = event.target.closest('[data-favorites] [data-fav-toggle]');
|
|
if (!button) return;
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
var card = button.closest('.vino-card');
|
|
if (!card) return;
|
|
var parent = card.parentNode;
|
|
var next = card.nextSibling;
|
|
var neighbour = card.nextElementSibling || card.previousElementSibling;
|
|
card.remove();
|
|
if (status) status.textContent = root.getAttribute('data-favorites-removed') || '';
|
|
var focus = (neighbour && neighbour.querySelector('.vino-card__link')) || document.querySelector('h1');
|
|
if (focus) {
|
|
if (focus.tagName === 'H1') focus.setAttribute('tabindex', '-1');
|
|
focus.focus();
|
|
}
|
|
BX.ajax.runAction('jinove:vinoteca.favorite.set', { data: { id: button.getAttribute('data-fav-toggle'), on: 'N' } }).then(function (response) {
|
|
document.dispatchEvent(new CustomEvent('vino:favorite', { detail: response.data }));
|
|
if (!root.querySelector('.vino-card')) window.location.href = window.location.pathname;
|
|
}, function () {
|
|
parent.insertBefore(card, next);
|
|
if (status) status.textContent = '';
|
|
});
|
|
}, true);
|
|
});
|