- busStop.html: added the third tab (data-mode=near) and its own panel with the location button, hint and result area; removed the near button from the toolbar and deleted the old on-screen numpad markup - busStop.search.js: MODE_NEAR in setMode, disables the other panels, hides the stop list while ranked results are shown, skips filtering in that mode, and removed the dead initNumpad code - security gate: the tab is revealed only when isSecureContext and navigator.geolocation exist; otherwise it stays hidden and the mode is refused - map.js: re-enable the button after a successful lookup - i18n: stops.modeNear / nearCta / nearHint - bump SW version to 2026.09.27.98 Verified on localhost:8090 - three tabs, near mode shows a map with 8 ranked stops, button re-enables and works again, switching back restores the list; with isSecureContext forced false the tab is hidden and unselectable.
166 lines
8.7 KiB
JavaScript
166 lines
8.7 KiB
JavaScript
/* ============================================================
|
|
Atroute — hash router (single-page app)
|
|
------------------------------------------------------------
|
|
สลับ view โดยไม่โหลดหน้าใหม่ (no reflash):
|
|
- #/ → หน้าแรก (partials/home.html)
|
|
- #/stops → ป้ายหยุดรถ (partials/busStop.html)
|
|
- #/routes → รายละเอียดสาย (partials/busDetail.html)
|
|
- #/routes/:id → สายเดียวกัน + ระบุ routeId
|
|
- #/stops/:code → รายละเอียดป้าย (partials/stopDetail.html)
|
|
- #/credits → เครดิต (partials/credits.html)
|
|
|
|
Partial ที่ inject เข้าไปมี <style> + <script> ของตัวเอง
|
|
— router รัน <script> ให้อัตโนมัติหลัง inject
|
|
ลิงก์ภายในทั้งหมดใช้ <a href="#/..."> — hashchange ไม่รีโหลดหน้า
|
|
============================================================ */
|
|
(function () {
|
|
'use strict';
|
|
|
|
var VIEWS = {
|
|
home: { partial: 'partials/home.html', nav: '/', title: 'หน้าแรก', tkey: 'page.home' },
|
|
stops: { partial: 'partials/busStop.html', nav: '/stops', title: 'ป้ายหยุดรถ', tkey: 'page.stops' },
|
|
routes: { partial: 'partials/busDetail.html', nav: '/routes', title: 'รายละเอียดสายรถ', tkey: 'page.routes' },
|
|
check: { partial: 'partials/checkSchedule.html', nav: '/check', title: 'เช็กเวลาเดินรถ', tkey: 'page.check' },
|
|
stop: { partial: 'partials/stopDetail.html', nav: '/stops/', title: 'รายละเอียดป้าย', tkey: 'page.stop' },
|
|
credits: { partial: 'partials/credits.html', nav: '/credits', title: 'เครดิต', tkey: 'page.credits' },
|
|
about: { partial: 'partials/about.html', nav: '/about', title: 'เกี่ยวกับเรา', tkey: 'page.about' },
|
|
blog: { partial: 'partials/blog.html', nav: '/blog', title: 'บ่นไปเรื่อย', tkey: 'page.blog' },
|
|
};
|
|
|
|
function parseHash() {
|
|
var raw = window.location.hash.replace(/^#/, '');
|
|
var query = {};
|
|
if (raw === '' || raw === '/') return { name: 'home', params: {}, query: query };
|
|
|
|
var qIdx = raw.indexOf('?');
|
|
var path = qIdx === -1 ? raw : raw.slice(0, qIdx);
|
|
if (qIdx !== -1) {
|
|
new URLSearchParams(raw.slice(qIdx + 1)).forEach(function (v, k) { query[k] = v; });
|
|
}
|
|
|
|
var seg = path.split('/').filter(Boolean);
|
|
if (seg[0] === 'routes') {
|
|
if (seg[1]) return { name: 'routes', params: { routeId: decodeURIComponent(seg[1]) }, query: query };
|
|
return { name: 'routes', params: {}, query: query };
|
|
}
|
|
if (seg[0] === 'stops') {
|
|
if (seg[1]) return { name: 'stop', params: { code: decodeURIComponent(seg[1]) }, query: query };
|
|
return { name: 'stops', params: {}, query: query };
|
|
}
|
|
if (seg[0] === 'credits') return { name: 'credits', params: {}, query: query };
|
|
if (seg[0] === 'about') return { name: 'about', params: {}, query: query };
|
|
if (seg[0] === 'blog') {
|
|
if (seg[1]) return { name: 'blog', params: { postId: decodeURIComponent(seg[1]) }, query: query };
|
|
return { name: 'blog', params: {}, query: query };
|
|
}
|
|
if (seg[0] === 'check') {
|
|
if (seg[1]) return { name: 'check', params: { routeId: decodeURIComponent(seg[1]) }, query: query };
|
|
return { name: 'check', params: {}, query: query };
|
|
}
|
|
return { name: 'home', params: {}, query: query };
|
|
}
|
|
|
|
var viewEl = document.getElementById('view');
|
|
var lastKey = '';
|
|
|
|
/* สคริปต์ที่ inject ผ่าน innerHTML ไม่รันเอง — ต้องสร้าง element ใหม่ */
|
|
function execScripts(container) {
|
|
var scripts = container.querySelectorAll('script');
|
|
Array.prototype.forEach.call(scripts, function (old) {
|
|
var s = document.createElement('script');
|
|
if (old.src) {
|
|
s.src = old.src;
|
|
} else {
|
|
s.textContent = old.textContent;
|
|
}
|
|
old.parentNode.replaceChild(s, old);
|
|
});
|
|
}
|
|
|
|
/* เน้นเมนูที่ตรงกับ route ปัจจุบัน */
|
|
function setNavActive(route) {
|
|
var navPath = VIEWS[route.name].nav;
|
|
document.querySelectorAll('.mu-nav a, .mu-footer-col a').forEach(function (a) {
|
|
var href = a.getAttribute('href') || '';
|
|
if (!href.indexOf('#/')) {
|
|
var isActive = navPath === '/' ? href === '#/' : href.indexOf('#/' + navPath) === 0;
|
|
a.classList.toggle('active', isActive);
|
|
}
|
|
});
|
|
}
|
|
|
|
/* footer "สายที่เปิดดูบ่อย" — โหลด routes.json แล้วให้ Popular จัดอันดับ+วาด
|
|
(เรียกทุกครั้งที่ render: ตามภาษา + อันดับล่าสุด · ถ้า Popular ไม่มี = ไม่ทำอะไร) */
|
|
var popularCache = null;
|
|
function mountPopular() {
|
|
if (!window.Popular || !document.getElementById('footer-popular-list')) return;
|
|
var paint = function (routes) { window.Popular.mount('footer-popular-list', routes); };
|
|
if (popularCache) { paint(popularCache); return; }
|
|
fetch('../resource/data/routes.json', { cache: 'no-store' })
|
|
.then(function (r) { return r.ok ? r.json() : null; })
|
|
.then(function (d) {
|
|
popularCache = (d && d.routes) || [];
|
|
if (popularCache.length) paint(popularCache);
|
|
})
|
|
.catch(function () { /* ไม่มีข้อมูล → คง li เดิมใน HTML */ });
|
|
}
|
|
|
|
function render() {
|
|
var route = parseHash();
|
|
var view = VIEWS[route.name];
|
|
if (!view) return;
|
|
|
|
/* นับว่าเปิดดูสายนี้ 1 ครั้ง — ต้องอยู่ก่อนเช็ค lastKey
|
|
(เปิดสายเดิมซ้ำ เช่นออกไปหน้าแรกแล้วกลับมา ต้องนับเพิ่มด้วย) */
|
|
if (window.Popular && route.params && route.params.routeId) {
|
|
window.Popular.hit(route.params.routeId);
|
|
}
|
|
|
|
var key = route.name + '|' + JSON.stringify(route.params) + '|' + JSON.stringify(route.query);
|
|
if (key === lastKey) return; // hash เดิม — ไม่ render ซ้ำ
|
|
lastKey = key;
|
|
|
|
window.__route = route; // ให้ script ใน partial อ่าน params ได้
|
|
|
|
|
|
fetch(view.partial, { cache: 'no-store' })
|
|
.then(function (res) {
|
|
if (!res.ok) throw new Error('HTTP ' + res.status);
|
|
return res.text();
|
|
})
|
|
.then(function (html) {
|
|
viewEl.innerHTML = html;
|
|
execScripts(viewEl);
|
|
if (window.I18N) window.I18N.apply(); // แปล chrome + view ใหม่
|
|
setNavActive(route);
|
|
if (route.params.routeId) {
|
|
var stateEl = viewEl.querySelector('.mu-inline-context strong');
|
|
if (stateEl) stateEl.textContent = '?route=' + route.params.routeId;
|
|
}
|
|
document.title = (window.I18N && view.tkey) ? I18N.t(view.tkey) : ('Atroute — ' + view.title);
|
|
window.scrollTo(0, 0);
|
|
|
|
/* footer "สายที่เปิดดูบ่อย" — เรนเดอร์ทุกครั้ง (ตามภาษา + อันดับล่าสุด) */
|
|
mountPopular();
|
|
})
|
|
.catch(function () {
|
|
viewEl.innerHTML =
|
|
'<div class="mu-container" style="padding:64px 28px;text-align:center;color:#6f7b95;">' +
|
|
(window.I18N ? I18N.t('router.loadFail') : 'โหลดเนื้อหาไม่สำเร็จ') + '</div>';
|
|
});
|
|
}
|
|
|
|
window.addEventListener('hashchange', render);
|
|
|
|
/* เปลี่ยนภาษา → render หน้าใหม่ (กันข้อความที่ JS สร้างค้างเป็นภาษาเก่า) */
|
|
window.addEventListener('langchange', function () {
|
|
lastKey = '';
|
|
render();
|
|
});
|
|
|
|
/* รอไฟล์คำแปลโหลดเสร็จก่อนวาดหน้าแรก — ไม่งั้นข้อความที่ JS สร้างจะได้แค่ key
|
|
(ข้ามไปเลยถ้าไม่มี I18N หรือเบราว์เซอร์เก่าที่ไม่รองรับ Promise) */
|
|
if (window.I18N && window.I18N.ready) window.I18N.ready.then(render);
|
|
else render();
|
|
})();
|