diff --git a/samy.js b/samy.js index af197f9..aec1fa9 100644 --- a/samy.js +++ b/samy.js @@ -459,6 +459,7 @@ async function installSelectedPrinters() { // ======================================================================= // Run Selected (main trigger) // ======================================================================= + async function triggerInstall() { const runBtn = document.querySelector(".run-button"); if (!runBtn) return; @@ -469,46 +470,41 @@ async function triggerInstall() { if (statusBox) statusBox.innerHTML = ""; try { - // Figure out which standard tasks are checked + // Grab special-case elements ONCE + const dattoCB = document.getElementById("installDattoRMM"); + const svsCB = document.getElementById("installSVSMSPModule"); + const renameCB = document.getElementById("renameComputer"); + const newNameInput = document.getElementById("txtNewComputerName"); + + // Standard tasks are all tasks EXCEPT special-case ones const checkedTasks = tasks.filter((t) => { if (["installDattoRMM", "installSVSMSPModule", "renameComputer"].includes(t.id)) return false; const cb = document.getElementById(t.id); return cb && cb.checked; }); - // Rename checkbox / textbox - const renameCB = document.getElementById("renameComputer"); - const newNameInput = document.getElementById("txtNewComputerName"); - - // Count special-case tasks (handled outside the main loop) + // Count special-case tasks let specialTasks = 0; - - const dattoCB = document.getElementById("installDattoRMM"); if (dattoCB && dattoCB.checked) specialTasks++; - - const svsCB = document.getElementById("installSVSMSPModule"); if (svsCB && svsCB.checked) specialTasks++; - // Rename is also a special-case task - const renameCB = document.getElementById("renameComputer"); - let extraTasks = 0; - if (renameCB && renameCB.checked) extraTasks = 1; + const extraTasks = (renameCB && renameCB.checked) ? 1 : 0; - if ((checkedTasks.length + specialTasks + extraTasks) === 0) { + const total = checkedTasks.length + specialTasks + extraTasks; + + if (total === 0) { alert("Please select at least one task."); return; } + setTotalTaskCount(total); - setTotalTaskCount(checkedTasks.length + specialTasks + extraTasks); - - - // 1. DattoRMM first - const dattoCB = document.getElementById("installDattoRMM"); + // 1) DattoRMM first if (dattoCB && dattoCB.checked) { const sub = Array.from( document.querySelectorAll(".sub-option-installDattoRMM:checked") ).map((x) => x.value); + const dropdown = document.getElementById("dattoDropdown"); const uid = dropdown?.value; const name = dropdown?.selectedOptions?.[0]?.text || "Datto"; @@ -531,8 +527,7 @@ async function triggerInstall() { } } - // 2. SVSMSP module second - const svsCB = document.getElementById("installSVSMSPModule"); + // 2) SVSMSP module second if (svsCB && svsCB.checked) { try { await fetch("/installSVSMSPModule", { method: "GET" }); @@ -543,7 +538,7 @@ async function triggerInstall() { } } - // 3. Remaining tasks + // 3) Remaining tasks for (const t of tasks) { if (["installDattoRMM", "installSVSMSPModule", "renameComputer"].includes(t.id)) continue; @@ -559,54 +554,14 @@ async function triggerInstall() { } } - // 4. Rename computer (LAST) + // 4) Rename computer (LAST) if (renameCB && renameCB.checked && newNameInput) { const newName = newNameInput.value.trim(); - // Same basic rules you'll enforce server-side const nameIsValid = newName.length > 0 && newName.length <= 15 && - /^[A-Za-z0-9-]+$/.test(newName); - - if (!nameIsValid) { - alert( - "Invalid computer name. Must be 1-15 characters and only letters, numbers, and hyphens." - ); - // still mark it as a failed task so progress reaches 100% - logProgress("Rename computer", false); - } else { - try { - await fetch("/renameComputer", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ newName }), - }); - logProgress("Rename computer", true); - } catch (e) { - console.error("Error calling /renameComputer:", e); - logProgress("Rename computer", false); - } - } - } - } catch (e) { - console.error("triggerInstall fatal error:", e); - } finally { - runBtn.disabled = false; - if (totalTasks > 0) { - console.info( - `[Info] All tasks completed (${completedTasks}/${totalTasks})` - ); - } - - // Best-effort notification to the server - try { - await fetch("/tasksCompleted", { method: "POST" }); - } catch (err) { - console.warn("Could not notify server about completion:", err); - } - } -} + /^[A-Za-z]() // =======================================================================