247 lines
9.1 KiB
JavaScript
247 lines
9.1 KiB
JavaScript
/* ============================================================
|
|
หน้าป้ายหยุดรถ — render จาก resource/data/busstop_list.json
|
|
------------------------------------------------------------
|
|
ข้อมูล: busstop_list.json (shape = array ตรง ๆ ไม่มี wrapper)
|
|
{
|
|
"route_code": "3064", // Highway No.
|
|
"section_code": "0100", // Control Section
|
|
"route_name": "อ่างทอง - ปากดง", // Highway Name
|
|
"kilometer": "3+000", // ★ รหัสป้าย (Survey point — แสดงผลคงรูปนี้)
|
|
"position": "ขวาทาง", // Location of the pavilion
|
|
"type": "ที่รอรถประจำทาง", // Picture / Size of the pavilion
|
|
"latitude": 14.6016517229,
|
|
"longitude": 100.4323316104,
|
|
"buses-line": [15, 101] // ★ สายรถประจำทางที่ผ่านป้ายนี้ (ว่าง = [])
|
|
}
|
|
|
|
กติกา:
|
|
- รหัสป้ายบนจอ = kilometer คงรูปเดิมเสมอ ("3+000") ตาม AGENTS.md ข้อ 9
|
|
- ชื่อไฟล์รูป = pad5(kilometer) → "3+000" = "03000" → .image/stops/03000.jpg
|
|
(ตัด "+" ออก + เติม 0 หน้าให้ครบ 5 หลัก)
|
|
- รูปหาย → ใช้ placeholder minibus-512x512.png
|
|
- KPI คำนวณจากข้อมูลจริง ไม่ hardcode
|
|
============================================================ */
|
|
(function () {
|
|
'use strict';
|
|
|
|
var DATA_URL = '../resource/data/busstop_list.json';
|
|
var IMG_DIR = '../.image/stops/';
|
|
var IMG_EXT = '.jpg';
|
|
var PLACEHOLDER = '../.image/icons/minibus-512x512.png';
|
|
|
|
function t(key, vars) {
|
|
return window.I18N ? window.I18N.t(key, vars) : key;
|
|
}
|
|
|
|
/* "3+000" → "03000" (ตัดเครื่องหมาย + ออก แล้วเติม 0 หน้าให้ครบ 5 หลัก) */
|
|
function pad5(km) {
|
|
if (!km) return '';
|
|
return String(km)
|
|
.split('+')
|
|
.map(function (s) { return s.replace(/\D/g, ''); })
|
|
.join('')
|
|
.padStart(5, '0');
|
|
}
|
|
|
|
function fetchJSON(url) {
|
|
return fetch(url, { cache: 'no-store' })
|
|
.then(function (r) { return r.ok ? r.json() : null; })
|
|
.catch(function () { return null; });
|
|
}
|
|
|
|
/* ---------- รูป: ลองรูปจริงก่อน ถ้าไม่มีค่อยใช้ placeholder ---------- */
|
|
function resolveImage(km) {
|
|
var code = pad5(km);
|
|
if (!code) return Promise.resolve(PLACEHOLDER);
|
|
var url = IMG_DIR + code + IMG_EXT;
|
|
return fetch(url, { method: 'HEAD', cache: 'no-store' })
|
|
.then(function (r) { return r.ok ? url : PLACEHOLDER; })
|
|
.catch(function () { return PLACEHOLDER; });
|
|
}
|
|
|
|
/* ---------- KPI (คำนวณจากข้อมูลจริง) ---------- */
|
|
function renderKpi(stops) {
|
|
var host = document.getElementById('stops-kpi');
|
|
if (!host) return;
|
|
|
|
var routes = {};
|
|
var types = {};
|
|
var busLines = {};
|
|
stops.forEach(function (s) {
|
|
if (s.route_code) routes[s.route_code] = true;
|
|
if (s.type) types[s.type] = true;
|
|
(s['buses-line'] || []).forEach(function (n) { busLines[n] = true; });
|
|
});
|
|
|
|
// สายที่ผ่านป้ายเยอะที่สุด (นับจาก buses-line)
|
|
var countByBus = {};
|
|
stops.forEach(function (s) {
|
|
(s['buses-line'] || []).forEach(function (n) {
|
|
countByBus[n] = (countByBus[n] || 0) + 1;
|
|
});
|
|
});
|
|
var busKeys = Object.keys(countByBus);
|
|
var topBus = busKeys.sort(function (a, b) { return countByBus[b] - countByBus[a]; })[0];
|
|
|
|
var kpis = [
|
|
{ label: t('stops.kpi.total'), value: stops.length + ' ' + t('stops.unitPoints') },
|
|
{ label: t('stops.kpi.routes'), value: Object.keys(routes).length + ' ' + t('stops.unitRoutes') },
|
|
{ label: t('stops.kpi.types'), value: Object.keys(types).length + ' ' + t('stops.unitTypes') },
|
|
{ label: t('stops.kpi.buses'), value: topBus ? ('สาย ' + topBus) : '—' }
|
|
];
|
|
|
|
host.innerHTML = '';
|
|
kpis.forEach(function (k) {
|
|
var box = document.createElement('div');
|
|
box.className = 'mu-kpi';
|
|
var label = document.createElement('p');
|
|
label.className = 'label';
|
|
label.textContent = k.label;
|
|
var value = document.createElement('p');
|
|
value.className = 'value';
|
|
value.textContent = k.value;
|
|
box.appendChild(label);
|
|
box.appendChild(value);
|
|
host.appendChild(box);
|
|
});
|
|
}
|
|
|
|
/* ---------- การ์ดป้าย ---------- */
|
|
function buildCard(stop, imgUrl, idx) {
|
|
var li = document.createElement('li');
|
|
li.setAttribute('data-idx', String(idx));
|
|
|
|
var img = document.createElement('img');
|
|
img.className = 'mu-stop-thumb';
|
|
img.src = imgUrl;
|
|
img.alt = stop.route_name || '';
|
|
img.loading = 'lazy';
|
|
img.decoding = 'async';
|
|
if (imgUrl === PLACEHOLDER) img.classList.add('is-placeholder');
|
|
li.appendChild(img);
|
|
|
|
var card = document.createElement('div');
|
|
card.className = 'mu-stop-card local';
|
|
|
|
var top = document.createElement('div');
|
|
top.className = 'mu-stop-top';
|
|
|
|
var left = document.createElement('div');
|
|
|
|
var pill = document.createElement('span');
|
|
pill.className = 'mu-type-pill local';
|
|
pill.textContent = stop.type || '';
|
|
left.appendChild(pill);
|
|
|
|
var title = document.createElement('h2');
|
|
title.className = 'mu-stop-title';
|
|
title.textContent = stop.place || stop.route_name || '—';
|
|
left.appendChild(title);
|
|
|
|
var loc = document.createElement('p');
|
|
loc.className = 'mu-loc';
|
|
loc.textContent = t('stops.highway') + ' ' + (stop.route_code || '—') +
|
|
' · ' + t('stops.section') + ' ' + (stop.section_code || '—');
|
|
left.appendChild(loc);
|
|
|
|
top.appendChild(left);
|
|
|
|
var codeChip = document.createElement('span');
|
|
codeChip.className = 'mu-chip count stop-code';
|
|
codeChip.textContent = stop.kilometer || '—';
|
|
top.appendChild(codeChip);
|
|
|
|
card.appendChild(top);
|
|
|
|
var row = document.createElement('div');
|
|
row.className = 'mu-chip-row';
|
|
|
|
// ★ สายรถประจำทางที่ผ่านป้ายนี้ (buses-line)
|
|
var buses = stop['buses-line'] || [];
|
|
if (buses.length) {
|
|
buses.forEach(function (n) {
|
|
var b = document.createElement('span');
|
|
b.className = 'mu-chip route';
|
|
b.textContent = t('stops.busPrefix') + ' ' + n;
|
|
row.appendChild(b);
|
|
});
|
|
} else {
|
|
var nb = document.createElement('span');
|
|
nb.className = 'mu-chip';
|
|
nb.textContent = t('stops.noBusLine');
|
|
row.appendChild(nb);
|
|
}
|
|
card.appendChild(row);
|
|
|
|
var action = document.createElement('div');
|
|
action.className = 'mu-stop-action';
|
|
|
|
var note = document.createElement('p');
|
|
note.className = 'mu-stop-note';
|
|
note.textContent = stop.position || '';
|
|
action.appendChild(note);
|
|
|
|
var coords = document.createElement('span');
|
|
coords.className = 'mu-chip';
|
|
coords.textContent = (stop.latitude && stop.longitude)
|
|
? stop.latitude.toFixed(4) + ', ' + stop.longitude.toFixed(4)
|
|
: '—';
|
|
action.appendChild(coords);
|
|
|
|
card.appendChild(action);
|
|
li.appendChild(card);
|
|
return li;
|
|
}
|
|
|
|
function renderStops(stops, host) {
|
|
host.innerHTML = '';
|
|
if (!stops.length) {
|
|
var empty = document.createElement('li');
|
|
empty.className = 'mu-stop-empty';
|
|
empty.textContent = t('stops.noResult');
|
|
host.appendChild(empty);
|
|
return;
|
|
}
|
|
// โหลดรูปทีละใบ แล้วค่อยต่อการ์ด (รักษาลำดับ)
|
|
var chain = Promise.resolve();
|
|
stops.forEach(function (stop, idx) {
|
|
chain = chain.then(function () {
|
|
return resolveImage(stop.kilometer).then(function (url) {
|
|
host.appendChild(buildCard(stop, url, idx));
|
|
});
|
|
});
|
|
});
|
|
return chain;
|
|
}
|
|
|
|
/* ---------- init ---------- */
|
|
function init() {
|
|
var host = document.getElementById('stops-list');
|
|
if (!host) return;
|
|
|
|
fetchJSON(DATA_URL).then(function (data) {
|
|
var stops = Array.isArray(data) ? data : (data && data.stops) || [];
|
|
if (!stops.length) {
|
|
host.innerHTML = '<li class="mu-stop-empty">' + t('stops.noResult') + '</li>';
|
|
return;
|
|
}
|
|
renderKpi(stops);
|
|
// ผูก event ก่อน render การ์ด — กันเคสที่ render ค้างแล้วปุ่มไม่ตอบสนอง
|
|
if (window.StopsSearch) window.StopsSearch.init(stops);
|
|
renderStops(stops, host).then(function () {
|
|
// การ์ดพร้อมแล้ว → ให้ search module กรองซ้ำครั้งแรก
|
|
if (window.StopsSearch) window.StopsSearch.init(stops);
|
|
});
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
|
|
// เปิดให้ search module ใช้ helper ร่วมกัน
|
|
window.StopsRender = { pad5: pad5, PLACEHOLDER: PLACEHOLDER };
|
|
})();
|