-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
335 lines (279 loc) · 10.8 KB
/
script.js
File metadata and controls
335 lines (279 loc) · 10.8 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
// Fitness Hearts — clean single version
(() => {
const KEY = "fitness-hearts-v2";
// Elements
const startInput = document.getElementById("startDate");
const weeksSel = document.getElementById("weeks");
const grid = document.getElementById("grid");
const progressEl = document.getElementById("progress");
const summaryEl = document.getElementById("summary");
const pctLabel = document.getElementById("pctLabel");
const heartFill = document.getElementById("heartFill");
const applyBtn = document.getElementById("apply");
const resetBtn = document.getElementById("resetToday");
const clearBtn = document.getElementById("clearAll");
// Utils
const pad = n => String(n).padStart(2,"0");
const fmt = d => `${d.getFullYear()}-${pad(d.getMonth()+1)}-${pad(d.getDate())}`;
const validDateStr = s => /^\d{4}-\d{2}-\d{2}$/.test(s);
const load = () => {
try { return JSON.parse(localStorage.getItem(KEY)) || {}; }
catch { return {}; }
};
const save = (data) => localStorage.setItem(KEY, JSON.stringify(data));
function ensureStartDate(){
let v = startInput.value;
if(!validDateStr(v)){
v = fmt(new Date());
startInput.value = v;
}
return v;
}
function build(){
const data = load();
// Meta
const startStr = ensureStartDate();
const start = new Date(startStr + "T00:00:00");
const weeks = Number(weeksSel.value) || 12;
data._meta = { start: fmt(start), weeks };
save(data);
// Grid
grid.innerHTML = "";
const days = weeks * 7;
let totalChecks = 0, totalPossible = days * 3;
let aDays=0, bDays=0, cDays=0, perfect=0;
let bestStreak=0, currStreak=0;
for(let i=0;i<days;i++){
const d = new Date(start); d.setDate(start.getDate()+i);
const id = fmt(d);
if(!data[id]) data[id] = { a:false, b:false, c:false };
const rec = data[id];
// perfect-day streak
if(rec.a && rec.b && rec.c){ currStreak++; bestStreak = Math.max(bestStreak,currStreak); }
else { currStreak = 0; }
// cell
const cell = document.createElement("div");
cell.className = "cell";
const dateEl = document.createElement("div");
dateEl.className = "date";
dateEl.textContent = d.toLocaleDateString(undefined,{weekday:"short",month:"short",day:"numeric"});
const hearts = document.createElement("div");
hearts.className = "hearts";
function heart(key){
const btn = document.createElement("button");
btn.type = "button";
btn.className = "heart" + (rec[key] ? " on" : "");
btn.setAttribute("aria-pressed", rec[key] ? "true" : "false");
btn.setAttribute("title", "Toggle action");
btn.addEventListener("click", ()=>{
const latest = load();
// Guard in case meta or day record went missing
if(!latest[id]) latest[id] = { a:false, b:false, c:false };
latest[id][key] = !latest[id][key];
save(latest);
build();
});
btn.addEventListener("keydown", (e)=>{
if(e.key==="Enter" || e.key===" "){ e.preventDefault(); btn.click(); }
});
return btn;
}
hearts.appendChild(heart("a"));
hearts.appendChild(heart("b"));
hearts.appendChild(heart("c"));
cell.appendChild(dateEl);
cell.appendChild(hearts);
grid.appendChild(cell);
// counts
totalChecks += (rec.a?1:0) + (rec.b?1:0) + (rec.c?1:0);
aDays += rec.a?1:0; bDays += rec.b?1:0; cDays += rec.c?1:0;
perfect += (rec.a && rec.b && rec.c) ? 1 : 0;
}
// Progress + labels
const pct = Math.round((totalChecks/totalPossible)*100);
progressEl.value = pct;
pctLabel.textContent = `${pct}%`;
summaryEl.innerHTML = `
<span class="pill">Checks: <strong>${totalChecks}</strong> / ${totalPossible} (${pct}%)</span>
<span class="pill">10K+ Steps days: <strong>${aDays}</strong></span>
<span class="pill">Morning Workout days: <strong>${bDays}</strong></span>
<span class="pill">Afternoon Workout days: <strong>${cDays}</strong></span>
<span class="pill">Perfect days: <strong>${perfect}</strong></span>
<span class="pill">Best perfect streak: <strong>${bestStreak}</strong></span>
`;
// Big SVG heart fill: viewBox height = 110
const svgH = 110, fillH = (pct/100) * svgH;
heartFill.setAttribute("y", svgH - fillH);
heartFill.setAttribute("height", fillH);
}
function init(){
const data = load();
if(data._meta){
startInput.value = data._meta.start;
weeksSel.value = data._meta.weeks;
} else {
startInput.value = fmt(new Date());
}
build();
}
// wire up
// Your script tag is already at the end of <body>, so DOM is ready:
init();
applyBtn.addEventListener("click", build);
resetBtn.addEventListener("click", ()=>{
const data = load();
const t = new Date();
const id = fmt(t);
data[id] = { a:false, b:false, c:false };
save(data);
build();
});
clearBtn.addEventListener("click", ()=>{
if(confirm("Clear all saved progress?")){
localStorage.removeItem(KEY);
location.reload();
}
});
})();
(() => {
const KEY = "fitness-hearts-v2";
// Elements
const startInput = document.getElementById("startDate");
const weeksSel = document.getElementById("weeks");
const grid = document.getElementById("grid");
const progressEl = document.getElementById("progress");
const summaryEl = document.getElementById("summary");
const pctLabel = document.getElementById("pctLabel");
const heartFill = document.getElementById("heartFill");
const applyBtn = document.getElementById("apply");
const resetBtn = document.getElementById("resetToday");
const clearBtn = document.getElementById("clearAll");
// 👉 NEW: buddy hearts container
const buddyMeter = document.getElementById("buddyMeter");
// Utils
const pad = n => String(n).padStart(2,"0");
const fmt = d => `${d.getFullYear()}-${pad(d.getMonth()+1)}-${pad(d.getDate())}`;
const validDateStr = s => /^\d{4}-\d{2}-\d{2}$/.test(s);
const load = () => { try { return JSON.parse(localStorage.getItem(KEY)) || {}; } catch { return {}; } };
const save = (data) => localStorage.setItem(KEY, JSON.stringify(data));
function ensureStartDate(){
let v = startInput.value;
if(!validDateStr(v)){ v = fmt(new Date()); startInput.value = v; }
return v;
}
// 👉 NEW: build the 5 buddy hearts with a fractional fill on the last one
function renderBuddyHearts(overallPct){
if(!buddyMeter) return;
buddyMeter.innerHTML = "";
// Map 0–100% progress -> 0–5 hearts
const heartsValue = Math.max(0, Math.min(5, (overallPct/100)*5));
for(let i=0;i<5;i++){
const portion = Math.max(0, Math.min(1, heartsValue - i)); // 0..1 for this heart
const el = document.createElement("div");
el.className = "buddy-heart";
if(portion >= 1){
el.classList.add("full");
}else{
el.style.setProperty("--pct", String(Math.round(portion*100)));
}
const glyph = document.createElement("span");
glyph.textContent = "❤";
el.appendChild(glyph);
buddyMeter.appendChild(el);
}
}
function build(){
const data = load();
const startStr = ensureStartDate();
const start = new Date(startStr + "T00:00:00");
const weeks = Number(weeksSel.value) || 12;
data._meta = { start: fmt(start), weeks };
save(data);
grid.innerHTML = "";
const days = weeks * 7;
let totalChecks = 0, totalPossible = days * 3;
let aDays=0, bDays=0, cDays=0, perfect=0;
let bestStreak=0, currStreak=0;
for(let i=0;i<days;i++){
const d = new Date(start); d.setDate(start.getDate()+i);
const id = fmt(d);
if(!data[id]) data[id] = { a:false, b:false, c:false };
const rec = data[id];
if(rec.a && rec.b && rec.c){ currStreak++; bestStreak = Math.max(bestStreak,currStreak); }
else { currStreak = 0; }
const cell = document.createElement("div");
cell.className = "cell";
const dateEl = document.createElement("div");
dateEl.className = "date";
dateEl.textContent = d.toLocaleDateString(undefined,{weekday:"short",month:"short",day:"numeric"});
const hearts = document.createElement("div");
hearts.className = "hearts";
function heart(key){
const btn = document.createElement("button");
btn.type = "button";
btn.className = "heart" + (rec[key] ? " on" : "");
btn.setAttribute("aria-pressed", rec[key] ? "true" : "false");
btn.setAttribute("title", "Toggle action");
btn.addEventListener("click", ()=>{
const latest = load();
if(!latest[id]) latest[id] = { a:false, b:false, c:false };
latest[id][key] = !latest[id][key];
save(latest);
build();
});
btn.addEventListener("keydown", (e)=>{
if(e.key==="Enter" || e.key===" "){ e.preventDefault(); btn.click(); }
});
return btn;
}
hearts.appendChild(heart("a"));
hearts.appendChild(heart("b"));
hearts.appendChild(heart("c"));
cell.appendChild(dateEl);
cell.appendChild(hearts);
grid.appendChild(cell);
totalChecks += (rec.a?1:0) + (rec.b?1:0) + (rec.c?1:0);
aDays += rec.a?1:0; bDays += rec.b?1:0; cDays += rec.c?1:0;
perfect += (rec.a && rec.b && rec.c) ? 1 : 0;
}
const pct = Math.round((totalChecks/totalPossible)*100);
progressEl.value = pct;
pctLabel.textContent = `${pct}%`;
summaryEl.innerHTML = `
<span class="pill">Checks: <strong>${totalChecks}</strong> / ${totalPossible} (${pct}%)</span>
<span class="pill">10K+ Steps days: <strong>${aDays}</strong></span>
<span class="pill">Morning Workout days: <strong>${bDays}</strong></span>
<span class="pill">Afternoon Workout days: <strong>${cDays}</strong></span>
<span class="pill">Perfect days: <strong>${perfect}</strong></span>
<span class="pill">Best perfect streak: <strong>${bestStreak}</strong></span>
`;
// Top big heart fill (your existing SVG)
const svgH = 110, fillH = (pct/100) * svgH;
heartFill.setAttribute("y", svgH - fillH);
heartFill.setAttribute("height", fillH);
// 👉 NEW: update the 5 buddy hearts
renderBuddyHearts(pct);
}
function init(){
const data = load();
if(data._meta){ startInput.value = data._meta.start; weeksSel.value = data._meta.weeks; }
else { startInput.value = fmt(new Date()); }
build();
}
// wire up
init();
applyBtn.addEventListener("click", build);
resetBtn.addEventListener("click", ()=>{
const data = load();
const id = fmt(new Date());
data[id] = { a:false, b:false, c:false };
save(data);
build();
});
clearBtn.addEventListener("click", ()=>{
if(confirm("Clear all saved progress?")){
localStorage.removeItem(KEY);
location.reload();
}
});
})();