feat: hobby project landing page (PWA + SPA) with admin backoffice
- landing page: welcome hero, live search, project card grid (10 projects from owner list) - warm brown/white theme (fastwork-inspired layout), embedded K2D Thai font - PWA: manifest, PIL-generated icons, versioned service worker with auto-update - backoffice at #/admin: edit name/description/url, reorder, delete, publish - server/publish.mjs: zero-dependency static server + PUT api/projects with validation, JSON write, git commit and push; loopback-only unless ADMIN_TOKEN - docs: README, DESIGN.md (color/type contract), AGENTS.md
This commit is contained in:
1 parent
e55b193644
commit
c00bc62272
35 files changed
+3432
-2
No files matched your search
@@ -0,0 +1,170 @@
|
||||
/* views/home.js — หน้าแรก: ข้อความต้อนรับ + ค้นหา + การ์ดโปรเจค */
|
||||
|
||||
import { escapeHtml, hostOf, icon, normalizeUrl } from "../ui.js";
|
||||
|
||||
export function homeView(ctx) {
|
||||
let root = null;
|
||||
let query = "";
|
||||
let inputEl = null;
|
||||
let gridEl = null;
|
||||
let countEl = null;
|
||||
|
||||
function filtered() {
|
||||
const items = ctx.data.projects;
|
||||
const q = query.trim().toLowerCase();
|
||||
if (!q) return items;
|
||||
return items.filter((p) => {
|
||||
const haystack = [p.project_name, p.project_description, hostOf(p.project_url), p.project_url]
|
||||
.join(" ")
|
||||
.toLowerCase();
|
||||
return haystack.includes(q);
|
||||
});
|
||||
}
|
||||
|
||||
function cardHtml(project) {
|
||||
const url = normalizeUrl(project.project_url);
|
||||
const host = hostOf(project.project_url);
|
||||
const name = escapeHtml(project.project_name || "ไม่มีชื่อโปรเจค");
|
||||
const desc = escapeHtml(project.project_description || "ยังไม่มีคำอธิบาย");
|
||||
const nameHtml = url
|
||||
? `<a href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">${name}</a>`
|
||||
: `<span>${name}</span>`;
|
||||
return `
|
||||
<article class="project-card">
|
||||
<div class="project-card__top">
|
||||
<span class="project-card__icon" aria-hidden="true">${icon(project.icon)}</span>
|
||||
<div class="project-card__heading">
|
||||
<h3 class="project-card__name">${nameHtml}</h3>
|
||||
<p class="project-card__desc">${desc}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="project-card__foot">
|
||||
<span class="project-card__host">${escapeHtml(host || "ยังไม่ใส่ลิงก์")}</span>
|
||||
<span class="project-card__go">เปิดเว็บไซต์ ${icon("arrowRight")}</span>
|
||||
</div>
|
||||
</article>`;
|
||||
}
|
||||
|
||||
function renderGrid() {
|
||||
if (!gridEl) return;
|
||||
const items = filtered();
|
||||
if (countEl) {
|
||||
countEl.textContent =
|
||||
query.trim().length > 0
|
||||
? `พบ ${items.length} จาก ${ctx.data.projects.length} โปรเจค`
|
||||
: `${ctx.data.projects.length} โปรเจค`;
|
||||
}
|
||||
if (items.length === 0) {
|
||||
const isSearch = query.trim().length > 0;
|
||||
gridEl.className = "";
|
||||
gridEl.innerHTML = `
|
||||
<div class="empty">
|
||||
<span class="empty__icon" aria-hidden="true">${icon(isSearch ? "search" : "folder")}</span>
|
||||
<p class="empty__title">${isSearch ? "ไม่พบโปรเจคที่ค้นหา" : "ยังไม่มีโปรเจคในรายการ"}</p>
|
||||
<p class="empty__text">${
|
||||
isSearch
|
||||
? "ลองพิมพ์คำอื่น หรือล้างคำค้นหาเพื่อดูทั้งหมด"
|
||||
: "เข้าไปเพิ่มโปรเจคได้ที่หน้า “จัดการข้อมูล”"
|
||||
}</p>
|
||||
</div>`;
|
||||
return;
|
||||
}
|
||||
gridEl.className = "project-grid";
|
||||
gridEl.innerHTML = items.map(cardHtml).join("");
|
||||
}
|
||||
|
||||
return {
|
||||
mount(container) {
|
||||
root = container;
|
||||
const site = ctx.data.site;
|
||||
const total = ctx.data.projects.length;
|
||||
const withEmail = ctx.data.projects.filter((p) => p.contact_email).length;
|
||||
|
||||
root.innerHTML = `
|
||||
<div class="wrap">
|
||||
<section class="hero" aria-labelledby="hero-title">
|
||||
<div class="hero__inner">
|
||||
<p class="hero__eyebrow">${icon("spark")}<span>${escapeHtml(
|
||||
site.site_title,
|
||||
)}</span></p>
|
||||
<h1 class="hero__title" id="hero-title">${escapeHtml(
|
||||
site.welcome_message,
|
||||
)}</h1>
|
||||
<p class="hero__lead">รวมเว็บที่ทำเองไว้ในที่เดียว เลือกดูได้ตามใจ ไม่มีโฆษณา ไม่มีล็อกอิน</p>
|
||||
<form class="searchbar" role="search" autocomplete="off">
|
||||
<span class="searchbar__icon" aria-hidden="true">${icon("search")}</span>
|
||||
<label class="visually-hidden" for="project-search">ค้นหาโปรเจค</label>
|
||||
<input id="project-search" type="search" name="q"
|
||||
placeholder="ค้นหาชื่อโปรเจค หรือคำอธิบาย" enterkeyhint="search" />
|
||||
<button class="searchbar__clear" type="button" aria-label="ล้างคำค้นหา" hidden>
|
||||
${icon("close")}
|
||||
</button>
|
||||
</form>
|
||||
<div class="hero__meta">
|
||||
<span>${icon("folder")}${total} โปรเจค</span>
|
||||
<span>${icon("shield")}${escapeHtml(site.ssl_note)}</span>
|
||||
${
|
||||
withEmail
|
||||
? `<span>${icon("list")}${withEmail} โปรเจคมีอีเมลติดต่อ</span>`
|
||||
: ""
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="hero__deco" aria-hidden="true">
|
||||
${ctx.data.projects
|
||||
.slice(0, 3)
|
||||
.map(
|
||||
(project, index) => `
|
||||
<div class="hero__deco-card" style="--i:${index}">
|
||||
<span class="hero__deco-icon">${icon(project.icon)}</span>
|
||||
<span class="hero__deco-name">${escapeHtml(project.project_name)}</span>
|
||||
</div>`,
|
||||
)
|
||||
.join("")}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="section-head">
|
||||
<div>
|
||||
<h2 class="section-head__title" id="projects-title">รายชื่อโปรเจค</h2>
|
||||
<p class="section-head__sub">กดที่การ์ดเพื่อเปิดเว็บนั้นในแท็บใหม่</p>
|
||||
</div>
|
||||
<p class="section-head__count" data-count></p>
|
||||
</div>
|
||||
|
||||
<div data-grid aria-labelledby="projects-title"></div>
|
||||
</div>`;
|
||||
|
||||
gridEl = root.querySelector("[data-grid]");
|
||||
countEl = root.querySelector("[data-count]");
|
||||
inputEl = root.querySelector("#project-search");
|
||||
const clearBtn = root.querySelector(".searchbar__clear");
|
||||
const form = root.querySelector(".searchbar");
|
||||
|
||||
form.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
inputEl.blur();
|
||||
});
|
||||
inputEl.addEventListener("input", () => {
|
||||
query = inputEl.value;
|
||||
clearBtn.hidden = query.length === 0;
|
||||
renderGrid();
|
||||
});
|
||||
clearBtn.addEventListener("click", () => {
|
||||
inputEl.value = "";
|
||||
query = "";
|
||||
clearBtn.hidden = true;
|
||||
inputEl.focus();
|
||||
renderGrid();
|
||||
});
|
||||
|
||||
renderGrid();
|
||||
},
|
||||
destroy() {
|
||||
root = null;
|
||||
inputEl = null;
|
||||
gridEl = null;
|
||||
countEl = null;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user