102 lines
2.7 KiB
JavaScript
102 lines
2.7 KiB
JavaScript
/**
|
|
* Hesabix2 WooCommerce فرانتاند — مینیسبد هدر، هماهنگ با fragment AJAX
|
|
*
|
|
* @package Hesabix2
|
|
*/
|
|
(function ($) {
|
|
'use strict';
|
|
|
|
function closePanels(wraps) {
|
|
wraps.find('.hesabix-wc-mini-cart-panel').each(function () {
|
|
window.clearTimeout($(this).data('hxCloseTimer'));
|
|
});
|
|
|
|
wraps.removeClass('is-open');
|
|
wraps.find('.hesabix-wc-mini-cart-panel').attr('aria-hidden', 'true');
|
|
wraps.find('.hesabix-wc-mini-cart__trigger').attr('aria-expanded', 'false');
|
|
|
|
wraps.find('.hesabix-wc-mini-cart-panel').each(function () {
|
|
const $panel = $(this);
|
|
const reduceMotion = typeof window.matchMedia !== 'undefined' && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
deferFinalizeClose($panel, reduceMotion ? 0 : 210);
|
|
});
|
|
}
|
|
|
|
function openPanel(wrap, panel, trigger) {
|
|
window.clearTimeout(panel.data('hxCloseTimer'));
|
|
panel.removeData('hxCloseTimer');
|
|
panel.removeAttr('hidden');
|
|
const el = panel.get(0);
|
|
if (el) {
|
|
void el.offsetHeight;
|
|
}
|
|
wrap.addClass('is-open');
|
|
panel.attr('aria-hidden', 'false');
|
|
trigger.attr('aria-expanded', 'true');
|
|
}
|
|
|
|
function finalizeClose(panel) {
|
|
if (!panel.length) {
|
|
return;
|
|
}
|
|
const wrap = panel.closest('.hesabix-wc-mini-cart-wrap');
|
|
if (!wrap.hasClass('is-open')) {
|
|
panel.attr('hidden', 'hidden');
|
|
}
|
|
}
|
|
|
|
function deferFinalizeClose(panel, delayMs) {
|
|
window.clearTimeout(panel.data('hxCloseTimer'));
|
|
const id = window.setTimeout(function () {
|
|
panel.removeData('hxCloseTimer');
|
|
finalizeClose(panel);
|
|
}, delayMs);
|
|
panel.data('hxCloseTimer', id);
|
|
}
|
|
|
|
function togglePanel(wrap) {
|
|
const panel = wrap.find('.hesabix-wc-mini-cart-panel');
|
|
const trigger = wrap.find('.hesabix-wc-mini-cart__trigger');
|
|
|
|
if (wrap.hasClass('is-open')) {
|
|
closePanels(wrap);
|
|
} else {
|
|
closePanels($('.hesabix-wc-mini-cart-wrap'));
|
|
openPanel(wrap, panel, trigger);
|
|
}
|
|
}
|
|
|
|
function initMiniCart() {
|
|
$(document.body)
|
|
.off('click.hesabixWcMc')
|
|
.on('click.hesabixWcMc', '.hesabix-wc-mini-cart__trigger', function (e) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
togglePanel($(this).closest('.hesabix-wc-mini-cart-wrap'));
|
|
});
|
|
|
|
$(document)
|
|
.off('click.hesabixWcMcClose')
|
|
.on('click.hesabixWcMcClose', function (e) {
|
|
if ($(e.target).closest('.hesabix-wc-mini-cart-wrap').length) {
|
|
return;
|
|
}
|
|
closePanels($('.hesabix-wc-mini-cart-wrap'));
|
|
});
|
|
|
|
$(document).off('keydown.hesabixWcMcEsc');
|
|
$(document).on('keydown.hesabixWcMcEsc', function (e) {
|
|
if (e.key === 'Escape') {
|
|
closePanels($('.hesabix-wc-mini-cart-wrap'));
|
|
}
|
|
});
|
|
}
|
|
|
|
$(document).ready(function () {
|
|
initMiniCart();
|
|
});
|
|
|
|
$(document.body).on('wc_fragments_refreshed wc_fragments_loaded', function () {
|
|
initMiniCart();
|
|
});
|
|
})(window.jQuery);
|