Files
web-camera-category/app/js/ui.js
T
2026-10-24 17:32:25 +07:00

320 lines
18 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ── UI: markup + toast + bottom sheet ────────────────────────
import { humanDateTime } from "./naming.js";
import { formatExposure } from "./exif.js";
export const esc = (s) =>
String(s ?? "")
.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
.replace(/"/g, "&quot;").replace(/'/g, "&#39;");
export function fmtBytes(n) {
if (!Number.isFinite(n) || n <= 0) return "0 B";
if (n < 1024) return n + " B";
if (n < 1024 ** 2) return (n / 1024).toFixed(1) + " KB";
if (n < 1024 ** 3) return (n / 1024 ** 2).toFixed(1) + " MB";
return (n / 1024 ** 3).toFixed(2) + " GB";
}
const ICON = {
torch: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 2h6l-1 7h4l-7 13 1-8H8z"/></svg>`,
flip: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M23 4v6h-6M1 20v-6h6"/><path d="M3.5 9a9 9 0 0 1 14.9-3.4L23 10M1 14l4.6 4.4A9 9 0 0 0 20.5 15"/></svg>`,
grid: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M9 3v18M15 3v18M3 9h18M3 15h18"/></svg>`,
pencil: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"/><path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z"/></svg>`,
trash: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18M8 6V4h8v2M19 6l-1 14H6L5 6"/><path d="M10 11v6M14 11v6"/></svg>`,
zip: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 8v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h7"/><path d="M12 2v4M10 6h4M10 10h4M10 14h4"/></svg>`,
share: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 16V3"/><path d="M8 7l4-4 4 4"/><path d="M4 14v5a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-5"/></svg>`,
camera: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/><circle cx="12" cy="13" r="4"/></svg>`,
lock: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="10" width="16" height="11" rx="2"/><path d="M8 10V7a4 4 0 0 1 8 0v3"/></svg>`,
block: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="9"/><path d="M5.6 5.6l12.8 12.8"/></svg>`,
folder: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"/></svg>`,
image: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>`,
};
// ── Toast ──
let toastTimer = null;
export function toast(msg, ms = 2600) {
const el = document.getElementById("toast");
el.textContent = msg;
el.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => el.classList.remove("show"), ms);
}
// ── Bottom sheet ──
export function openSheet(html) {
const overlay = document.getElementById("sheet-overlay");
document.getElementById("sheet-content").innerHTML = html;
overlay.hidden = false;
overlay.setAttribute("aria-hidden", "false");
}
export function closeSheet() {
const overlay = document.getElementById("sheet-overlay");
overlay.hidden = true;
overlay.setAttribute("aria-hidden", "true");
document.getElementById("sheet-content").innerHTML = "";
}
export function isSheetOpen() {
return !document.getElementById("sheet-overlay").hidden;
}
// ── Camera ──
export function cameraShell() {
return `
<div id="camera-view">
<div class="stage">
<video id="video" autoplay playsinline muted></video>
<div class="grid-lines" id="grid-lines"></div>
<div class="cam-top">
<button class="cam-chip" type="button" data-action="toggle-grid" aria-label="เส้นแบ่ง 3 ส่วน">${ICON.grid}</button>
<button class="cam-chip" type="button" id="chip-torch" data-action="toggle-torch" aria-pressed="false" aria-label="ไฟแฟลช" hidden>${ICON.torch}</button>
<button class="cam-chip" type="button" data-action="switch-camera" aria-label="สลับกล้องหน้า/หลัง">${ICON.flip}</button>
</div>
</div>
<div class="cam-bottom">
<div class="zoom-pills" id="zoom-pills" hidden></div>
<div class="shutter-row">
<div class="shutter-side"><button class="thumb-btn" id="last-thumb" type="button" data-action="open-last" aria-label="รูปล่าสุด"></button></div>
<button id="shutter" type="button" data-action="shoot" aria-label="ถ่ายรูป"></button>
<div class="shutter-side"><span id="shot-count"></span></div>
</div>
</div>
<div id="cam-overlay"></div>
</div>`;
}
export function overlay(html) {
const el = document.getElementById("cam-overlay");
if (el) el.innerHTML = html;
}
export function hero(kind, { title, text = "", steps = [], action = null, note = "" }) {
const stepsHtml = steps.length
? `<ol>${steps.map((s) => `<li>${s}</li>`).join("")}</ol>`
: "";
const actionHtml = action
? `<button class="btn btn-primary" type="button" data-action="${action.act}">${action.label}</button>`
: "";
return `<div class="hero cover">
<div class="ico">${kind === "denied" ? ICON.block : kind === "insecure" ? ICON.lock : ICON.camera}</div>
<h2>${esc(title)}</h2>
${text ? `<p>${esc(text)}</p>` : ""}
${stepsHtml ? `<div class="notice" style="text-align:left">${stepsHtml}</div>` : ""}
${actionHtml}
${note ? `<p style="font-size:11px">${note}</p>` : ""}
</div>`;
}
export function syncCameraControls({ torchAvailable, torchOn, zoomRange, zoomValue, gridOn, count, thumbUrl, ready }) {
const chip = document.getElementById("chip-torch");
if (chip) {
chip.hidden = !torchAvailable;
chip.setAttribute("aria-pressed", torchOn ? "true" : "false");
}
const pills = document.getElementById("zoom-pills");
if (pills) {
const min = zoomRange ? Number(zoomRange.min) : 1;
const max = zoomRange ? Number(zoomRange.max) : 1;
if (zoomRange && max > min) {
const levels = [1, 2, 3].filter((z) => z >= min - 0.05 && z <= max + 0.05);
if (!levels.length) levels.push(min);
const current = zoomValue ?? min;
pills.hidden = false;
pills.innerHTML = levels
.map(
(z) =>
`<button class="zoom-pill ${Math.abs(current - z) < 0.25 ? "on" : ""}" type="button" data-action="zoom-set" data-zoom="${z}" aria-label="ซูม ${z} เท่า" aria-pressed="${Math.abs(current - z) < 0.25}">${z}×</button>`
)
.join("");
} else {
pills.hidden = true;
pills.innerHTML = "";
}
}
const grid = document.getElementById("grid-lines");
if (grid) grid.classList.toggle("on", !!gridOn);
const cnt = document.getElementById("shot-count");
if (cnt) cnt.textContent = count ? count + " รูป" : "";
const thumb = document.getElementById("last-thumb");
if (thumb && thumbUrl) thumb.innerHTML = `<img src="${thumbUrl}" alt="รูปล่าสุด">`;
const sh = document.getElementById("shutter");
if (sh) sh.disabled = !ready;
}
// ── Folders ──
export function renderFolders({ folders, counts, activeId, storage }) {
const total = counts.reduce((a, b) => a + b, 0);
let html = `<div class="section-head">
<div><h2>โฟลเดอร์</h2><div class="sub">${folders.length} โฟลเดอร์ · ${total} รูป</div></div>
<button class="btn btn-primary" type="button" data-action="new-folder">+ โฟลเดอร์ใหม่</button>
</div>`;
if (!folders.length) {
html += `<div class="empty"><span class="ico">${ICON.folder}</span>ยังไม่มีโฟลเดอร์</div>`;
} else {
html += `<div class="folder-list">`;
folders.forEach((f, i) => {
const active = f.id === activeId;
html += `<div class="folder-item ${active ? "active" : ""}" data-action="select-folder" data-id="${esc(f.id)}" role="button" tabindex="0" aria-label="โฟลเดอร์ ${esc(f.name)} · ${counts[i]} รูป">
<span class="f-ico" aria-hidden="true">${ICON.folder}</span>
<div class="f-main">
<div class="f-name">${esc(f.name)}</div>
<div class="f-meta">${counts[i]} รูป${active ? " · ใช้งานอยู่" : ""}</div>
</div>
<div class="f-actions">
<button class="icon-btn" type="button" data-action="rename-folder" data-id="${esc(f.id)}" aria-label="เปลี่ยนชื่อโฟลเดอร์">${ICON.pencil}</button>
<button class="icon-btn danger" type="button" data-action="delete-folder" data-id="${esc(f.id)}" aria-label="ลบโฟลเดอร์">${ICON.trash}</button>
</div>
</div>`;
});
html += `</div>`;
}
html += `<div class="storage-line">
พื้นที่เก็บในเครื่อง${storage ? ` · ใช้ ${fmtBytes(storage.usage)} จาก ${fmtBytes(storage.quota)}` : ""}
<br><button class="btn btn-ghost" type="button" style="min-height:34px;height:34px;font-size:11px;margin-top:8px" data-action="diagnostics">ข้อมูลอุปกรณ์และกล้อง</button>
<button class="btn btn-ghost" type="button" style="min-height:34px;height:34px;font-size:11px;margin-top:8px;margin-left:6px" data-action="show-qr">QR ลิงก์ทดสอบ</button>
</div>`;
return html;
}
// ── Gallery ──
export function renderGallery({ folder, images, thumbUrl }) {
const bytes = images.reduce((a, i) => a + (i.size || i.blob?.size || 0), 0);
let html = `<div class="gallery-view"><div class="section-head">
<div><h2>${esc(folder ? folder.name : "ยังไม่เลือกโฟลเดอร์")}</h2>
<div class="sub">${images.length} รูป · ${fmtBytes(bytes)}</div></div>
</div>`;
if (!folder) {
return html + `<div class="empty"><span class="ico">${ICON.folder}</span>เลือกโฟลเดอร์ก่อน</div></div>`;
}
if (!images.length) {
return html + `<div class="empty"><span class="ico">${ICON.image}</span>ยังไม่มีรูปในโฟลเดอร์นี้</div></div>`;
}
html += `<div class="gallery-grid">`;
images.forEach((img) => {
html += `<button class="gallery-item" type="button" data-action="open-photo" data-id="${esc(img.id)}">
<img src="${thumbUrl(img)}" alt="${esc(img.name || "")}" loading="lazy" decoding="async">
</button>`;
});
html += `</div>`;
html += `<div class="action-bar">
<button class="btn btn-ghost" type="button" data-action="zip-download">${ICON.zip} ZIP (${images.length})</button>
<button class="btn btn-primary" type="button" data-action="zip-share">${ICON.share} แชร์ ZIP</button>
</div>`;
return html;
}
// ── Photo sheet ──
export function photoSheet({ record, url, folderName }) {
const m = record.meta || {};
const rows = [
["ไฟล์", record.name || "-"],
["ขนาด", fmtBytes(record.size || record.blob?.size || 0)],
["ความละเอียด", m.width && m.height ? `${m.width} × ${m.height} (${(m.width * m.height / 1e6).toFixed(1)} MP)` : "—"],
["ถ่ายเมื่อ", humanDateTime(record.createdAt)],
["โฟลเดอร์", folderName || "-"],
["อุปกรณ์", m.device || "—"],
["กล้อง", m.facingLabel || "—"],
["ที่มาไฟล์", m.sourceLabel || "—"],
];
if (m.gps) rows.push(["พิกัด GPS", `${m.gps.lat.toFixed(6)}, ${m.gps.lng.toFixed(6)}${m.gps.accuracy ? ` (±${Math.round(m.gps.accuracy)} ม.)` : ""}`]);
if (m.exif && (m.exif.fNumber || m.exif.iso || m.exif.exposureTime)) rows.push(["ค่าแสง EXIF", formatExposure(m.exif)]);
if (m.exif?.model) {
const make = (m.exif.make || "").trim();
const model = m.exif.model.trim();
const label = make && !model.toLowerCase().startsWith(make.toLowerCase()) ? `${make} ${model}` : model;
rows.push(["รุ่นใน EXIF", label]);
}
if (m.exif?.orientation) rows.push(["Orientation", String(m.exif.orientation)]);
if (m.exif?.dateTimeOriginal) rows.push(["EXIF DateTimeOriginal", m.exif.dateTimeOriginal]);
const rowsHtml = rows
.map(([k, v]) => `<div class="row"><span class="k">${esc(k)}</span><span class="v">${esc(v)}</span></div>`)
.join("");
return `
<h3>${esc(record.name || "รูปภาพ")}</h3>
<div class="hint">${esc(humanDateTime(record.createdAt))}</div>
<img class="preview" src="${url}" alt="">
<div class="meta-table">${rowsHtml}</div>
<div class="sheet-actions" style="margin-top:14px">
<button class="btn btn-ghost" type="button" data-action="close-sheet">ปิด</button>
<button class="btn btn-primary" type="button" data-action="share-photo" data-id="${esc(record.id)}">${ICON.share} แชร์รูปนี้</button>
</div>
<div class="sheet-actions" style="margin-top:8px">
<button class="btn btn-danger btn-block" type="button" data-action="delete-photo" data-id="${esc(record.id)}">${ICON.trash} ลบรูปนี้</button>
</div>`;
}
// ── Folder sheet ──
export function folderSheet(mode, { id = "", name = "", suggested = "", count = 0 }) {
if (mode === "delete") {
return `<h3>ลบโฟลเดอร์นี้?</h3>
<div class="hint">"${esc(name)}" · ${count} รูป จะถูกลบทั้งหมดและกู้คืนไม่ได้</div>
<div class="sheet-actions">
<button class="btn btn-ghost" type="button" data-action="close-sheet">ยกเลิก</button>
<button class="btn btn-danger" type="button" data-action="confirm-delete-folder" data-id="${esc(id)}">ลบ</button>
</div>`;
}
const isNew = mode === "new";
return `<h3>${isNew ? "โฟลเดอร์ใหม่" : "เปลี่ยนชื่อโฟลเดอร์"}</h3>
<div class="hint">ชื่อเริ่มต้นอิงวันที่ไทย (พ.ศ.) ของวันนี้ — แก้ไขได้</div>
<input id="folder-name-input" type="text" inputmode="text" autocomplete="off"
value="${esc(isNew ? suggested : name)}" placeholder="Site A 2569-10-24">
${isNew ? `<div class="hint" style="margin-top:-6px">ถ้าชื่อซ้ำ ระบบจะเติม (2) ให้อัตโนมัติ</div>` : ""}
<div class="sheet-actions">
<button class="btn btn-ghost" type="button" data-action="close-sheet">ยกเลิก</button>
<button class="btn btn-primary" type="button" data-action="${isNew ? "confirm-new-folder" : "confirm-rename-folder"}" data-id="${esc(id)}">
${isNew ? "สร้าง" : "บันทึก"}
</button>
</div>`;
}
// ── QR ลิงก์ทดสอบบนมือถือ ────────────────────────────
export function qrSheet(url) {
return `<h3>ทดสอบบนมือถือ</h3>
<div class="hint">สแกนด้วยกล้อง Samsung S23 แล้วเปิดใน Chrome</div>
<div class="qr-box"><div id="qr-holder"></div></div>
<div class="qr-url">${esc(url)}</div>
<div class="hint">กล้องใช้ได้เฉพาะหน้า HTTPS — ถ้าลิงก์ขึ้นต้น http:// กล้องจะถูกบล็อก</div>
<div class="sheet-actions">
<button class="btn btn-ghost" type="button" data-action="copy-url">คัดลอกลิงก์</button>
<button class="btn btn-primary" type="button" data-action="zip-share">${ICON.share} แชร์ ZIP</button>
</div></div>`;
}
// ── Diagnostics ──
export function diagnosticsSheet(rows) {
const body = rows
.map(([k, v]) => `<div class="row"><span class="k">${esc(k)}</span><span class="v">${esc(v)}</span></div>`)
.join("");
return `<h3>ข้อมูลอุปกรณ์และกล้อง</h3>
<div class="hint">ใช้ตรวจว่ารันบน S23 จริงและกล้องได้ความละเอียดเท่าไร</div>
<div class="meta-table">${body}</div>
<div class="sheet-actions" style="margin-top:14px">
<button class="btn btn-ghost btn-block" type="button" data-action="close-sheet">ปิด</button>
</div>`;
}
// ── Progress ──
export function progressSheet(label) {
openSheet(`<div class="progress-card" id="progress-card">
<div class="spinner"></div>
<div class="label" id="progress-label">${esc(label)}</div>
</div>`);
}
export function progressUpdate(pct, label) {
const card = document.getElementById("progress-card");
if (!card) return;
const p = Math.max(0, Math.min(100, Math.round(pct)));
card.innerHTML = `<div class="spinner"></div><div class="label">${esc(label || "")} ${p}%</div>`;
}