From 6076c05cfb3ff147b1ac6661dc9f7f98eef8c6c6 Mon Sep 17 00:00:00 2001 From: grey Date: Fri, 4 Mar 2022 21:09:28 +0100 Subject: [PATCH] more senisble default values, added layout persistance --- .gitignore | 1 + index.js | 45 +++++++++++++++++++++++++++++++++++- templates/newAdminPanel.html | 36 +++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index bcc7222..b3aed83 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules rename.sh +data-persistence.json diff --git a/index.js b/index.js index 6f34552..ce084e5 100644 --- a/index.js +++ b/index.js @@ -16,13 +16,22 @@ app.use( }) ); +let loadedData = {} + +if(fs.existsSync("data-persistence.json")) { + const loadedDataRaw = fs.readFileSync("data-persistence.json", "utf8"); + loadedData = JSON.parse(loadedDataRaw); +}else{ + console.warn("Unable to load persistent data"); +} + currentState = { mode: "clock", countdownGoal: new Date().getTime(), showMilliSeconds: true, defaultFullScreen: true, timeAmountInital: 0, - timerRunState: true, + timerRunState: false, pauseMoment: 0, showTimeOnCountdown: true, message: "", @@ -36,8 +45,16 @@ currentState = { debug: false }; +const dataToBeWritten = {}; + +currentState = Object.assign({}, currentState, loadedData); + +console.log(currentState) + currentState.textColors = currentState.colorSegments + + console.log("Preparing routes..."); app.get("/", function (req, res) { const data = fs.readFileSync("templates/newAdminPanel.html", "utf8"); @@ -67,6 +84,9 @@ app.get("/api/v1/set/mode", function (req, res) { app.get("/api/v1/set/layout/showMillis", function (req, res) { currentState.showMilliSeconds = (req.query.show === 'true'); + if(req.query.persist === 'true'){ + dataToBeWritten.showMilliSeconds = currentState.showMilliSeconds + } res.json({ status: "ok" }); }); @@ -103,17 +123,26 @@ app.get("/api/v1/ctrl/timer/restart", function (req, res) { app.get("/api/v1/set/layout/showTime", function (req, res) { currentState.showTimeOnCountdown = (req.query.show === 'true'); + if(req.query.persist === 'true'){ + dataToBeWritten.showTimeOnCountdown = currentState.showTimeOnCountdown + } res.json({ status: "ok" }); }); app.get("/api/v1/set/progressbar/show", function (req, res) { currentState.showProgressbar = (req.query.show === 'true'); + if(req.query.persist === 'true'){ + dataToBeWritten.showProgressbar = currentState.showProgressbar + } res.json({ status: "ok" }); }); app.get("/api/v1/set/progressbar/colors", function (req, res) { try { currentState.colorSegments = JSON.parse(req.query.colors); + if(req.query.persist === 'true'){ + dataToBeWritten.colorSegments = currentState.colorSegments + } res.json({ status: "ok" }); } catch (error) { res.json({ status: "error", message: error }); @@ -127,6 +156,9 @@ app.get("/api/v1/set/text/colors", function (req, res) { } else { currentState.textColors = JSON.parse(req.query.colors); } + if(req.query.persist === 'true'){ + dataToBeWritten.textColors = currentState.textColors + } res.json({ status: "ok" }); } catch (error) { res.json({ status: "error", message: error }); @@ -135,6 +167,9 @@ app.get("/api/v1/set/text/colors", function (req, res) { app.get("/api/v1/set/text/enableColoring", function (req, res) { currentState.enableColoredText = (req.query.enable === 'true'); + if(req.query.persist === 'true'){ + dataToBeWritten.enableColoredText = currentState.enableColoredText + } res.json({ status: "ok" }); }); @@ -154,6 +189,14 @@ app.get("/api/v1/ctrl/message/hide", function (req, res) { currentState.showMessage = false res.json({ status: "ok" }); }); + +app.get("/api/v1/storage/commit", function (req, res) { + const tempString = JSON.stringify(dataToBeWritten); + fs.writeFileSync("data-persistence.json", tempString); + res.json({ status: "ok" }); +}); + + console.log("Starting server..."); const port = 8005 app.listen(port); diff --git a/templates/newAdminPanel.html b/templates/newAdminPanel.html index 417ac57..aff67e7 100644 --- a/templates/newAdminPanel.html +++ b/templates/newAdminPanel.html @@ -567,6 +567,42 @@ }) + $("#saveLayout").click(function (event) { + $("#saveLayout")[0].innerHTML = '
' + + // Collect all data, build all paths3 + const allPathes = []; + + const showTimeB = $("#showTime")[0].checked + const showMillisB = $("#showMillis")[0].checked + const progBarShowB = $("#progBarShow")[0].checked + const textColorsB = $("#textColors")[0].checked + + allPathes.push("/api/v1/set/layout/showTime?persist=true&show=" + showTimeB) + allPathes.push("/api/v1/set/layout/showMillis?persist=true&show=" + showMillisB) + allPathes.push("/api/v1/set/progressbar/show?persist=true&show=" + progBarShowB) + allPathes.push("/api/v1/set/text/enableColoring?persist=true&enable=" + textColorsB) + + for (pI in allPathes) { + const path = allPathes[pI]; + console.warn(path) + saveOption(path, function (event) { + console.debug(event) + }) + } + + saveOption("/api/v1/storage/commit", function (event) { + console.debug(event) + setTimeout(function () { + $("#saveLayout")[0].innerHTML = 'Save as startup settings (Layout only)' + }, 500) + }) + + + + }) + + function msToTime(s, data) { var ms = s % 1000; s = (s - ms) / 1000;