From 403099ebd6405c1450651ee085b9ba9bffd8a686 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 14 Feb 2023 12:42:31 +0100 Subject: [PATCH] refactoring --- index.html | 12 +++++++----- js/functions.js | 48 +++++++++++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/index.html b/index.html index 2d0bea6..0f1ba5d 100644 --- a/index.html +++ b/index.html @@ -1,8 +1,9 @@ - + + Genderwatch-protocol @@ -122,13 +123,14 @@ closedTopics = [] } - watches = JSON.parse(localStorage.getItem("watches")) - if(!watches){ + actualTopic = JSON.parse(localStorage.getItem("actualTopic")) + if(!actualTopic) { + actualTopic = {} reset() } + document.getElementById("topicInput").value = actualTopic.name - document.getElementById("topicInput").value = localStorage.getItem("actualTopic") - document.getElementById("topicInput").addEventListener('keyup', saveActualTopicToLocalStorage); + document.getElementById("topicInput").addEventListener('keyup', saveActualTopicName); refreshClosedTopics() setInterval(() => { refreshWatch(0) diff --git a/js/functions.js b/js/functions.js index 28c419e..f97adc9 100644 --- a/js/functions.js +++ b/js/functions.js @@ -1,6 +1,6 @@ function reset() { actualTopic.watches = [{"actions": []}, {"actions": []}, {"actions": []}] - saveWatchesToLocalStorage() + saveActualTopicToLocalStorage() } function resetButton() { @@ -24,8 +24,9 @@ function buttonDeleteClosedTopic(topicIndex) { } function closeTopic() { + stop() let defaultname = "Thema" - let name = document.getElementById("topicInput").value + let name = actualTopic.name let nameLessCounter = 1 while(!name){ let foundName = false @@ -40,11 +41,11 @@ function closeTopic() { name = defaultname + nameLessCounter } } - - closedTopics.push({"name": name, "watches": watches}) - stop() + actualTopic.name = name + closedTopics.push(actualTopic) + actualTopic = {"name": ""} reset() - document.getElementById("topicInput").value = "" + document.getElementById("topicInput").value = actualTopic.name saveActualTopicToLocalStorage() saveClosedTopicsToLocalStorage() refreshClosedTopics() @@ -57,36 +58,36 @@ function deleteTopic(topicIndex) { } function reopenTopic(topicIndex) { - if(watches[0]["actions"]["length"] > 0 || watches[1]["actions"]["length"] > 0 ||watches[2]["actions"]["length"] > 0 || document.getElementById("topicInput").value != "") { + if(actualTopic.watches[0]["actions"]["length"] > 0 || actualTopic.watches[1]["actions"]["length"] > 0 ||actualTopic.watches[2]["actions"]["length"] > 0 || actualTopic.name !== "") { closeTopic() } - watches = closedTopics[topicIndex]["watches"] - document.getElementById("topicInput").value = closedTopics[topicIndex]["name"] + actualTopic = closedTopics[topicIndex] + document.getElementById("topicInput").value = actualTopic.name deleteTopic(topicIndex) - saveWatchesToLocalStorage() + saveClosedTopicsToLocalStorage() saveActualTopicToLocalStorage() } function start(watchIndex) { stop() - watches[watchIndex].actions.push({"type": "start", "time": new Date().getTime()}) - saveWatchesToLocalStorage() + actualTopic.watches[watchIndex].actions.push({"type": "start", "time": new Date().getTime()}) + saveActualTopicToLocalStorage() } function stop() { - watches.forEach(watch => { + actualTopic.watches.forEach(watch => { if(watch["actions"]["length"] > 0) { - if(watch["actions"]["length"] % 2 == 1){ + if(watch["actions"]["length"] % 2 === 1){ watch["actions"].push({"type": "stop", "time": new Date().getTime()}) } } }); - saveWatchesToLocalStorage() + saveActualTopicToLocalStorage() } function calcPercForWatch(watchIndex, topic) { let sumAll = sumAllwatches(topic) - if(sumAll == 0) { + if(sumAll === 0) { return 0 } return Math.round(sumForWatch(topic.watches[watchIndex]) / sumAllwatches(topic) * 100) @@ -112,7 +113,7 @@ function countForWatch(watch) { function calcPercCountForWatch(watchindex, topic) { let sumAll = sumCountAllWatches(topic) - if(sumAll == 0) { + if(sumAll === 0) { return 0 } return Math.round(countForWatch(topic.watches[watchindex]) / sumAll * 100) @@ -132,14 +133,14 @@ function timeToISO(time){ function refreshWatch(watchIndex) { let timeCell = document.getElementById("time-" + watchIndex) - timeCell.textContent = timeToISO(sumForWatch(watches[watchIndex])) - if(watches[watchIndex]["actions"].length != 0 && watches[watchIndex]["actions"].slice(-1)[0]["type"] == "start"){ + timeCell.textContent = timeToISO(sumForWatch(actualTopic.watches[watchIndex])) + if(actualTopic.watches[watchIndex]["actions"].length !== 0 && actualTopic.watches[watchIndex]["actions"].slice(-1)[0]["type"] === "start"){ timeCell.classList.add("active") } else { timeCell.classList.remove("active") } let countCell = document.getElementById("count-" + watchIndex) - countCell.textContent = countForWatch(watches[watchIndex]) + " Wortmeldungen" + countCell.textContent = countForWatch(actualTopic.watches[watchIndex]) + " Wortmeldungen" } function refreshClosedTopics() { @@ -178,12 +179,13 @@ function refreshClosedTopics() { } } -function saveWatchesToLocalStorage() { - localStorage.setItem("watches", JSON.stringify(watches)) +function saveActualTopicName() { + actualTopic.name = document.getElementById("topicInput").value + saveActualTopicToLocalStorage() } function saveActualTopicToLocalStorage() { - localStorage.setItem("actualTopic", document.getElementById("topicInput").value) + localStorage.setItem("actualTopic", JSON.stringify(actualTopic)) } function saveClosedTopicsToLocalStorage() {