refactor(webui): extract DOM and format helpers
This commit is contained in:
1 parent
01ce77eaa6
commit
3491970090
3 files changed
+93
-93
No files matched your search
@@ -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;
|
||||
|
||||
+3
-2
@@ -1278,7 +1278,8 @@
|
||||
</button>
|
||||
|
||||
<!-- WebUI JS modules are loaded before the compatibility remainder. -->
|
||||
<script src="js/constants.js?v=console-20260531-split-1"></script>
|
||||
<script src="app.js?v=console-20260531-split-1"></script>
|
||||
<script src="js/constants.js?v=console-20260531-split-2"></script>
|
||||
<script src="js/dom_utils.js?v=console-20260531-split-2"></script>
|
||||
<script src="app.js?v=console-20260531-split-2"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -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("'", "'");
|
||||
}
|
||||
Reference in new issue
Block a user