- frontend/src/lib/ui/motion.ts + components/MotionBoot.tsx: page-enter and scroll reveal per pathname change (ported from mock motion.js); IntersectionObserver plus sweep fallback for jump-scroll/hidden tabs, maps never transformed - frontend/src/lib/ui/analytics.ts: cookie-less local-first search analytics (localStorage at_stats_v1, DNT/GPC + opt-out, term sanitiser, 900ms debounce, caps 300 terms / 200 views / 120 days); optional Umami endpoint via AT_ANALYTICS - /stats page (StatsView + styles/pages/stats.css): KPIs, 14-day chart, top terms, top route/stop pages, no-result terms, JSON export, clear, opt-out - hooks: stops search apply(), stop detail route filter - remove footer visitor counter (lib/ui/visitors.ts, .mu-visitor/.mu-vreset, i18n keys) - remove floating install-app button; PwaBoot keeps SW + guarded version toast (sessionStorage guard, silent reload when tab hidden) - i18n th/en: nav.stats, update.applying, stats.* (public + src/data locales) - version.json + sw VERSION -> 2026.10.24.3; docs/UI-PORT.md section for this round
206 lines
7.4 KiB
TypeScript
206 lines
7.4 KiB
TypeScript
// @ts-nocheck
|
|
/**
|
|
* motion.ts — อนิเมชันร่วมทุกหน้า (ported from web-at-transit-mock/ui/motion.js)
|
|
*
|
|
* window.Motion.enter(#view) ← MotionBoot เรียกทุกครั้งที่ pathname เปลี่ยน
|
|
* window.Motion.scan() ← สแกนการ์ดที่จะค่อย ๆ โผล่ตอนเลื่อน
|
|
* window.Motion.loading(bool) ← แถบโหลดบางบนสุด
|
|
*
|
|
* หลักการเดียวกับต้นแบบ:
|
|
* - แตะเฉพาะ opacity + transform (ไม่เกิด CLS) · ใช้ของที่ยังไม่เห็นบนจอเท่านั้น
|
|
* - เคลียร์ class/transform หลังจบ → hover เดิมของแต่ละหน้าไม่ถูกทับ
|
|
* - ไม่แตะของที่ครอบแผนที่ (Leaflet) · prefers-reduced-motion = ปิดทั้งหมด
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var mqReduce = window.matchMedia ? window.matchMedia('(prefers-reduced-motion: reduce)') : null;
|
|
function reduced() { return !!(mqReduce && mqReduce.matches); }
|
|
|
|
/* ----------------------------------------------------------
|
|
1) แถบโหลดด้านบน
|
|
---------------------------------------------------------- */
|
|
var bar = null, barFill = null, growTimer = null, outTimer = null;
|
|
|
|
function ensureBar() {
|
|
if (bar && document.body.contains(bar)) return;
|
|
bar = document.createElement('div');
|
|
bar.className = 'mu-loadbar';
|
|
bar.setAttribute('aria-hidden', 'true');
|
|
barFill = document.createElement('div');
|
|
barFill.className = 'mu-loadbar-fill';
|
|
bar.appendChild(barFill);
|
|
document.body.appendChild(bar);
|
|
}
|
|
|
|
function setProgress(p) {
|
|
if (!barFill) return;
|
|
barFill.style.transition = 'transform .4s cubic-bezier(.22,.61,.36,1)';
|
|
barFill.style.transform = 'scaleX(' + p + ')';
|
|
}
|
|
|
|
function loading(on) {
|
|
if (reduced() || !document.body) return;
|
|
ensureBar();
|
|
clearTimeout(growTimer);
|
|
clearTimeout(outTimer);
|
|
|
|
if (on) {
|
|
bar.classList.add('is-on');
|
|
barFill.style.transition = 'none';
|
|
barFill.style.transform = 'scaleX(0)';
|
|
void barFill.offsetWidth;
|
|
setProgress(0.18);
|
|
growTimer = setTimeout(function () { setProgress(0.5); }, 200);
|
|
growTimer = setTimeout(function () { setProgress(0.78); }, 700);
|
|
return;
|
|
}
|
|
|
|
setProgress(1);
|
|
outTimer = setTimeout(function () {
|
|
bar.classList.remove('is-on');
|
|
outTimer = setTimeout(function () {
|
|
if (!barFill) return;
|
|
barFill.style.transition = 'none';
|
|
barFill.style.transform = 'scaleX(0)';
|
|
}, 260);
|
|
}, 180);
|
|
}
|
|
|
|
/* ----------------------------------------------------------
|
|
2) เข้าหน้า
|
|
---------------------------------------------------------- */
|
|
function enter(el) {
|
|
if (!el || reduced()) { firstPass = false; return; }
|
|
el.classList.remove('mu-view-in');
|
|
void el.offsetWidth;
|
|
el.classList.add('mu-view-in');
|
|
firstPass = true;
|
|
scan(el);
|
|
setTimeout(function () { firstPass = false; scan(el); }, 400);
|
|
}
|
|
|
|
/* ----------------------------------------------------------
|
|
3) เลื่อนแล้วค่อยโผล่
|
|
---------------------------------------------------------- */
|
|
var SELECTOR = [
|
|
'.mu-zone', '.mu-card', '.mu-stop-card', '.mu-blog-card', '.mu-blog-row',
|
|
'.mu-credit-section', '.mu-summary', '.mu-stat', '.mu-zone-head',
|
|
'.mu-sponsor-head', '.mu-schedule', '.mu-kpi', '.mu-post-figure',
|
|
'.mu-nearby', '.mu-route-tools', '.st-card', '.st-kpi'
|
|
].join(',');
|
|
|
|
var SKIP_INSIDE = '.leaflet-container, .at-map, .blog-mapx, [data-no-reveal]';
|
|
|
|
var io = ('IntersectionObserver' in window)
|
|
? new IntersectionObserver(onIntersect, { rootMargin: '0px 0px -8% 0px', threshold: 0.05 })
|
|
: null;
|
|
|
|
var firstPass = false;
|
|
var pending = [];
|
|
|
|
function reveal(el) {
|
|
if (!el || el.classList.contains('is-in')) return;
|
|
var at = pending.indexOf(el);
|
|
if (at !== -1) pending.splice(at, 1);
|
|
io.unobserve(el);
|
|
el.classList.add('is-in');
|
|
var d = parseFloat(el.style.getPropertyValue('--mu-d')) || 0;
|
|
setTimeout(function () {
|
|
el.classList.remove('mu-reveal', 'is-in');
|
|
el.style.removeProperty('--mu-d');
|
|
}, 700 + d);
|
|
}
|
|
|
|
function onIntersect(entries) {
|
|
for (var i = 0; i < entries.length; i++) {
|
|
if (entries[i].isIntersecting) reveal(entries[i].target);
|
|
}
|
|
sweep();
|
|
}
|
|
|
|
function sweep() {
|
|
if (!pending.length) return;
|
|
var vh = window.innerHeight || document.documentElement.clientHeight;
|
|
for (var i = pending.length - 1; i >= 0; i--) {
|
|
var el = pending[i];
|
|
if (!el.isConnected) { pending.splice(i, 1); io.unobserve(el); continue; }
|
|
if (el.getBoundingClientRect().top < vh) reveal(el);
|
|
}
|
|
}
|
|
|
|
var sweepQueued = 0;
|
|
function queueSweep() {
|
|
if (sweepQueued) return;
|
|
sweepQueued = 1;
|
|
setTimeout(function () { sweepQueued = 0; sweep(); }, 90);
|
|
}
|
|
|
|
function scan(root) {
|
|
if (!io || reduced()) return;
|
|
root = root || document.getElementById('view');
|
|
if (!root) return;
|
|
|
|
var nodes = root.querySelectorAll(SELECTOR);
|
|
if (!nodes.length) return;
|
|
|
|
pending = pending.filter(function (el) { return el.isConnected; });
|
|
|
|
var vh = window.innerHeight || document.documentElement.clientHeight;
|
|
var idx = 0;
|
|
|
|
for (var i = 0; i < nodes.length; i++) {
|
|
var el = nodes[i];
|
|
if (el.classList.contains('mu-reveal') || el.classList.contains('is-in')) continue;
|
|
if (el.closest(SKIP_INSIDE)) continue;
|
|
if (el.parentElement && el.parentElement.closest(SELECTOR) && root.contains(el.parentElement.closest(SELECTOR))) continue;
|
|
|
|
var rect = el.getBoundingClientRect();
|
|
if (rect.width === 0 && rect.height === 0) continue;
|
|
if (firstPass && rect.top < vh) continue;
|
|
|
|
el.style.setProperty('--mu-d', (Math.min(idx, 4) * 45) + 'ms');
|
|
el.classList.add('mu-reveal');
|
|
pending.push(el);
|
|
idx++;
|
|
io.observe(el);
|
|
}
|
|
|
|
if (idx) setTimeout(sweep, 300);
|
|
}
|
|
|
|
window.addEventListener('scroll', queueSweep, { passive: true });
|
|
window.addEventListener('resize', queueSweep);
|
|
document.addEventListener('visibilitychange', function () {
|
|
if (document.visibilityState === 'visible') queueSweep();
|
|
});
|
|
window.addEventListener('beforeprint', function () {
|
|
var rest = pending.slice();
|
|
for (var i = 0; i < rest.length; i++) reveal(rest[i]);
|
|
});
|
|
|
|
/* การ์ดที่ JS สร้างทีหลัง (fetch JSON / กรอง / เปลี่ยนภาษา) → สแกนซ้ำให้เอง */
|
|
var scanTimer = 0;
|
|
function scheduleScan() {
|
|
if (scanTimer) return;
|
|
scanTimer = setTimeout(function () { scanTimer = 0; scan(); }, 80);
|
|
}
|
|
|
|
function watch() {
|
|
var host = document.getElementById('view');
|
|
if (!host || !window.MutationObserver) return;
|
|
new MutationObserver(function (list) {
|
|
for (var i = 0; i < list.length; i++) {
|
|
if (list[i].addedNodes && list[i].addedNodes.length) { scheduleScan(); return; }
|
|
}
|
|
}).observe(host, { childList: true, subtree: true });
|
|
}
|
|
|
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', watch);
|
|
else watch();
|
|
|
|
window.Motion = { enter: enter, scan: scan, loading: loading, reduced: reduced };
|
|
})();
|
|
|
|
export {};
|