พอร์ต UI จาก web-at-transit-mock เข้า Next.js + data sync เข้า Postgres/SeaweedFS
- 8 หน้า (home/stops/routes/stop/check/blog/about/credits) แปลง partials → React component ตรง mock
- ตรรกะพอร์ตเป็น src/lib/ui/*.ts (mount()) + runtime/PageRuntime
- CSS: base + pages/*.css import ต่อ route (คงลำดับ cascade ของ mock)
- asset/content: public/{image,resource/data,content,locales,vendor}
- แก้ path รูปไทล์พาร์ทเนอร์ (.image → /image) + fallback absolute
- ลบ UI เดิมที่ไม่ตรง mock (expanded/stops details/old components/globals.css)
- script/sync_data_to_postgres.mjs + sync_images_to_storage.mjs (dry-run/--apply, backup)
- docs/UI-PORT.md + DESIGN.md + README
This commit is contained in:
1 parent
24187fc3b7
commit
138a3d15d8
300 files changed
+20213
-1978
No files matched your search
@@ -0,0 +1,612 @@
|
||||
// @ts-nocheck
|
||||
/* Ported from web-at-transit-mock/ui/partials/blog.init.js — logic unchanged; bootstrap converted to mount(). */
|
||||
/* ============================================================
|
||||
หน้าบทความ — loader + สลับมุมมอง ตาราง/รายการ + หน้าอ่านบทความ
|
||||
------------------------------------------------------------
|
||||
ข้อมูล: partials/blog-data/ (ระบบเดียวกับ about us)
|
||||
text1.json + pic1.<ext> , text2.json + pic2.<ext> , ...
|
||||
{ "th": { "title","body":["..."],"tag" }, "en": {...}, "date": "2026-09-01" }
|
||||
|
||||
route:
|
||||
#/blog → รายการบทความ (มุมมอง grid/list — จำค่าใน localStorage)
|
||||
#/blog/:n → อ่านบทความที่ n
|
||||
============================================================ */
|
||||
/* ---- ported module body ---- */
|
||||
'use strict';
|
||||
|
||||
var DATA_DIR = '/content/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 = imgUrl(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 = '<div class="mu-blog-meta"></div><h2></h2><p class="mu-blog-excerpt"></p><span class="mu-blog-more"></span>';
|
||||
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 = '<div class="mu-blog-meta"></div><h2></h2><p class="mu-blog-excerpt"></p>';
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/* ---------- มินิมาร์กดาวน์สำหรับเนื้อหาบทความ ----------
|
||||
ปลอดภัย: escape HTML "ก่อน" แล้วจึงแปลงมาร์กดาวน์ (ไม่รับ HTML ดิบจากไฟล์)
|
||||
รองรับ:
|
||||
**หนา** / __หนา__ → <strong>
|
||||
*เอียง* / _เอียง_ → <em>
|
||||
[ข้อความ](url) → <a target=_blank rel=noopener>
|
||||
 → <img> (ย่อหน้าที่เป็นรูปเดี่ยว ๆ)
|
||||
- ข้อ / * ข้อ → <ul><li> (ติดกันหลายบรรทัด)
|
||||
`โค้ด` → <code>
|
||||
หมายเหตุ: URL ที่ไม่ใช่ http/https/./../ จะถูกทิ้ง (กัน javascript:) */
|
||||
function escHtml(s) {
|
||||
return String(s == null ? '' : s)
|
||||
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
||||
.replace(/"/g, '"').replace(/'/g, ''');
|
||||
}
|
||||
function safeUrl(u) {
|
||||
var s = String(u == null ? '' : u).trim();
|
||||
if (/^https?:\/\//i.test(s)) return s; /* เว็บภายนอก */
|
||||
if (/^(\.{0,2}\/)?[\w.\-]+\/[\w.\-/]*[\w.\-]$/.test(s)) return s; /* เส้นทางไฟล์: pic1.jpg · ./a/b.png · ../x/y.jpg */
|
||||
if (/^[\w.\-]+\.(jpg|jpeg|png|webp|gif|svg)$/i.test(s)) return s; /* ชื่อไฟล์เดี่ยว ๆ */
|
||||
return ''; /* javascript: / data: / อื่น ๆ → ทิ้ง */
|
||||
}
|
||||
/* แปลง inline (ตัวหนา/เอียง/ลิงก์/โค้ด) บนข้อความที่ escape แล้ว */
|
||||
function inlineMd(txt) {
|
||||
var s = escHtml(txt);
|
||||
s = s.replace(/!\[([^\]]*)\]\(([^()\s]*(?:\([^()]*\)[^()\s]*)*)\)/g, function (_, alt, url) {
|
||||
var u = safeUrl(url);
|
||||
return u ? '<img src="' + u + '" alt="' + alt + '" loading="lazy" decoding="async">' : alt;
|
||||
});
|
||||
s = s.replace(/\[([^\]]+)\]\(([^()\s]*(?:\([^()]*\)[^()\s]*)*)\)/g, function (_, label, url) {
|
||||
var u = safeUrl(url);
|
||||
return u ? '<a href="' + u + '" target="_blank" rel="noopener noreferrer">' + label + '</a>' : label;
|
||||
});
|
||||
s = s.replace(/`([^`]+)`/g, '<code>$1</code>');
|
||||
s = s.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
|
||||
s = s.replace(/__([^_]+)__/g, '<strong>$1</strong>');
|
||||
s = s.replace(/(^|[^*])\*([^*\s][^*]*?)\*/g, '$1<em>$2</em>');
|
||||
s = s.replace(/(^|[^_])_([^_\s][^_]*?)_/g, '$1<em>$2</em>');
|
||||
return s;
|
||||
}
|
||||
/* เรนเดอร์ body[] ลง host — จัดกลุ่มรายการ "- " ต่อกันเป็น <ul> */
|
||||
function renderBody(host, body) {
|
||||
var i = 0, list = null;
|
||||
function closeList() { if (list) { host.appendChild(list); list = null; } }
|
||||
body.forEach(function (raw) {
|
||||
var line = String(raw == null ? '' : raw);
|
||||
var bullet = line.match(/^\s*[-*]\s+(.+)$/);
|
||||
if (bullet) {
|
||||
if (!list) list = document.createElement('ul');
|
||||
var li = document.createElement('li');
|
||||
li.innerHTML = inlineMd(bullet[1]);
|
||||
list.appendChild(li);
|
||||
return;
|
||||
}
|
||||
closeList();
|
||||
/* ย่อหน้าที่เป็นรูปเดี่ยว  → ใช้ <figure> */
|
||||
var onlyImg = line.trim().match(/^!\[([^\]]*)\]\(([^)\s]+)\)$/);
|
||||
if (onlyImg) {
|
||||
var u = safeUrl(onlyImg[2]);
|
||||
if (u) {
|
||||
var fig = document.createElement('figure');
|
||||
fig.className = 'mu-post-figure';
|
||||
fig.innerHTML = '<img src="' + u + '" alt="' + escHtml(onlyImg[1]) + '" loading="lazy" decoding="async">' +
|
||||
(onlyImg[1] ? '<figcaption>' + escHtml(onlyImg[1]) + '</figcaption>' : '');
|
||||
host.appendChild(fig);
|
||||
return;
|
||||
}
|
||||
}
|
||||
var p = document.createElement('p');
|
||||
p.innerHTML = inlineMd(line);
|
||||
host.appendChild(p);
|
||||
});
|
||||
closeList();
|
||||
}
|
||||
|
||||
/* รูปปก — เติม ?v= เวอร์ชันแอป (กัน cache-first ของ SW ค้างรูปเก่า) ให้ตรงกับหน้าอื่น */
|
||||
function imgUrl(u) {
|
||||
return (window.ImgURL && ImgURL.of) ? ImgURL.of(u) : u;
|
||||
}
|
||||
|
||||
/* ---------- แผนที่เส้นทางแทนรูปปก (กำหนดใน textN.json) ----------
|
||||
"map": { "route_id": "101", "height": 320, "interactive": false, "caption": "…" }
|
||||
เงื่อนไข: ถ้าไม่มีไฟล์ geojson ของสายนั้น → คงรูป picN ไว้ + ป้ายบอก (ไม่พัง ไม่ขึ้นแผนที่เปล่า)
|
||||
ใช้ ATMap.route() ตัวเดียวกับหน้า #/routes/:id (ไม่ต้องโหลดไลบรารีเพิ่ม) */
|
||||
var GEO_DIR = '/resource/data/geojson-web/';
|
||||
|
||||
function hasGeo(id) {
|
||||
return fetch(GEO_DIR + id + '.geojson', { method: 'HEAD', cache: 'no-store' })
|
||||
.then(function (r) { return r.ok; })
|
||||
.catch(function () { return false; });
|
||||
}
|
||||
|
||||
function mountBlogMap(cover, spec) {
|
||||
var rid = String((spec && spec.route_id) || '').trim();
|
||||
if (!cover || !rid) return;
|
||||
hasGeo(rid).then(function (ok) {
|
||||
if (!ok) { /* ไม่มีเส้นทาง → ใช้รูปเดิม + ป้าย */
|
||||
var note = document.createElement('span');
|
||||
note.className = 'mu-cover-note';
|
||||
note.textContent = t('blog.noGeo');
|
||||
cover.appendChild(note);
|
||||
return;
|
||||
}
|
||||
cover.innerHTML = ''; /* แผนที่แทนรูป */
|
||||
cover.classList.remove('no-img');
|
||||
cover.classList.add('is-map');
|
||||
var box = document.createElement('div');
|
||||
box.className = 'at-map' + (spec.interactive === false ? ' is-static' : '');
|
||||
if (spec.height) box.style.height = spec.height + 'px';
|
||||
cover.appendChild(box);
|
||||
if (spec.caption) {
|
||||
var cap = document.createElement('span');
|
||||
cap.className = 'mu-cover-cap';
|
||||
cap.textContent = spec.caption;
|
||||
cover.appendChild(cap);
|
||||
}
|
||||
if (window.ATMap) {
|
||||
ATMap.route(box, rid);
|
||||
/* container เพิ่งมีขนาด → สั่งวัดใหม่ (กันบั๊กแผนที่ 0×0) */
|
||||
var hit = (ATMap.maps || []).filter(function (m) { return m.el === box; })[0];
|
||||
if (hit && hit.map) setTimeout(function () { hit.map.invalidateSize(); }, 60);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function renderPost(post, host) {
|
||||
var c = pick(post.data, lang());
|
||||
var art = document.createElement('article');
|
||||
art.className = 'mu-post';
|
||||
|
||||
/* ---------- แผนที่ "เส้นทางที่เสนอ" (ความฝัน) — กำหนดใน textN.json → "map"."routes"[]
|
||||
"map": { "origin": {"station_code":"at_main_terminal","label":"บขส.อ่างทอง"},
|
||||
"routes": [ { "id":"…", "line_color":"#c8102e", "line_name":"สายสีแดง",
|
||||
"dest":{"name":"…","lat":13.9,"lng":100.6},
|
||||
"via":[{"name":"…","lat":…,"lng":…}],
|
||||
"distance_km":null, "duration_min":null, "fare_thb":null, "note":"" } ] }
|
||||
เงื่อนไข: เส้นทางที่ยังไม่ใส่พิกัดปลายทาง → ไม่วาดบนแผนที่ แต่ขึ้นในรายการพร้อมป้ายบอก
|
||||
ค่าที่เว้น null → ซ่อนในแผงรายละเอียด (ไม่โชว์ 0 หรือค่าว่าง) */
|
||||
var STATIONS_URL = '/resource/data/stations.json';
|
||||
var stationsP = null;
|
||||
|
||||
function numOrNull(v) {
|
||||
var n = parseFloat(v);
|
||||
return isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
/* พิกัดจากได้ทั้ง coords=[lat,lng] (แบบใหม่) หรือ lat/lng (แบบเดิม) — คืน [lat,lng] หรือ null */
|
||||
function coordsOf(o) {
|
||||
o = o || {};
|
||||
if (Array.isArray(o.coords) && o.coords.length >= 2) {
|
||||
var a = numOrNull(o.coords[0]), c = numOrNull(o.coords[1]);
|
||||
return (a == null || c == null) ? null : [a, c];
|
||||
}
|
||||
var la = numOrNull(o.lat), ln = numOrNull(o.lng);
|
||||
return (la == null || ln == null) ? null : [la, ln];
|
||||
}
|
||||
|
||||
function stationByCode(code) {
|
||||
if (!stationsP) {
|
||||
stationsP = fetch(STATIONS_URL, { cache: 'no-store' })
|
||||
.then(function (r) { return r.ok ? r.json() : {}; })
|
||||
.then(function (d) { return (d && d.stations) || []; })
|
||||
.catch(function () { return []; });
|
||||
}
|
||||
return stationsP.then(function (list) {
|
||||
return list.filter(function (s) { return String(s.station_code) === String(code); })[0] || null;
|
||||
});
|
||||
}
|
||||
|
||||
/* พิกัดต้นทาง: ใช้ lat/lng ที่ระบุมาตรง ๆ ก่อน ถ้าไม่มีจึงค้นจาก stations.json ด้วย station_code */
|
||||
function resolveOrigin(o) {
|
||||
o = o || {};
|
||||
var c = coordsOf(o);
|
||||
if (c) return Promise.resolve({ coords: c, label: o.label || '' });
|
||||
if (!o.station_code) return Promise.resolve(null);
|
||||
return stationByCode(o.station_code).then(function (s) {
|
||||
if (!s || numOrNull(s.latitude) == null) return null;
|
||||
return { coords: [numOrNull(s.latitude), numOrNull(s.longitude)], label: o.label || s.station_name_th || '' };
|
||||
});
|
||||
}
|
||||
|
||||
function buildProposal(spec) {
|
||||
var wrap = document.createElement('div');
|
||||
wrap.className = 'blog-mapx';
|
||||
var host = document.createElement('div');
|
||||
host.className = 'at-map blog-mapx-map';
|
||||
var legend = document.createElement('ul');
|
||||
legend.className = 'blog-mapx-legend';
|
||||
var panel = document.createElement('div');
|
||||
panel.className = 'blog-mapx-panel';
|
||||
panel.hidden = true;
|
||||
wrap.appendChild(host);
|
||||
wrap.appendChild(legend);
|
||||
wrap.appendChild(panel);
|
||||
|
||||
/* โครงได้ 2 แบบ:
|
||||
- ใหม่ "groups": [ {id, label, routes[]} ] → แสดงหัวกลุ่มคั่นในรายการ
|
||||
- เดิม "routes": [ ... ] → ไม่มีกลุ่ม (backward-compat) */
|
||||
var groups = Array.isArray(spec.groups) && spec.groups.length
|
||||
? spec.groups
|
||||
: [{ id: '', label: '', routes: spec.routes || [] }];
|
||||
var routes = [];
|
||||
groups.forEach(function (g) {
|
||||
(g.routes || []).forEach(function (r) {
|
||||
if (r && (r.line_name || r.dest)) { r.__group = g; routes.push(r); }
|
||||
});
|
||||
});
|
||||
var rows = [];
|
||||
|
||||
routes.forEach(function (r) {
|
||||
var li = document.createElement('li');
|
||||
li.className = 'blog-mapx-row';
|
||||
li.tabIndex = 0;
|
||||
var sw = document.createElement('span');
|
||||
sw.className = 'blog-mapx-sw';
|
||||
sw.style.background = r.line_color || '#7a4cc0';
|
||||
var nm = document.createElement('span');
|
||||
nm.className = 'blog-mapx-nm';
|
||||
nm.textContent = r.line_name || (r.dest && r.dest.name) || '';
|
||||
var ds = document.createElement('span');
|
||||
ds.className = 'blog-mapx-ds';
|
||||
ds.textContent = (r.dest && r.dest.name) || '';
|
||||
li.appendChild(sw);
|
||||
li.appendChild(nm);
|
||||
li.appendChild(ds);
|
||||
if (!coordsOf(r.dest)) { /* ยังไม่ใส่พิกัด → บอกให้รู้ */
|
||||
var badge = document.createElement('span');
|
||||
badge.className = 'blog-mapx-badge';
|
||||
badge.textContent = t('blog.mapNoCoord');
|
||||
li.appendChild(badge);
|
||||
}
|
||||
li.addEventListener('click', function () { pick(r); });
|
||||
li.addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); pick(r); }
|
||||
});
|
||||
var badge = li.querySelector('.blog-mapx-badge');
|
||||
rows.push({ r: r, el: li, badge: badge });
|
||||
/* หัวกลุ่ม: แทรกเมื่อขึ้นกลุ่มใหม่ (มี label เท่านั้น) */
|
||||
if (r.__group && r.__group.label && !r.__group.__head) {
|
||||
r.__group.__head = document.createElement('li');
|
||||
r.__group.__head.className = 'blog-mapx-group';
|
||||
r.__group.__head.textContent = r.__group.label;
|
||||
legend.appendChild(r.__group.__head);
|
||||
}
|
||||
legend.appendChild(li);
|
||||
});
|
||||
|
||||
function pick(r) {
|
||||
highlight(r);
|
||||
show(r);
|
||||
}
|
||||
|
||||
/* ไฮไลต์เส้นที่เลือก — เส้นอื่นจางลง */
|
||||
function highlight(sel) {
|
||||
rows.forEach(function (x) { x.el.classList.toggle('is-on', x.r === sel); });
|
||||
routes.forEach(function (x) {
|
||||
if (!x.__layer) return;
|
||||
var on = x === sel;
|
||||
x.__layer.line.setStyle({ weight: on ? 7 : 4, opacity: on ? 1 : .3 });
|
||||
x.__layer.dot.setStyle({ opacity: on ? 1 : .35, fillOpacity: on ? 1 : .35 });
|
||||
});
|
||||
}
|
||||
|
||||
function show(r) {
|
||||
var d = r.dest || {};
|
||||
function line(k, v) {
|
||||
if (v == null || v === '') return;
|
||||
var p = document.createElement('p');
|
||||
p.className = 'blog-mapx-line';
|
||||
var s = document.createElement('span');
|
||||
s.textContent = k;
|
||||
var b = document.createElement('b');
|
||||
b.textContent = v;
|
||||
p.appendChild(s);
|
||||
p.appendChild(b);
|
||||
panel.appendChild(p);
|
||||
}
|
||||
panel.innerHTML = '';
|
||||
panel.hidden = false;
|
||||
var head = document.createElement('div');
|
||||
head.className = 'blog-mapx-head';
|
||||
var sw = document.createElement('span');
|
||||
sw.className = 'blog-mapx-sw';
|
||||
sw.style.background = r.line_color || '#7a4cc0';
|
||||
var title = document.createElement('strong');
|
||||
title.textContent = r.line_name || d.name || '';
|
||||
head.appendChild(sw);
|
||||
head.appendChild(title);
|
||||
panel.appendChild(head);
|
||||
line(t('blog.mapDest'), d.name);
|
||||
var via = (r.via || []).map(function (v) { return v && v.name; }).filter(Boolean).join(' · ');
|
||||
line(t('blog.mapVia'), via);
|
||||
var facts = [];
|
||||
if (numOrNull(r.distance_km) != null) facts.push(r.distance_km + ' ' + t('blog.km'));
|
||||
if (numOrNull(r.duration_min) != null) facts.push(r.duration_min + ' ' + t('blog.min'));
|
||||
if (numOrNull(r.fare_thb) != null) facts.push(r.fare_thb + ' ' + t('blog.thb'));
|
||||
if (facts.length) {
|
||||
var f = document.createElement('p');
|
||||
f.className = 'blog-mapx-facts';
|
||||
f.textContent = facts.join(' · ');
|
||||
panel.appendChild(f);
|
||||
}
|
||||
line(t('blog.mapNote'), r.note);
|
||||
}
|
||||
|
||||
if (!routes.length) {
|
||||
panel.hidden = false;
|
||||
panel.textContent = t('blog.mapNoRoutes');
|
||||
} else {
|
||||
var hint = document.createElement('p');
|
||||
hint.className = 'blog-mapx-hint';
|
||||
hint.textContent = t('blog.mapPick');
|
||||
wrap.appendChild(hint);
|
||||
}
|
||||
|
||||
resolveOrigin(spec.origin).then(function (org) {
|
||||
if (!org) {
|
||||
panel.hidden = false;
|
||||
panel.textContent = t('blog.mapNoOrigin');
|
||||
host.remove();
|
||||
return;
|
||||
}
|
||||
if (window.ATMap && ATMap.proposal) {
|
||||
ATMap.proposal(host, { origin: org, routes: routes }, function (r) { pick(r); }); /* groups ถูก flatten เป็น routes แล้ว */
|
||||
/* พิกัดปลายทางที่อ่านจาก geojson สำเร็จ → ลบป้าย "ยังไม่ใส่พิกัด" ออก */
|
||||
setTimeout(function () {
|
||||
rows.forEach(function (x) { if (x.r.__geoOk && x.badge) { x.badge.remove(); x.badge = null; } });
|
||||
}, 250);
|
||||
} else {
|
||||
host.remove();
|
||||
}
|
||||
});
|
||||
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/* ปก: รูป picN หรือแผนที่ (ถ้า textN.json ระบุ "map") — แผนที่ใช้เมื่อมีไฟล์เส้นทางจริงเท่านั้น */
|
||||
var spec = post.data.map;
|
||||
if (spec && (Array.isArray(spec.routes) || Array.isArray(spec.groups))) {
|
||||
/* ความฝัน: แผนที่เส้นทางที่เสนอ (ยังไม่มีจริง) — วาดจากโครงใน textN.json */
|
||||
var mapWrap = buildProposal(spec);
|
||||
if (c.tag) { /* ป้ายหมวด: แสดงมุมขวาบนของแผนที่ด้วย (แผนที่แทนที่ปก จึงไม่มีป้ายจาก .mu-cover) */
|
||||
var mapTag = document.createElement('span');
|
||||
mapTag.className = 'blog-mapx-tag';
|
||||
mapTag.textContent = c.tag;
|
||||
mapWrap.appendChild(mapTag);
|
||||
}
|
||||
art.appendChild(mapWrap);
|
||||
if (spec.note) {
|
||||
var src = document.createElement('p');
|
||||
src.className = 'blog-mapx-src';
|
||||
src.textContent = t('blog.mapNote') + ': ' + spec.note;
|
||||
art.appendChild(src);
|
||||
}
|
||||
} else if (post.img || spec) {
|
||||
var cover = document.createElement('div');
|
||||
cover.className = 'mu-cover' + (post.img ? '' : ' no-img');
|
||||
if (post.img) {
|
||||
var im = document.createElement('img');
|
||||
im.src = imgUrl(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);
|
||||
if (spec) mountBlogMap(cover, spec);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
renderBody(body, c.body || []); /* รองรับมาร์กดาวน์เบา ๆ (หนา/เอียง/ลิงก์/รูป/รายการ) */
|
||||
|
||||
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 = '<p class="mu-blog-empty">' + t('blog.notFound') + '</p>';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!posts.length) {
|
||||
host.innerHTML = '<p class="mu-blog-empty">' + t('blog.empty') + '</p>';
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default function mount(): void {
|
||||
init();
|
||||
}
|
||||
Reference in new issue
Block a user