พอร์ต 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,128 @@
|
||||
// @ts-nocheck
|
||||
/* Ported from web-at-transit-mock/ui/popular.js — logic unchanged. */
|
||||
/* popular.js — อัลกอริทึมนับ "สายที่เปิดดูบ่อย" สำหรับ footer (ฝั่งเบราว์เซอร์ ไม่มี backend)
|
||||
|
||||
กลไก
|
||||
• จับการเปิดดูสาย: Popular.hit(route_id) — เรียกจาก busDetail.init.js / stopDetail / รายการสาย
|
||||
• เก็บใน localStorage: { "102": [hits, lastTs], ... } (id → [จำนวนครั้ง, เวลาล่าสุด])
|
||||
• คะแนนไม่เฟ้อ: score = hits × 0.5 ^ (อายุวัน / HALF_LIFE) ← ครึ่งชีวิต 30 วัน
|
||||
สายที่ไม่มีคนเปิดนาน ๆ จะค่อย ๆ ตกลงเองโดยไม่ต้องลบข้อมูล
|
||||
• แสดง 4 อันดับใน footer · ไม่มีข้อมูลพอ → ใช้ DEFAULT_IDS (ของเดิม) เป็น fallback
|
||||
• ประหยัดพื้นที่: เก็บแค่ 2 ตัวเลข/สาย · ทิ้งข้อมูลที่ตายเกิน PRUNE_DAYS วัน
|
||||
|
||||
ไม่มี dependency — โหลดก่อน router.js (หลัง i18n.js เพื่อให้ข้อความถูกภาษา)
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var KEY = 'at_route_hits'; /* คีย์ใน localStorage */
|
||||
var HALF_LIFE = 30; /* วัน — คะแนนลดครึ่งเมื่อไม่ถูกเปิดนาน 30 วัน */
|
||||
var PRUNE_DAYS = 180; /* ทิ้งข้อมูลที่ไม่ถูกเปิดเกิน 180 วัน (กันบวม) */
|
||||
var TOP_N = 4; /* จำนวนที่แสดงใน footer */
|
||||
var DAY_MS = 86400000;
|
||||
|
||||
/* ค่าเริ่มต้น (ของเดิมใน footer) — ใช้เมื่อยังไม่มีข้อมูลจริง หรือมีไม่ครบ TOP_N */
|
||||
var DEFAULT_IDS = ['101', '902', '15', '464'];
|
||||
|
||||
function now() { return Date.now(); }
|
||||
|
||||
/* ── อ่าน/เขียน storage (ทน localStorage ใช้ไม่ได้ เช่น โหมดส่วนตัว) ── */
|
||||
function read() {
|
||||
try {
|
||||
var raw = localStorage.getItem(KEY);
|
||||
if (!raw) return {};
|
||||
var d = JSON.parse(raw);
|
||||
return (d && typeof d === 'object') ? d : {};
|
||||
} catch (e) { return {}; }
|
||||
}
|
||||
|
||||
function write(d) {
|
||||
try { localStorage.setItem(KEY, JSON.stringify(d)); } catch (e) { /* เงียบ */ }
|
||||
}
|
||||
|
||||
/* คะแนนของรายการเดียว: hits × 0.5^(อายุวัน/ครึ่งชีวิต) */
|
||||
function score(entry, ts) {
|
||||
if (!entry || !entry.length) return 0;
|
||||
var hits = Number(entry[0]) || 0;
|
||||
var last = Number(entry[1]) || 0;
|
||||
if (hits <= 0 || last <= 0) return 0;
|
||||
var ageDays = Math.max(0, (ts - last) / DAY_MS);
|
||||
return hits * Math.pow(0.5, ageDays / HALF_LIFE);
|
||||
}
|
||||
|
||||
/* ตัดข้อมูลที่ตายเกิน PRUNE_DAYS ทิ้ง (คืนค่า true ถ้ามีการแก้) */
|
||||
function prune(d, ts) {
|
||||
var cut = PRUNE_DAYS * DAY_MS;
|
||||
var changed = false;
|
||||
Object.keys(d).forEach(function (id) {
|
||||
var e = d[id];
|
||||
var last = (e && Number(e[1])) || 0;
|
||||
if (!last || (ts - last) > cut) { delete d[id]; changed = true; }
|
||||
});
|
||||
return changed;
|
||||
}
|
||||
|
||||
var Popular = {
|
||||
/* นับการเปิดดูสาย 1 ครั้ง */
|
||||
hit: function (routeId) {
|
||||
var id = String(routeId == null ? '' : routeId).trim();
|
||||
if (!id) return;
|
||||
var ts = now();
|
||||
var d = read();
|
||||
var e = d[id];
|
||||
d[id] = [(e && Number(e[0]) || 0) + 1, ts];
|
||||
prune(d, ts);
|
||||
write(d);
|
||||
},
|
||||
|
||||
/* อันดับ id เรียงจากคะแนนมาก→น้อย · ไม่มีข้อมูล → DEFAULT_IDS */
|
||||
ranked: function (n) {
|
||||
var ts = now();
|
||||
var d = read();
|
||||
if (prune(d, ts)) write(d);
|
||||
|
||||
var rows = Object.keys(d).map(function (id) {
|
||||
return { id: id, score: score(d[id], ts) };
|
||||
}).filter(function (r) { return r.score > 0; });
|
||||
|
||||
rows.sort(function (a, b) { return b.score - a.score || (a.id < b.id ? -1 : 1); });
|
||||
|
||||
var top = rows.slice(0, n || TOP_N).map(function (r) { return r.id; });
|
||||
/* เติมด้วยค่าเริ่มต้นถ้ายังไม่ครบ (ไม่ให้ footer ว่าง) */
|
||||
DEFAULT_IDS.forEach(function (id) {
|
||||
if (top.length < (n || TOP_N) && top.indexOf(id) < 0) top.push(id);
|
||||
});
|
||||
return top.slice(0, n || TOP_N);
|
||||
},
|
||||
|
||||
/* เรนเดอร์รายการใน footer (เรียกหลัง i18n พร้อม — ชื่อสายตามภาษา) */
|
||||
mount: function (ulId, routes) {
|
||||
var ul = document.getElementById(ulId || 'footer-popular-list');
|
||||
if (!ul || !routes || !routes.length) return false;
|
||||
|
||||
var byId = {};
|
||||
routes.forEach(function (r) { byId[String(r.route_id)] = r; });
|
||||
|
||||
var ids = Popular.ranked(TOP_N);
|
||||
ul.innerHTML = '';
|
||||
ids.forEach(function (id) {
|
||||
var r = byId[id];
|
||||
if (!r) return;
|
||||
var li = document.createElement('li');
|
||||
var a = document.createElement('a');
|
||||
a.href = '/routes/' + id;
|
||||
a.textContent = 'สาย ' + id + (r.route_name_th ? ' · ' + r.route_name_th : '');
|
||||
li.appendChild(a);
|
||||
ul.appendChild(li);
|
||||
});
|
||||
return ul.children.length > 0;
|
||||
},
|
||||
|
||||
/* สำหรับทดสอบ/debug */
|
||||
_score: score,
|
||||
_key: KEY
|
||||
};
|
||||
|
||||
window.Popular = Popular;
|
||||
})();
|
||||
export {};
|
||||
Reference in new issue
Block a user