/* ============================================================ หน้าบทความ — loader + สลับมุมมอง ตาราง/รายการ + หน้าอ่านบทความ ------------------------------------------------------------ ข้อมูล: partials/blog-data/ (ระบบเดียวกับ about us) text1.json + pic1. , text2.json + pic2. , ... { "th": { "title","body":["..."],"tag" }, "en": {...}, "date": "2026-09-01" } route: #/blog → รายการบทความ (มุมมอง grid/list — จำค่าใน localStorage) #/blog/:n → อ่านบทความที่ n ============================================================ */ (function () { 'use strict'; var DATA_DIR = './partials/blog-data/'; var IMG_EXTS = ['jpg', 'jpeg', 'png', 'webp']; var MAX_POSTS = 50; var LS_VIEW = 'at_blog_view'; var EXCERPT_LEN = 150; function lang() { return (window.I18N && I18N.getLang()) || 'th'; } function t(key) { return window.I18N ? I18N.t(key) : key; } function pick(data, l) { if (!data) return null; if (l !== 'th' && data[l]) return data[l]; return data.th || data[l] || data[Object.keys(data)[0]] || data; } function fetchJSON(n) { return fetch(DATA_DIR + 'text' + n + '.json', { cache: 'no-store' }) .then(function (r) { return r.ok ? r.json() : null; }) .catch(function () { return null; }); } function findImage(n) { var i = 0; function tryNext() { if (i >= IMG_EXTS.length) return Promise.resolve(null); var url = DATA_DIR + 'pic' + n + '.' + IMG_EXTS[i++]; return fetch(url, { method: 'HEAD', cache: 'no-store' }) .then(function (r) { return r.ok ? url : tryNext(); }) .catch(tryNext); } return tryNext(); } function loadPosts() { var posts = []; var n = 1; function next() { if (n > MAX_POSTS) return Promise.resolve(posts); var current = n++; return fetchJSON(current).then(function (data) { if (!data) return posts; return findImage(current).then(function (imgUrl) { posts.push({ n: current, data: data, img: imgUrl }); return next(); }); }); } return next(); } function getView() { try { var v = localStorage.getItem(LS_VIEW); return v === 'list' ? 'list' : 'grid'; } catch (e) { return 'grid'; } } function setView(v) { try { localStorage.setItem(LS_VIEW, v); } catch (e) {} } function readTime(body) { var words = (body || []).join(' ').length; return Math.max(1, Math.round(words / 350)); // ~350 ตัวอักษร/นาที } function renderGrid(posts, host) { posts.forEach(function (p) { var c = pick(p.data, lang()); var a = document.createElement('a'); a.className = 'mu-blog-card'; a.href = '#/blog/' + p.n; var cover = document.createElement('div'); cover.className = 'mu-cover' + (p.img ? '' : ' no-img'); if (p.img) { var im = document.createElement('img'); im.src = p.img; im.alt = ''; im.loading = 'lazy'; im.decoding = 'async'; cover.appendChild(im); } if (c.tag) { var tag = document.createElement('span'); tag.className = 'mu-cover-tag'; tag.textContent = c.tag; cover.appendChild(tag); } var ct = document.createElement('p'); ct.className = 'mu-cover-title'; ct.textContent = c.title || ''; cover.appendChild(ct); a.appendChild(cover); var body = document.createElement('div'); body.className = 'mu-blog-body'; body.innerHTML = '

'; body.querySelector('h2').textContent = c.title || ''; body.querySelector('.mu-blog-excerpt').textContent = (c.body || []).join(' ').slice(0, EXCERPT_LEN) + '…'; body.querySelector('.mu-blog-more').textContent = t('blog.readMore') + ' →'; var meta = body.querySelector('.mu-blog-meta'); if (p.data.date) { var tm = document.createElement('time'); tm.dateTime = p.data.date; tm.textContent = p.data.date; meta.appendChild(tm); var dot = document.createElement('span'); dot.textContent = '·'; meta.appendChild(dot); } var rt = document.createElement('span'); rt.textContent = readTime(c.body) + ' ' + t('blog.minRead'); meta.appendChild(rt); a.appendChild(body); host.appendChild(a); }); } function renderList(posts, host) { posts.forEach(function (p) { var c = pick(p.data, lang()); var a = document.createElement('a'); a.className = 'mu-blog-row'; a.href = '#/blog/' + p.n; if (p.img) { var im = document.createElement('img'); im.className = 'thumb'; im.src = p.img; im.alt = ''; im.loading = 'lazy'; im.decoding = 'async'; a.appendChild(im); } var box = document.createElement('div'); box.innerHTML = '

'; box.querySelector('h2').textContent = c.title || ''; box.querySelector('.mu-blog-excerpt').textContent = (c.body || []).join(' ').slice(0, 110) + '…'; var meta = box.querySelector('.mu-blog-meta'); var bits = []; if (p.data.date) bits.push(p.data.date); if (c.tag) bits.push(c.tag); bits.push(readTime(c.body) + ' ' + t('blog.minRead')); meta.textContent = bits.join(' · '); a.appendChild(box); host.appendChild(a); }); } function renderPost(post, host) { var c = pick(post.data, lang()); var art = document.createElement('article'); art.className = 'mu-post'; if (post.img) { var cover = document.createElement('div'); cover.className = 'mu-cover'; var im = document.createElement('img'); im.src = post.img; im.alt = c.title || ''; im.decoding = 'async'; cover.appendChild(im); if (c.tag) { var tag = document.createElement('span'); tag.className = 'mu-cover-tag'; tag.textContent = c.tag; cover.appendChild(tag); } art.appendChild(cover); } var body = document.createElement('div'); body.className = 'mu-post-body'; var back = document.createElement('a'); back.className = 'mu-post-back'; back.href = '#/blog'; back.textContent = '← ' + t('blog.backList'); body.appendChild(back); var meta = document.createElement('div'); meta.className = 'mu-blog-meta'; var bits = []; if (post.data.date) bits.push(post.data.date); bits.push(readTime(c.body) + ' ' + t('blog.minRead')); meta.textContent = bits.join(' · '); body.appendChild(meta); var h1 = document.createElement('h1'); h1.textContent = c.title || ''; body.appendChild(h1); (c.body || []).forEach(function (pText) { var p = document.createElement('p'); p.textContent = pText; body.appendChild(p); }); art.appendChild(body); host.appendChild(art); } function init() { var host = document.getElementById('blog-list'); if (!host) return; var route = window.__route || { params: {} }; var postId = route.params && route.params.postId; loadPosts().then(function (posts) { host.innerHTML = ''; if (postId) { var post = posts.filter(function (p) { return String(p.n) === String(postId); })[0]; var bar = document.getElementById('blog-bar'); if (bar) bar.hidden = true; if (post) { renderPost(post, host); } else { host.innerHTML = '

' + t('blog.notFound') + '

'; } return; } if (!posts.length) { host.innerHTML = '

' + t('blog.empty') + '

'; return; } var bar = document.getElementById('blog-bar'); var count = document.getElementById('blog-count'); if (count) count.textContent = t('blog.count') + ' ' + posts.length + ' ' + t('blog.items'); if (bar) bar.hidden = false; var mode = getView(); function paint() { host.className = mode === 'list' ? 'mode-list' : 'mode-grid'; host.innerHTML = ''; (mode === 'list' ? renderList : renderGrid)(posts, host); document.querySelectorAll('.mu-view-btn').forEach(function (b) { b.classList.toggle('active', b.getAttribute('data-view') === mode); b.setAttribute('aria-pressed', String(b.getAttribute('data-view') === mode)); }); } paint(); document.querySelectorAll('.mu-view-btn').forEach(function (b) { b.addEventListener('click', function () { mode = b.getAttribute('data-view'); setView(mode); paint(); }); }); }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();