From 149abb716aded1d9ad44c7776ab096b6fbc210c6 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Tue, 12 Aug 2025 16:09:08 -0600 Subject: [PATCH] Added a bunch of operational functions --- main.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/main.js b/main.js index 6e3d4c7..38f6018 100644 --- a/main.js +++ b/main.js @@ -25,6 +25,92 @@ class auxillery_dialog { } } +// Functions + +function systemMsg (message,level=0) { // abstraction? + // Yes, but it allows us to easilly redirect error + // output to where we want, for now, just the console... + switch (level) { + case 0: + console.log("INFO: "+message); + break; + case 1: + console.error("WARNING: "+message); + break; + case 2: + console.error("ERROR: "+message); + break; + case 3: + console.log("DEBUG: "+message); + break; + default: + console.log(message); + } + // but later we will also have it push an error toast in the ui. +} + +function inRange(target,min,max) { + if (isNaN(target) || isNaN(min) || isNaN(max)) { return false; } + return (min <= target && target <= max); +} + +function invokeLibrary(e) { + // Breaking this out of event so its not an + // annon function, so we can swap it in and out + var targetID = ((e.currentTarget.getAttribute("id")).split("pec"))[1]; + if (isNaN(targetID)) { return systemMsg("invokeLibrary targetID not a number",2); } + var targetPec = app.gui["pec"+targetID]; + if (undefined == targetPec) { return systemMsg("invokeLibrary targetPec undefined: "+targetID,2); } + if (!inRange(targetID,0,15)) { return systemMsg("invokeLibrary targetID out of range: "+targetPecIndex,2); } + app.targetPec = targetPec; + app.targetPecIndex = targetID; + app.gui.library.root.classList.add("focused"); +} + +function clearPec (index=null) { + if (!inRange(index,0,15)) { return systemMsg("clearPec was given an out of range index",2); } + var targetPec = app.gui["pec"+index]; + if (undefined == targetPec) { return systemMsg("clearPec targetPec undefined",2); } + targetPec.classList.add("empty"); + targetPec.innerHTML = "

+

"; + targetPec.addEventListener("click", invokeLibrary); +} + +function setPec(pecTemplate=null, targetIndex=null) { + if (null == pecTemplate) { return systemMsg("setPec was not provided data",2); } + if (!(pecTemplate instanceof pec)) { return systemMsg("setPec was not provided a valid template",2); } + if (isNaN(targetIndex)) { return systemMsg("setPec was not provided a valid targetIndex",2); } + var targetPec = app.gui["pec"+targetIndex]; + targetPec.removeEventListener("click", invokeLibrary); + targetPec.innerHTML = ""; + targetPec.classList.remove("empty"); + targetPec.appendChild(pecTemplate.DOM); +} + +function repopGrid (board=null) { + if (null == board) { return systemMsg("repopGrid was not provided data",2); } + if (!(board instanceof pecBoard)) { return systemMsg("repopGrid was not provided data of type pecBoard",2); } + board.pecs.forEach((p,i)=>{ + if (null == p) { return clearPec(i); } + if (!(p instanceof pec)) { + systemMsg("Invalid Pec found, scrubbing and returning to null",1); + board.pecs[i] = null; + return clearPec(i); + } + setPec(p,i); + }); +} + +function useThisPec (e) { + var sourceID = ((e.currentTarget.getAttribute("id")).split("lPec"))[1]; + app.gui.library.root.classList.remove("focused"); + app.gui.library.body.childNodes.entries().forEach(p=>{ + p[1].getAttribute("id") == "lPec"+sourceID ? p[1].style.display = "none" : null; + }); + app.currentBoard.pecs[app.targetPecIndex] = app.library[sourceID]; + repopGrid(app.currentBoard); +} + // Initialize gui window.app = {};