hesabixWpTheme/assets/js/front-page.js

338 lines
12 KiB
JavaScript

/**
* صفحه نخست — اتاق فرمان، MCP، تور، FAQ
*
* @package Hesabix2
*/
(function () {
'use strict';
var reducedMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var coarsePointer = window.matchMedia && window.matchMedia('(pointer: coarse)').matches;
function animateStat(el) {
var target = parseInt(el.getAttribute('data-stat-value'), 10);
if (isNaN(target)) {
return;
}
if (reducedMotion) {
el.textContent = String(target);
return;
}
var duration = 1400;
var start = null;
function step(ts) {
if (!start) {
start = ts;
}
var p = Math.min((ts - start) / duration, 1);
var eased = 1 - Math.pow(1 - p, 3);
el.textContent = String(Math.round(target * eased));
if (p < 1) {
requestAnimationFrame(step);
}
}
requestAnimationFrame(step);
}
function initStats() {
var nodes = document.querySelectorAll('[data-stat-value]');
if (!nodes.length) {
return;
}
if (!('IntersectionObserver' in window)) {
nodes.forEach(animateStat);
return;
}
var obs = new IntersectionObserver(function (entries, o) {
entries.forEach(function (entry) {
if (!entry.isIntersecting) {
return;
}
animateStat(entry.target);
o.unobserve(entry.target);
});
}, { threshold: 0.35 });
nodes.forEach(function (n) { obs.observe(n); });
}
function initTour() {
var root = document.querySelector('[data-fp-tour]');
if (!root) {
return;
}
var tabs = Array.prototype.slice.call(root.querySelectorAll('[data-tour-tab]'));
var panels = Array.prototype.slice.call(root.querySelectorAll('[data-tour-panel]'));
if (!tabs.length || !panels.length) {
return;
}
var timer = null;
var index = 0;
var userStopped = false;
var interval = 7000;
function activate(id, user) {
var found = false;
tabs.forEach(function (tab, i) {
var on = tab.getAttribute('data-tour-tab') === id;
tab.classList.toggle('is-active', on);
tab.setAttribute('aria-selected', on ? 'true' : 'false');
if (on) {
index = i;
found = true;
var bar = tab.querySelector('.fp-tour__tab-progress');
if (bar) {
bar.style.animation = 'none';
void bar.offsetWidth;
if (!reducedMotion && !user) {
bar.style.animation = '';
}
}
}
});
panels.forEach(function (panel) {
var on = panel.getAttribute('data-tour-panel') === id;
panel.classList.toggle('is-active', on);
panel.hidden = !on;
});
if (user) {
userStopped = true;
stop();
tabs.forEach(function (tab) {
var bar = tab.querySelector('.fp-tour__tab-progress');
if (bar) {
bar.style.animation = 'none';
}
});
}
return found;
}
function next() {
var i = (index + 1) % tabs.length;
activate(tabs[i].getAttribute('data-tour-tab'), false);
}
function play() {
if (reducedMotion || userStopped) {
return;
}
stop();
timer = window.setInterval(next, interval);
}
function stop() {
if (timer) {
window.clearInterval(timer);
timer = null;
}
}
tabs.forEach(function (tab) {
tab.addEventListener('click', function () {
activate(tab.getAttribute('data-tour-tab'), true);
});
});
root.addEventListener('keydown', function (e) {
if (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') {
return;
}
e.preventDefault();
var dir = e.key === 'ArrowLeft' ? 1 : -1;
var i = (index + dir + tabs.length) % tabs.length;
activate(tabs[i].getAttribute('data-tour-tab'), true);
tabs[i].focus();
});
if ('IntersectionObserver' in window) {
var io = new IntersectionObserver(function (entries) {
if (entries[0] && entries[0].isIntersecting) {
var current = tabs[index];
if (current) {
activate(current.getAttribute('data-tour-tab'), false);
}
play();
} else {
stop();
}
}, { threshold: 0.25 });
io.observe(root);
} else {
play();
}
}
function initFaq() {
document.querySelectorAll('.fp-faq-item').forEach(function (item) {
var btn = item.querySelector('.fp-faq-question');
var panel = item.querySelector('.fp-faq-answer');
if (!btn || !panel) {
return;
}
btn.addEventListener('click', function () {
var open = item.classList.toggle('fp-faq-item--open');
btn.setAttribute('aria-expanded', open ? 'true' : 'false');
panel.hidden = !open;
});
});
}
function initReveal() {
var els = document.querySelectorAll('.fp-reveal');
if (!els.length) {
return;
}
if (reducedMotion || !('IntersectionObserver' in window)) {
els.forEach(function (el) { el.classList.add('fp-reveal--visible'); });
return;
}
var obs = new IntersectionObserver(function (entries, o) {
entries.forEach(function (entry) {
if (!entry.isIntersecting) {
return;
}
entry.target.classList.add('fp-reveal--visible');
o.unobserve(entry.target);
});
}, { threshold: 0.1, rootMargin: '0px 0px -36px 0px' });
els.forEach(function (el) { obs.observe(el); });
}
function initStage() {
var stage = document.querySelector('[data-fp-stage]');
if (!stage) {
return;
}
var space = stage.querySelector('[data-fp-space]');
if (!space || reducedMotion || coarsePointer) {
return;
}
stage.addEventListener('mousemove', function (e) {
var rect = stage.getBoundingClientRect();
var x = (e.clientX - rect.left) / rect.width - 0.5;
var y = (e.clientY - rect.top) / rect.height - 0.5;
stage.classList.add('is-tilting');
space.style.transform = 'rotateY(' + (14 + x * -18) + 'deg) rotateX(' + (9 + y * 12) + 'deg)';
});
stage.addEventListener('mouseleave', function () {
stage.classList.remove('is-tilting');
space.style.transform = '';
});
}
function initHeader() {
var header = document.getElementById('site-header');
if (!header) {
return;
}
function onScroll() {
header.classList.toggle('is-scrolled', window.scrollY > 18);
}
onScroll();
window.addEventListener('scroll', onScroll, { passive: true });
}
function initMcpTools() {
var root = document.querySelector('[data-fp-mcp-tools]');
if (!root) {
return;
}
var items = Array.prototype.slice.call(root.querySelectorAll('li'));
var ask = document.querySelector('[data-fp-mcp-ask]');
var out = document.querySelector('[data-fp-mcp-out]');
if (items.length < 2) {
return;
}
var copy = [
{
ask: 'فروش این ماه را با ماه قبل مقایسه کن و ماندهٔ مشتریان را خلاصه کن.',
out: 'رشد فروش نسبت به دورهٔ قبل ثبت شد. فهرست مانده‌ها از دادهٔ حساب شما خوانده شده است.'
},
{
ask: 'ماندهٔ بدهکاران بالای ده میلیون را فهرست کن.',
out: 'اشخاص با ماندهٔ بالا از دفتر شما خوانده شد. جزئیات مطابق نقش کاربر محدود شده است.'
},
{
ask: 'کدام کالاها زیر نقطهٔ سفارش هستند؟',
out: 'موجودی انبار بررسی شد. اقلام زیر نقطهٔ سفارش برای تأمین مشخص شدند.'
},
{
ask: 'برای فاکتورهای امروز پیام یادآوری بساز؛ اول تأیید بگیر.',
out: 'سناریو آماده است. هیچ اقدام نوشتنی بدون تأیید جداگانه انجام نمی‌شود.'
}
];
var i = 0;
function show(next) {
i = next;
items.forEach(function (item, idx) {
item.classList.toggle('is-active', idx === i);
});
if (ask && copy[i]) {
ask.textContent = copy[i].ask;
}
if (out && copy[i]) {
out.textContent = copy[i].out;
}
}
items.forEach(function (item, idx) {
item.addEventListener('click', function () {
show(idx);
});
});
if (reducedMotion) {
return;
}
window.setInterval(function () {
show((i + 1) % items.length);
}, 4200);
}
function initAbTest() {
var cfg = window.hesabix2FrontPage;
if (!cfg || !cfg.abEnabled || !cfg.ajaxUrl) {
return;
}
var variant = cfg.abVariant || 'a';
function send(event) {
var body = new URLSearchParams();
body.set('action', cfg.abAction || 'hesabix2_fp_ab');
body.set('nonce', cfg.abNonce || '');
body.set('variant', variant);
body.set('event', event);
fetch(cfg.ajaxUrl, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: body.toString(),
keepalive: true,
}).catch(function () {});
}
if (!sessionStorage.getItem('hesabix_fp_ab_seen')) {
send('impression');
sessionStorage.setItem('hesabix_fp_ab_seen', '1');
}
document.querySelectorAll('[data-fp-ab-cta]').forEach(function (el) {
el.addEventListener('click', function () {
send('cta_click');
});
});
}
function init() {
initHeader();
initStats();
initTour();
initFaq();
initReveal();
initStage();
initMcpTools();
initAbTest();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();