147 lines
4.8 KiB
JavaScript
147 lines
4.8 KiB
JavaScript
const HOST = 'http://110.42.217.133'
|
|
const KINDS = [
|
|
['all', '全部'],
|
|
['site', '网站'],
|
|
['repo', '仓库'],
|
|
['plugin', '插件'],
|
|
['mirror', '镜像'],
|
|
['file', '文件'],
|
|
]
|
|
const KIND_NAME = { site: '网站', repo: '仓库', plugin: '插件', mirror: '镜像', dir: '目录', file: '文件' }
|
|
|
|
let data = null
|
|
let kind = 'all'
|
|
let q = ''
|
|
let sort = 'size'
|
|
|
|
const $ = (s) => document.querySelector(s)
|
|
|
|
function el(tag, attrs = {}, ...kids) {
|
|
const n = document.createElement(tag)
|
|
for (const [k, v] of Object.entries(attrs)) {
|
|
if (k === 'class') n.className = v
|
|
else if (k === 'html') n.innerHTML = v
|
|
else if (k.startsWith('on')) n.addEventListener(k.slice(2), v)
|
|
else if (v !== undefined && v !== null) n.setAttribute(k, v)
|
|
}
|
|
for (const kid of kids.flat()) if (kid != null && kid !== false) n.append(kid.nodeType ? kid : String(kid))
|
|
return n
|
|
}
|
|
|
|
function fmtDate(iso) {
|
|
const d = new Date(iso)
|
|
const diff = (Date.now() - d.getTime()) / 1000
|
|
if (diff < 60) return '刚刚'
|
|
if (diff < 3600) return `${Math.floor(diff / 60)} 分钟前`
|
|
if (diff < 86400) return `${Math.floor(diff / 3600)} 小时前`
|
|
if (diff < 86400 * 30) return `${Math.floor(diff / 86400)} 天前`
|
|
return `${d.getMonth() + 1} 月 ${d.getDate()} 日`
|
|
}
|
|
|
|
function matches(e) {
|
|
if (kind !== 'all' && e.kind !== kind) return false
|
|
if (!q) return true
|
|
const hay = `${e.name} ${e.title || ''} ${e.git?.branch || ''} ${e.git?.subject || ''}`.toLowerCase()
|
|
return hay.includes(q.toLowerCase())
|
|
}
|
|
|
|
function paint() {
|
|
const grid = $('#grid')
|
|
const list = data.entries.filter(matches)
|
|
if (sort === 'name') list.sort((a, b) => a.name.localeCompare(b.name))
|
|
else if (sort === 'time') list.sort((a, b) => new Date(b.mtime) - new Date(a.mtime))
|
|
else list.sort((a, b) => b.bytes - a.bytes)
|
|
|
|
const max = Math.max(1, ...data.entries.map((e) => e.bytes))
|
|
grid.innerHTML = ''
|
|
$('#empty').hidden = list.length > 0
|
|
|
|
for (const e of list) {
|
|
const pct = Math.max(1.5, (e.bytes / max) * 100)
|
|
const acts = []
|
|
if (e.port) acts.push(el('a', { href: `${HOST}:${e.port}/`, target: '_blank', rel: 'noopener' }, `打开 :${e.port}`))
|
|
if (e.git?.webRepo) acts.push(el('a', { href: e.git.webRepo, target: '_blank', rel: 'noopener' }, 'Gitea 仓库'))
|
|
if (e.kind === 'file' && e.ext) acts.push(el('span', {}, `${e.ext} 文件`))
|
|
if (!acts.length) acts.push(el('span', {}, '本地目录'))
|
|
|
|
grid.append(
|
|
el('div', { class: 'card' },
|
|
el('div', { class: 'row1' },
|
|
el('h3', { title: e.name }, e.name),
|
|
el('span', { class: `badge ${e.kind}` }, KIND_NAME[e.kind] || e.kind),
|
|
),
|
|
el('p', { class: 'desc' }, e.title || (e.kind === 'file' ? `单文件 · ${e.size}` : '没有 README 说明')),
|
|
el('div', { class: 'barc' }, el('i', { style: `width:${pct}%` })),
|
|
el('div', { class: 'meta' },
|
|
el('span', {}, el('b', {}, e.size)),
|
|
e.files > 1 ? el('span', {}, `${e.files} 个文件`) : null,
|
|
el('span', {}, fmtDate(e.mtime)),
|
|
),
|
|
e.git
|
|
? el('div', { class: 'meta' },
|
|
el('span', {}, `⎇ ${e.git.branch}`),
|
|
el('span', { title: e.git.subject }, `${e.git.sha} ${e.git.subject.slice(0, 18)}${e.git.subject.length > 18 ? '…' : ''}`),
|
|
e.git.dirty ? el('span', { class: 'dirty' }, '● 有改动') : null,
|
|
)
|
|
: null,
|
|
el('div', { class: 'acts' }, acts),
|
|
),
|
|
)
|
|
}
|
|
$('#stamp').textContent = `扫描于 ${new Date(data.scannedAt).toLocaleString('zh-CN')} · 根目录 ${data.root}`
|
|
}
|
|
|
|
function paintChips() {
|
|
const box = $('#chips')
|
|
box.innerHTML = ''
|
|
const counts = { all: data.entries.length }
|
|
for (const e of data.entries) counts[e.kind] = (counts[e.kind] || 0) + 1
|
|
for (const [k, label] of KINDS) {
|
|
if (k !== 'all' && !counts[k]) continue
|
|
box.append(
|
|
el('button', {
|
|
class: 'chip',
|
|
'aria-selected': String(kind === k),
|
|
onclick: () => {
|
|
kind = k
|
|
paintChips()
|
|
paint()
|
|
},
|
|
}, `${label} ${counts[k] || 0}`),
|
|
)
|
|
}
|
|
}
|
|
|
|
async function boot() {
|
|
try {
|
|
const res = await fetch('manifest.json?t=' + Date.now(), { cache: 'no-store' })
|
|
data = await res.json()
|
|
} catch (err) {
|
|
$('#sub').textContent = '读不到 manifest.json,先跑 node scan.mjs'
|
|
return
|
|
}
|
|
$('#sub').textContent = `${data.totals.projects} 项 · ${data.totals.size} · ${data.totals.files} 个文件`
|
|
const t = data.totals
|
|
$('#totals').innerHTML = [
|
|
['项目', t.projects],
|
|
['占用', t.size],
|
|
['文件', t.files],
|
|
]
|
|
.map(([k, v]) => `<div class="stat"><b>${v}</b><span>${k}</span></div>`)
|
|
.join('')
|
|
|
|
paintChips()
|
|
paint()
|
|
|
|
$('#q').addEventListener('input', (ev) => {
|
|
q = ev.target.value.trim()
|
|
paint()
|
|
})
|
|
$('#sort').addEventListener('change', (ev) => {
|
|
sort = ev.target.value
|
|
paint()
|
|
})
|
|
}
|
|
|
|
boot()
|