128 lines
4.3 KiB
HTML
128 lines
4.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
<title>看板</title>
|
|
<link rel="stylesheet" href="./base.css">
|
|
<style>
|
|
.container { max-width: 1400px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<a class="crumb" href="./">← 首页</a>
|
|
<h1 id="title">看板</h1>
|
|
<div class="board-outer" id="outer"><div class="msg">加载中…</div></div>
|
|
</div>
|
|
|
|
<button class="fab" aria-label="返回对话" title="返回发起本数据的对话" onclick="backToApp()">
|
|
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 10.5 12 3l9 7.5"/><path d="M5 9.5V21h14V9.5"/></svg>
|
|
</button>
|
|
|
|
<script src="./config-kanban.js"></script>
|
|
<script>
|
|
const DEFAULT = {
|
|
table: 'tasks', pk: 'id', title: '任务看板',
|
|
groupField: 'status',
|
|
groups: ['待办', '进行中', '已完成'],
|
|
titleField: 'title',
|
|
metaFields: ['assignee', 'due'],
|
|
};
|
|
const CONFIG = Object.assign({}, DEFAULT, window.PAGE_CONFIG || {});
|
|
|
|
const API = `/api/db/${location.pathname.split('/').filter(Boolean)[1]}/query`;
|
|
const esc = s => String(s ?? '').replace(/[&<>"']/g, c =>
|
|
({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
|
|
async function q(sql, params = []) {
|
|
const r = await fetch(API, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ sql, params }),
|
|
});
|
|
const d = await r.json();
|
|
if (d.error) throw new Error(d.error);
|
|
return d;
|
|
}
|
|
|
|
let rows = [];
|
|
|
|
async function load() {
|
|
try {
|
|
rows = await q(`SELECT * FROM ${CONFIG.table}`);
|
|
render();
|
|
} catch (e) {
|
|
document.getElementById('outer').innerHTML = `<div class="msg err">加载失败:${esc(e.message)}</div>`;
|
|
}
|
|
}
|
|
|
|
function render() {
|
|
document.getElementById('title').textContent = CONFIG.title;
|
|
const groups = CONFIG.groups;
|
|
const html = `<div class="board">${groups.map((g, i) => {
|
|
const items = rows.filter(r => String(r[CONFIG.groupField] ?? '') === g);
|
|
return `<div class="col">
|
|
<div class="col-hd"><span>${esc(g)}</span><span class="n">${items.length}</span></div>
|
|
<div class="col-bd">
|
|
${items.length ? items.map(r => card(r, i)).join('') : '<div class="empty">暂无</div>'}
|
|
</div>
|
|
</div>`;
|
|
}).join('')}</div>`;
|
|
document.getElementById('outer').innerHTML = html;
|
|
bind();
|
|
}
|
|
|
|
function card(r, gi) {
|
|
const meta = CONFIG.metaFields
|
|
.filter(f => r[f] != null && r[f] !== '')
|
|
.map(f => `<span>${esc(r[f])}</span>`).join('');
|
|
return `<div class="kanban-card" data-pk="${esc(r[CONFIG.pk])}">
|
|
<div class="t">${esc(r[CONFIG.titleField] ?? '(无标题)')}</div>
|
|
${meta ? `<div class="meta">${meta}</div>` : ''}
|
|
<div class="mv">
|
|
<button data-dir="-1" ${gi === 0 ? 'disabled' : ''}>← 上一列</button>
|
|
<button data-dir="1" ${gi === CONFIG.groups.length - 1 ? 'disabled' : ''}>下一列 →</button>
|
|
</div>
|
|
</div>`;
|
|
}
|
|
|
|
function bind() {
|
|
document.querySelectorAll('.kanban-card').forEach(cardEl => {
|
|
const pk = cardEl.dataset.pk;
|
|
cardEl.querySelectorAll('.mv button').forEach(btn => {
|
|
btn.onclick = async () => {
|
|
const dir = Number(btn.dataset.dir);
|
|
const row = rows.find(r => String(r[CONFIG.pk]) === String(pk));
|
|
if (!row) return;
|
|
const cur = CONFIG.groups.indexOf(String(row[CONFIG.groupField] ?? ''));
|
|
const next = cur + dir;
|
|
if (next < 0 || next >= CONFIG.groups.length) return;
|
|
const newGroup = CONFIG.groups[next];
|
|
btn.disabled = true;
|
|
try {
|
|
await q(`UPDATE ${CONFIG.table} SET ${CONFIG.groupField} = ? WHERE ${CONFIG.pk} = ?`, [newGroup, pk]);
|
|
row[CONFIG.groupField] = newGroup;
|
|
render();
|
|
} catch (e) {
|
|
alert('移动失败:' + e.message);
|
|
btn.disabled = false;
|
|
}
|
|
};
|
|
});
|
|
});
|
|
}
|
|
|
|
function backToApp() {
|
|
const id = location.pathname.split('/').filter(Boolean)[1];
|
|
location.href = id ? '/?s=' + encodeURIComponent(id) : '/';
|
|
}
|
|
|
|
// 单页场景:本页的副本就是 index.html,「← 首页」指向自身 → 移除(多页导航不受影响)
|
|
if (/\/(index\.html)?$/.test(location.pathname)) { const c = document.querySelector('.crumb'); if (c) c.remove(); }
|
|
|
|
load();
|
|
</script>
|
|
</body>
|
|
</html>
|