refactoring

This commit is contained in:
schrom01 2023-02-14 12:42:31 +01:00
parent 5e0793c7fc
commit 403099ebd6
2 changed files with 32 additions and 28 deletions

View File

@ -1,8 +1,9 @@
<html> <html lang="de">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="stylesheet" href="css/custom.css"> <link rel="stylesheet" href="css/custom.css">
<link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/bootstrap.css">
<title>Genderwatch-protocol</title>
</head> </head>
<body> <body>
@ -122,13 +123,14 @@
closedTopics = [] closedTopics = []
} }
watches = JSON.parse(localStorage.getItem("watches")) actualTopic = JSON.parse(localStorage.getItem("actualTopic"))
if(!watches){ if(!actualTopic) {
actualTopic = {}
reset() reset()
} }
document.getElementById("topicInput").value = actualTopic.name
document.getElementById("topicInput").value = localStorage.getItem("actualTopic") document.getElementById("topicInput").addEventListener('keyup', saveActualTopicName);
document.getElementById("topicInput").addEventListener('keyup', saveActualTopicToLocalStorage);
refreshClosedTopics() refreshClosedTopics()
setInterval(() => { setInterval(() => {
refreshWatch(0) refreshWatch(0)

View File

@ -1,6 +1,6 @@
function reset() { function reset() {
actualTopic.watches = [{"actions": []}, {"actions": []}, {"actions": []}] actualTopic.watches = [{"actions": []}, {"actions": []}, {"actions": []}]
saveWatchesToLocalStorage() saveActualTopicToLocalStorage()
} }
function resetButton() { function resetButton() {
@ -24,8 +24,9 @@ function buttonDeleteClosedTopic(topicIndex) {
} }
function closeTopic() { function closeTopic() {
stop()
let defaultname = "Thema" let defaultname = "Thema"
let name = document.getElementById("topicInput").value let name = actualTopic.name
let nameLessCounter = 1 let nameLessCounter = 1
while(!name){ while(!name){
let foundName = false let foundName = false
@ -40,11 +41,11 @@ function closeTopic() {
name = defaultname + nameLessCounter name = defaultname + nameLessCounter
} }
} }
actualTopic.name = name
closedTopics.push({"name": name, "watches": watches}) closedTopics.push(actualTopic)
stop() actualTopic = {"name": ""}
reset() reset()
document.getElementById("topicInput").value = "" document.getElementById("topicInput").value = actualTopic.name
saveActualTopicToLocalStorage() saveActualTopicToLocalStorage()
saveClosedTopicsToLocalStorage() saveClosedTopicsToLocalStorage()
refreshClosedTopics() refreshClosedTopics()
@ -57,36 +58,36 @@ function deleteTopic(topicIndex) {
} }
function reopenTopic(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() closeTopic()
} }
watches = closedTopics[topicIndex]["watches"] actualTopic = closedTopics[topicIndex]
document.getElementById("topicInput").value = closedTopics[topicIndex]["name"] document.getElementById("topicInput").value = actualTopic.name
deleteTopic(topicIndex) deleteTopic(topicIndex)
saveWatchesToLocalStorage() saveClosedTopicsToLocalStorage()
saveActualTopicToLocalStorage() saveActualTopicToLocalStorage()
} }
function start(watchIndex) { function start(watchIndex) {
stop() stop()
watches[watchIndex].actions.push({"type": "start", "time": new Date().getTime()}) actualTopic.watches[watchIndex].actions.push({"type": "start", "time": new Date().getTime()})
saveWatchesToLocalStorage() saveActualTopicToLocalStorage()
} }
function stop() { function stop() {
watches.forEach(watch => { actualTopic.watches.forEach(watch => {
if(watch["actions"]["length"] > 0) { 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()}) watch["actions"].push({"type": "stop", "time": new Date().getTime()})
} }
} }
}); });
saveWatchesToLocalStorage() saveActualTopicToLocalStorage()
} }
function calcPercForWatch(watchIndex, topic) { function calcPercForWatch(watchIndex, topic) {
let sumAll = sumAllwatches(topic) let sumAll = sumAllwatches(topic)
if(sumAll == 0) { if(sumAll === 0) {
return 0 return 0
} }
return Math.round(sumForWatch(topic.watches[watchIndex]) / sumAllwatches(topic) * 100) return Math.round(sumForWatch(topic.watches[watchIndex]) / sumAllwatches(topic) * 100)
@ -112,7 +113,7 @@ function countForWatch(watch) {
function calcPercCountForWatch(watchindex, topic) { function calcPercCountForWatch(watchindex, topic) {
let sumAll = sumCountAllWatches(topic) let sumAll = sumCountAllWatches(topic)
if(sumAll == 0) { if(sumAll === 0) {
return 0 return 0
} }
return Math.round(countForWatch(topic.watches[watchindex]) / sumAll * 100) return Math.round(countForWatch(topic.watches[watchindex]) / sumAll * 100)
@ -132,14 +133,14 @@ function timeToISO(time){
function refreshWatch(watchIndex) { function refreshWatch(watchIndex) {
let timeCell = document.getElementById("time-" + watchIndex) let timeCell = document.getElementById("time-" + watchIndex)
timeCell.textContent = timeToISO(sumForWatch(watches[watchIndex])) timeCell.textContent = timeToISO(sumForWatch(actualTopic.watches[watchIndex]))
if(watches[watchIndex]["actions"].length != 0 && watches[watchIndex]["actions"].slice(-1)[0]["type"] == "start"){ if(actualTopic.watches[watchIndex]["actions"].length !== 0 && actualTopic.watches[watchIndex]["actions"].slice(-1)[0]["type"] === "start"){
timeCell.classList.add("active") timeCell.classList.add("active")
} else { } else {
timeCell.classList.remove("active") timeCell.classList.remove("active")
} }
let countCell = document.getElementById("count-" + watchIndex) let countCell = document.getElementById("count-" + watchIndex)
countCell.textContent = countForWatch(watches[watchIndex]) + " Wortmeldungen" countCell.textContent = countForWatch(actualTopic.watches[watchIndex]) + " Wortmeldungen"
} }
function refreshClosedTopics() { function refreshClosedTopics() {
@ -178,12 +179,13 @@ function refreshClosedTopics() {
} }
} }
function saveWatchesToLocalStorage() { function saveActualTopicName() {
localStorage.setItem("watches", JSON.stringify(watches)) actualTopic.name = document.getElementById("topicInput").value
saveActualTopicToLocalStorage()
} }
function saveActualTopicToLocalStorage() { function saveActualTopicToLocalStorage() {
localStorage.setItem("actualTopic", document.getElementById("topicInput").value) localStorage.setItem("actualTopic", JSON.stringify(actualTopic))
} }
function saveClosedTopicsToLocalStorage() { function saveClosedTopicsToLocalStorage() {