243 lines
9.0 KiB
JavaScript
243 lines
9.0 KiB
JavaScript
(() => {
|
|
const initialized = new WeakSet();
|
|
const motion = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
|
|
function initializeCarousel(root) {
|
|
if (initialized.has(root)) return;
|
|
const items = root.querySelector(':scope > .ell-items');
|
|
const controls = root.querySelector(':scope > .ell-carousel-controls');
|
|
if (!items || !controls) return;
|
|
initialized.add(root);
|
|
|
|
const previous = controls.querySelector('[data-ell-direction="-1"]');
|
|
const next = controls.querySelector('[data-ell-direction="1"]');
|
|
const indicator = controls.querySelector('[data-ell-indicator]');
|
|
const play = controls.querySelector('[data-ell-play]');
|
|
const wrap = controls.dataset.ellWrap === '1';
|
|
const step = Number(controls.dataset.ellStep) || 1;
|
|
const delay = Number(controls.dataset.ellAutoplay) * 1000;
|
|
let playing = !!delay && !motion.matches;
|
|
let hovered = false;
|
|
let timer;
|
|
let frame;
|
|
let positions = [];
|
|
let active = 0;
|
|
let dots = [];
|
|
let progress;
|
|
|
|
function inlineDirection() {
|
|
return getComputedStyle(items).direction === 'rtl' ? -1 : 1;
|
|
}
|
|
|
|
function measure() {
|
|
const styles = getComputedStyle(items);
|
|
const gap = parseFloat(styles.columnGap) || 0;
|
|
const overflow = Math.max(0, items.scrollWidth - items.clientWidth);
|
|
let offset = 0;
|
|
positions = [0];
|
|
for (const card of items.children) {
|
|
offset += card.getBoundingClientRect().width + gap;
|
|
const position = Math.min(overflow, offset);
|
|
if (position - positions[positions.length - 1] < 1) break;
|
|
positions.push(position);
|
|
}
|
|
if (indicator?.dataset.ellIndicator === 'dots' && dots.length !== positions.length) {
|
|
const restoreFocus = indicator.contains(document.activeElement);
|
|
indicator.replaceChildren();
|
|
dots = positions.map((_, index) => {
|
|
const dot = document.createElement('button');
|
|
dot.type = 'button';
|
|
dot.className = 'ell-carousel-dot';
|
|
dot.setAttribute('aria-label', indicator.dataset.slideLabel.replace('%d', index + 1));
|
|
dot.addEventListener('click', () => goTo(index));
|
|
indicator.append(dot);
|
|
return dot;
|
|
});
|
|
if (restoreFocus) dots[Math.min(active, dots.length - 1)]?.focus();
|
|
}
|
|
if (indicator?.dataset.ellIndicator === 'progress' && !progress) {
|
|
progress = document.createElement('progress');
|
|
progress.setAttribute('aria-label', indicator.getAttribute('aria-label'));
|
|
indicator.append(progress);
|
|
}
|
|
update();
|
|
restart();
|
|
}
|
|
|
|
function update() {
|
|
const position = Math.abs(items.scrollLeft);
|
|
active = positions.reduce((closest, left, index) =>
|
|
Math.abs(left - position) < Math.abs(positions[closest] - position) ? index : closest, 0);
|
|
const count = positions.length;
|
|
controls.hidden = count < 2 || controls.children.length === 0;
|
|
if (previous) previous.disabled = !wrap && active === 0;
|
|
if (next) next.disabled = !wrap && active >= count - 1;
|
|
if (indicator?.dataset.ellIndicator === 'fraction') indicator.textContent = `${active + 1} / ${count}`;
|
|
if (progress) {
|
|
progress.max = count;
|
|
progress.value = active + 1;
|
|
}
|
|
dots.forEach((dot, index) => {
|
|
if (index === active) dot.setAttribute('aria-current', 'true');
|
|
else dot.removeAttribute('aria-current');
|
|
});
|
|
if (root.classList.contains('ell-focus-effect')) {
|
|
const focused = items.children[active];
|
|
items.querySelector('.ell-active')?.classList.remove('ell-active');
|
|
focused?.classList.add('ell-active');
|
|
}
|
|
}
|
|
|
|
function goTo(index) {
|
|
const last = positions.length - 1;
|
|
const target = wrap && index < 0 ? last : wrap && index > last ? 0 : Math.max(0, Math.min(last, index));
|
|
items.scrollTo({left: inlineDirection() * positions[target], behavior: motion.matches ? 'auto' : 'smooth'});
|
|
}
|
|
|
|
function restart() {
|
|
window.clearInterval(timer);
|
|
if (play) play.textContent = playing ? play.dataset.pause : play.dataset.play;
|
|
if (!playing || hovered || positions.length < 2 || document.hidden || motion.matches) return;
|
|
timer = window.setInterval(() => {
|
|
if (!root.isConnected || !playing || hovered || document.hidden || motion.matches) {
|
|
window.clearInterval(timer);
|
|
return;
|
|
}
|
|
const last = positions.length - 1;
|
|
if (!wrap && active === last) {
|
|
playing = false;
|
|
restart();
|
|
} else {
|
|
goTo(active + step);
|
|
}
|
|
}, delay);
|
|
}
|
|
|
|
controls.addEventListener('click', (event) => {
|
|
const arrow = event.target.closest('[data-ell-direction]');
|
|
if (arrow) goTo(active + step * Number(arrow.dataset.ellDirection));
|
|
if (event.target.closest('[data-ell-play]')) {
|
|
playing = !playing && !motion.matches;
|
|
restart();
|
|
}
|
|
});
|
|
root.addEventListener('pointerenter', () => { hovered = true; restart(); });
|
|
root.addEventListener('pointerleave', () => { hovered = false; restart(); });
|
|
root.addEventListener('focusin', (event) => {
|
|
if (playing && !root.contains(event.relatedTarget)) {
|
|
playing = false;
|
|
restart();
|
|
}
|
|
});
|
|
items.addEventListener('scroll', () => {
|
|
window.cancelAnimationFrame(frame);
|
|
frame = window.requestAnimationFrame(update);
|
|
}, {passive: true});
|
|
const observer = new ResizeObserver(measure);
|
|
observer.observe(items);
|
|
root.addEventListener('ell:cards-added', measure);
|
|
measure();
|
|
}
|
|
|
|
function initializeWithin(scope) {
|
|
scope.querySelectorAll('.ell-layout-carousel').forEach(initializeCarousel);
|
|
}
|
|
|
|
function reinitializeCards(cards) {
|
|
const elementor = window.elementorFrontend;
|
|
if (!elementor?.elementsHandler?.runReadyTrigger || !window.jQuery) return;
|
|
cards.forEach(card => {
|
|
card.querySelectorAll('.elementor-element').forEach(element => {
|
|
elementor.elementsHandler.runReadyTrigger(window.jQuery(element));
|
|
});
|
|
});
|
|
}
|
|
|
|
async function loadMore(event) {
|
|
const link = event.target.closest('[data-ell-more] a[rel="next"]');
|
|
if (!link || event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || link.dataset.ellFallback) return;
|
|
const url = new URL(link.href);
|
|
const root = link.closest('.ell-loop');
|
|
const items = root?.querySelector(':scope > .ell-items');
|
|
const status = root?.querySelector('[data-ell-more-status]');
|
|
if (!items || !status || url.origin !== location.origin) return;
|
|
event.preventDefault();
|
|
if (link.dataset.ellLoading) return;
|
|
link.dataset.ellLoading = '1';
|
|
root.setAttribute('aria-busy', 'true');
|
|
try {
|
|
const response = await fetch(url.href, {credentials: 'same-origin'});
|
|
if (!response.ok || new URL(response.url).origin !== location.origin) throw new Error();
|
|
const page = new DOMParser().parseFromString(await response.text(), 'text/html');
|
|
const loop = page.getElementById(root.id);
|
|
const nextItems = loop?.querySelector(':scope > .ell-items');
|
|
if (!nextItems?.querySelector('.ell-item')) throw new Error();
|
|
const lastMonth = Array.from(items.querySelectorAll(':scope > .ell-month')).at(-1);
|
|
const firstMonth = nextItems.querySelector(':scope > .ell-month');
|
|
if (firstMonth && lastMonth?.dataset.ellMonth === firstMonth.dataset.ellMonth) firstMonth.remove();
|
|
const cards = Array.from(nextItems.querySelectorAll(':scope > .ell-item'));
|
|
items.append(...nextItems.children);
|
|
root.dispatchEvent(new Event('ell:cards-added'));
|
|
const nextPage = loop.querySelector('a[rel="next"]');
|
|
const pageNumber = loop.querySelector('.ell-page');
|
|
if (pageNumber) root.querySelector('.ell-page').textContent = pageNumber.textContent;
|
|
status.textContent = nextPage ? status.dataset.loaded : status.dataset.done;
|
|
if (nextPage) {
|
|
link.href = nextPage.getAttribute('href');
|
|
} else {
|
|
if (document.activeElement === link) status.focus();
|
|
link.remove();
|
|
}
|
|
reinitializeCards(cards);
|
|
} catch {
|
|
link.dataset.ellFallback = '1';
|
|
status.textContent = status.dataset.error;
|
|
status.focus();
|
|
} finally {
|
|
delete link.dataset.ellLoading;
|
|
root.removeAttribute('aria-busy');
|
|
}
|
|
}
|
|
|
|
async function copyLink(event) {
|
|
const button = event.target.closest('[data-ell-copy]');
|
|
if (!button) return;
|
|
const input = button.parentElement.querySelector('input');
|
|
const status = button.parentElement.querySelector('[role="status"]');
|
|
input.focus();
|
|
input.select();
|
|
try {
|
|
if (navigator.clipboard) {
|
|
await navigator.clipboard.writeText(input.value);
|
|
} else if (!document.execCommand('copy')) {
|
|
return;
|
|
}
|
|
status.textContent = button.dataset.copied;
|
|
button.focus();
|
|
} catch {
|
|
// The selected URL remains available for manual copying.
|
|
}
|
|
}
|
|
|
|
document.addEventListener('click', loadMore);
|
|
document.addEventListener('click', copyLink);
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', () => initializeWithin(document), {once: true});
|
|
} else {
|
|
initializeWithin(document);
|
|
}
|
|
|
|
let registered = false;
|
|
function registerElementorPreview() {
|
|
if (!registered && window.elementorFrontend?.hooks) {
|
|
registered = true;
|
|
window.elementorFrontend.hooks.addAction('frontend/element_ready/eventlabs-event-loop.default', (scope) => {
|
|
if (scope[0]) initializeWithin(scope[0]);
|
|
});
|
|
}
|
|
}
|
|
|
|
registerElementorPreview();
|
|
if (!registered && window.jQuery) window.jQuery(window).one('elementor/frontend/init', registerElementorPreview);
|
|
})();
|