Rip Some Duplicate Code Into Functions
This commit is contained in:
1 parent
b0de8cf38c
commit
3bc99258a2
12 files changed
+308
-664
No files matched your search
+2
-1
@@ -1,5 +1,6 @@
|
||||
dist/
|
||||
dist*/
|
||||
bin/
|
||||
.tmp/
|
||||
.env
|
||||
*.exe
|
||||
*.log
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
function relativeTime(unixSeconds) {
|
||||
const seconds = Math.max(0, Math.floor(Date.now() / 1000) - unixSeconds);
|
||||
if (seconds < 5) {
|
||||
return "just now";
|
||||
}
|
||||
if (seconds < 60) {
|
||||
return `${seconds}s ago`;
|
||||
}
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
if (minutes < 60) {
|
||||
return `${minutes}m ago`;
|
||||
}
|
||||
const hours = Math.floor(minutes / 60);
|
||||
return `${hours}h ago`;
|
||||
}
|
||||
|
||||
function escapeHTML(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
function fact(label, value) {
|
||||
return `<div><dt>${escapeHTML(label)}</dt><dd>${escapeHTML(value)}</dd></div>`;
|
||||
}
|
||||
@@ -52,6 +52,8 @@
|
||||
|
||||
<script src="/static/session.js"></script>
|
||||
<script src="/static/printer-switcher.js"></script>
|
||||
<script src="/static/common.js"></script>
|
||||
<script src="/static/printer-shared.js"></script>
|
||||
<script src="/static/dashboard.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -90,217 +90,6 @@ function renderPrinter(printer) {
|
||||
`;
|
||||
}
|
||||
|
||||
function relativeTime(unixSeconds) {
|
||||
const seconds = Math.max(0, Math.floor(Date.now() / 1000) - unixSeconds);
|
||||
if (seconds < 5) {
|
||||
return "just now";
|
||||
}
|
||||
if (seconds < 60) {
|
||||
return `${seconds}s ago`;
|
||||
}
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
if (minutes < 60) {
|
||||
return `${minutes}m ago`;
|
||||
}
|
||||
const hours = Math.floor(minutes / 60);
|
||||
return `${hours}h ago`;
|
||||
}
|
||||
|
||||
function escapeHTML(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
function formatMmuMaterialsHtml(printStatus) {
|
||||
if (!printStatus || typeof printStatus !== "object") {
|
||||
return "";
|
||||
}
|
||||
const state = collectMmuMaterialState(printStatus);
|
||||
if (!state.visible) {
|
||||
return "";
|
||||
}
|
||||
if (state.disabled) {
|
||||
return `<span class="mmu-material-chip mmu-disabled"><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text">${escapeHTML(state.label)}: disabled</span></span>`;
|
||||
}
|
||||
if (state.rows.length === 0) {
|
||||
return `<span class="mmu-material-chip"><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text">${escapeHTML(state.label)}: enabled</span></span>`;
|
||||
}
|
||||
return [
|
||||
...state.rows.map((row) => mmuMaterialRowHtml(row)),
|
||||
].join("");
|
||||
}
|
||||
|
||||
function collectMmuMaterialState(printStatus) {
|
||||
const mmu = printStatus.moonraker_status && printStatus.moonraker_status.mmu;
|
||||
if (!mmu || typeof mmu !== "object" || !Object.prototype.hasOwnProperty.call(mmu, "enabled")) {
|
||||
return { visible: false, disabled: false, label: "MMU", rows: [] };
|
||||
}
|
||||
const machine = printStatus.moonraker_status && printStatus.moonraker_status.mmu_machine;
|
||||
const enabled = isTruthyMetadataValue(mmu.enabled);
|
||||
const label = machine?.unit_0?.name || "MMU";
|
||||
if (!enabled) {
|
||||
return { visible: true, disabled: true, label, rows: [] };
|
||||
}
|
||||
const names = normalizeMetadataList(mmu.gate_filament_name);
|
||||
const materials = normalizeMetadataList(mmu.gate_material);
|
||||
const colors = normalizeMetadataList(mmu.gate_color);
|
||||
const count = Math.max(names.length, materials.length, colors.length, Number(mmu.num_gates || 0));
|
||||
if (count === 0) {
|
||||
return { visible: true, disabled: false, label, rows: [{ label, value: "Enabled", color: "" }] };
|
||||
}
|
||||
return {
|
||||
visible: true,
|
||||
disabled: false,
|
||||
label,
|
||||
rows: Array.from({ length: count }, (_, index) => {
|
||||
const parts = [];
|
||||
let color = "";
|
||||
if (names[index] && names[index] !== "Unknown") {
|
||||
parts.push(String(names[index]));
|
||||
}
|
||||
if (materials[index] && materials[index] !== names[index]) {
|
||||
parts.push(`(${String(materials[index])})`);
|
||||
}
|
||||
if (colors[index]) {
|
||||
color = String(colors[index]).replace(/[^0-9a-fA-F]/g, "");
|
||||
}
|
||||
return {
|
||||
label: `Gate ${index + 1}`,
|
||||
value: parts.join(" ") || "Unknown material",
|
||||
color,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeMetadataList(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.filter((item) => item !== null && item !== undefined && item !== "");
|
||||
}
|
||||
if (value === null || value === undefined || value === "") {
|
||||
return [];
|
||||
}
|
||||
return [value];
|
||||
}
|
||||
|
||||
function isTruthyMetadataValue(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.length > 0;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
return normalized === "1" || normalized === "true" || normalized === "yes";
|
||||
}
|
||||
return Boolean(value);
|
||||
}
|
||||
|
||||
function mmuMaterialRowHtml(row) {
|
||||
const label = escapeHTML(row.label);
|
||||
const value = row.value ? escapeHTML(row.value) : "";
|
||||
const color = row.color ? mmuNormalizeHexColor(row.color) : "";
|
||||
const swatchStyle = color ? ` style="--mmu-swatch:${escapeHTML(color)}"` : "";
|
||||
return `<span class="mmu-material-chip"${swatchStyle}><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text"><strong>${label}</strong>${value ? ` ${value}` : ""}</span></span>`;
|
||||
}
|
||||
|
||||
function mmuNormalizeHexColor(value) {
|
||||
const raw = String(value || "").trim().replace(/[^0-9a-fA-F]/g, "");
|
||||
if (raw.length === 8) {
|
||||
return `#${raw.slice(0, 6)}`;
|
||||
}
|
||||
if (raw.length === 6) {
|
||||
return `#${raw}`;
|
||||
}
|
||||
if (raw.length === 3) {
|
||||
return `#${raw.split("").map((ch) => ch + ch).join("")}`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function formatTemperatureSummary(printStatus) {
|
||||
const state = collectTemperatureState(printStatus);
|
||||
if (!state.visible) {
|
||||
return "";
|
||||
}
|
||||
return state.rows.map((row) => `${row.label} ${row.value}`).join(" · ");
|
||||
}
|
||||
|
||||
function collectTemperatureState(printStatus) {
|
||||
const status = printStatus && printStatus.moonraker_status;
|
||||
if (!status || typeof status !== "object") {
|
||||
return { visible: false, rows: [] };
|
||||
}
|
||||
const rows = [];
|
||||
const seen = new Set();
|
||||
const add = (key, label) => {
|
||||
if (!Object.prototype.hasOwnProperty.call(status, key)) {
|
||||
return;
|
||||
}
|
||||
const sensor = status[key];
|
||||
if (!sensor || typeof sensor !== "object") {
|
||||
return;
|
||||
}
|
||||
const reading = formatTemperatureReading(sensor);
|
||||
if (!reading) {
|
||||
return;
|
||||
}
|
||||
rows.push({ label, value: reading });
|
||||
seen.add(key);
|
||||
};
|
||||
add("extruder", "Hotend");
|
||||
add("heater_bed", "Bed");
|
||||
add("temperature_sensor chamber", "Chamber");
|
||||
Object.keys(status).sort().forEach((key) => {
|
||||
if (seen.has(key) || !isTemperatureSensorKey(key)) {
|
||||
return;
|
||||
}
|
||||
const sensor = status[key];
|
||||
if (!sensor || typeof sensor !== "object") {
|
||||
return;
|
||||
}
|
||||
const reading = formatTemperatureReading(sensor);
|
||||
if (!reading) {
|
||||
return;
|
||||
}
|
||||
rows.push({ label: prettifyTemperatureLabel(key), value: reading });
|
||||
});
|
||||
return { visible: rows.length > 0, rows };
|
||||
}
|
||||
|
||||
function isTemperatureSensorKey(key) {
|
||||
return typeof key === "string" && (
|
||||
key.startsWith("temperature_sensor ") ||
|
||||
key.startsWith("temperature_fan ") ||
|
||||
key.startsWith("temperature_probe ")
|
||||
);
|
||||
}
|
||||
|
||||
function prettifyTemperatureLabel(key) {
|
||||
const raw = String(key || "")
|
||||
.replace(/^temperature_(sensor|fan|probe)\s+/i, "")
|
||||
.replace(/_/g, " ")
|
||||
.trim();
|
||||
if (!raw) {
|
||||
return "Sensor";
|
||||
}
|
||||
return raw.replace(/\b\w/g, (ch) => ch.toUpperCase());
|
||||
}
|
||||
|
||||
function formatTemperatureReading(sensor) {
|
||||
const current = Number(sensor.temperature);
|
||||
if (!Number.isFinite(current)) {
|
||||
return "";
|
||||
}
|
||||
const target = Number(sensor.target);
|
||||
if (Number.isFinite(target) && target > 0) {
|
||||
return `${current.toFixed(1)} / ${target.toFixed(1)}°C`;
|
||||
}
|
||||
return `${current.toFixed(1)}°C`;
|
||||
}
|
||||
|
||||
refresh.addEventListener("click", loadPrinters);
|
||||
const sessionReady = window.__KA_SESSION_PROMISE__ || Promise.resolve();
|
||||
sessionReady.finally(() => {
|
||||
|
||||
@@ -183,6 +183,8 @@
|
||||
|
||||
<script src="/static/session.js"></script>
|
||||
<script src="/static/printer-switcher.js"></script>
|
||||
<script src="/static/common.js"></script>
|
||||
<script src="/static/printer-page-common.js"></script>
|
||||
<script src="/static/printer-agent.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -647,40 +647,6 @@ function updateUILink(health) {
|
||||
uiLink.textContent = "UI unavailable";
|
||||
}
|
||||
|
||||
function computeSettingsSync(settings, health) {
|
||||
const updatedAt = parseTimestamp(settings.updated_at);
|
||||
const lastSyncedAt = parseTimestamp(settings.last_synced_at);
|
||||
const controlMode = String(settings.control_mode || "").toLowerCase() || "readonly";
|
||||
const appliedMode = String(health.control_mode || "").toLowerCase() || "unknown";
|
||||
const uiURL = String(settings.ui_url || "").trim();
|
||||
const agentUIURL = String(health.ui_url || "").trim();
|
||||
const autoDetect = Boolean(settings.ui_auto_detect);
|
||||
const agentAutoDetect = Boolean(health.ui_auto_detect);
|
||||
const synced = Boolean(updatedAt && lastSyncedAt && lastSyncedAt >= updatedAt);
|
||||
if (synced) {
|
||||
return {
|
||||
synced: true,
|
||||
state: "Synced",
|
||||
detail: "Agent acknowledged the latest saved settings.",
|
||||
};
|
||||
}
|
||||
const detailBits = [];
|
||||
if (controlMode !== appliedMode && appliedMode !== "unknown") {
|
||||
detailBits.push(`control mode ${controlMode} -> ${appliedMode}`);
|
||||
}
|
||||
if (uiURL && uiURL !== agentUIURL) {
|
||||
detailBits.push(`UI URL ${uiURL} -> ${agentUIURL || "unset"}`);
|
||||
}
|
||||
if (autoDetect !== agentAutoDetect) {
|
||||
detailBits.push(`auto-detect ${autoDetect} -> ${agentAutoDetect}`);
|
||||
}
|
||||
return {
|
||||
synced: false,
|
||||
state: "Pending",
|
||||
detail: detailBits.length > 0 ? `Waiting for agent confirmation; ${detailBits.join("; ")}` : "Waiting for the agent to confirm the latest settings.",
|
||||
};
|
||||
}
|
||||
|
||||
function computeVersionState(health) {
|
||||
const current = String(health && health.agent_version ? health.agent_version : "Unknown");
|
||||
const available = String(window.__KA_SESSION__ && window.__KA_SESSION__.server_version ? window.__KA_SESSION__.server_version : "dev");
|
||||
@@ -703,80 +669,6 @@ function setStatus(label, ok) {
|
||||
status.classList.toggle("warn", !ok);
|
||||
}
|
||||
|
||||
function fact(label, value) {
|
||||
return `<div><dt>${escapeHTML(label)}</dt><dd>${escapeHTML(value)}</dd></div>`;
|
||||
}
|
||||
|
||||
function parseTimestamp(value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isFinite(parsed) ? parsed : 0;
|
||||
}
|
||||
|
||||
function relativeTime(unixSeconds) {
|
||||
const seconds = Math.max(0, Math.floor(Date.now() / 1000) - unixSeconds);
|
||||
if (seconds < 5) {
|
||||
return "just now";
|
||||
}
|
||||
if (seconds < 60) {
|
||||
return `${seconds}s ago`;
|
||||
}
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
if (minutes < 60) {
|
||||
return `${minutes}m ago`;
|
||||
}
|
||||
const hours = Math.floor(minutes / 60);
|
||||
return `${hours}h ago`;
|
||||
}
|
||||
|
||||
function formatProgress(value) {
|
||||
const progress = Number(value);
|
||||
if (!Number.isFinite(progress) || progress <= 0) {
|
||||
return "0%";
|
||||
}
|
||||
if (progress <= 1) {
|
||||
return `${Math.round(progress * 100)}%`;
|
||||
}
|
||||
return `${Math.round(progress)}%`;
|
||||
}
|
||||
|
||||
function formatDuration(value) {
|
||||
const seconds = Number(value);
|
||||
if (!Number.isFinite(seconds) || seconds < 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
const whole = Math.floor(seconds);
|
||||
const hours = Math.floor(whole / 3600);
|
||||
const minutes = Math.floor((whole % 3600) / 60);
|
||||
const secs = whole % 60;
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${minutes}m ${secs}s`;
|
||||
}
|
||||
if (minutes > 0) {
|
||||
return `${minutes}m ${secs}s`;
|
||||
}
|
||||
return `${secs}s`;
|
||||
}
|
||||
|
||||
function formatFilament(value) {
|
||||
const number = Number(value);
|
||||
if (!Number.isFinite(number) || number <= 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
return `${number.toFixed(1)} mm`;
|
||||
}
|
||||
|
||||
function escapeHTML(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
refresh.addEventListener("click", loadPrinter);
|
||||
if (updateCheck) {
|
||||
updateCheck.addEventListener("click", handleUpdateCheckClick);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
function computeSettingsSync(settings, health) {
|
||||
const updatedAt = parseTimestamp(settings.updated_at);
|
||||
const lastSyncedAt = parseTimestamp(settings.last_synced_at);
|
||||
const controlMode = String(settings.control_mode || "").toLowerCase() || "readonly";
|
||||
const appliedMode = String(health.control_mode || "").toLowerCase() || "unknown";
|
||||
const uiURL = String(settings.ui_url || "").trim();
|
||||
const agentUIURL = String(health.ui_url || "").trim();
|
||||
const autoDetect = Boolean(settings.ui_auto_detect);
|
||||
const agentAutoDetect = Boolean(health.ui_auto_detect);
|
||||
const synced = Boolean(updatedAt && lastSyncedAt && lastSyncedAt >= updatedAt);
|
||||
if (synced) {
|
||||
return {
|
||||
synced: true,
|
||||
state: "Synced",
|
||||
detail: "Agent acknowledged the latest saved settings.",
|
||||
};
|
||||
}
|
||||
const detailBits = [];
|
||||
if (controlMode !== appliedMode && appliedMode !== "unknown") {
|
||||
detailBits.push(`control mode ${controlMode} -> ${appliedMode}`);
|
||||
}
|
||||
if (uiURL && uiURL !== agentUIURL) {
|
||||
detailBits.push(`UI URL ${uiURL} -> ${agentUIURL || "unset"}`);
|
||||
}
|
||||
if (autoDetect !== agentAutoDetect) {
|
||||
detailBits.push(`auto-detect ${autoDetect} -> ${agentAutoDetect}`);
|
||||
}
|
||||
return {
|
||||
synced: false,
|
||||
state: "Pending",
|
||||
detail: detailBits.length > 0 ? `Waiting for agent confirmation; ${detailBits.join("; ")}` : "Waiting for the agent to confirm the latest settings.",
|
||||
};
|
||||
}
|
||||
|
||||
function parseTimestamp(value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isFinite(parsed) ? parsed : 0;
|
||||
}
|
||||
|
||||
function formatProgress(value) {
|
||||
const progress = Number(value);
|
||||
if (!Number.isFinite(progress) || progress <= 0) {
|
||||
return "0%";
|
||||
}
|
||||
if (progress <= 1) {
|
||||
return `${Math.round(progress * 100)}%`;
|
||||
}
|
||||
return `${Math.round(progress)}%`;
|
||||
}
|
||||
|
||||
function formatDuration(value) {
|
||||
const seconds = Number(value);
|
||||
if (!Number.isFinite(seconds) || seconds < 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
const whole = Math.floor(seconds);
|
||||
const hours = Math.floor(whole / 3600);
|
||||
const minutes = Math.floor((whole % 3600) / 60);
|
||||
const secs = whole % 60;
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${minutes}m ${secs}s`;
|
||||
}
|
||||
if (minutes > 0) {
|
||||
return `${minutes}m ${secs}s`;
|
||||
}
|
||||
return `${secs}s`;
|
||||
}
|
||||
|
||||
function formatFilament(value) {
|
||||
const number = Number(value);
|
||||
if (!Number.isFinite(number) || number <= 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
return `${number.toFixed(1)} mm`;
|
||||
}
|
||||
|
||||
function formatHistoryTime(value) {
|
||||
const unix = Number(value);
|
||||
if (!Number.isFinite(unix) || unix <= 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
return new Date(unix * 1000).toLocaleString();
|
||||
}
|
||||
@@ -160,6 +160,8 @@
|
||||
|
||||
<script src="/static/session.js"></script>
|
||||
<script src="/static/printer-switcher.js"></script>
|
||||
<script src="/static/common.js"></script>
|
||||
<script src="/static/printer-page-common.js"></script>
|
||||
<script src="/static/printer-settings.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -236,40 +236,6 @@ function installURL(install) {
|
||||
return `/install?${params.toString()}`;
|
||||
}
|
||||
|
||||
function computeSettingsSync(settings, health) {
|
||||
const updatedAt = parseTimestamp(settings.updated_at);
|
||||
const lastSyncedAt = parseTimestamp(settings.last_synced_at);
|
||||
const controlMode = String(settings.control_mode || "").toLowerCase() || "readonly";
|
||||
const appliedMode = String(health.control_mode || "").toLowerCase() || "unknown";
|
||||
const uiURL = String(settings.ui_url || "").trim();
|
||||
const agentUIURL = String(health.ui_url || "").trim();
|
||||
const autoDetect = Boolean(settings.ui_auto_detect);
|
||||
const agentAutoDetect = Boolean(health.ui_auto_detect);
|
||||
const synced = Boolean(updatedAt && lastSyncedAt && lastSyncedAt >= updatedAt);
|
||||
if (synced) {
|
||||
return {
|
||||
synced: true,
|
||||
state: "Synced",
|
||||
detail: "Agent acknowledged the latest synced settings.",
|
||||
};
|
||||
}
|
||||
const detailBits = [];
|
||||
if (controlMode !== appliedMode && appliedMode !== "unknown") {
|
||||
detailBits.push(`control mode ${controlMode} -> ${appliedMode}`);
|
||||
}
|
||||
if (uiURL && uiURL !== agentUIURL) {
|
||||
detailBits.push(`UI URL ${uiURL} -> ${agentUIURL || "unset"}`);
|
||||
}
|
||||
if (autoDetect !== agentAutoDetect) {
|
||||
detailBits.push(`auto-detect ${autoDetect} -> ${agentAutoDetect}`);
|
||||
}
|
||||
return {
|
||||
synced: false,
|
||||
state: "Pending",
|
||||
detail: detailBits.length > 0 ? `Waiting for agent confirmation; ${detailBits.join("; ")}` : "Waiting for the agent to confirm the latest settings.",
|
||||
};
|
||||
}
|
||||
|
||||
function setStatus(label, ok) {
|
||||
status.textContent = label;
|
||||
status.classList.toggle("ok", ok);
|
||||
@@ -386,27 +352,6 @@ function renderSnapshotCameraOptions(cameras, selectedCamera) {
|
||||
settingsNotificationSnapshotCamera.disabled = snapshotCameras.length === 0 && selected === "";
|
||||
}
|
||||
|
||||
function fact(label, value) {
|
||||
return `<div><dt>${escapeHTML(label)}</dt><dd>${escapeHTML(value)}</dd></div>`;
|
||||
}
|
||||
|
||||
function parseTimestamp(value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isFinite(parsed) ? parsed : 0;
|
||||
}
|
||||
|
||||
function escapeHTML(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
refresh.addEventListener("click", loadPrinter);
|
||||
if (settingsForm) {
|
||||
settingsForm.addEventListener("submit", saveSettings);
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
function formatMmuMaterialsHtml(printStatus) {
|
||||
if (!printStatus || typeof printStatus !== "object") {
|
||||
return "";
|
||||
}
|
||||
const state = collectMmuMaterialState(printStatus);
|
||||
if (!state.visible) {
|
||||
return "";
|
||||
}
|
||||
if (state.disabled) {
|
||||
return `<span class="mmu-material-chip mmu-disabled"><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text">${escapeHTML(state.label)}: disabled</span></span>`;
|
||||
}
|
||||
if (state.rows.length === 0) {
|
||||
return `<span class="mmu-material-chip"><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text">${escapeHTML(state.label)}: enabled</span></span>`;
|
||||
}
|
||||
return state.rows.map((row) => mmuMaterialRowHtml(row)).join("");
|
||||
}
|
||||
|
||||
function collectMmuMaterialState(printStatus) {
|
||||
const mmu = printStatus.moonraker_status && printStatus.moonraker_status.mmu;
|
||||
if (!mmu || typeof mmu !== "object" || !Object.prototype.hasOwnProperty.call(mmu, "enabled")) {
|
||||
return { visible: false, disabled: false, label: "MMU", rows: [] };
|
||||
}
|
||||
const machine = printStatus.moonraker_status && printStatus.moonraker_status.mmu_machine;
|
||||
const enabled = isTruthyMetadataValue(mmu.enabled);
|
||||
const label = machine?.unit_0?.name || "MMU";
|
||||
if (!enabled) {
|
||||
return { visible: true, disabled: true, label, rows: [] };
|
||||
}
|
||||
const names = normalizeMetadataList(mmu.gate_filament_name);
|
||||
const materials = normalizeMetadataList(mmu.gate_material);
|
||||
const colors = normalizeMetadataList(mmu.gate_color);
|
||||
const count = Math.max(names.length, materials.length, colors.length, Number(mmu.num_gates || 0));
|
||||
if (count === 0) {
|
||||
return { visible: true, disabled: false, label, rows: [{ label, value: "Enabled", color: "" }] };
|
||||
}
|
||||
return {
|
||||
visible: true,
|
||||
disabled: false,
|
||||
label,
|
||||
rows: Array.from({ length: count }, (_, index) => {
|
||||
const parts = [];
|
||||
let color = "";
|
||||
if (names[index] && names[index] !== "Unknown") {
|
||||
parts.push(String(names[index]));
|
||||
}
|
||||
if (materials[index] && materials[index] !== names[index]) {
|
||||
parts.push(`(${String(materials[index])})`);
|
||||
}
|
||||
if (colors[index]) {
|
||||
color = String(colors[index]).replace(/[^0-9a-fA-F]/g, "");
|
||||
}
|
||||
return {
|
||||
label: `Gate ${index + 1}`,
|
||||
value: parts.join(" ") || "Unknown material",
|
||||
color,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeMetadataList(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.filter((item) => item !== null && item !== undefined && item !== "");
|
||||
}
|
||||
if (value === null || value === undefined || value === "") {
|
||||
return [];
|
||||
}
|
||||
return [value];
|
||||
}
|
||||
|
||||
function isTruthyMetadataValue(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.length > 0;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
return normalized === "1" || normalized === "true" || normalized === "yes";
|
||||
}
|
||||
return Boolean(value);
|
||||
}
|
||||
|
||||
function mmuMaterialRowHtml(row) {
|
||||
const label = escapeHTML(row.label);
|
||||
const value = row.value ? escapeHTML(row.value) : "";
|
||||
const color = row.color ? mmuNormalizeHexColor(row.color) : "";
|
||||
const swatchStyle = color ? ` style="--mmu-swatch:${escapeHTML(color)}"` : "";
|
||||
return `<span class="mmu-material-chip"${swatchStyle}><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text"><strong>${label}</strong>${value ? ` ${value}` : ""}</span></span>`;
|
||||
}
|
||||
|
||||
function mmuNormalizeHexColor(value) {
|
||||
const raw = String(value || "").trim().replace(/[^0-9a-fA-F]/g, "");
|
||||
if (raw.length === 8) {
|
||||
return `#${raw.slice(0, 6)}`;
|
||||
}
|
||||
if (raw.length === 6) {
|
||||
return `#${raw}`;
|
||||
}
|
||||
if (raw.length === 3) {
|
||||
return `#${raw.split("").map((ch) => ch + ch).join("")}`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function formatTemperatureSummary(printStatus) {
|
||||
const state = collectTemperatureState(printStatus);
|
||||
if (!state.visible) {
|
||||
return "";
|
||||
}
|
||||
return state.rows.map((row) => `${row.label} ${row.value}`).join(" · ");
|
||||
}
|
||||
|
||||
function collectTemperatureState(printStatus) {
|
||||
const status = printStatus && printStatus.moonraker_status;
|
||||
if (!status || typeof status !== "object") {
|
||||
return { visible: false, rows: [] };
|
||||
}
|
||||
const rows = [];
|
||||
const seen = new Set();
|
||||
const add = (key, label) => {
|
||||
if (!Object.prototype.hasOwnProperty.call(status, key)) {
|
||||
return;
|
||||
}
|
||||
const sensor = status[key];
|
||||
if (!sensor || typeof sensor !== "object") {
|
||||
return;
|
||||
}
|
||||
const reading = formatTemperatureReading(sensor);
|
||||
if (!reading) {
|
||||
return;
|
||||
}
|
||||
rows.push({ label, value: reading });
|
||||
seen.add(key);
|
||||
};
|
||||
add("extruder", "Hotend");
|
||||
add("heater_bed", "Bed");
|
||||
add("temperature_sensor chamber", "Chamber");
|
||||
Object.keys(status).sort().forEach((key) => {
|
||||
if (seen.has(key) || !isTemperatureSensorKey(key)) {
|
||||
return;
|
||||
}
|
||||
const sensor = status[key];
|
||||
if (!sensor || typeof sensor !== "object") {
|
||||
return;
|
||||
}
|
||||
const reading = formatTemperatureReading(sensor);
|
||||
if (!reading) {
|
||||
return;
|
||||
}
|
||||
rows.push({ label: prettifyTemperatureLabel(key), value: reading });
|
||||
});
|
||||
return { visible: rows.length > 0, rows };
|
||||
}
|
||||
|
||||
function isTemperatureSensorKey(key) {
|
||||
return typeof key === "string" && (
|
||||
key.startsWith("temperature_sensor ") ||
|
||||
key.startsWith("temperature_fan ") ||
|
||||
key.startsWith("temperature_probe ")
|
||||
);
|
||||
}
|
||||
|
||||
function prettifyTemperatureLabel(key) {
|
||||
const raw = String(key || "")
|
||||
.replace(/^temperature_(sensor|fan|probe)\s+/i, "")
|
||||
.replace(/_/g, " ")
|
||||
.trim();
|
||||
if (!raw) {
|
||||
return "Sensor";
|
||||
}
|
||||
return raw.replace(/\b\w/g, (ch) => ch.toUpperCase());
|
||||
}
|
||||
|
||||
function formatTemperatureReading(sensor) {
|
||||
const current = Number(sensor.temperature);
|
||||
if (!Number.isFinite(current)) {
|
||||
return "";
|
||||
}
|
||||
const target = Number(sensor.target);
|
||||
if (Number.isFinite(target) && target > 0) {
|
||||
return `${current.toFixed(1)} / ${target.toFixed(1)}°C`;
|
||||
}
|
||||
return `${current.toFixed(1)}°C`;
|
||||
}
|
||||
@@ -146,6 +146,9 @@
|
||||
|
||||
<script src="/static/session.js"></script>
|
||||
<script src="/static/printer-switcher.js"></script>
|
||||
<script src="/static/common.js"></script>
|
||||
<script src="/static/printer-page-common.js"></script>
|
||||
<script src="/static/printer-shared.js"></script>
|
||||
<script src="/static/printer.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -319,179 +319,6 @@ function renderPrintStatus(status, error, controlMode) {
|
||||
renderPrintActionRow(state, controlMode);
|
||||
}
|
||||
|
||||
function collectMmuMaterialState(printStatus) {
|
||||
if (!printStatus || typeof printStatus !== "object") {
|
||||
return { visible: false, disabled: false, label: "MMU", rows: [] };
|
||||
}
|
||||
const mmu = printStatus.moonraker_status && printStatus.moonraker_status.mmu;
|
||||
if (!mmu || typeof mmu !== "object" || !Object.prototype.hasOwnProperty.call(mmu, "enabled")) {
|
||||
return { visible: false, disabled: false, label: "MMU", rows: [] };
|
||||
}
|
||||
const machine = printStatus.moonraker_status && printStatus.moonraker_status.mmu_machine;
|
||||
const label = machine?.unit_0?.name || "MMU";
|
||||
if (!isTruthyMetadataValue(mmu.enabled)) {
|
||||
return { visible: true, disabled: true, label, rows: [] };
|
||||
}
|
||||
const names = normalizeMetadataList(mmu.gate_filament_name);
|
||||
const materials = normalizeMetadataList(mmu.gate_material);
|
||||
const colors = normalizeMetadataList(mmu.gate_color);
|
||||
const count = Math.max(names.length, materials.length, colors.length, Number(mmu.num_gates || 0));
|
||||
if (count === 0) {
|
||||
return { visible: true, disabled: false, label, rows: [{ label, value: "Enabled" }] };
|
||||
}
|
||||
return {
|
||||
visible: true,
|
||||
disabled: false,
|
||||
label,
|
||||
rows: Array.from({ length: count }, (_, index) => {
|
||||
const parts = [];
|
||||
let color = "";
|
||||
if (names[index] && names[index] !== "Unknown") {
|
||||
parts.push(String(names[index]));
|
||||
}
|
||||
if (materials[index] && materials[index] !== names[index]) {
|
||||
parts.push(`(${String(materials[index])})`);
|
||||
}
|
||||
if (colors[index]) {
|
||||
color = String(colors[index]).replace(/[^0-9a-fA-F]/g, "");
|
||||
}
|
||||
return {
|
||||
label: `Gate ${index + 1}`,
|
||||
value: parts.join(" ") || "Unknown material",
|
||||
color,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeMetadataList(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.filter((item) => item !== null && item !== undefined && item !== "");
|
||||
}
|
||||
if (value === null || value === undefined || value === "") {
|
||||
return [];
|
||||
}
|
||||
return [value];
|
||||
}
|
||||
|
||||
function isTruthyMetadataValue(value) {
|
||||
if (Array.isArray(value)) {
|
||||
return value.length > 0;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
return normalized === "1" || normalized === "true" || normalized === "yes";
|
||||
}
|
||||
return Boolean(value);
|
||||
}
|
||||
|
||||
function mmuMaterialChipHtml(row, disabled) {
|
||||
const label = escapeHTML(row && row.label ? row.label : "");
|
||||
const value = row && row.value ? escapeHTML(row.value) : "";
|
||||
const color = row && row.color ? mmuNormalizeHexColor(row.color) : "";
|
||||
const style = color ? ` style="--mmu-swatch:${escapeHTML(color)}"` : "";
|
||||
const classes = ["mmu-material-chip"];
|
||||
if (disabled) {
|
||||
classes.push("mmu-disabled");
|
||||
}
|
||||
return `<span class="${classes.join(" ")}"${style}><span class="mmu-material-chip-swatch"></span><span class="mmu-material-chip-text"><strong>${label}</strong>${value ? ` ${value}` : ""}</span></span>`;
|
||||
}
|
||||
|
||||
function mmuNormalizeHexColor(value) {
|
||||
const raw = String(value || "").trim().replace(/[^0-9a-fA-F]/g, "");
|
||||
if (raw.length === 8) {
|
||||
return `#${raw.slice(0, 6)}`;
|
||||
}
|
||||
if (raw.length === 6) {
|
||||
return `#${raw}`;
|
||||
}
|
||||
if (raw.length === 3) {
|
||||
return `#${raw.split("").map((ch) => ch + ch).join("")}`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function formatTemperatureSummary(printStatus) {
|
||||
const state = collectTemperatureState(printStatus);
|
||||
if (!state.visible) {
|
||||
return "";
|
||||
}
|
||||
return state.rows.map((row) => `${row.label} ${row.value}`).join(" · ");
|
||||
}
|
||||
|
||||
function collectTemperatureState(printStatus) {
|
||||
const status = printStatus && printStatus.moonraker_status;
|
||||
if (!status || typeof status !== "object") {
|
||||
return { visible: false, rows: [] };
|
||||
}
|
||||
const rows = [];
|
||||
const seen = new Set();
|
||||
const add = (key, label) => {
|
||||
if (!Object.prototype.hasOwnProperty.call(status, key)) {
|
||||
return;
|
||||
}
|
||||
const sensor = status[key];
|
||||
if (!sensor || typeof sensor !== "object") {
|
||||
return;
|
||||
}
|
||||
const value = formatTemperatureReading(sensor);
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
rows.push({ label, value });
|
||||
seen.add(key);
|
||||
};
|
||||
add("extruder", "Hotend");
|
||||
add("heater_bed", "Bed");
|
||||
add("temperature_sensor chamber", "Chamber");
|
||||
Object.keys(status).sort().forEach((key) => {
|
||||
if (seen.has(key) || !isTemperatureSensorKey(key)) {
|
||||
return;
|
||||
}
|
||||
const sensor = status[key];
|
||||
if (!sensor || typeof sensor !== "object") {
|
||||
return;
|
||||
}
|
||||
const value = formatTemperatureReading(sensor);
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
rows.push({ label: prettifyTemperatureLabel(key), value });
|
||||
});
|
||||
return { visible: rows.length > 0, rows };
|
||||
}
|
||||
|
||||
function isTemperatureSensorKey(key) {
|
||||
return typeof key === "string" && (
|
||||
key.startsWith("temperature_sensor ") ||
|
||||
key.startsWith("temperature_fan ") ||
|
||||
key.startsWith("temperature_probe ")
|
||||
);
|
||||
}
|
||||
|
||||
function prettifyTemperatureLabel(key) {
|
||||
const raw = String(key || "")
|
||||
.replace(/^temperature_(sensor|fan|probe)\s+/i, "")
|
||||
.replace(/_/g, " ")
|
||||
.trim();
|
||||
if (!raw) {
|
||||
return "Sensor";
|
||||
}
|
||||
return raw.replace(/\b\w/g, (ch) => ch.toUpperCase());
|
||||
}
|
||||
|
||||
function formatTemperatureReading(sensor) {
|
||||
const current = Number(sensor.temperature);
|
||||
if (!Number.isFinite(current)) {
|
||||
return "";
|
||||
}
|
||||
const target = Number(sensor.target);
|
||||
if (Number.isFinite(target) && target > 0) {
|
||||
return `${current.toFixed(1)} / ${target.toFixed(1)}\u00b0C`;
|
||||
}
|
||||
return `${current.toFixed(1)}\u00b0C`;
|
||||
}
|
||||
|
||||
function renderPrintActionRow(state, controlMode) {
|
||||
if (!printActionRow) {
|
||||
return;
|
||||
@@ -1011,128 +838,12 @@ function cameraMeta(camera) {
|
||||
return parts.join(" / ") || "Configured camera";
|
||||
}
|
||||
|
||||
function relativeTime(unixSeconds) {
|
||||
const seconds = Math.max(0, Math.floor(Date.now() / 1000) - unixSeconds);
|
||||
if (seconds < 5) {
|
||||
return "just now";
|
||||
}
|
||||
if (seconds < 60) {
|
||||
return `${seconds}s ago`;
|
||||
}
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
if (minutes < 60) {
|
||||
return `${minutes}m ago`;
|
||||
}
|
||||
const hours = Math.floor(minutes / 60);
|
||||
return `${hours}h ago`;
|
||||
}
|
||||
|
||||
function setStatus(label, ok) {
|
||||
status.textContent = label;
|
||||
status.classList.toggle("ok", ok);
|
||||
status.classList.toggle("warn", !ok);
|
||||
}
|
||||
|
||||
function fact(label, value) {
|
||||
return `<div><dt>${escapeHTML(label)}</dt><dd>${escapeHTML(value)}</dd></div>`;
|
||||
}
|
||||
|
||||
function computeSettingsSync(settings, health) {
|
||||
const updatedAt = parseTimestamp(settings.updated_at);
|
||||
const lastSyncedAt = parseTimestamp(settings.last_synced_at);
|
||||
const controlMode = String(settings.control_mode || "").toLowerCase() || "readonly";
|
||||
const appliedMode = String(health.control_mode || "").toLowerCase() || "unknown";
|
||||
const uiURL = String(settings.ui_url || "").trim();
|
||||
const agentUIURL = String(health.ui_url || "").trim();
|
||||
const autoDetect = Boolean(settings.ui_auto_detect);
|
||||
const agentAutoDetect = Boolean(health.ui_auto_detect);
|
||||
const synced = Boolean(updatedAt && lastSyncedAt && lastSyncedAt >= updatedAt);
|
||||
if (synced) {
|
||||
return {
|
||||
synced: true,
|
||||
state: "Synced",
|
||||
detail: "Agent acknowledged the latest saved settings.",
|
||||
};
|
||||
}
|
||||
const detailBits = [];
|
||||
if (controlMode !== appliedMode && appliedMode !== "unknown") {
|
||||
detailBits.push(`control mode ${controlMode} -> ${appliedMode}`);
|
||||
}
|
||||
if (uiURL && uiURL !== agentUIURL) {
|
||||
detailBits.push(`UI URL ${uiURL} -> ${agentUIURL || "unset"}`);
|
||||
}
|
||||
if (autoDetect !== agentAutoDetect) {
|
||||
detailBits.push(`auto-detect ${autoDetect} -> ${agentAutoDetect}`);
|
||||
}
|
||||
return {
|
||||
synced: false,
|
||||
state: "Pending",
|
||||
detail: detailBits.length > 0 ? `Waiting for agent confirmation; ${detailBits.join("; ")}` : "Waiting for the agent to confirm the latest settings.",
|
||||
};
|
||||
}
|
||||
|
||||
function parseTimestamp(value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isFinite(parsed) ? parsed : 0;
|
||||
}
|
||||
|
||||
function formatProgress(value) {
|
||||
const progress = Number(value);
|
||||
if (!Number.isFinite(progress) || progress <= 0) {
|
||||
return "0%";
|
||||
}
|
||||
if (progress <= 1) {
|
||||
return `${Math.round(progress * 100)}%`;
|
||||
}
|
||||
return `${Math.round(progress)}%`;
|
||||
}
|
||||
|
||||
function formatDuration(value) {
|
||||
const seconds = Number(value);
|
||||
if (!Number.isFinite(seconds) || seconds < 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
const whole = Math.floor(seconds);
|
||||
const hours = Math.floor(whole / 3600);
|
||||
const minutes = Math.floor((whole % 3600) / 60);
|
||||
const secs = whole % 60;
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${minutes}m ${secs}s`;
|
||||
}
|
||||
if (minutes > 0) {
|
||||
return `${minutes}m ${secs}s`;
|
||||
}
|
||||
return `${secs}s`;
|
||||
}
|
||||
|
||||
function formatFilament(value) {
|
||||
const number = Number(value);
|
||||
if (!Number.isFinite(number) || number <= 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
return `${number.toFixed(1)} mm`;
|
||||
}
|
||||
|
||||
function formatHistoryTime(value) {
|
||||
const unix = Number(value);
|
||||
if (!Number.isFinite(unix) || unix <= 0) {
|
||||
return "Unknown";
|
||||
}
|
||||
return new Date(unix * 1000).toLocaleString();
|
||||
}
|
||||
|
||||
function escapeHTML(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
refresh.addEventListener("click", loadPrinter);
|
||||
loadSession().then(loadPrinter);
|
||||
setInterval(loadPrinter, 15000);
|
||||
|
||||
Reference in new issue
Block a user