- sample 3 non-repeating projects from the full list with Fisher-Yates shuffle - order inside the stack is randomised too; stays decorative (aria-hidden) - docs: DESIGN.md notes the random sample
187 lines
7.8 KiB
JavaScript
187 lines
7.8 KiB
JavaScript
/* views/home.js — หน้าแรก: ข้อความต้อนรับ + ค้นหา + การ์ดโปรเจค */
|
|
|
|
import { escapeHtml, hostOf, icon, normalizeUrl } from "../ui.js";
|
|
import { initReveal, splitWordsHtml } from "../motion.js";
|
|
|
|
/** สุ่มลำดับด้วย Fisher–Yates แล้วหยิบมา count ใบ (ไม่ซ้ำ) */
|
|
function sampleRandom(list, count) {
|
|
const pool = [...list];
|
|
for (let i = pool.length - 1; i > 0; i -= 1) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[pool[i], pool[j]] = [pool[j], pool[i]];
|
|
}
|
|
return pool.slice(0, Math.max(0, count));
|
|
}
|
|
|
|
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, index = 0) {
|
|
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" data-reveal="pop" style="--fx-delay:${(index % 3) * 95}ms">
|
|
<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((project, index) => cardHtml(project, index)).join("");
|
|
// การ์ดชุดใหม่หลังกรองต้องถูก observe/เปิดให้เห็นด้วย ไม่งั้นจะค้างที่ opacity 0
|
|
initReveal(gridEl);
|
|
}
|
|
|
|
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__ambience" aria-hidden="true">
|
|
<span class="hero__orb hero__orb--a"></span>
|
|
<span class="hero__orb hero__orb--b"></span>
|
|
</div>
|
|
<div class="hero__inner">
|
|
<p class="hero__eyebrow">${icon("spark")}<span>${escapeHtml(
|
|
site.site_title,
|
|
)}</span></p>
|
|
<h1 class="hero__title" id="hero-title">${splitWordsHtml(
|
|
site.welcome_message,
|
|
escapeHtml,
|
|
)}</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")}<span data-countup="${total}">${total}</span> โปรเจค</span>
|
|
${
|
|
withEmail
|
|
? `<span>${icon("list")}${withEmail} โปรเจคมีอีเมลติดต่อ</span>`
|
|
: ""
|
|
}
|
|
</div>
|
|
</div>
|
|
<div class="hero__deco" aria-hidden="true">
|
|
${sampleRandom(ctx.data.projects, 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" data-reveal="fade">
|
|
<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;
|
|
},
|
|
};
|
|
}
|