-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.js
More file actions
411 lines (360 loc) · 15.2 KB
/
progress.js
File metadata and controls
411 lines (360 loc) · 15.2 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* ========================================
MUSCLE TIMER — Progress Charts
Using Chart.js for exercise progression
======================================== */
const ProgressManager = (function () {
'use strict';
let dom = {};
let chartInstance = null;
function init() {
dom = {
progressTab: document.getElementById('tab-progress'),
progressExerciseSelect: document.getElementById('progress-exercise-select'),
progressWeightSelect: document.getElementById('progress-weight-select'),
chartContainer: document.getElementById('chart-container'),
chartCanvas: document.getElementById('progress-chart'),
noDataMsg: document.getElementById('no-data-msg'),
historyList: document.getElementById('history-list'),
clearHistoryBtn: document.getElementById('clear-history-btn')
};
bindEvents();
}
function onTabActivated() {
populateExerciseSelect();
renderWeeklyStats();
renderHistory();
}
function renderWeeklyStats() {
const card = document.getElementById('weekly-stats-card');
if (!card) return;
const sessions = SessionManager.getSessions();
if (sessions.length === 0) { card.style.display = 'none'; return; }
const now = Date.now();
const weekMs = 7 * 24 * 60 * 60 * 1000;
const weeklySessions = sessions.filter(s => (now - new Date(s.date).getTime()) <= weekMs);
if (weeklySessions.length === 0) { card.style.display = 'none'; return; }
card.style.display = '';
let totalVolume = 0;
let totalSets = 0;
const muscles = {};
weeklySessions.forEach(s => {
totalVolume += s.totalVolume || 0;
totalSets += s.totalSets || 0;
(s.exercises || []).forEach(ex => {
muscles[ex.muscle] = (muscles[ex.muscle] || 0) + 1;
});
});
const sortedMuscles = Object.entries(muscles)
.sort((a, b) => b[1] - a[1])
.slice(0, 4)
.map(([m]) => m);
const muscleChips = sortedMuscles.map(m => `<span class="wk-muscle-chip">${m}</span>`).join('');
card.innerHTML = `
<div class="weekly-card-title">📅 Cette semaine</div>
<div class="weekly-grid">
<div class="wk-item">
<span class="wk-value">${weeklySessions.length}</span>
<span class="wk-label">Séances</span>
</div>
<div class="wk-item">
<span class="wk-value">${totalSets}</span>
<span class="wk-label">Séries</span>
</div>
<div class="wk-item">
<span class="wk-value">${formatVolumeShort(totalVolume)}</span>
<span class="wk-label">Volume</span>
</div>
</div>
${sortedMuscles.length ? `<div class="weekly-muscles">${muscleChips}</div>` : ''}
`;
}
function populateExerciseSelect() {
const sessions = SessionManager.getSessions();
const exerciseSet = new Map();
for (const session of sessions) {
for (const exo of session.exercises) {
if (!exerciseSet.has(exo.id)) {
exerciseSet.set(exo.id, { id: exo.id, name: exo.name, emoji: exo.emoji });
}
}
}
const select = dom.progressExerciseSelect;
const currentValue = select.value;
select.innerHTML = '<option value="">— Choisir un exercice —</option>';
for (const [id, exo] of exerciseSet) {
const opt = document.createElement('option');
opt.value = id;
opt.textContent = `${exo.emoji} ${exo.name}`;
select.appendChild(opt);
}
if (currentValue && exerciseSet.has(currentValue)) {
select.value = currentValue;
}
if (exerciseSet.size === 0) {
dom.noDataMsg.style.display = 'block';
dom.chartContainer.style.display = 'none';
dom.progressWeightSelect.parentElement.style.display = 'none';
}
}
function populateWeightSelect(exerciseId) {
const sessions = SessionManager.getSessions();
const weights = new Set();
for (const session of sessions) {
for (const exo of session.exercises) {
if (exo.id === exerciseId) {
for (const set of exo.sets) {
weights.add(set.weight);
}
}
}
}
const select = dom.progressWeightSelect;
select.innerHTML = '<option value="all">Tous les poids</option>';
const sortedWeights = [...weights].sort((a, b) => a - b);
for (const w of sortedWeights) {
const opt = document.createElement('option');
opt.value = w;
opt.textContent = `${w} kg`;
select.appendChild(opt);
}
select.parentElement.style.display = sortedWeights.length > 0 ? 'flex' : 'none';
}
function renderChart(exerciseId, weightFilter) {
const sessions = SessionManager.getSessions();
// Collect data points: { date, maxReps, weight, volume }
const dataPoints = [];
for (const session of sessions) {
for (const exo of session.exercises) {
if (exo.id !== exerciseId) continue;
const filteredSets = weightFilter === 'all'
? exo.sets
: exo.sets.filter(s => s.weight === parseFloat(weightFilter));
if (filteredSets.length === 0) continue;
const maxReps = Math.max(...filteredSets.map(s => s.reps));
const bestSet = filteredSets.reduce((best, s) =>
(s.weight * s.reps > best.weight * best.reps) ? s : best
, filteredSets[0]);
const totalVolume = filteredSets.reduce((sum, s) => sum + s.weight * s.reps, 0);
dataPoints.push({
date: new Date(session.date),
dateLabel: formatDate(session.date),
maxReps,
bestWeight: bestSet.weight,
bestSetReps: bestSet.reps,
totalVolume
});
}
}
if (dataPoints.length === 0) {
dom.chartContainer.style.display = 'none';
dom.noDataMsg.style.display = 'block';
dom.noDataMsg.textContent = 'Aucune donnée pour cet exercice';
return;
}
dom.chartContainer.style.display = 'block';
dom.noDataMsg.style.display = 'none';
// Sort by date
dataPoints.sort((a, b) => a.date - b.date);
const labels = dataPoints.map(d => d.dateLabel);
const repsData = dataPoints.map(d => d.maxReps);
const volumeData = dataPoints.map(d => d.totalVolume);
// Destroy old chart
if (chartInstance) {
chartInstance.destroy();
}
const ctx = dom.chartCanvas.getContext('2d');
const accentColor = getComputedStyle(document.documentElement).getPropertyValue('--accent').trim() || '#7c5cfc';
const secondaryColor = getComputedStyle(document.documentElement).getPropertyValue('--accent-secondary').trim() || '#ff6b6b';
chartInstance = new Chart(ctx, {
type: 'line',
data: {
labels,
datasets: [
{
label: 'Reps max',
data: repsData,
borderColor: accentColor,
backgroundColor: accentColor + '33',
fill: true,
tension: 0.4,
pointRadius: 5,
pointHoverRadius: 8,
pointBackgroundColor: accentColor,
borderWidth: 2,
yAxisID: 'y'
},
{
label: 'Volume (kg)',
data: volumeData,
borderColor: secondaryColor,
backgroundColor: secondaryColor + '22',
fill: false,
tension: 0.4,
pointRadius: 4,
pointHoverRadius: 7,
pointBackgroundColor: secondaryColor,
borderWidth: 2,
borderDash: [5, 5],
yAxisID: 'y1'
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
interaction: {
mode: 'index',
intersect: false
},
plugins: {
legend: {
position: 'top',
labels: {
color: '#f0f0f5',
font: { family: 'Inter', size: 11 },
usePointStyle: true,
padding: 16
}
},
tooltip: {
backgroundColor: 'rgba(16, 16, 40, 0.9)',
titleColor: '#f0f0f5',
bodyColor: '#f0f0f5',
borderColor: 'rgba(255,255,255,0.1)',
borderWidth: 1,
cornerRadius: 8,
padding: 10,
titleFont: { family: 'Inter', weight: '600' },
bodyFont: { family: 'Inter' }
}
},
scales: {
x: {
ticks: { color: 'rgba(240,240,245,0.5)', font: { family: 'Inter', size: 10 } },
grid: { color: 'rgba(255,255,255,0.05)' }
},
y: {
type: 'linear',
display: true,
position: 'left',
title: {
display: true,
text: 'Reps',
color: 'rgba(240,240,245,0.5)',
font: { family: 'Inter', size: 11 }
},
ticks: {
color: 'rgba(240,240,245,0.5)',
font: { family: 'Inter', size: 10 },
stepSize: 1
},
grid: { color: 'rgba(255,255,255,0.05)' },
beginAtZero: true
},
y1: {
type: 'linear',
display: true,
position: 'right',
title: {
display: true,
text: 'Volume (kg)',
color: 'rgba(240,240,245,0.5)',
font: { family: 'Inter', size: 11 }
},
ticks: {
color: 'rgba(240,240,245,0.5)',
font: { family: 'Inter', size: 10 }
},
grid: { drawOnChartArea: false },
beginAtZero: true
}
}
}
});
}
// ---- Session history ----
function renderHistory() {
const sessions = SessionManager.getSessions();
const list = dom.historyList;
if (sessions.length === 0) {
list.innerHTML = '<p class="no-history">Aucune séance enregistrée</p>';
dom.clearHistoryBtn.style.display = 'none';
return;
}
dom.clearHistoryBtn.style.display = 'block';
// Show most recent first
const reversed = [...sessions].reverse();
list.innerHTML = reversed.map(session => {
const dateStr = formatDate(session.date);
const volume = session.totalVolume || 0;
const sets = session.totalSets || 0;
const exoCount = session.exercises ? session.exercises.length : 0;
const durationStr = session.duration ? formatDuration(session.duration) : '—';
const notesHtml = session.notes
? `<div class="history-notes">📝 ${session.notes.replace(/</g, '<').replace(/>/g, '>')}</div>`
: '';
return `
<div class="history-card">
<div class="history-header">
<span class="history-date">📅 ${dateStr}</span>
<span class="history-duration">⏱️ ${durationStr}</span>
</div>
<div class="history-stats">
<span>🏋️ ${exoCount} exo</span>
<span>📊 ${sets} séries</span>
<span>💪 ${formatVolumeShort(volume)}</span>
</div>
<div class="history-exercises">
${(session.exercises || []).map(e => `<span class="history-exo-tag">${e.emoji} ${e.name}</span>`).join('')}
</div>
${notesHtml}
</div>
`;
}).join('');
}
function formatDate(isoStr) {
const d = new Date(isoStr);
return d.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: '2-digit' });
}
function formatDuration(minutes) {
if (minutes < 60) return `${minutes}min`;
const hours = Math.floor(minutes / 60);
const rem = minutes % 60;
return `${hours}h${rem.toString().padStart(2, '0')}`;
}
function formatVolumeShort(kg) {
if (kg >= 1000) return `${(kg / 1000).toFixed(1)}T`;
return `${Math.round(kg)}kg`;
}
// ---- Events ----
function bindEvents() {
dom.progressExerciseSelect.addEventListener('change', () => {
const exoId = dom.progressExerciseSelect.value;
if (!exoId) {
dom.chartContainer.style.display = 'none';
dom.progressWeightSelect.parentElement.style.display = 'none';
dom.noDataMsg.style.display = 'block';
dom.noDataMsg.textContent = 'Sélectionne un exercice pour voir ta progression';
return;
}
populateWeightSelect(exoId);
renderChart(exoId, 'all');
});
dom.progressWeightSelect.addEventListener('change', () => {
const exoId = dom.progressExerciseSelect.value;
if (exoId) renderChart(exoId, dom.progressWeightSelect.value);
});
dom.clearHistoryBtn.addEventListener('click', () => {
if (confirm('Supprimer tout l\'historique des séances ?')) {
try { localStorage.removeItem('muscle-timer-sessions'); } catch (e) { }
renderHistory();
populateExerciseSelect();
if (chartInstance) { chartInstance.destroy(); chartInstance = null; }
dom.chartContainer.style.display = 'none';
dom.noDataMsg.style.display = 'block';
dom.noDataMsg.textContent = 'Aucune donnée disponible';
}
});
}
return { init, onTabActivated };
})();