Files
web-at-transit-mock/ui/pwa.js
T
Bkk1234 750b4a2ea8 mock: three search tabs - stop code, place name, near me
- 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.
2026-10-24 00:11:56 +07:00

78 lines
3.8 KiB
JavaScript

/* ============================================================
Atroute — PWA registration + auto-update
ผู้ใช้ไม่ต้องล้างแคชเอง:
1. register แบบ updateViaCache:"none" → ไม่เชื่อ HTTP cache ของ sw.js
2. เรียก reg.update() ทุกครั้งที่โหลดหน้า → ตรวจ SW ใหม่ทันที
(ปกติ browser เช็คแค่ทุก 24 ชม.)
3. ตรวจซ้ำทุก 30 นาที → เหมาะกับจอแสดงข้อมูลที่เปิดทิ้งไว้
4. SW ใหม่ skipWaiting+claim → controllerchange → reload เงียบ ๆ
============================================================ */
(function () {
if (!("serviceWorker" in navigator)) return;
var CHECK_INTERVAL_MS = 30 * 60 * 1000; // ตรวจอัปเดตทุก 30 นาที
/* ---- ปุ่มติดตั้งแอป (ก่อน install prompt) ---- */
var deferredPrompt = null;
var installBtn = null;
function makeInstallBtn() {
var btn = document.createElement("button");
btn.type = "button";
btn.className = "mu-install-btn";
btn.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5"/><path d="M12 15V3"/></svg> ' + ((window.I18N && I18N.t) ? I18N.t('pwa.install') : 'ติดตั้งแอป');
btn.addEventListener("click", function () {
if (!deferredPrompt) return;
deferredPrompt.prompt();
deferredPrompt.userChoice.then(function () {
deferredPrompt = null;
if (installBtn) installBtn.style.display = "none";
});
});
document.body.appendChild(btn);
/* เปลี่ยนภาษาแล้ว → อัปเดตข้อความบนปุ่ม */
window.addEventListener('langchange', function () {
if (!installBtn) return;
var svg = installBtn.querySelector('svg');
installBtn.textContent = '';
if (svg) installBtn.appendChild(svg);
installBtn.appendChild(document.createTextNode(
(window.I18N && I18N.t) ? I18N.t('pwa.install') : 'ติดตั้งแอป'));
});
return btn;
}
window.addEventListener("beforeinstallprompt", function (e) {
e.preventDefault();
deferredPrompt = e;
if (!installBtn) installBtn = makeInstallBtn();
installBtn.style.display = "inline-flex";
});
window.addEventListener("appinstalled", function () {
if (installBtn) installBtn.style.display = "none";
});
window.addEventListener("load", function () {
navigator.serviceWorker
.register("./sw.js", { updateViaCache: "none" })
.then(function (reg) {
// ตรวจหา SW เวอร์ชันใหม่ทุกครั้งที่โหลดหน้า
reg.update();
// ตรวจซ้ำเป็นระยะ (กันจอที่เปิดค้างไม่เคยโหลดหน้าใหม่)
setInterval(function () {
reg.update();
}, CHECK_INTERVAL_MS);
// SW ใหม่接管การควบคุมแล้ว → รีโหลดหน้ารับเวอร์ชันใหม่ทันที
navigator.serviceWorker.addEventListener("controllerchange", function () {
window.location.reload();
});
})
.catch(function () {
/* ไม่มี SW = ไม่มีโหมดออฟไลน์ — เว็บยังใช้งานปกติ */
});
});
})();