modify landing page
This commit is contained in:
commit
dd232b1c02
2599 files changed
+226992
No files matched your search
+155
@@ -0,0 +1,155 @@
|
||||
/* pwa.js — ลงทะเบียน SW + อัปเดตอัตโนมัติ (ผู้ใช้ไม่ต้องล้างแคช) + ปุ่มติดตั้ง + สถานะออนไลน์
|
||||
กลไก:
|
||||
1) register ด้วย updateViaCache:'none' → ไม่เชื่อ HTTP cache ของ sw.js
|
||||
2) reg.update() ทุกครั้งที่โหลดหน้า + ทุก 30 นาที
|
||||
3) controllerchange → reload เงียบ ๆ ครั้งเดียวต่อเวอร์ชัน (กัน reload วน)
|
||||
4) เทียบ app/version.json กับ build ที่ฝังมา → reload เมื่อมี build ใหม่
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var BAKED_VERSION = window.FARM_VERSION || '';
|
||||
var CHECK_INTERVAL_MS = 30 * 60 * 1000;
|
||||
var VERSION_POLL_MS = 5 * 60 * 1000;
|
||||
|
||||
/* ---------- ป้าย build ใน footer ---------- */
|
||||
var buildEl = document.getElementById('build-version');
|
||||
if (buildEl) buildEl.textContent = BAKED_VERSION || 'dev';
|
||||
|
||||
/* ---------- สถานะออนไลน์/ออฟไลน์ ---------- */
|
||||
var netBadge = document.getElementById('net-badge');
|
||||
function paintNet() {
|
||||
if (!netBadge) return;
|
||||
var online = navigator.onLine;
|
||||
netBadge.textContent = online ? 'ออนไลน์' : 'ออฟไลน์ — ใช้ข้อมูลที่บันทึกไว้';
|
||||
netBadge.classList.toggle('badge-net--off', !online);
|
||||
}
|
||||
window.addEventListener('online', paintNet);
|
||||
window.addEventListener('offline', paintNet);
|
||||
paintNet();
|
||||
|
||||
/* ---------- ปุ่มกลับขึ้นด้านบน (custom-style เดิมใช้ #myBtn) ---------- */
|
||||
var topBtn = document.getElementById('myBtn');
|
||||
if (topBtn) {
|
||||
topBtn.addEventListener('click', function () {
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
});
|
||||
window.addEventListener('scroll', function () {
|
||||
topBtn.style.display = window.scrollY > 20 ? 'block' : 'none';
|
||||
});
|
||||
topBtn.style.display = 'none';
|
||||
}
|
||||
|
||||
/* ---------- reload ครั้งเดียวต่อเวอร์ชัน ---------- */
|
||||
function reloadFor(key, guardMs) {
|
||||
var now = Date.now();
|
||||
var last = 0;
|
||||
try {
|
||||
last = Number(sessionStorage.getItem(key) || 0);
|
||||
} catch (error) {
|
||||
last = 0;
|
||||
}
|
||||
/* กัน reload วน: ถ้าเพิ่ง reload ไปไม่นาน ให้ข้าม */
|
||||
if (guardMs && now - last < guardMs) return;
|
||||
try {
|
||||
sessionStorage.setItem(key, String(now));
|
||||
} catch (error) {
|
||||
/* sessionStorage ปิดอยู่ → ยอมให้ reload รอบเดียว */
|
||||
}
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
/* ---------- service worker ---------- */
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', function () {
|
||||
navigator.serviceWorker
|
||||
.register('./sw.js', { updateViaCache: 'none' })
|
||||
.then(function (registration) {
|
||||
registration.update();
|
||||
setInterval(function () {
|
||||
registration.update();
|
||||
}, CHECK_INTERVAL_MS);
|
||||
|
||||
/* SW ใหม่อาจปรากฏหลัง register() resolve — ดักที่ updatefound ด้วย
|
||||
ไม่งั้นจะค้างสถานะ waiting จนกว่าจะปิดแท็บทั้งหมด */
|
||||
registration.addEventListener('updatefound', function () {
|
||||
var incoming = registration.installing;
|
||||
if (!incoming) return;
|
||||
incoming.addEventListener('statechange', function () {
|
||||
if (incoming.state === 'installed' && navigator.serviceWorker.controller) {
|
||||
incoming.postMessage('SKIP_WAITING');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
navigator.serviceWorker.addEventListener('controllerchange', function () {
|
||||
/* SW ใหม่เข้าควบคุม → โหลดเอกสารใหม่ทันที (กัน reload วนด้วย guard 8 วินาที) */
|
||||
reloadFor('fp_reload_at', 8000);
|
||||
});
|
||||
|
||||
if (registration.waiting) {
|
||||
registration.waiting.postMessage('SKIP_WAITING');
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
/* ไม่มี SW = ไม่มีโหมดออฟไลน์ — ตัวเว็บยังทำงานปกติ */
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------- ตรวจ version.json (จับกรณีเอกสารเก่าค้างใน cache) ---------- */
|
||||
function checkVersion() {
|
||||
fetch('app/version.json', { cache: 'no-store' })
|
||||
.then(function (response) {
|
||||
return response.ok ? response.json() : null;
|
||||
})
|
||||
.then(function (data) {
|
||||
if (!data || !data.version || !BAKED_VERSION) return;
|
||||
if (data.version !== BAKED_VERSION) reloadFor('fp_reload_ver_' + data.version, 0);
|
||||
})
|
||||
.catch(function () {
|
||||
/* ออฟไลน์ — เทียบไม่ได้ ไม่เป็นไร */
|
||||
});
|
||||
}
|
||||
window.addEventListener('load', checkVersion);
|
||||
document.addEventListener('visibilitychange', function () {
|
||||
if (document.visibilityState === 'visible') checkVersion();
|
||||
});
|
||||
setInterval(checkVersion, VERSION_POLL_MS);
|
||||
|
||||
/* ---------- ปุ่มติดตั้งแอป ---------- */
|
||||
var installPrompt = null;
|
||||
var installWrap = document.getElementById('install-wrap');
|
||||
var installBtn = document.getElementById('install-btn');
|
||||
|
||||
window.addEventListener('beforeinstallprompt', function (event) {
|
||||
event.preventDefault();
|
||||
installPrompt = event;
|
||||
if (installWrap) installWrap.hidden = false;
|
||||
});
|
||||
|
||||
if (installBtn) {
|
||||
installBtn.addEventListener('click', function () {
|
||||
if (!installPrompt) return;
|
||||
installPrompt.prompt();
|
||||
installPrompt.userChoice.then(function () {
|
||||
installPrompt = null;
|
||||
if (installWrap) installWrap.hidden = true;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener('appinstalled', function () {
|
||||
installPrompt = null;
|
||||
if (installWrap) installWrap.hidden = true;
|
||||
});
|
||||
|
||||
/* บอกสาเหตุตรง ๆ ถ้าเปิดผ่าน http://<lan-ip> ซึ่งไม่ใช่ secure context */
|
||||
if (!window.isSecureContext && installWrap) {
|
||||
installWrap.hidden = false;
|
||||
if (installBtn) {
|
||||
installBtn.disabled = true;
|
||||
installBtn.textContent = 'ติดตั้งได้เฉพาะ https หรือ localhost';
|
||||
}
|
||||
}
|
||||
})();
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/* router.js — hash router ของ SPA (vanilla, ไม่มี build)
|
||||
กติกา:
|
||||
- ลิงก์ภายในทุกจุดเป็น "#/..." เท่านั้น (ห้าม href="./x.html" เพราะจะโหลดหน้าใหม่)
|
||||
- partial แต่ละไฟล์ใส่ <style>/<script> ของตัวเองได้ · innerHTML ไม่รัน <script>
|
||||
จึงต้องสร้าง script node ใหม่ (และคง type="module" ไว้)
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var VIEWS = {
|
||||
home: { partial: 'partials/home.html', title: 'หน้าหลัก' },
|
||||
'crop-water': { partial: 'partials/crop-water.html', title: 'น้ำที่พืชต้องการ' },
|
||||
pond: { partial: 'partials/pond.html', title: 'ขนาดสระน้ำ' },
|
||||
irrigation: { partial: 'partials/irrigation.html', title: 'ระบบให้น้ำ' },
|
||||
glossary: { partial: 'partials/glossary.html', title: 'คำศัพท์' },
|
||||
contact: { partial: 'partials/contact.html', title: 'ติดต่อ' },
|
||||
credits: { partial: 'partials/credits.html', title: 'เครดิตและลิขสิทธิ์' },
|
||||
offline: { partial: 'partials/offline.html', title: 'ออฟไลน์' }
|
||||
};
|
||||
|
||||
var APP_NAME = 'farm-planning';
|
||||
var viewEl = document.getElementById('view');
|
||||
|
||||
function parseHash() {
|
||||
var raw = window.location.hash.replace(/^#/, '');
|
||||
if (raw === '' || raw === '/') return { name: 'home', query: {} };
|
||||
|
||||
var query = {};
|
||||
var qIndex = raw.indexOf('?');
|
||||
var path = qIndex === -1 ? raw : raw.slice(0, qIndex);
|
||||
if (qIndex !== -1) {
|
||||
new URLSearchParams(raw.slice(qIndex + 1)).forEach(function (value, key) {
|
||||
query[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
var segments = path.split('/').filter(Boolean);
|
||||
var name = segments[0] || 'home';
|
||||
return { name: VIEWS[name] ? name : 'home', query: query };
|
||||
}
|
||||
|
||||
/* innerHTML ไม่รัน <script> — สร้างใหม่เพื่อให้สคริปต์ของ view ทำงาน */
|
||||
function execScripts(container) {
|
||||
Array.prototype.forEach.call(container.querySelectorAll('script'), function (old) {
|
||||
var script = document.createElement('script');
|
||||
if (old.type) script.type = old.type;
|
||||
if (old.src) script.src = old.src;
|
||||
else script.textContent = old.textContent;
|
||||
old.parentNode.replaceChild(script, old);
|
||||
});
|
||||
}
|
||||
|
||||
function setNavActive(name) {
|
||||
Array.prototype.forEach.call(document.querySelectorAll('[data-nav]'), function (link) {
|
||||
var active = link.getAttribute('data-nav') === name;
|
||||
link.classList.toggle('active', active);
|
||||
if (active) link.setAttribute('aria-current', 'page');
|
||||
else link.removeAttribute('aria-current');
|
||||
});
|
||||
}
|
||||
|
||||
function closeMobileMenu() {
|
||||
var menu = document.getElementById('navbarResponsive');
|
||||
if (menu && menu.classList.contains('show') && window.jQuery) {
|
||||
window.jQuery(menu).collapse('hide');
|
||||
}
|
||||
}
|
||||
|
||||
function showError() {
|
||||
viewEl.innerHTML =
|
||||
'<div class="container py-5 text-center">' +
|
||||
'<h2>เปิดหน้านี้ไม่ได้</h2>' +
|
||||
'<p class="text-muted">ถ้าอยู่ระหว่างออฟไลน์ ให้เปิดหน้าที่เคยเข้าอย่างน้อยหนึ่งครั้งก่อน</p>' +
|
||||
'<a class="btn btn-primary" href="#/home">กลับหน้าหลัก</a>' +
|
||||
'</div>';
|
||||
}
|
||||
|
||||
function render() {
|
||||
var route = parseHash();
|
||||
var view = VIEWS[route.name];
|
||||
window.__route = route;
|
||||
|
||||
fetch(view.partial, { cache: 'no-store' })
|
||||
.then(function (response) {
|
||||
if (!response.ok) throw new Error('HTTP ' + response.status);
|
||||
return response.text();
|
||||
})
|
||||
.then(function (html) {
|
||||
viewEl.innerHTML = html;
|
||||
execScripts(viewEl);
|
||||
setNavActive(route.name);
|
||||
document.title = APP_NAME + ' — ' + view.title;
|
||||
window.scrollTo(0, 0);
|
||||
closeMobileMenu();
|
||||
})
|
||||
.catch(showError);
|
||||
}
|
||||
|
||||
window.addEventListener('hashchange', render);
|
||||
document.addEventListener('DOMContentLoaded', render);
|
||||
})();
|
||||
@@ -0,0 +1,4 @@
|
||||
/* version.js — ค่า build เดียวของแอป
|
||||
ต้องตรงกับ VERSION ใน sw.js และ app/version.json
|
||||
(แก้ที่ scripts/bump-version.mjs เท่านั้น เพื่อไม่ให้สามที่นี้เพี้ยนกัน) */
|
||||
window.FARM_VERSION = "2026.11.16.7";
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"app": "farm-planning",
|
||||
"version": "2026.11.16.7",
|
||||
"builtAt": "2026-11-15T18:30:53.522Z"
|
||||
}
|
||||
Reference in new issue
Block a user