/* ============================================================ Atroute — motion layer ------------------------------------------------------------ อนิเมชันเบา ๆ ที่ใช้ร่วมทุกหน้า (โหลดจาก shell ก่อน router.js) 1) แถบโหลดบาง ๆ บนสุด Motion.loading(true|false) 2) หน้าใหม่ค่อย ๆ โผล่ Motion.enter(#view) ← router เรียกหลัง inject 3) เลื่อนแล้วค่อยโผล่ สแกนอัตโนมัติใน #view + MutationObserver (การ์ดที่ JS สร้างทีหลังก็ได้อนิเมชัน) หลักการ - แตะเฉพาะ opacity + transform → ไม่เกิด layout shift (CLS) และไม่กระตุก - ทำเฉพาะอิลิเมนต์ที่ยังไม่เห็นบนจอ → ของที่เห็นอยู่แล้วไม่กะพริบ - เคลียร์คลาส/transform ทิ้งหลังจบ → hover/transform เดิมของแต่ละหน้าไม่ถูกทับ - prefers-reduced-motion: reduce → ปิดทั้งหมด (ไม่แตะ DOM เลย) ============================================================ */ (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 + ')'; barFill.style.transformOrigin = '0 50%'; barFill.style.width = '100%'; } 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; // reflow → ให้เริ่มจาก 0 จริง 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) หน้าใหม่ค่อย ๆ โผล่ (เรียกจาก router ทุกครั้งที่ inject) ---------------------------------------------------------- */ 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); /* partial บางหน้าเรนเดอร์การ์ดหลัง fetch JSON — รอบสองรับของที่มาช้า */ 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' ].join(','); /* ห้ามใส่ transform ให้ของที่ครอบแผนที่ (Leaflet คำนวณพิกัดจากกล่องแม่) */ 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(); // กันของที่ถูก "ข้าม" ตอนเลื่อนจัมป์ } /* เปิดของที่ผ่านขึ้นไปเหนือจอแล้วโดยไม่ต้องรอIntersect (กด End / กดลิงก์ข้ามส่วน / เบราว์เซอร์คืนตำแหน่งเดิม → IO ไม่เคยเห็นของกลางทาง) */ 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; /* ซ้อนกันอยู่ → ปล่อยให้ตัวแม่เป็นคนอนิเมต (ลด observer + ไม่กระตุกซ้อน) */ 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); } /* เลื่อน/ย่อหน้า → เปิดของที่เลยขึ้นไปแล้ว (กันค้าง opacity 0) */ window.addEventListener('scroll', queueSweep, { passive: true }); window.addEventListener('resize', queueSweep); /* กลับมาที่แท็บ (จากพื้นหลัง) → ตรวจอีกรอบ เผื่อของค้างจากตอนที่ยังไม่วาดจอ */ document.addEventListener('visibilitychange', function () { if (document.visibilityState === 'visible') queueSweep(); }); /* ปริ้นต์/บันทึก PDF ต้องเห็นครบทุกการ์ด */ window.addEventListener('beforeprint', function () { var rest = pending.slice(); for (var i = 0; i < rest.length; i++) reveal(rest[i]); }); /* การ์ดที่ JS สร้างทีหลัง (fetch JSON / กรอง / เปลี่ยนภาษา) → สแกนซ้ำให้เอง ใช้ setTimeout ไม่ใช่ rAF เพราะแท็บที่ถูกซ่อนจะไม่ยิง rAF */ 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 }; })();