Files
xmarticaims/wp-content/themes/xmartica/js/scripts.js
T
2026-11-02 10:26:32 -06:00

461 lines
11 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
'use strict';
var header = document.getElementById('site-header');
var toggle = document.getElementById('site-menu-toggle');
var navigation = document.getElementById('site-navigation');
var submenuToggle = navigation ? navigation.querySelector('.xmartica-menu__submenu-toggle') : null;
var solutionsSubmenu = document.getElementById('xmartica-solutions-submenu');
if (!header || !toggle || !navigation) {
return;
}
var mobileBreakpoint = 1024;
function setSubmenuState(isOpen) {
if (!submenuToggle || !solutionsSubmenu) {
return;
}
solutionsSubmenu.classList.toggle('is-open', isOpen);
submenuToggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
submenuToggle.setAttribute('aria-label', isOpen ? 'Ocultar soluciones' : 'Mostrar soluciones');
}
function setMenuState(isOpen) {
navigation.classList.toggle('is-open', isOpen);
toggle.classList.toggle('is-open', isOpen);
toggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
toggle.setAttribute('aria-label', isOpen ? 'Cerrar menú' : 'Abrir menú');
if (isOpen && window.innerWidth <= mobileBreakpoint) {
setSubmenuState(true);
}
}
function closeMenu() {
setSubmenuState(false);
setMenuState(false);
}
toggle.addEventListener('click', function () {
setMenuState(toggle.getAttribute('aria-expanded') !== 'true');
});
if (submenuToggle && solutionsSubmenu) {
submenuToggle.addEventListener('click', function () {
setSubmenuState(submenuToggle.getAttribute('aria-expanded') !== 'true');
});
}
navigation.addEventListener('click', function (event) {
if (event.target.closest('a') && window.innerWidth <= mobileBreakpoint) {
closeMenu();
}
});
document.addEventListener('keydown', function (event) {
if (event.key === 'Escape') {
closeMenu();
toggle.focus();
}
});
window.addEventListener('resize', function () {
if (window.innerWidth > mobileBreakpoint) {
closeMenu();
}
});
function updateStickyState() {
header.classList.toggle('is-scrolled', window.scrollY > 12);
}
window.addEventListener('scroll', updateStickyState, { passive: true });
updateStickyState();
});
document.addEventListener('DOMContentLoaded', function () {
'use strict';
var deliverablesViewport = document.querySelector('.consulting-deliverables__viewport');
if (!deliverablesViewport) {
return;
}
deliverablesViewport.setAttribute('data-lenis-prevent', '');
var responsiveQuery = window.matchMedia('(max-width: 1024px)');
function resetDeliverablesCarousel() {
deliverablesViewport.scrollLeft = 0;
deliverablesViewport.scrollTop = 0;
}
resetDeliverablesCarousel();
window.requestAnimationFrame(resetDeliverablesCarousel);
window.addEventListener('pageshow', resetDeliverablesCarousel);
if (typeof responsiveQuery.addEventListener === 'function') {
responsiveQuery.addEventListener('change', resetDeliverablesCarousel);
} else {
responsiveQuery.addListener(resetDeliverablesCarousel);
}
});
document.addEventListener('DOMContentLoaded', function () {
'use strict';
var counters = document.querySelectorAll('[data-animated-count]');
if (!counters.length) {
return;
}
var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var duration = 1800;
function formatCount(element, value) {
var padding = parseInt(element.getAttribute('data-count-pad') || '0', 10);
return String(value).padStart(padding, '0');
}
function setFinalCount(element) {
var target = parseInt(element.getAttribute('data-count-target'), 10);
element.textContent = formatCount(element, target);
}
function animateCount(element) {
if (element.dataset.counted === 'true') {
return;
}
element.dataset.counted = 'true';
var target = parseInt(element.getAttribute('data-count-target'), 10);
var startTime = null;
function updateCount(timestamp) {
if (startTime === null) {
startTime = timestamp;
}
var progress = Math.min((timestamp - startTime) / duration, 1);
var easedProgress = 1 - Math.pow(1 - progress, 3);
var current = Math.floor(target * easedProgress);
element.textContent = formatCount(element, current);
if (progress < 1) {
window.requestAnimationFrame(updateCount);
return;
}
setFinalCount(element);
}
window.requestAnimationFrame(updateCount);
}
if (reduceMotion) {
counters.forEach(setFinalCount);
return;
}
if (!('IntersectionObserver' in window)) {
counters.forEach(animateCount);
return;
}
var observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (!entry.isIntersecting) {
return;
}
animateCount(entry.target);
observer.unobserve(entry.target);
});
}, {
threshold: 0.35
});
counters.forEach(function (counter) {
observer.observe(counter);
});
});
document.addEventListener('DOMContentLoaded', function () {
'use strict';
var faqSection = document.querySelector('.home-faq');
var faqItems = faqSection ? faqSection.querySelector('.wp-faq-schema-items') : null;
if (!faqItems) {
return;
}
var questions = Array.prototype.filter.call(faqItems.children, function (child) {
return child.tagName === 'H3';
});
if (!questions.length) {
return;
}
var iconUrl = faqSection.getAttribute('data-faq-icon');
if (iconUrl) {
questions.forEach(function (question) {
var icon = document.createElement('span');
var image = document.createElement('img');
icon.className = 'home-faq__toggle-icon';
icon.setAttribute('aria-hidden', 'true');
image.src = iconUrl;
image.alt = '';
image.width = 20;
image.height = 20;
icon.appendChild(image);
question.appendChild(icon);
});
}
if (faqItems.classList.contains('ui-accordion')) {
return;
}
faqItems.classList.add('home-faq-accordion-ready');
function setQuestionState(question, isOpen) {
var answer = question.nextElementSibling;
if (!answer) {
return;
}
question.classList.toggle('is-open', isOpen);
question.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
answer.hidden = !isOpen;
}
function toggleQuestion(question) {
var shouldOpen = question.getAttribute('aria-expanded') !== 'true';
questions.forEach(function (item) {
setQuestionState(item, item === question && shouldOpen);
});
}
questions.forEach(function (question, index) {
var answer = question.nextElementSibling;
var questionId = 'xmartica-faq-question-' + (index + 1);
var answerId = 'xmartica-faq-answer-' + (index + 1);
question.id = questionId;
question.setAttribute('role', 'button');
question.setAttribute('tabindex', '0');
question.setAttribute('aria-controls', answerId);
if (answer) {
answer.id = answerId;
answer.setAttribute('role', 'region');
answer.setAttribute('aria-labelledby', questionId);
}
setQuestionState(question, index === 0);
question.addEventListener('click', function () {
toggleQuestion(question);
});
question.addEventListener('keydown', function (event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
toggleQuestion(question);
}
});
});
});
document.addEventListener('DOMContentLoaded', function () {
'use strict';
var intro = document.getElementById('xmartica-intro');
var video = document.getElementById('xmartica-intro-video');
var skip = document.getElementById('xmartica-intro-skip');
if (!intro || !video || !skip) {
return;
}
var storageKey = 'xmartica_intro_seen';
var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var userAgent = window.navigator.userAgent;
var isIOS = /iPhone|iPad|iPod/i.test(userAgent) ||
(window.navigator.maxTouchPoints > 1 && /Macintosh/i.test(userAgent));
var isMobile = /Android|iPhone|iPad|iPod|IEMobile|Opera Mini/i.test(userAgent) ||
(window.navigator.maxTouchPoints > 1 && !window.matchMedia('(any-pointer: fine)').matches);
var hasSeenIntro = false;
var hasExited = false;
var backgroundCleared = false;
var playAttempted = false;
var exitTimer = null;
try {
hasSeenIntro = window.sessionStorage.getItem(storageKey) === '1';
} catch (error) {
hasSeenIntro = false;
}
function removeIntro() {
document.body.classList.remove('xmartica-intro-active');
if (intro.parentNode) {
intro.parentNode.removeChild(intro);
}
}
function clearBackground() {
if (backgroundCleared) {
return;
}
backgroundCleared = true;
intro.classList.add('is-background-clear');
}
function exitIntro() {
if (hasExited) {
return;
}
hasExited = true;
window.clearTimeout(exitTimer);
clearBackground();
try {
window.sessionStorage.setItem(storageKey, '1');
} catch (error) {
// Storage can be unavailable in privacy modes; the intro still exits.
}
try {
video.pause();
} catch (error) {
// The video may not have initialized yet.
}
intro.classList.add('is-exiting');
window.setTimeout(removeIntro, 400);
}
if (hasSeenIntro || reduceMotion) {
removeIntro();
return;
}
document.body.classList.add('xmartica-intro-active');
if (isIOS) {
var webmSource = video.querySelector('source[type="video/webm"]');
if (webmSource && webmSource.parentNode) {
webmSource.parentNode.removeChild(webmSource);
video.load();
}
}
if (!isMobile) {
video.addEventListener('timeupdate', function () {
if (video.duration && video.currentTime > video.duration * 0.45) {
clearBackground();
}
});
}
function tryPlay() {
if (playAttempted || hasExited) {
return;
}
playAttempted = true;
var playPromise = video.play();
if (playPromise && typeof playPromise.catch === 'function') {
playPromise.catch(function (error) {
if (error && error.name === 'AbortError') {
return;
}
exitIntro();
});
}
}
video.addEventListener('ended', exitIntro);
video.addEventListener('error', exitIntro);
skip.addEventListener('click', exitIntro);
if (video.readyState >= 2) {
tryPlay();
} else {
video.addEventListener('loadeddata', tryPlay, { once: true });
}
window.setTimeout(function () {
if (!hasExited && video.readyState < 2) {
exitIntro();
}
}, 3000);
exitTimer = window.setTimeout(exitIntro, isMobile ? 5500 : 8500);
});
document.addEventListener('DOMContentLoaded', function () {
'use strict';
var glow = document.querySelector('.home-hero__copy-glow');
if (!glow || window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
return;
}
var cycleDuration = 8000;
var hiddenPoint = cycleDuration * 0.5;
var lastPosition = { x: 0, y: 0 };
var positionTimer = null;
function randomBetween(min, max) {
return min + Math.random() * (max - min);
}
function chooseNextPosition() {
var nextPosition;
var attempts = 0;
do {
nextPosition = {
x: randomBetween(-18, 18),
y: randomBetween(-10, 10)
};
attempts += 1;
} while (
attempts < 8 &&
Math.abs(nextPosition.x - lastPosition.x) < 8 &&
Math.abs(nextPosition.y - lastPosition.y) < 5
);
lastPosition = nextPosition;
glow.style.setProperty('--hero-glow-x', nextPosition.x.toFixed(2) + '%');
glow.style.setProperty('--hero-glow-y', nextPosition.y.toFixed(2) + '%');
}
function schedulePositionChange() {
window.clearTimeout(positionTimer);
positionTimer = window.setTimeout(chooseNextPosition, hiddenPoint);
}
schedulePositionChange();
glow.addEventListener('animationiteration', schedulePositionChange);
});