modify landing page
This commit is contained in:
commit
dd232b1c02
2599 files changed
+226992
No files matched your search
+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);
|
||||
})();
|
||||
Reference in new issue
Block a user