From 349197009063ffa72df9e10f6ed82949c88bdb96 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 31 May 2026 11:07:39 +0800 Subject: [PATCH] refactor(webui): extract DOM and format helpers --- webui/app.js | 91 ------------------------------------------- webui/index.html | 5 ++- webui/js/dom_utils.js | 90 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 93 deletions(-) create mode 100644 webui/js/dom_utils.js diff --git a/webui/app.js b/webui/app.js index cf3e634..bf027a4 100644 --- a/webui/app.js +++ b/webui/app.js @@ -21,8 +21,6 @@ let actionsLogFilter = ""; const actionsLogMaxLines = UI_LIMITS.actionsLogMaxLines; let deviceOverviewState = { devices: [], adb: "" }; -function $(id) { return document.getElementById(id); } - function initTheme() { const root = document.documentElement; const btn = $("btnTheme"); @@ -777,33 +775,6 @@ function attachNumberSteppers() { }); } -function fmtTime(ts) { - if (!ts) return "-"; - try { - return new Date(ts * 1000).toLocaleString(); - } catch { - return "-"; - } -} - -function fmtNum(v) { - if (v === null || typeof v === "undefined") return "-"; - const n = Number(v); - if (!Number.isFinite(n)) return String(v); - return n.toLocaleString("en-US"); -} - -function fmtDuration(seconds) { - const s = Math.max(0, parseInt(seconds || 0, 10)); - const h = Math.floor(s / 3600); - const m = Math.floor((s % 3600) / 60); - const sec = s % 60; - const pad = (n) => String(n).padStart(2, "0"); - // mono 格式:HH:MM:SS(>1h)/ MM:SS(其他),方便 tabular-nums 对齐 - if (h > 0) return `${pad(h)}:${pad(m)}:${pad(sec)}`; - return `${pad(m)}:${pad(sec)}`; -} - function pollShot() { const img = $("liveShot"); if (!img) return; @@ -856,54 +827,6 @@ function startShotPolling() { shotTimer = setInterval(pollShot, UI_LIMITS.screenshotPollMs); } -function setTextIfExists(id, text) { - const el = $(id); - if (!el) return; - const next = String(text ?? "-"); - // 仅取文本节点(跳过子元素如 .copyBtn / .statTrend)来比较和读取当前值 - const getTextOnly = (node) => { - let s = ""; - for (const ch of node.childNodes) if (ch.nodeType === Node.TEXT_NODE) s += ch.textContent; - return s; - }; - const prev = getTextOnly(el); - // 仅对 statValue 有效:值变化时短暂高亮 + 趋势箭头 - if (el.classList.contains("statValue") && prev !== next && next !== "-") { - el.classList.remove("isFlash"); - void el.offsetWidth; - el.classList.add("isFlash"); - setTimeout(() => el.classList.remove("isFlash"), 700); - // 数字字段才显示趋势 - const prevNum = Number(prev); - const nextNum = Number(next); - if (Number.isFinite(prevNum) && Number.isFinite(nextNum) && prevNum !== nextNum) { - let trend = el.querySelector(".statTrend"); - if (!trend) { - trend = document.createElement("span"); - trend.className = "statTrend"; - el.appendChild(trend); - } - const isUp = nextNum > prevNum; - trend.textContent = isUp ? "▲" : "▼"; - trend.classList.toggle("isUp", isUp); - trend.classList.toggle("isDown", !isUp); - trend.classList.remove("isFading"); - void trend.offsetWidth; - trend.classList.add("isFading"); - // 1s 后自动消失 - clearTimeout(trend._fadeTimer); - trend._fadeTimer = setTimeout(() => { - if (trend) trend.remove(); - }, 1100); - } - } - // 替换:保留所有非文本子节点(如 .copyBtn / .statTrend),仅更新文本 - for (const ch of Array.from(el.childNodes)) { - if (ch.nodeType === Node.TEXT_NODE) ch.remove(); - } - el.insertBefore(document.createTextNode(next), el.firstChild || null); -} - function setButtonBusy(id, busy) { const btn = $(id); if (!btn) return; @@ -934,11 +857,6 @@ function updateDeviceOverview() { setTextIfExists("topAdbState", deviceOverviewState.adb ? UI_COPY.device.adbReady : UI_COPY.device.adbMissing); } -function clearEl(el) { - while (el && el.firstChild) el.removeChild(el.firstChild); -} - - function renderBars(containerId, dataMap, labelMap) { const root = $(containerId); if (!root) return; @@ -1013,15 +931,6 @@ function renderTopContent(containerId, items) { }); } -function escapeHtml(s) { - return String(s ?? "") - .replaceAll("&", "&") - .replaceAll("<", "<") - .replaceAll(">", ">") - .replaceAll('"', """) - .replaceAll("'", "'"); -} - function renderWinsTable(containerId, wins) { const root = $(containerId); if (!root) return; diff --git a/webui/index.html b/webui/index.html index 8afdff3..02916bf 100644 --- a/webui/index.html +++ b/webui/index.html @@ -1278,7 +1278,8 @@ - - + + + diff --git a/webui/js/dom_utils.js b/webui/js/dom_utils.js new file mode 100644 index 0000000..ebae2cc --- /dev/null +++ b/webui/js/dom_utils.js @@ -0,0 +1,90 @@ +// DOM and formatting helpers shared by WebUI classic scripts. +function $(id) { return document.getElementById(id); } + +function fmtTime(ts) { + if (!ts) return "-"; + try { + return new Date(ts * 1000).toLocaleString(); + } catch { + return "-"; + } +} + +function fmtNum(v) { + if (v === null || typeof v === "undefined") return "-"; + const n = Number(v); + if (!Number.isFinite(n)) return String(v); + return n.toLocaleString("en-US"); +} + +function fmtDuration(seconds) { + const s = Math.max(0, parseInt(seconds || 0, 10)); + const h = Math.floor(s / 3600); + const m = Math.floor((s % 3600) / 60); + const sec = s % 60; + const pad = (n) => String(n).padStart(2, "0"); + // mono 格式:HH:MM:SS(>1h)/ MM:SS(其他),方便 tabular-nums 对齐 + if (h > 0) return `${pad(h)}:${pad(m)}:${pad(sec)}`; + return `${pad(m)}:${pad(sec)}`; +} + +function setTextIfExists(id, text) { + const el = $(id); + if (!el) return; + const next = String(text ?? "-"); + // 仅取文本节点(跳过子元素如 .copyBtn / .statTrend)来比较和读取当前值 + const getTextOnly = (node) => { + let s = ""; + for (const ch of node.childNodes) if (ch.nodeType === Node.TEXT_NODE) s += ch.textContent; + return s; + }; + const prev = getTextOnly(el); + // 仅对 statValue 有效:值变化时短暂高亮 + 趋势箭头 + if (el.classList.contains("statValue") && prev !== next && next !== "-") { + el.classList.remove("isFlash"); + void el.offsetWidth; + el.classList.add("isFlash"); + setTimeout(() => el.classList.remove("isFlash"), 700); + // 数字字段才显示趋势 + const prevNum = Number(prev); + const nextNum = Number(next); + if (Number.isFinite(prevNum) && Number.isFinite(nextNum) && prevNum !== nextNum) { + let trend = el.querySelector(".statTrend"); + if (!trend) { + trend = document.createElement("span"); + trend.className = "statTrend"; + el.appendChild(trend); + } + const isUp = nextNum > prevNum; + trend.textContent = isUp ? "▲" : "▼"; + trend.classList.toggle("isUp", isUp); + trend.classList.toggle("isDown", !isUp); + trend.classList.remove("isFading"); + void trend.offsetWidth; + trend.classList.add("isFading"); + // 1s 后自动消失 + clearTimeout(trend._fadeTimer); + trend._fadeTimer = setTimeout(() => { + if (trend) trend.remove(); + }, 1100); + } + } + // 替换:保留所有非文本子节点(如 .copyBtn / .statTrend),仅更新文本 + for (const ch of Array.from(el.childNodes)) { + if (ch.nodeType === Node.TEXT_NODE) ch.remove(); + } + el.insertBefore(document.createTextNode(next), el.firstChild || null); +} + +function clearEl(el) { + while (el && el.firstChild) el.removeChild(el.firstChild); +} + +function escapeHtml(s) { + return String(s ?? "") + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """) + .replaceAll("'", "'"); +}