+
+
${index + 1}
+
${escapeHtml(project.project_name || "ไม่มีชื่อโปรเจค")}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
`;
+ }
+
+ /* -------------------------------------------------------------- state */
+ function markDirty() {
+ saveDraft();
+ refreshDirty();
+ }
+
+ function refreshDirty() {
+ if (!dirtyBar) return;
+ dirtyBar.hidden = !isDirty();
+ const publishBtn = dirtyBar.querySelector('[data-act="publish"]');
+ if (publishBtn) publishBtn.disabled = !apiState.available;
+ }
+
+ async function checkApi() {
+ apiState = await probePublishApi();
+ if (!statusEl) return;
+ const textEl = statusEl.querySelector("[data-status-text]");
+ const extraEl = statusEl.querySelector("[data-status-extra]");
+ const hintEl = root.querySelector("[data-publish-hint]");
+ if (apiState.available) {
+ statusEl.dataset.state = "ok";
+ textEl.textContent = "พร้อมเผยแพร่ผ่านเซิร์ฟเวอร์";
+ extraEl.textContent = apiState.writable
+ ? "บันทึกลง data/projects.json ได้ทันที"
+ : "อ่านได้ แต่เขียนไม่ได้ (ตรวจรหัสเผยแพร่)";
+ hintEl.innerHTML =
+ "กด “เผยแพร่” แล้วไฟล์ รายการที่แก้ไว้แต่ยังไม่เผยแพร่จะหายไป การกระทำนี้ย้อนกลับไม่ได้
+ `;
+ document.body.appendChild(dialog);
+ dialog.addEventListener("click", (event) => {
+ if (event.target.closest("[data-close]")) dialog.close();
+ if (event.target.closest("[data-confirm]")) {
+ clearDraft();
+ draft = normalizeData(published);
+ mount(root);
+ toast("ทิ้งการแก้ไขแล้ว");
+ dialog.close();
+ }
+ });
+ dialog.addEventListener("close", () => dialog.remove());
+ dialog.showModal();
+ }
+
+ function askRemove(index) {
+ const project = draft.projects[index];
+ const dialog = document.createElement("dialog");
+ dialog.className = "dialog";
+ dialog.innerHTML = `
+ “${escapeHtml(
+ project.project_name || "ไม่มีชื่อโปรเจค",
+ )}” จะถูกเอาออกจากรายการ (ยังไม่ถึงเว็บจนกว่าจะเผยแพร่)
+ `;
+ document.body.appendChild(dialog);
+ dialog.addEventListener("click", (event) => {
+ if (event.target.closest("[data-close]")) dialog.close();
+ if (event.target.closest("[data-confirm]")) {
+ draft.projects.splice(index, 1);
+ renderList();
+ markDirty();
+ toast("ลบโปรเจคออกจากรายการแล้ว");
+ dialog.close();
+ }
+ });
+ dialog.addEventListener("close", () => dialog.remove());
+ dialog.showModal();
+ }
+
+ async function doPublish(trigger) {
+ if (!validateAll()) {
+ toast("ยังมีช่องที่ต้องแก้ก่อนเผยแพร่", "error");
+ const firstError = listEl.querySelector(".field--error input, .field--error textarea");
+ firstError?.focus();
+ return;
+ }
+ const emailValid = !draft.site.contact_email.trim() || isEmail(draft.site.contact_email);
+ if (!emailValid) {
+ toast("อีเมลติดต่อของเว็บไม่ถูกต้อง", "error");
+ root.querySelector("#site-email")?.focus();
+ return;
+ }
+ const label = trigger.querySelector("span");
+ const original = label ? label.textContent : "";
+ if (label) label.textContent = "กำลังเผยแพร่…";
+ trigger.disabled = true;
+ try {
+ const result = await publishProjects(draft);
+ clearDraft();
+ published = normalizeData({ ...draft, updated_at: result.updated_at || "" });
+ draft = normalizeData(published);
+ ctx.setData(published);
+ refreshDirty();
+ toast("เผยแพร่แล้ว — หน้าเว็บอัปเดตเรียบร้อย");
+ } catch (error) {
+ toast(`เผยแพร่ไม่สำเร็จ: ${error.message}`, "error");
+ } finally {
+ trigger.disabled = false;
+ if (label) label.textContent = original;
+ refreshDirty();
+ }
+ }
+
+ return {
+ mount,
+ destroy() {
+ root = null;
+ listEl = null;
+ dirtyBar = null;
+ statusEl = null;
+ draft = null;
+ published = null;
+ },
+ };
+}
diff --git a/ui/views/home.js b/ui/views/home.js
new file mode 100644
index 0000000..cdfb33c
--- /dev/null
+++ b/ui/views/home.js
@@ -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
+ ? `
+
${icon(isSearch ? "search" : "folder")}
+
${isSearch ? "ไม่พบโปรเจคที่ค้นหา" : "ยังไม่มีโปรเจคในรายการ"}
+
${
+ isSearch
+ ? "ลองพิมพ์คำอื่น หรือล้างคำค้นหาเพื่อดูทั้งหมด"
+ : "เข้าไปเพิ่มโปรเจคได้ที่หน้า “จัดการข้อมูล”"
+ }
+
`;
+ 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 = `
+