Files
web-at-transit-mock/ui/partials/home.init.js
T
2026-09-23 00:53:14 +07:00

117 lines
4.8 KiB
JavaScript

(function () {
'use strict';
// แหล่งข้อมูลเดียว (source of truth) — อย่า hardcode รายชื่อจังหวัด/สถานีไว้ในไฟล์นี้
var DATA_URL = {
provinces: '../resource/data/provinces.json',
stations: '../resource/data/stations.json'
};
// จังหวัดเริ่มต้น (home province ของเว็บนี้) — ใช้เป็นค่า default ของ hero + ตัวเลือก
var DEFAULT_PROVINCE = 'อ่างทอง';
function fetchJSON(url) {
return fetch(url, { cache: 'no-store' })
.then(function (r) { return r.ok ? r.json() : null; })
.catch(function () { return null; });
}
function initHomeSelectors() {
var provinceSelect = document.getElementById('province-select');
var stationSelect = document.getElementById('station-select');
var stationHint = document.getElementById('station-hint');
var heroProvince = document.getElementById('hero-province');
if (!provinceSelect || !stationSelect) return;
var tr = window.I18N ? window.I18N.t : function (k) { return k; };
// โหลดจังหวัด + สถานี จาก resource/data/*.json (โหลดพร้อมกันรอบเดียว)
Promise.all([
fetchJSON(DATA_URL.provinces),
fetchJSON(DATA_URL.stations)
]).then(function (results) {
var provincesData = results[0];
var stationsData = results[1];
var provinces = ((provincesData && provincesData.provinces) || [])
.map(function (p) { return p.province_name_th; })
.filter(Boolean);
var stations = (stationsData && stationsData.stations) || [];
if (!provinces.length) return; // โหลดข้อมูลไม่สำเร็จ → ปล่อย select ว่างไว้
var selectedProvince = provinces.indexOf(DEFAULT_PROVINCE) !== -1
? DEFAULT_PROVINCE
: provinces[0];
provinceSelect.innerHTML = '';
provinces.forEach(function (name) {
var opt = document.createElement('option');
opt.value = name;
opt.textContent = name;
if (name === selectedProvince) opt.selected = true;
provinceSelect.appendChild(opt);
});
function stationsFor(province) {
return stations
.filter(function (s) {
// สถานีที่ไม่มี province (เช่น "จุดขึ้นรถประจำทางอื่นๆ") ให้ถือว่าอยู่ในจังหวัดหลัก
var p = s.province_th || DEFAULT_PROVINCE;
return p === province;
})
.map(function (s) { return s.station_name_th; })
.filter(Boolean);
}
function populateStations(province) {
stationSelect.innerHTML = '';
var list = stationsFor(province);
if (list.length === 0) {
var empty = document.createElement('option');
empty.value = '';
empty.textContent = tr('home.stationEmpty');
empty.disabled = true;
empty.selected = true;
stationSelect.appendChild(empty);
if (stationHint) stationHint.textContent = tr('home.stationEmptyHint') + ' ' + province;
} else {
list.forEach(function (name) {
var opt = document.createElement('option');
opt.value = name;
opt.textContent = name;
stationSelect.appendChild(opt);
});
if (stationHint) stationHint.innerHTML = tr('home.stationCount', { n: list.length }) + ' ' + province;
}
var prefix = tr('home.provincePrefix');
// กรุงเทพมหานครเป็นเขตปกครองพิเศษ ไม่ใช่ "จังหวัด" → ไม่เติมคำนำหน้า
var label = (prefix && province !== 'กรุงเทพมหานคร')
? prefix + ' ' + province
: province;
if (heroProvince) heroProvince.textContent = label;
}
provinceSelect.addEventListener('change', function () {
populateStations(provinceSelect.value);
});
populateStations(selectedProvince);
});
}
function initMarquee() {
var track = document.querySelector('.mu-marquee-track');
if (!track || track.dataset.cloned) return;
var reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!reduce) {
track.innerHTML += track.innerHTML;
track.dataset.cloned = '1';
}
}
initHomeSelectors();
initMarquee();
})();