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 · นามสกุลรูป · ชิปสาย · หน้ารายละเอียดป้ายมาจากไหน
325 lines
17 KiB
JavaScript
325 lines
17 KiB
JavaScript
/* ============================================================
|
||
checkSchedule.init.js — หน้า #/check/<รหัสสาย>
|
||
------------------------------------------------------------
|
||
ดึงข้อมูลจริง:
|
||
resource/data/routes.json ชื่อสาย / ตัวเลือก / station_ids / province_ids
|
||
resource/data/stations.json ชื่อจุดจอด + จังหวัด + รหัสป้าย
|
||
resource/data/schedules.json เวลาออกรถ (แยกตามจุดจอด/ทิศทาง) + หมายเหตุ
|
||
|
||
การไฮไลต์ (ตามเวลาบนเครื่องผู้ใช้):
|
||
- ไฮไลต์ "เที่ยวถัดไป" = เที่ยวแรกที่เวลายังไม่ผ่าน (>= เวลาปัจจุบัน)
|
||
- เลยเที่ยวสุดท้ายของวันแล้ว → ไม่ไฮไลต์เลย
|
||
- หลังเที่ยงคืน เวลาปัจจุบันน้อยกว่าเที่ยวแรก → เที่ยวแรกของวันถูกไฮไลต์เอง
|
||
- ตรวจซ้ำทุก 30 วินาที (และตอนสลับสาย/กลับเข้าแท็บ)
|
||
============================================================ */
|
||
(function () {
|
||
'use strict';
|
||
|
||
var DATA = '../resource/data/';
|
||
var FALLBACK_ID = '101';
|
||
var REFRESH_MS = 30000;
|
||
var TONES = { origin: 'tone-purple', transfer: 'tone-sky', pass: 'tone-mint', destination: 'tone-pink' };
|
||
|
||
/* นโยบายการแสดงจุดจอดบนหน้าเช็กเวลา
|
||
false = แสดงทุกจุดใน routes.json → station_ids (จุดที่ยังไม่มีตารางจะขึ้นการ์ด "ยังไม่มีข้อมูลตารางเวลา")
|
||
← ค่าเริ่มต้น: ตรงกับดีไซน์เดิม (หน้าเดิมโชว์จุดที่ยังไม่มีตารางพร้อมข้อความนี้)
|
||
true = แสดงเฉพาะจุดที่มี record ใน schedules.json (กระดานเวลาล้วน ๆ) */
|
||
var ONLY_STOPS_WITH_SCHEDULE = false;
|
||
|
||
var sections = []; /* [{ chips:[], times:[], station }] */
|
||
var netRoute = null; /* routes.json ของสายที่แสดงอยู่ */
|
||
var timer = null;
|
||
|
||
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, '&').replace(/</g, '<')
|
||
.replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
||
}
|
||
function fetchJSON(u) {
|
||
return fetch(u, { cache: 'no-store' }).then(function (r) { return r.ok ? r.json() : null; }).catch(function () { return null; });
|
||
}
|
||
function toMin(hhmm) {
|
||
var a = String(hhmm).split(':');
|
||
return (parseInt(a[0], 10) || 0) * 60 + (parseInt(a[1], 10) || 0);
|
||
}
|
||
function nowMin() { var d = new Date(); return d.getHours() * 60 + d.getMinutes(); }
|
||
function hhmm(min) {
|
||
var h = Math.floor(min / 60), m = min % 60;
|
||
return (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m;
|
||
}
|
||
function setStat(id, s) {
|
||
var el = $(id);
|
||
if (!el) return;
|
||
var v = (s == null ? '' : String(s)).trim();
|
||
el.textContent = v || '–';
|
||
}
|
||
|
||
/* ---------- จำแนกประเภทจุดจอดจากข้อมูล + ลำดับในเส้นทาง ---------- */
|
||
function kindOf(st, i, n) {
|
||
if (i === 0) return { key: 'route.origin', icon: 'ic-map-pin', tone: TONES.origin };
|
||
if (i === n - 1) return { key: 'route.destination', icon: 'ic-map-pin', tone: TONES.destination };
|
||
if (st && st.station_type === 'main_terminal') return { key: 'route.transfer', icon: 'ic-refresh-cw', tone: TONES.transfer };
|
||
return { key: 'route.pass', icon: 'ic-map-pin', tone: TONES.pass };
|
||
}
|
||
|
||
/* ---------- สร้างการ์ดจุดจอด ---------- */
|
||
function renderList(listEl, route, groups) {
|
||
listEl.innerHTML = '';
|
||
sections = [];
|
||
|
||
groups.forEach(function (g, i) {
|
||
var kind = kindOf(g.st, i, groups.length);
|
||
var li = document.createElement('li');
|
||
var det = document.createElement('details');
|
||
det.className = 'mu-stop-card';
|
||
det.open = true;
|
||
|
||
var sum = document.createElement('summary');
|
||
sum.innerHTML =
|
||
'<span class="mu-stop-head ' + kind.tone + '">' +
|
||
'<span class="mu-stop-kind"><svg class="ic" aria-hidden="true"><use href="#' + kind.icon + '"/></svg> <span>' + esc(t(kind.key)) + '</span></span>' +
|
||
'<span class="mu-stop-title"><strong>' + esc(g.name) + '</strong><small>' + esc(g.city) + '</small></span>' +
|
||
'<span class="mu-stop-chevron">\u2304</span>' +
|
||
'</span>';
|
||
det.appendChild(sum);
|
||
|
||
var body = document.createElement('div');
|
||
body.className = 'mu-stop-body';
|
||
|
||
if (!g.recs.length) {
|
||
var none = document.createElement('p');
|
||
none.className = 'mu-stop-sub';
|
||
none.textContent = t('route.empty');
|
||
body.appendChild(none);
|
||
}
|
||
|
||
/* มีอย่างน้อย 1 ทิศที่มีตารางจริงไหม — ถ้ามี ห้ามขึ้น "ยังไม่มีข้อมูล" ของทิศที่ว่าง
|
||
(จุดเดียวมีได้หลายทิศ · ทิศว่างไม่ควรทำให้การ์ดที่ตารางขึ้นแล้วดูเหมือนไม่มีข้อมูล) */
|
||
var hasAny = g.recs.some(function (x) { return (x.departure_times || []).length > 0; });
|
||
|
||
g.recs.forEach(function (r) {
|
||
var times = (r.departure_times || []).slice();
|
||
|
||
/* ทิศนี้ยังไม่มีเวลา → ข้ามไปเลยถ้าทิศอื่นมีการ์ดข้อมูลอยู่แล้ว */
|
||
if (!times.length) {
|
||
if (hasAny) return;
|
||
var empty = document.createElement('p');
|
||
empty.className = 'mu-stop-sub';
|
||
empty.textContent = t('route.empty');
|
||
body.appendChild(empty);
|
||
return;
|
||
}
|
||
|
||
/* หัวแถว = ชื่อทิศ (dir_label) → ไม่มีค่อยใช้ dir_note → ไม่มีอีกค่อยบอกว่าอยู่ที่จุดนี้
|
||
ทิศที่ไปคนละทางจากจุดเดียวกัน (ป่าโมกไปอ่างทอง / ป่าโมกไปอยุธยา) จึงแยกให้เห็นชัด */
|
||
var sub = document.createElement('p');
|
||
sub.className = 'mu-stop-sub';
|
||
if (r.dir_label) {
|
||
sub.innerHTML = esc(r.dir_label) +
|
||
(r.direction_note ? ' <span class="mu-direction-tag">' + esc(r.direction_note) + '</span>' : '');
|
||
} else if (r.direction_note) {
|
||
sub.innerHTML = esc(r.direction_note) + ' <span class="mu-direction-tag">' + esc(t('check.direction')) + '</span>';
|
||
} else {
|
||
sub.textContent = t('check.atStop') + ' ' + g.name;
|
||
}
|
||
body.appendChild(sub);
|
||
|
||
var grid = document.createElement('div');
|
||
grid.className = 'mu-time-grid' + (r.estimated ? ' is-est' : ''); /* เวลาที่คำนวณจากต้นทาง */
|
||
var chips = [];
|
||
times.forEach(function (tm) {
|
||
var c = document.createElement('div');
|
||
c.className = 'mu-time-chip';
|
||
c.textContent = tm;
|
||
grid.appendChild(c);
|
||
chips.push(c);
|
||
});
|
||
body.appendChild(grid);
|
||
if (r.estimated) {
|
||
var est = document.createElement('p');
|
||
est.className = 'mu-est-note';
|
||
/* มาจากกฎ "จุดนี้ = เวลาจุดอ้างอิง ± n นาที" → บอกจุดอ้างอิง + เตือนเรื่องการจราจร */
|
||
est.textContent = r.estimateFrom && window.Schedules.offsetLabel
|
||
? t('time.derived', {
|
||
base: r.estimateFrom.name || ('#' + r.estimateFrom.station_id),
|
||
n: Schedules.offsetLabel(r.estimateFrom.offsetMin)
|
||
})
|
||
: t('time.estimated');
|
||
body.appendChild(est);
|
||
}
|
||
sections.push({ chips: chips, times: times, station: g.name });
|
||
|
||
if (r.schedule_note) {
|
||
var note = document.createElement('p');
|
||
note.className = 'mu-schedule-note';
|
||
note.innerHTML = '<svg class="ic" aria-hidden="true"><use href="#ic-triangle-alert"/></svg> ' + esc(r.schedule_note);
|
||
body.appendChild(note);
|
||
}
|
||
});
|
||
|
||
if (g.code) {
|
||
var act = document.createElement('div');
|
||
act.className = 'mu-inline-actions';
|
||
var a = document.createElement('a');
|
||
a.className = 'mu-btn ghost';
|
||
a.href = '#/stops/' + encodeURIComponent(g.code) + '?route=' + encodeURIComponent(route.route_id);
|
||
a.textContent = t('check.toStopDetail');
|
||
act.appendChild(a);
|
||
body.appendChild(act);
|
||
}
|
||
|
||
det.appendChild(body);
|
||
li.appendChild(det);
|
||
listEl.appendChild(li);
|
||
});
|
||
}
|
||
|
||
/* ---------- ไฮไลต์เที่ยวถัดไป + บรรทัดเวลาปัจจุบัน ---------- */
|
||
function refreshHighlights() {
|
||
var nowm = nowMin();
|
||
var next = null; /* เที่ยวถัดไปที่เร็วที่สุดในทุกจุดจอด */
|
||
|
||
sections.forEach(function (sec) {
|
||
sec.chips.forEach(function (c) { c.classList.remove('next'); });
|
||
for (var i = 0; i < sec.times.length; i++) {
|
||
var m = toMin(sec.times[i]);
|
||
if (m >= nowm) { /* เที่ยวแรกที่ยังไม่ผ่าน */
|
||
sec.chips[i].classList.add('next');
|
||
if (next === null || m < next.m) next = { m: m, time: sec.times[i], station: sec.station };
|
||
break;
|
||
}
|
||
}
|
||
});
|
||
|
||
setStat('check-stat-next', next ? next.time : '');
|
||
|
||
var line = $('check-now-line');
|
||
if (!line) return;
|
||
var clock = '<svg class="ic" aria-hidden="true"><use href="#ic-clock"/></svg> ';
|
||
var body = esc(t('check.now')) + ' <b>' + hhmm(nowm) + '</b>';
|
||
if (next) body += ' · ' + esc(t('check.nextAt')) + ' <b>' + esc(next.time) + '</b> (' + esc(next.station) + ')';
|
||
else if (sections.length) body += ' · <b>' + esc(t('check.noMoreToday')) + '</b>';
|
||
line.innerHTML = clock + body;
|
||
}
|
||
|
||
/* ---------- โหลด + แสดงผล ---------- */
|
||
function init() {
|
||
var params = (window.__route && window.__route.params) || {};
|
||
var query = (window.__route && window.__route.query) || {};
|
||
var want = String(params.routeId || query.route || '');
|
||
|
||
Promise.all([
|
||
fetchJSON(DATA + 'routes.json'),
|
||
fetchJSON(DATA + 'stations.json'),
|
||
Schedules.load().then(function (sd) { return Schedules.normSub(sd); })
|
||
]).then(function (res) {
|
||
var routes = (res[0] || {}).routes || [];
|
||
var sts = (res[1] || {}).stations || [];
|
||
if (window.Schedules && Schedules.setStationNames) Schedules.setStationNames(sts); /* ให้ข้อความ "คำนวณจาก ..." มีชื่อจุด */
|
||
var recs = window.Schedules ? Schedules.records(res[2]) : ((res[2] || {}).schedules || res[2] || []);
|
||
if (!routes.length) return;
|
||
|
||
var route = routes.filter(function (r) { return String(r.route_id) === want; })[0] ||
|
||
routes.filter(function (r) { return String(r.route_id) === FALLBACK_ID; })[0] || routes[0];
|
||
var rid = String(route.route_id);
|
||
netRoute = route;
|
||
|
||
var byId = {};
|
||
sts.forEach(function (s) { byId[s.station_id] = s; });
|
||
|
||
/* ตัวเลือกสายรถ (จาก routes.json) */
|
||
var sel = $('check-route-select');
|
||
if (sel) {
|
||
sel.innerHTML = '';
|
||
routes.forEach(function (r) {
|
||
var o = document.createElement('option');
|
||
o.value = r.route_id;
|
||
o.textContent = 'สาย ' + r.route_id + (r.route_name_th ? ' - ' + r.route_name_th : '');
|
||
sel.appendChild(o);
|
||
});
|
||
sel.value = rid;
|
||
sel.addEventListener('change', function () {
|
||
window.location.hash = '#/check/' + encodeURIComponent(sel.value);
|
||
});
|
||
}
|
||
|
||
/* Hero + คำอธิบายโซน */
|
||
var hero = $('check-hero-route');
|
||
if (hero) hero.textContent = 'สาย ' + rid + (route.route_name_th ? ' · ' + route.route_name_th : '');
|
||
|
||
/* จัดกลุ่มตามลำดับจุดจอดของสาย แล้วต่อด้วยจุดที่เหลือ (ถ้ามีข้อมูล) */
|
||
var order = (route.station_ids || []).slice();
|
||
/* schema 2: ใช้ station_order ที่ตรวจทิศทางมาแล้วจากไฟล์ (ถ้ามี) */
|
||
var sched = window.Schedules ? Schedules.route(res[2], rid) : null;
|
||
if (sched && sched.station_order.length) order = sched.station_order.slice();
|
||
recs.forEach(function (r) {
|
||
if (String(r.route_id) === rid && order.indexOf(r.station_id) === -1) order.push(r.station_id);
|
||
});
|
||
|
||
var groups = order.map(function (sid) {
|
||
var st = byId[sid] || null;
|
||
return {
|
||
sid: sid,
|
||
st: st,
|
||
name: st ? st.station_name_th : 'จุดจอด ' + sid,
|
||
city: st ? st.city_th : '',
|
||
code: st ? st.station_code : '',
|
||
recs: recs.filter(function (r) { return String(r.route_id) === rid && r.station_id === sid; })
|
||
};
|
||
}).filter(function (g) {
|
||
return !ONLY_STOPS_WITH_SCHEDULE || g.recs.length;
|
||
});
|
||
|
||
var listEl = $('check-schedule-list');
|
||
if (listEl) renderList(listEl, route, groups);
|
||
|
||
var desc = $('check-zone-desc');
|
||
if (desc) {
|
||
var names = groups.map(function (g) { return g.name; });
|
||
/* บอกว่า "ตารางวันนี้" เป็นของวันไหน — เฉพาะสายที่มีการแยกตารางตามวัน (service) เท่านั้น
|
||
สายอื่นข้อความเหมือนเดิมทุกตัวอักษร */
|
||
/* ป้ายบอกว่า "ตารางที่เห็นอยู่นี้เป็นของวันไหน" — ใช้ผลตรวจวันจากตัวอ่านกลาง (ไม่คำนวณเองในหน้านี้) */
|
||
var dayLine = '';
|
||
if (window.Schedules && Schedules.dayInfo && Schedules.directions) {
|
||
var di = Schedules.dayInfo();
|
||
var infos = (Schedules.directions(res[2], rid) || []).map(function (d) { return d.service || { from: 'ไม่ระบุ' }; });
|
||
var conflict = infos.some(function (x) { return x.conflict; }); /* ทิศเดียวมีทั้งวันทำงาน+วันหยุด */
|
||
var unknown = infos.some(function (x) { return x.unknown; }); /* ยังไม่ระบุวันเลย */
|
||
var selectedByDay = infos.some(function (x) { return x.from === 'service' || x.from === 'ระบุตรง' || x.from === 'คำในโน้ต'; });
|
||
if (unknown) {
|
||
dayLine = ' · ' + t('check.dayUnknown');
|
||
} else if (conflict) {
|
||
dayLine = ' · ' + t('check.dayMixed');
|
||
} else if (selectedByDay) {
|
||
dayLine = ' · ' + t(di.dayType === 'holiday' ? 'check.dayHoliday' : 'check.dayWeekday') +
|
||
' (' + t('check.dayOfWeek') + ' ' + di.weekday_th + ')';
|
||
}
|
||
}
|
||
desc.textContent = 'สาย ' + rid + ' · ' +
|
||
(names.length > 1 ? names[0] + ' \u2192 ' + names[names.length - 1] + ' · ' : '') +
|
||
groups.length + ' ' + t('check.stopsCount') + dayLine;
|
||
}
|
||
|
||
/* KPI */
|
||
setStat('check-stat-stops', groups.length); /* นับจุดจอดที่แสดงจริง (ให้ตรงกับรายการด้านล่าง) */
|
||
setStat('check-stat-provinces', route.province_ids ? route.province_ids.length : '');
|
||
var trips = 0;
|
||
recs.forEach(function (r) {
|
||
if (String(r.route_id) === rid) trips += (r.departure_times || []).length;
|
||
});
|
||
setStat('check-stat-trips', trips ? String(trips) : '');
|
||
|
||
refreshHighlights();
|
||
if (timer) clearInterval(timer);
|
||
timer = setInterval(refreshHighlights, REFRESH_MS);
|
||
});
|
||
}
|
||
|
||
document.addEventListener('visibilitychange', function () {
|
||
if (!document.hidden) refreshHighlights();
|
||
});
|
||
|
||
init();
|
||
})();
|