-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
354 lines (304 loc) · 11.5 KB
/
script.js
File metadata and controls
354 lines (304 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const STORAGE_KEY = "sportApp_Multi_v1";
const SETTINGS_KEY = "sportApp_Settings_v3";
const exercises = [
"Liegestütze", "Kniebeugen", "Sit-ups", "Klimmzüge", "Burpees",
];
let currentExercise = "Liegestütze";
let settings = { showAllHistory: false };
let myChart = null;
let chartFullLabels = [];
let chartFullData = [];
window.onload = function() {
loadSettings();
var today = new Date().toISOString().split('T')[0];
var picker = document.getElementById("datePicker");
if(picker) picker.value = today;
updateStatusDisplay();
showHistory();
updateExerciseTitle();
};
function loadSettings() {
var saved = JSON.parse(localStorage.getItem(SETTINGS_KEY));
if(saved) settings = saved;
var toggleHist = document.getElementById("showAllToggle");
if(toggleHist) toggleHist.checked = settings.showAllHistory;
}
function updateSettings() {
var toggleHist = document.getElementById("showAllToggle");
settings.showAllHistory = toggleHist.checked;
localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
showHistory();
}
// --- HAUPT LOGIK ---
function saveTraining() {
var input = document.getElementById("repInput");
var count = parseInt(input.value);
if(!count) return;
saveToStorage(count);
input.value = "";
input.focus();
}
function addTen() { saveToStorage(10); }
function openExerciseMenu() {
var container = document.getElementById("exerciseListContainer");
container.innerHTML = "";
exercises.forEach(function(ex) {
var btn = document.createElement("button");
btn.className = "exercise-option";
if(ex === currentExercise) btn.className += " selected";
btn.innerText = ex;
btn.onclick = function() { selectExercise(ex); };
container.appendChild(btn);
});
document.getElementById("exerciseModal").style.display = "flex";
}
function selectExercise(name) {
currentExercise = name;
updateExerciseTitle();
closeExerciseMenu();
showHistory();
}
function closeExerciseMenu() { document.getElementById("exerciseModal").style.display = "none"; }
function openSettings() { document.getElementById("settingsModal").style.display = "flex"; }
function closeSettings() { document.getElementById("settingsModal").style.display = "none"; }
function updateExerciseTitle() { document.getElementById("currentExerciseTitle").innerText = currentExercise; }
function updateStatusDisplay() {
var selectedDate = document.getElementById("datePicker").value;
var today = new Date().toISOString().split('T')[0];
var display = document.getElementById("statusDisplay");
var dateObj = new Date(selectedDate);
var germanDate = dateObj.toLocaleDateString('de-DE', {day: '2-digit', month: '2-digit'});
if(selectedDate === today) {
display.innerHTML = "Heute";
display.style.color = "#888";
} else {
display.innerHTML = germanDate;
display.style.color = "#d63384";
}
}
// --- SPEICHERN ---
function saveToStorage(amountToAdd) {
var selectedDate = document.getElementById("datePicker").value;
var dateObj = new Date(selectedDate);
var displayDate = dateObj.toLocaleDateString('de-DE');
var history = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
var found = false;
for (var i = 0; i < history.length; i++) {
if (history[i].rawDate === selectedDate && history[i].exercise === currentExercise) {
history[i].count = parseInt(history[i].count) + amountToAdd;
found = true;
break;
}
}
if (!found) {
history.unshift({
rawDate: selectedDate,
displayDate: displayDate,
exercise: currentExercise,
count: amountToAdd
});
}
history.sort((a, b) => {
if (b.rawDate !== a.rawDate) return new Date(b.rawDate) - new Date(a.rawDate);
return a.exercise.localeCompare(b.exercise);
});
localStorage.setItem(STORAGE_KEY, JSON.stringify(history));
showHistory();
}
function showHistory() {
var listDiv = document.getElementById("list");
var fullHistory = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
var selectedDate = document.getElementById("datePicker").value;
listDiv.innerHTML = "";
var displayList = fullHistory;
if (!settings.showAllHistory) {
displayList = fullHistory.filter(item => item.exercise === currentExercise);
}
if(displayList.length === 0) {
listDiv.innerHTML = "<p style='color:#ccc; font-size:14px; text-align:center;'>Keine Einträge vorhanden</p>";
return;
}
displayList.forEach(function(item) {
var div = document.createElement("div");
if (item.rawDate === selectedDate && (item.exercise === currentExercise || settings.showAllHistory)) {
div.className = "entry active-day";
} else {
div.className = "entry";
}
div.innerHTML = `
<div class="entry-left">
<span class="entry-name">${item.exercise}</span>
<span class="entry-date">${item.displayDate}</span>
</div>
<span class="entry-count">${item.count}</span>
`;
listDiv.appendChild(div);
});
}
// --- ZOOMABLE CHART LOGIK ---
function openStats() {
document.getElementById("statsModal").style.display = "flex";
prepareChartData();
renderChart('1M');
}
function closeStats() { document.getElementById("statsModal").style.display = "none"; }
function prepareChartData() {
var fullHistory = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
var exerciseHistory = fullHistory.filter(item => item.exercise === currentExercise);
chartFullLabels = [];
chartFullData = [];
var startDate = new Date();
startDate.setMonth(startDate.getMonth() - 1);
if (exerciseHistory.length > 0) {
exerciseHistory.sort((a, b) => new Date(a.rawDate) - new Date(b.rawDate));
startDate = new Date(exerciseHistory[0].rawDate);
}
var endDate = new Date();
var dataMap = {};
exerciseHistory.forEach(item => { dataMap[item.rawDate] = item.count; });
for (var d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
var isoDate = d.toISOString().split('T')[0];
var niceDate = d.toLocaleDateString('de-DE', {day: '2-digit', month: '2-digit'});
chartFullLabels.push(niceDate);
chartFullData.push(dataMap[isoDate] || 0);
}
}
function renderChart(initialRange) {
var ctx = document.getElementById('myChart').getContext('2d');
if(myChart) { myChart.destroy(); }
myChart = new Chart(ctx, {
type: 'line',
data: {
labels: chartFullLabels,
datasets: [{
label: 'Wiederholungen',
data: chartFullData,
borderColor: '#007bff',
backgroundColor: 'rgba(0, 123, 255, 0.1)',
borderWidth: 2,
tension: 0.1,
fill: true,
pointRadius: 3,
pointHitRadius: 10
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: { y: { beginAtZero: true }, x: { ticks: { maxTicksLimit: 8 } } },
plugins: {
legend: { display: false },
zoom: {
zoom: {
wheel: { enabled: true },
pinch: { enabled: true },
mode: 'x',
},
pan: {
enabled: true,
mode: 'x',
modifierKey: null
}
}
}
}
});
setChartRange(initialRange, document.querySelector('.chart-btn.active'));
}
function setChartRange(range, btnElement) {
if(btnElement) {
var btns = document.querySelectorAll(".chart-btn");
btns.forEach(b => b.classList.remove("active"));
btnElement.classList.add("active");
}
if (!myChart) return;
var totalPoints = chartFullLabels.length;
var visiblePoints = totalPoints;
if (range === '1M') visiblePoints = 30;
if (range === '3M') visiblePoints = 90;
if (range === '6M') visiblePoints = 180;
if (range === '1Y') visiblePoints = 365;
if (range === 'ALL') visiblePoints = totalPoints;
var minIndex = totalPoints - visiblePoints;
if (minIndex < 0) minIndex = 0;
var maxIndex = totalPoints - 1;
myChart.options.scales.x.min = minIndex;
myChart.options.scales.x.max = maxIndex;
myChart.update();
}
function resetZoom() {
if(myChart) myChart.resetZoom();
setChartRange('1M', document.querySelector('.chart-btn'));
}
// --- EXPORT & IMPORT ---
function exportData() {
var history = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
if (history.length === 0) { alert("Keine Daten vorhanden."); return; }
var csvContent = "Datum;Uebung;Anzahl\n";
history.forEach(function(row) { csvContent += `${row.displayDate};${row.exercise};${row.count}\n`; });
var blob = new Blob(["\uFEFF" + csvContent], { type: 'text/csv;charset=utf-8;' });
var url = URL.createObjectURL(blob);
var link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", "training_export.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function importData(inputElement) {
var file = inputElement.files[0];
if (!file) return;
var reader = new FileReader();
reader.onload = function(e) {
var text = e.target.result;
var lines = text.split("\n");
var importedCount = 0;
var history = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
for (var i = 1; i < lines.length; i++) {
var line = lines[i].trim();
if (line === "") continue;
var parts = line.split(";");
if(parts.length < 3) parts = line.split(",");
if (parts.length >= 3) {
var csvDate = parts[0].trim();
var csvExercise = parts[1].trim();
var csvCount = parseInt(parts[2].trim());
var dateParts = csvDate.split(".");
if(dateParts.length === 3) {
var rawDate = `${dateParts[2]}-${dateParts[1]}-${dateParts[0]}`;
var existingIndex = -1;
for(var j=0; j<history.length; j++) {
if(history[j].rawDate === rawDate && history[j].exercise === csvExercise) {
existingIndex = j;
break;
}
}
if(existingIndex > -1) {
history[existingIndex].count = csvCount;
} else {
history.push({
rawDate: rawDate,
displayDate: csvDate,
exercise: csvExercise,
count: csvCount
});
}
importedCount++;
}
}
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(history));
inputElement.value = "";
alert(importedCount + " Einträge importiert!");
showHistory();
closeSettings();
};
reader.readAsText(file);
}
function clearAll() {
if(confirm("ACHTUNG: Alle Daten werden gelöscht!")) {
localStorage.removeItem(STORAGE_KEY);
showHistory();
closeSettings();
}
}