ตารางเวลาคำนวณได้ + หน้ารายละเอียดป้ายขับเคลื่อนด้วยข้อมูล + แผนที่ Leaflet

Schedules — กติกา "จุดนี้ = เวลาจุดอ้างอิง ± n นาที"
- ฟิลด์ derive { from_station_id, offset_min } คำนวณที่ ui/partials/schedules.js (ท่อเดียว) รองรับอ้างจุดข้ามทิศ
- จุดย่อยของ "8 other_stop" ใช้ offsets_min จับคู่ตามลำดับ (เช่น ตลาดสามโก้ = ตลาดท่าช้าง +45 นาที)
- เครื่องมือ resource/tools/derive-times.py + ตาราง resource/data/derive-times.csv (--check / --audit ชี้จุดที่ควรตั้งกฎ)
- แยกตารางเป็นรายสาย: scheduleslist/schedules<route_id>.json และ schedules.json เป็นสารบัญ (โหลดแบบ lazy + cache)
- ตรวจจับวันทำงาน/วันหยุด + schema 3 (weekday/holiday → directions) · ไฟล์ข้อมูลเป็น CRLF ทั้งชุด
- ล้างค่าที่ไม่ใช่ HH:MM ที่ฝังใน times (ทำให้ KPI เพี้ยน) + กันค่าหลอกในตัวสรุปเวลา

UI
- หน้ารายละเอียดป้าย/ตารางเวลา ขับเคลื่อนด้วย JSON (stopDetail.init.js) · จุดขึ้นรถประจำทางอื่นๆ รองรับหลายจุดต่อสาย
- #/stops: แสดง 6 ป้ายแรก + ปุ่ม "แสดงป้ายทั้งหมด" (โหลดรูปเฉพาะที่แสดง) · ชิปสาย → #/routes/<route_id>
- หน้าแรก: ตัดตัวเลือก "ทุกสถานีในจังหวัดนี้" · ตัวกรองจังหวัด/สถานี + ปุ่มแชร์ (share URL)
- การ์ดติดต่อ: ไอคอน/ชื่อ/สีเปลี่ยนตามชนิด URL (Facebook / LINE / เว็บไซต์) + ปุ่มเปิดลิงก์จาก web_url
- รูปรองรับหลายนามสกุล (.jpg → .jpeg → .webp → .png) · ป้าย "ประมาณ" + หมายเหตุสำหรับตารางที่คำนวณ
- หน้าเครดิตเหลือ 3 หมวด · แก้ข้อความหมวดแผนที่ให้ตรงกับข้อมูลจริง
- แผนที่ Leaflet (vendor ในโปรเจกต์ ไม่ใช้ CDN): เส้นทาง + หมุด + ป๊อปอัป · แก้ CSS ที่ทำให้ SVG ยุบ 0x0

เครื่องมือและเอกสาร
- qgis-to-web.py อ่าน .shp ตรง → geojson-web (สร้างใหม่ 10 ไฟล์) + audit-geojson.py ตรวจ 3 ส่วน
- stop-km.py วัดระยะ กม. ตามเส้นจริง → stop-km.json · schedules-editor.html (แก้ทีละไฟล์ · มุมมองตารางแบบ Excel)
- AGENTS.md: กติกา derive · offsets_min · นามสกุลรูป · ชิปสาย · หน้ารายละเอียดป้ายมาจากไหน
This commit is contained in:
Bkk1234 committed 2026-09-30 03:25:18 +07:00
1 parent 88311326e6
commit 1cfaeb1526
91 files changed
+7560 -844

No files matched your search

+573
View File
@@ -0,0 +1,573 @@
/* ============================================================
stopDetail.init.js — หน้ารายละเอียดป้าย (#/stops/<รหัส>)
เดิม: ข้อมูลทุกอย่างฝังตายใน stopDetail.html (ทุกป้ายแสดงเหมือนกันหมด)
ใหม่: เรนเดอร์จาก JSON จริง 100% — เปลี่ยนข้อมูลที่ไหนก็แก้ที่ไฟล์ข้อมูล ไม่ต้องแก้ HTML
แหล่งข้อมูล:
stations.json → ชื่อ/ประเภท/รหัส/จังหวัด/พิกัด (+ ฟิลด์เสริม photo/description/landmarks/facilities ถ้าใส่ไว้)
routes.json → สายรถที่ผ่านป้ายนี้ (station_ids) + ชื่อสาย
schedules.json → เวลารถของแต่ละสายที่ป้ายนี้ (schema 2 ผ่านตัวอ่านกลาง Schedules)
.image/stations/ → รูปสถานี <station_id>.jpg (+ รูปเสริม <station_id>-2.jpg)
พารามิเตอร์ URL:
#/stops/<รหัสป้าย> → แสดงป้ายนั้น
#/stops/<รหัสป้าย>?route=<รหัสสาย> → กรอง/เปิดเฉพาะสายนั้นทันที
============================================================ */
(function () {
'use strict';
var DATA = '../resource/data/';
var PHOTO_DIR = '../.image/stations/'; /* รูปสถานี: <station_id>.<jpg|jpeg|webp|png> (ไล่ลองนามสกุลให้) */
var PHOTO_EXTS = ['jpg', 'jpeg', 'webp', 'png'];
/* ประเภทป้าย → คีย์แปล + โทนสี */
var TYPE_KEY = {
main_terminal: 'type.terminal', local_terminal: 'type.local', local_stop: 'type.busstop',
market: 'type.market', private_stop: 'type.marketOpt'
};
var TYPE_TONE = {
main_terminal: 'purple', local_terminal: 'sky', local_stop: 'mint', market: 'mint', private_stop: 'sky'
};
function $(id) { return document.getElementById(id); }
function t(k, v) { return window.I18N ? I18N.t(k, v) : k; }
function esc(s) {
return String(s == null ? '' : s).replace(/&/g, '&amp;').replace(/</g, '&lt;')
.replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
}
function fetchJSON(u) {
return fetch(u, { cache: 'no-store' }).then(function (r) { return r.ok ? r.json() : null; }).catch(function () { return null; });
}
function pad2(n) { return (n < 10 ? '0' : '') + n; }
function toMin(s) {
var m = /^([01]?\d|2[0-3]):([0-5]\d)$/.exec(String(s || '').trim());
return m ? Number(m[1]) * 60 + Number(m[2]) : null;
}
function nowMin() { var d = new Date(); return d.getHours() * 60 + d.getMinutes(); }
function point(st) {
if (!st || st.latitude === '' || st.longitude === '' || st.latitude == null) return null;
return [parseFloat(st.longitude), parseFloat(st.latitude)];
}
/* ---------- หาป้ายของเรา: รหัส URL → stations.json ---------- */
function findStation(stations, code) {
var want = String(code || '').trim();
var hit = stations.filter(function (s) { return String(s.station_code) === want; })[0];
if (hit) return hit;
hit = stations.filter(function (s) { return String(s.station_code).toLowerCase() === want.toLowerCase(); })[0];
if (hit) return hit;
if (/^\d+$/.test(want)) {
hit = stations.filter(function (s) { return String(s.station_id) === want; })[0];
}
return hit || null;
}
/* ---------- สายรถที่ผ่านป้ายนี้ + เวลาที่ป้ายนี้ ---------- */
function routesAtStation(routes, sched, sid) {
var out = [];
routes.forEach(function (r) {
var ids = r.station_ids || [];
if (ids.indexOf(sid) === -1) return;
var legs = []; /* แต่ละทิศ = 1 รายการ */
Schedules.directions(sched, r.route_id).forEach(function (d) {
d.stops.forEach(function (s) {
if (s.station_id !== sid) return;
legs.push({ dir_id: d.dir_id, label: s.dir_note || d.dir_label || null, times: s.times || [], note: s.note || null, estimated: !!s.estimated, estimateFrom: s.estimateFrom || null });
});
});
var trips = legs.reduce(function (n, l) { return n + l.times.length; }, 0);
var pos = ids.indexOf(sid);
out.push({
route: r, legs: legs, trips: trips,
isOrigin: pos === 0, isTerminus: pos === ids.length - 1, index: pos, total: ids.length
});
});
/* เรียงตามลำดับใน routes.json (คงที่ — ตรงกับตัวเลือกสายในหน้าอื่น) */
return out;
}
/* ---------- รูปสถานี: .image/stations/<station_id>.jpg ----------
เลือกตาม station_id ใน stations.json · ไฟล์หาย → ไม่แสดงช่องว่าง (hero ยืดเต็ม) */
function findPhoto(station) {
if (station.photo) return station.photo; /* ระบุพาธเองได้ใน stations.json (ทับค่าเริ่มต้น) */
var n = station.__seq || 1; /* รูปเสริมของสถานีเดียวกัน: 2-2.jpg, 2-3.jpeg … */
var base = PHOTO_DIR + station.station_id + (n > 1 ? '-' + n : '');
return PHOTO_EXTS.map(function (e) { return base + '.' + e; }); /* เรียง候选ให้ renderHero ไล่ลอง */
}
/* ---------- ตัวเลขสรุป ---------- */
var HHMM = /^\d{1,2}:\d{2}$/; /* กันค่าหลอกในไฟล์ (เช่นข้อความ) ไม่ให้เพี้ยน KPI */
function summarise(rows) {
var times = [], trips = 0;
rows.forEach(function (x) {
trips += x.trips;
x.legs.forEach(function (l) {
times = times.concat((l.times || []).filter(function (t) { return HHMM.test(String(t)); }));
});
});
times.sort(function (a, b) { return toMin(a) - toMin(b); });
return { rows: rows.length, trips: trips, first: times[0] || null, last: times[times.length - 1] || null };
}
/* ---------- เรนเดอร์: หัวป้าย ---------- */
function renderHero(st, sum, photo, place) {
var baseName = st.station_name_th || ('ป้าย ' + st.station_code);
/* จุดขึ้นรถประจำทางอื่นๆ: ต่อท้ายด้วยชื่อสถานที่จริงของสายที่กำลังดูอยู่ */
var placeName = (place && window.OtherStop && OtherStop.firstName) ? OtherStop.firstName(place)
: (place && place.station_name_th) ? place.station_name_th : null;
var name = baseName + (placeName ? ' \u00b7 ' + placeName : '');
var city = (place && place.city_th) || st.city_th || '';
var prov = (place && place.province_th) || st.province_th || '';
var crumb = $('stop-breadcrumb');
if (crumb) crumb.innerHTML = '<a href="#/">' + esc(t('nav.home')) + '</a>/ <a href="#/stops">' +
esc(t('nav.stops')) + '</a>/ ' + esc(name);
var title = $('stop-title');
if (title) title.textContent = name;
var sub = $('stop-sub');
if (sub) {
sub.textContent = st.description ||
t('stop.subGeneric', { city: city || prov || t('stop.thisStop') });
}
var meta = $('stop-meta');
if (meta) {
meta.innerHTML =
'<span class="mu-meta-pill ' + (TYPE_TONE[st.station_type] || 'purple') + '">' +
esc(t(TYPE_KEY[st.station_type] || 'type.busstop')) + '</span>' +
'<span class="mu-meta-pill sky">' + esc(t('stop.code')) + ' ' + esc(st.station_code) + '</span>' +
(city || prov ? '<span class="mu-meta-pill mint">' + esc([city, prov].filter(Boolean).join(' · ')) + '</span>' : '');
}
var metrics = $('stop-metrics');
if (metrics) {
metrics.innerHTML =
'<div class="mu-hero-metric"><p class="mu-hero-metric-label">' + esc(t('stop.statRoutes')) + '</p>' +
'<p class="mu-hero-metric-value">' + sum.rows + '</p></div>' +
'<div class="mu-hero-metric"><p class="mu-hero-metric-label">' + esc(t('stop.statTrips')) + '</p>' +
'<p class="mu-hero-metric-value">' + sum.trips + '</p></div>' +
'<div class="mu-hero-metric"><p class="mu-hero-metric-label">' + esc(t('stop.statHours')) + '</p>' +
'<p class="mu-hero-metric-value">' + (sum.first ? esc(sum.first + ' - ' + sum.last) : '–') + '</p></div>' +
'<div class="mu-hero-metric"><p class="mu-hero-metric-label">' + esc(t('stop.nextArrival')) + '</p>' +
'<p class="mu-hero-metric-value" id="stop-next-trip">–</p></div>';
}
var img = $('stop-photo');
if (img) {
img.alt = name;
img.hidden = true; /* ซ่อนไว้ก่อน — รอผลตรวจว่ามีไฟล์จริง */
var cands = [].concat(photo || []).filter(Boolean); /* รองรับทั้งสตริงเดียวและรายการนามสกุล */
(function tryNext(i) {
if (i >= cands.length) return; /* ไม่มีไฟล์เลย → ไม่แสดงช่องว่าง */
var url = cands[i];
fetch(url, { method: 'HEAD', cache: 'no-store' })
.then(function (r) {
if (!r.ok) return tryNext(i + 1); /* ลองนามสกุลถัดไป (.jpg → .jpeg → .webp → .png) */
img.src = url;
img.hidden = false;
})
.catch(function () { tryNext(i + 1); });
})(0);
}
}
/* ---------- เรนเดอร์: การ์ดสรุป 4 ใบ ---------- */
function renderInfo(st, sum) {
var box = $('stop-info');
if (!box) return;
var tones = [
'background:linear-gradient(135deg, #f7f1ff, #f2e8ff);',
'background:linear-gradient(135deg, #edf6ff, #e8f3ff); border-color:#d7e7fb;',
'background:linear-gradient(135deg, #eefdf6, #e7f9f1); border-color:#d1efdf;',
'background:linear-gradient(135deg, #fff5fa, #ffeef6); border-color:#f7d6e5;'
];
var cards = [
[t('stop.statRoutes'), sum.rows ? t(sum.rows === 1 ? 'stop.nRoutes1' : 'stop.nRoutes', { n: sum.rows }) : '–'],
[t('stop.statTrips'), sum.trips ? String(sum.trips) : '–'],
[t('stop.statType'), t(TYPE_KEY[st.station_type] || 'type.busstop')],
[t('stop.statHours'), sum.first ? sum.first + ' - ' + sum.last : '–']
];
box.innerHTML = cards.map(function (c, i) {
return '<article class="mu-info-card" style="' + tones[i] + '">' +
'<p class="label">' + esc(c[0]) + '</p><p class="value">' + esc(c[1]) + '</p></article>';
}).join('');
}
/* ---------- เรนเดอร์: สายรถที่ผ่านป้ายนี้ ---------- */
function renderRoutes(rows, placeByRoute) {
placeByRoute = placeByRoute || {};
var list = $('stop-routes');
var filter = $('mu-route-filter');
if (!list) return;
if (!rows.length) {
list.innerHTML = '<li><p class="mu-stop-sub">' + esc(t('stop.noRoutes')) + '</p></li>';
if (filter) filter.disabled = true;
setCount(0, 0);
return;
}
list.innerHTML = '';
rows.forEach(function (x) {
var r = x.route;
var li = document.createElement('li');
var det = document.createElement('details');
det.className = 'mu-stop-expander';
if (x.isOrigin || x.isTerminus) det.open = true;
var posLabel = x.isOrigin ? t('route.origin') : (x.isTerminus ? t('route.destination') : t('route.pass'));
var place = placeByRoute[String(r.route_id)] || null;
/* จุดขึ้นรถประจำทางอื่นๆ → แสดง "ชื่อจุดจริง · อำเภอ" แทนคำว่าจุดจอดที่ N */
/* จุดขึ้นรถประจำทางอื่นๆ ที่มีหลายจุด → โชว์จุดแรก + "(+N จุด)" ไม่ยัดชื่อทั้งชุด */
var subs = (place && window.OtherStop && OtherStop.stopsOf) ? OtherStop.stopsOf(place) : [];
var pName = subs.length ? subs[0].name : (place && place.station_name_th) || '';
var pMore = subs.length > 1 ? ' ' + t('stop.otherMore', { n: subs.length - 1 }) : '';
var metaText = place
? pName + pMore + (place.city_th ? ' \u00b7 ' + place.city_th : '')
: posLabel + (x.total > 1 ? ' \u00b7 ' + (x.index + 1) + '/' + x.total : '');
var sum = document.createElement('summary');
sum.innerHTML =
'<span class="mu-route-chip">' + esc(r.route_id) + '</span>' +
'<span class="mu-route-type-badge"><svg class="ic" aria-hidden="true"><use href="#ic-bus-front"/></svg> ' +
esc(r.vehicle_type_th || t('type.terminal')) + '</span>' +
'<span class="mu-stop-summary-main">' +
'<strong>' + esc(r.route_name_th || ('สาย ' + r.route_id)) + '</strong>' +
'<span class="mu-stop-summary-meta">' + esc(metaText) + '</span>' +
'</span>' +
'<span class="mu-stop-summary-rail">' +
'<span class="mu-status-pill info">' + esc(t(x.trips === 1 ? 'stop.tripsPerDay1' : 'stop.tripsPerDay', { n: x.trips })) + '</span>' +
'<span class="mu-stop-chevron">\u2304</span>' +
'</span>';
det.appendChild(sum);
var body = document.createElement('div');
body.className = 'mu-stop-expander-body';
if (!x.legs.length || !x.trips) {
body.innerHTML = '<p class="mu-stop-sub">' + esc(t('stop.noTimes')) + '</p>';
} else {
x.legs.forEach(function (leg) {
/* ทิศที่ยังไม่มีเวลา → ข้าม (แถวนี้มีการแสดงตารางของทิศอื่นอยู่แล้ว) */
if (!(leg.times || []).length) return;
if (leg.label) {
var lab = document.createElement('p');
lab.className = 'mu-stop-sub';
lab.textContent = leg.label;
body.appendChild(lab);
}
var grid = document.createElement('div');
grid.className = 'mu-time-grid' + (leg.estimated ? ' is-est' : '');
leg.times.forEach(function (tm) {
var c = document.createElement('div');
c.className = 'mu-time-chip';
c.textContent = tm;
grid.appendChild(c);
});
body.appendChild(grid);
if (leg.estimated) {
var estN = document.createElement('p');
estN.className = 'mu-est-note';
/* กฎ "จุดนี้ = เวลาจุดอ้างอิง ± n นาที" → บอกจุดอ้างอิง + เตือนเรื่องการจราจร */
estN.textContent = leg.estimateFrom && window.Schedules.offsetLabel
? t('time.derived', {
base: leg.estimateFrom.name || ('#' + leg.estimateFrom.station_id),
n: Schedules.offsetLabel(leg.estimateFrom.offsetMin)
})
: t('time.estimated');
body.appendChild(estN);
}
if (leg.note) {
var n = document.createElement('p');
n.className = 'mu-schedule-note';
n.innerHTML = '<svg class="ic" aria-hidden="true"><use href="#ic-triangle-alert"/></svg> ' + esc(leg.note);
body.appendChild(n);
}
});
}
/* ลิงก์ไปตารางเวลาเต็มของสายนั้น */
var act = document.createElement('div');
act.className = 'mu-inline-actions';
act.innerHTML = '<a class="mu-btn ghost" href="#/check/' + encodeURIComponent(r.route_id) + '">' +
esc(t('stop.viewScheduleN', { n: r.route_id })) + '</a>';
body.appendChild(act);
det.appendChild(body);
li.appendChild(det);
list.appendChild(li);
});
setupFilter();
}
function setCount(shown, total) {
var el = $('mu-route-count');
if (el) el.textContent = total ? t('stop.showing', { a: shown, b: total }) : '';
}
/* ---------- กรองสายรถ (ค้นหา) ---------- */
function applyFilter() {
var input = $('mu-route-filter');
var items = Array.prototype.slice.call(document.querySelectorAll('#stop-routes > li'));
if (!items.length) return;
var kw = (input ? input.value : '').trim().toLowerCase();
var shown = 0;
items.forEach(function (li) {
var ok = !kw || li.textContent.toLowerCase().indexOf(kw) !== -1;
li.style.display = ok ? '' : 'none';
if (ok) shown++;
});
setCount(shown, items.length);
}
function setupFilter() {
var input = $('mu-route-filter');
if (input && !input.dataset.bound) {
input.dataset.bound = '1';
input.addEventListener('input', applyFilter);
}
}
/* ---------- ไฮไลต์เที่ยวถัดไป + นาฬิกา (เดิมอยู่ใน partial) ---------- */
function tick() {
var clock = $('mu-current-time');
var now = new Date();
if (clock) clock.textContent = pad2(now.getHours()) + ':' + pad2(now.getMinutes());
var nowm = nowMin();
var soonest = null;
document.querySelectorAll('.mu-stop-expander').forEach(function (det) {
var chips = Array.prototype.slice.call(det.querySelectorAll('.mu-time-chip'));
chips.forEach(function (c) { c.classList.remove('next-live'); });
var times = chips.map(function (c) { return { c: c, m: toMin(c.textContent) }; })
.filter(function (x) { return x.m !== null; })
.sort(function (a, b) { return a.m - b.m; });
if (!times.length) return;
var next = times.filter(function (x) { return x.m >= nowm; })[0];
var tomorrow = false;
if (!next) { next = times[0]; tomorrow = true; }
next.c.classList.add('next-live');
if (!soonest || next.m < soonest.m) soonest = { m: next.m, time: next.c.textContent.trim(), tomorrow: tomorrow };
var rail = det.querySelector('.mu-stop-summary-rail');
if (!rail) return;
var el = rail.querySelector('.mu-next-arrival');
if (!el) {
el = document.createElement('span');
el.className = 'mu-next-arrival';
var chev = rail.querySelector('.mu-stop-chevron');
chev ? rail.insertBefore(el, chev) : rail.appendChild(el);
}
var routeId = (det.querySelector('.mu-route-chip') || {}).textContent || '';
el.textContent = (tomorrow ? t('stop.tomorrow') : t('stop.nextAt')) + ' ' + next.c.textContent.trim();
el.dataset.route = routeId;
});
var hero = $('stop-next-trip');
if (hero) hero.textContent = soonest ? (soonest.tomorrow ? t('stop.tomorrow') + ' ' : '') + soonest.time : '–';
}
/* ---------- ป้ายที่ไม่พบข้อมูล ---------- */
function renderNotFound(code) {
var main = document.querySelector('#view .mu-container') || document.querySelector('#view');
if (!main) return;
main.innerHTML =
'<p class="mu-breadcrumb"><a href="#/stops">' + esc(t('common.backStops')) + '</a></p>' +
'<h1 class="mu-stop-hero-title">' + esc(t('stop.notFound')) + '</h1>' +
'<p class="mu-stop-hero-sub">' + esc(t('stop.notFoundSub', { code: code })) + '</p>';
}
/* ---------- เริ่มทำงาน ---------- */
function init() {
var route = window.__route || { params: {}, query: {} };
var code = (route.params && route.params.code) || '';
var wantRoute = (route.query && route.query.route) || '';
Promise.all([
fetchJSON(DATA + 'stations.json'),
fetchJSON(DATA + 'routes.json'),
Schedules.load().then(function (sd) { return Schedules.normSub(sd); }),
(window.OtherStop ? OtherStop.load() : Promise.resolve([]))
]).then(function (res) {
var stations = (res[0] || {}).stations || [];
if (window.Schedules && Schedules.setStationNames) Schedules.setStationNames(stations); /* ให้ข้อความ "คำนวณจาก ..." มีชื่อจุด */
var routes = (res[1] || {}).routes || [];
var sched = res[2] || {};
var others = res[3] || [];
var st = findStation(stations, code);
if (!st) { renderNotFound(code); return; }
var rows = routesAtStation(routes, sched, st.station_id);
var sum = summarise(rows);
/* สถานี 8 "จุดขึ้นรถประจำทางอื่นๆ" = ชื่อรวมของทุกสาย
→ จับคู่ route id เพื่อเอาชื่อสถานที่จริงของสายนั้น (เช่น 1011 → วัดสีบัวทอง / 1101 ในไฟล์) */
var validIds = routes.map(function (r) { return String(r.route_id); });
var isOther = !!(window.OtherStop && OtherStop.isOtherStop(st));
var placeByRoute = {};
if (isOther) {
routes.forEach(function (r) {
var e = OtherStop.forRoute(others, r.route_id, validIds);
if (e) placeByRoute[String(r.route_id)] = e;
});
}
var focusPlace = (isOther && wantRoute) ? (placeByRoute[String(wantRoute)] || null) : null;
renderOtherStops(isOther, routes, others, validIds, wantRoute, sched);
renderHero(st, sum, findPhoto(st), focusPlace);
renderInfo(st, sum);
renderToolbar(st, rows, wantRoute);
renderNearby(st);
renderChips(st, sum);
renderRoutes(rows, placeByRoute);
var filter = $('mu-route-filter');
if (filter && wantRoute) { filter.value = wantRoute; applyFilter(); }
tick();
setInterval(tick, 30000);
/* แผนที่: ใช้เส้นทางของสายที่ผ่านป้ายนี้ */
var mapEl = $('stop-map');
if (mapEl && window.ATMap) {
var mapOpts = {};
if (isOther && wantRoute) mapOpts.onlyRoutes = [String(wantRoute)];
if (focusPlace) mapOpts.focus = focusPlace;
try { ATMap.forStop(mapEl, st.station_code, mapOpts); } catch (e) { /* แผนที่พังไม่ควรทำให้หน้าเจ๊ง */ }
}
});
}
/* ---------- จุดขึ้นรถประจำทางอื่นๆ: แสดง "ทุกจุด" ของสายนั้น ----------
สายเดียวผ่านได้หลายจุด (เช่น 954 = ตลาดท่าช้าง · สามโก้ · วิเศษชัยชาญ · อ่างทอง)
จุดที่ยังไม่มีพิกัด → แสดงชื่อพร้อมป้าย "ยังไม่มีพิกัด" (ไม่ซ่อนข้อมูล) */
function renderOtherStops(isOther, routes, others, validIds, wantRoute, sched) {
var host = $('stop-other-stops');
if (!host) {
if (!isOther) return;
host = document.createElement('section');
host.className = 'mu-other-stops';
host.id = 'stop-other-stops';
var bar = $('stop-toolbar');
if (bar && bar.parentNode) bar.parentNode.insertBefore(host, bar);
else return;
}
if (!isOther) { host.hidden = true; return; }
var list = []; /* { route_id, stop } */
routes.forEach(function (r) {
if (wantRoute && String(r.route_id) !== String(wantRoute)) return;
OtherStop.allStops(others, r.route_id, validIds).forEach(function (st) {
list.push({ route_id: String(r.route_id), stop: st });
});
});
if (!list.length) { host.hidden = true; return; }
var one = !!wantRoute && routes.filter(function (r) { return String(r.route_id) === String(wantRoute); }).length === 1;
/* เวลาของ "จุดแรก" ของแต่ละสาย (แถว station 8 ในตารางเวลา) = ฐานสำหรับคำนวณจุดถัดไป
offsets_min ใน 8 other_stop.json บอกว่าแต่ละจุดห่างจากจุดแรกกี่นาที */
var baseByRoute = {};
if (window.Schedules && sched && Schedules.records) {
(Schedules.records(sched) || []).forEach(function (r) {
if (String(r.station_id) !== '8') return;
var rid = String(r.route_id), t2 = r.departure_times || [];
if (t2.length && (!baseByRoute[rid] || t2.length > baseByRoute[rid].length)) baseByRoute[rid] = t2;
});
}
var canShift = !!(window.Schedules && Schedules.shiftTimes && Schedules.offsetLabel);
host.hidden = false;
host.innerHTML =
'<h2 class="sect">' + esc(one
? t('stop.otherStopsTitle', { n: wantRoute, m: list.length })
: t('stop.otherStopsAll', { m: list.length })) + '</h2>' +
'<ul class="mu-list mu-other-list">' + list.map(function (x) {
var st = x.stop;
var where = [st.city_th, st.province_th].filter(Boolean).join(' · ');
var base = baseByRoute[String(x.route_id)] || null;
var hasOff = (st.offsetMin != null);
var times = (hasOff && base && canShift) ? Schedules.shiftTimes(base, st.offsetMin) : null;
var first = (hasOff && base) ? list.filter(function (y) { return String(y.route_id) === String(x.route_id); })[0] : null;
return '<li class="mu-other-item">' +
'<div class="mu-other-row">' +
'<span class="mu-other-idx">' + (st.index + 1) + '</span>' +
'<div class="mu-other-txt"><strong>' + esc(st.name) + '</strong>' +
(where ? '<span>' + esc(where) + '</span>' : '') +
(one ? '' : '<span>' + esc(t('stops.busPrefix')) + ' ' + esc(x.route_id) + '</span>') +
'</div>' +
'<span class="mu-chip ' + (st.hasCoord ? 'count' : 'warn') + '">' +
esc(st.hasCoord ? (Number(st.lat).toFixed(4) + ', ' + Number(st.lng).toFixed(4)) : t('stop.noCoord')) +
'</span>' +
'</div>' +
(times && times.length
? '<div class="mu-time-grid is-est">' + times.map(function (tm) {
return '<div class="mu-time-chip">' + esc(tm) + '</div>';
}).join('') + '</div>' +
(st.offsetMin !== 0 && first
? '<p class="mu-est-note">' + esc(t('time.derived', {
base: first.stop.name, n: Schedules.offsetLabel(st.offsetMin)
})) + '</p>'
: '')
: '') +
'</li>';
}).join('') + '</ul>';
}
/* ---------- ปุ่ม/ทางลัด ---------- */
function renderToolbar(st, rows, preferRoute) {
var bar = $('stop-toolbar');
if (!bar || !rows.length) return;
/* ถ้ามี ?route= ให้ปุ่มอ้างสายนั้นก่อน ไม่งั้นใช้สายแรก */
var top = rows[0].route;
if (preferRoute) {
var hit = rows.filter(function (x) { return String(x.route.route_id) === String(preferRoute); })[0];
if (hit) top = hit.route;
}
bar.innerHTML =
'<a class="mu-btn primary" href="#/check/' + encodeURIComponent(top.route_id) + '">' +
esc(t('stop.viewScheduleN', { n: top.route_id })) + '</a>' +
'<a class="mu-back-btn" href="#/stops">' + esc(t('common.backStops')) + '</a>';
}
/* ---------- สถานที่ใกล้เคียง (ต้องมีข้อมูลใน stations.json → landmarks[]) ---------- */
function renderNearby(st) {
var sec = $('stop-nearby');
var grid = $('stop-nearby-grid');
if (!sec || !grid) return;
var marks = st.landmarks || [];
if (!marks.length) { sec.hidden = true; return; }
sec.hidden = false;
var sub = $('stop-nearby-sub');
if (sub) sub.textContent = st.landmarks_note || t('stop.nearbySub');
grid.innerHTML = marks.map(function (m) {
return '<div class="mu-nearby-card">' +
'<img src="' + esc(m.img || '') + '" alt="' + esc(m.caption || '') + '" loading="lazy" decoding="async">' +
'<p class="cap">' + esc(m.caption || '') + (m.sub ? '<small>' + esc(m.sub) + '</small>' : '') + '</p></div>';
}).join('');
}
/* ---------- ชิป: เวลาเปิด–ปิด + สิ่งอำนวยความสะดวก (ถ้ามีใน stations.json) ---------- */
function renderChips(st, sum) {
var box = $('stop-chips');
if (!box) return;
var html = '';
if (sum.first) html += '<span class="mu-chip">' + esc(t('stop.open')) + ' ' + esc(sum.first) + '</span>';
if (sum.last) html += '<span class="mu-chip">' + esc(t('stop.close')) + ' ' + esc(sum.last) + '</span>';
(st.facilities || []).forEach(function (f) {
html += '<span class="mu-chip ok">' + esc(f) + '</span>';
});
box.innerHTML = html;
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();