- 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.
126 lines
5.8 KiB
JavaScript
126 lines
5.8 KiB
JavaScript
/* popular.js — อัลกอริทึมนับ "สายที่เปิดดูบ่อย" สำหรับ footer (ฝั่งเบราว์เซอร์ ไม่มี backend)
|
||
|
||
กลไก
|
||
• จับการเปิดดูสาย: Popular.hit(route_id) — เรียกจาก busDetail.init.js / stopDetail / รายการสาย
|
||
• เก็บใน localStorage: { "102": [hits, lastTs], ... } (id → [จำนวนครั้ง, เวลาล่าสุด])
|
||
• คะแนนไม่เฟ้อ: score = hits × 0.5 ^ (อายุวัน / HALF_LIFE) ← ครึ่งชีวิต 30 วัน
|
||
สายที่ไม่มีคนเปิดนาน ๆ จะค่อย ๆ ตกลงเองโดยไม่ต้องลบข้อมูล
|
||
• แสดง 4 อันดับใน footer · ไม่มีข้อมูลพอ → ใช้ DEFAULT_IDS (ของเดิม) เป็น fallback
|
||
• ประหยัดพื้นที่: เก็บแค่ 2 ตัวเลข/สาย · ทิ้งข้อมูลที่ตายเกิน PRUNE_DAYS วัน
|
||
|
||
ไม่มี dependency — โหลดก่อน router.js (หลัง i18n.js เพื่อให้ข้อความถูกภาษา)
|
||
*/
|
||
(function () {
|
||
'use strict';
|
||
|
||
var KEY = 'at_route_hits'; /* คีย์ใน localStorage */
|
||
var HALF_LIFE = 30; /* วัน — คะแนนลดครึ่งเมื่อไม่ถูกเปิดนาน 30 วัน */
|
||
var PRUNE_DAYS = 180; /* ทิ้งข้อมูลที่ไม่ถูกเปิดเกิน 180 วัน (กันบวม) */
|
||
var TOP_N = 4; /* จำนวนที่แสดงใน footer */
|
||
var DAY_MS = 86400000;
|
||
|
||
/* ค่าเริ่มต้น (ของเดิมใน footer) — ใช้เมื่อยังไม่มีข้อมูลจริง หรือมีไม่ครบ TOP_N */
|
||
var DEFAULT_IDS = ['101', '902', '15', '464'];
|
||
|
||
function now() { return Date.now(); }
|
||
|
||
/* ── อ่าน/เขียน storage (ทน localStorage ใช้ไม่ได้ เช่น โหมดส่วนตัว) ── */
|
||
function read() {
|
||
try {
|
||
var raw = localStorage.getItem(KEY);
|
||
if (!raw) return {};
|
||
var d = JSON.parse(raw);
|
||
return (d && typeof d === 'object') ? d : {};
|
||
} catch (e) { return {}; }
|
||
}
|
||
|
||
function write(d) {
|
||
try { localStorage.setItem(KEY, JSON.stringify(d)); } catch (e) { /* เงียบ */ }
|
||
}
|
||
|
||
/* คะแนนของรายการเดียว: hits × 0.5^(อายุวัน/ครึ่งชีวิต) */
|
||
function score(entry, ts) {
|
||
if (!entry || !entry.length) return 0;
|
||
var hits = Number(entry[0]) || 0;
|
||
var last = Number(entry[1]) || 0;
|
||
if (hits <= 0 || last <= 0) return 0;
|
||
var ageDays = Math.max(0, (ts - last) / DAY_MS);
|
||
return hits * Math.pow(0.5, ageDays / HALF_LIFE);
|
||
}
|
||
|
||
/* ตัดข้อมูลที่ตายเกิน PRUNE_DAYS ทิ้ง (คืนค่า true ถ้ามีการแก้) */
|
||
function prune(d, ts) {
|
||
var cut = PRUNE_DAYS * DAY_MS;
|
||
var changed = false;
|
||
Object.keys(d).forEach(function (id) {
|
||
var e = d[id];
|
||
var last = (e && Number(e[1])) || 0;
|
||
if (!last || (ts - last) > cut) { delete d[id]; changed = true; }
|
||
});
|
||
return changed;
|
||
}
|
||
|
||
var Popular = {
|
||
/* นับการเปิดดูสาย 1 ครั้ง */
|
||
hit: function (routeId) {
|
||
var id = String(routeId == null ? '' : routeId).trim();
|
||
if (!id) return;
|
||
var ts = now();
|
||
var d = read();
|
||
var e = d[id];
|
||
d[id] = [(e && Number(e[0]) || 0) + 1, ts];
|
||
prune(d, ts);
|
||
write(d);
|
||
},
|
||
|
||
/* อันดับ id เรียงจากคะแนนมาก→น้อย · ไม่มีข้อมูล → DEFAULT_IDS */
|
||
ranked: function (n) {
|
||
var ts = now();
|
||
var d = read();
|
||
if (prune(d, ts)) write(d);
|
||
|
||
var rows = Object.keys(d).map(function (id) {
|
||
return { id: id, score: score(d[id], ts) };
|
||
}).filter(function (r) { return r.score > 0; });
|
||
|
||
rows.sort(function (a, b) { return b.score - a.score || (a.id < b.id ? -1 : 1); });
|
||
|
||
var top = rows.slice(0, n || TOP_N).map(function (r) { return r.id; });
|
||
/* เติมด้วยค่าเริ่มต้นถ้ายังไม่ครบ (ไม่ให้ footer ว่าง) */
|
||
DEFAULT_IDS.forEach(function (id) {
|
||
if (top.length < (n || TOP_N) && top.indexOf(id) < 0) top.push(id);
|
||
});
|
||
return top.slice(0, n || TOP_N);
|
||
},
|
||
|
||
/* เรนเดอร์รายการใน footer (เรียกหลัง i18n พร้อม — ชื่อสายตามภาษา) */
|
||
mount: function (ulId, routes) {
|
||
var ul = document.getElementById(ulId || 'footer-popular-list');
|
||
if (!ul || !routes || !routes.length) return false;
|
||
|
||
var byId = {};
|
||
routes.forEach(function (r) { byId[String(r.route_id)] = r; });
|
||
|
||
var ids = Popular.ranked(TOP_N);
|
||
ul.innerHTML = '';
|
||
ids.forEach(function (id) {
|
||
var r = byId[id];
|
||
if (!r) return;
|
||
var li = document.createElement('li');
|
||
var a = document.createElement('a');
|
||
a.href = '#/routes/' + id;
|
||
a.textContent = (window.I18N ? I18N.t('common.routePrefix', { n: id }) : ('สาย ' + id)) + (r.route_name_th ? ' · ' + r.route_name_th : '');
|
||
li.appendChild(a);
|
||
ul.appendChild(li);
|
||
});
|
||
return ul.children.length > 0;
|
||
},
|
||
|
||
/* สำหรับทดสอบ/debug */
|
||
_score: score,
|
||
_key: KEY
|
||
};
|
||
|
||
window.Popular = Popular;
|
||
})();
|