-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
365 lines (317 loc) Β· 12.2 KB
/
Copy pathapp.js
File metadata and controls
365 lines (317 loc) Β· 12.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
/* Affirmations β a radiant little text pad. All data stays in localStorage. */
(() => {
"use strict";
const STORE_KEY = "affirmations.entries.v1";
const CATS = {
glow: { emoji: "β¨", label: "good thing" },
deed: { emoji: "π", label: "kind deed" },
plan: { emoji: "π±", label: "plan" },
grace: { emoji: "π", label: "gratitude" },
};
const PROMPTS = [
"what made you smile today, even a little?",
"name one thing your past self would be proud of.",
"what small kindness did you give or receive?",
"what's one thing you're quietly looking forward to?",
"what did your body do for you today? thank it.",
"who made your day 1% better? what did they do?",
"what's something hard you handled anyway?",
"what tiny beauty did you notice β light, a sound, a taste?",
"what would you like tomorrow-you to get to do?",
"what did you learn about yourself this week?",
"what's one worry that turned out okay?",
"if today had a highlight reel, what's on it?",
];
// βββββ state βββββ
let entries = load();
let activeCat = "glow";
let activeFilter = "all";
function load() {
try {
const raw = localStorage.getItem(STORE_KEY);
const parsed = raw ? JSON.parse(raw) : [];
return Array.isArray(parsed) ? parsed.filter(e => e && e.text && e.ts) : [];
} catch { return []; }
}
function save() { localStorage.setItem(STORE_KEY, JSON.stringify(entries)); }
// βββββ dom βββββ
const $ = id => document.getElementById(id);
const entryText = $("entryText"), entriesEl = $("entries"), emptyState = $("emptyState");
// βββββ greeting + prompts βββββ
function setGreeting() {
const h = new Date().getHours();
const g = h < 5 ? "hello, night owl" :
h < 12 ? "good morning, sunshine" :
h < 18 ? "good afternoon, radiant human" : "good evening, gentle soul";
$("greeting").textContent = g;
}
let promptIdx = Math.floor(Math.random() * PROMPTS.length);
function nextPrompt() {
$("promptText").textContent = PROMPTS[promptIdx % PROMPTS.length];
promptIdx++;
}
$("promptPill").addEventListener("click", () => {
nextPrompt();
entryText.focus();
});
// βββββ chips βββββ
function wireChips(rowId, onPick) {
const row = $(rowId);
row.addEventListener("click", e => {
const chip = e.target.closest(".chip");
if (!chip) return;
row.querySelectorAll(".chip").forEach(c => {
c.classList.toggle("is-active", c === chip);
if (c.getAttribute("role") === "radio") c.setAttribute("aria-checked", c === chip);
});
onPick(chip);
});
}
wireChips("catChips", chip => { activeCat = chip.dataset.cat; entryText.focus(); });
wireChips("filterChips", chip => { activeFilter = chip.dataset.filter; render(); });
// βββββ save βββββ
$("saveBtn").addEventListener("click", addEntry);
entryText.addEventListener("keydown", e => {
if ((e.metaKey || e.ctrlKey) && e.key === "Enter") addEntry();
});
function addEntry() {
const text = entryText.value.trim();
if (!text) { toast("write a little something first πΈ"); entryText.focus(); return; }
entries.unshift({ id: Date.now() + Math.random().toString(16).slice(2), text, cat: activeCat, ts: Date.now(), done: false });
save();
entryText.value = "";
render();
sparkleBurst();
toast(pick(["kept safe β¨", "one more light collected π", "your future self says thanks π±", "beautifully done π"]));
}
function pick(arr) { return arr[Math.floor(Math.random() * arr.length)]; }
// βββββ render βββββ
function fmtDay(ts) {
const d = new Date(ts), today = new Date();
const startOf = x => new Date(x.getFullYear(), x.getMonth(), x.getDate()).getTime();
const diff = Math.round((startOf(today) - startOf(d)) / 86400000);
if (diff === 0) return "today";
if (diff === 1) return "yesterday";
return d.toLocaleDateString(undefined, { weekday: "long", month: "long", day: "numeric" });
}
function fmtTime(ts) {
return new Date(ts).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
}
function render() {
const list = activeFilter === "all" ? entries : entries.filter(e => e.cat === activeFilter);
entriesEl.innerHTML = "";
emptyState.hidden = list.length > 0;
let lastDay = null;
for (const e of list) {
const day = fmtDay(e.ts);
if (day !== lastDay) {
const label = document.createElement("p");
label.className = "day-label";
label.textContent = day;
entriesEl.appendChild(label);
lastDay = day;
}
entriesEl.appendChild(entryNode(e));
}
renderStats();
renderReflect(false);
}
function entryNode(e) {
const cat = CATS[e.cat] || CATS.glow;
const div = document.createElement("div");
div.className = "entry" + (e.done ? " is-done" : "");
const emoji = document.createElement("span");
emoji.className = "entry-emoji";
emoji.textContent = cat.emoji;
const body = document.createElement("div");
body.className = "entry-body";
const text = document.createElement("p");
text.className = "entry-text";
text.textContent = e.text;
const meta = document.createElement("p");
meta.className = "entry-meta";
meta.textContent = `${cat.label} Β· ${fmtTime(e.ts)}`;
body.append(text, meta);
const actions = document.createElement("div");
actions.className = "entry-actions";
if (e.cat === "plan" && !e.done) {
const done = btn("πΈ", "mark this plan as done", () => {
e.done = true; save(); render(); sparkleBurst();
toast("a plan bloomed into a good thing πΈ");
});
actions.appendChild(done);
}
actions.appendChild(btn("β", "let this one go", () => {
if (!confirm("Let this one go? It'll be removed for good.")) return;
entries = entries.filter(x => x.id !== e.id);
save(); render();
toast("released ποΈ");
}));
div.append(emoji, body, actions);
return div;
}
function btn(txt, label, onClick) {
const b = document.createElement("button");
b.className = "icon-btn";
b.type = "button";
b.textContent = txt;
b.title = b.ariaLabel = label;
b.addEventListener("click", onClick);
return b;
}
// βββββ stats βββββ
function renderStats() {
$("glowbar").hidden = entries.length === 0;
$("statTotal").textContent = entries.length;
$("statDeeds").textContent = entries.filter(e => e.cat === "deed").length;
$("statDone").textContent = entries.filter(e => e.cat === "plan" && e.done).length;
$("statStreak").textContent = streak();
}
function streak() {
const days = new Set(entries.map(e => new Date(e.ts).toDateString()));
let n = 0;
const d = new Date();
if (!days.has(d.toDateString())) d.setDate(d.getDate() - 1); // streak survives until today ends
while (days.has(d.toDateString())) { n++; d.setDate(d.getDate() - 1); }
return n;
}
// βββββ reflect βββββ
let reflectId = null;
function renderReflect(forceNew) {
const card = $("reflectCard");
if (entries.length < 2) { card.hidden = true; return; }
card.hidden = false;
let e = entries.find(x => x.id === reflectId);
if (!e || forceNew) {
const pool = entries.length > 3 ? entries.slice(1) : entries; // prefer not the one just written
let next = pick(pool);
if (forceNew && pool.length > 1) {
while (next.id === reflectId) next = pick(pool);
}
e = next;
reflectId = e.id;
}
const cat = CATS[e.cat] || CATS.glow;
$("reflectText").textContent = e.text;
$("reflectMeta").textContent = `${cat.emoji} ${cat.label} Β· ${fmtDay(e.ts)} at ${fmtTime(e.ts)}`;
}
$("anotherMemory").addEventListener("click", () => renderReflect(true));
// βββββ export / import βββββ
$("exportBtn").addEventListener("click", () => {
const blob = new Blob([JSON.stringify(entries, null, 2)], { type: "application/json" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = `affirmations-${new Date().toISOString().slice(0, 10)}.json`;
a.click();
URL.revokeObjectURL(a.href);
toast("your light, exported βοΈ");
});
$("importBtn").addEventListener("click", () => $("importFile").click());
$("importFile").addEventListener("change", async e => {
const file = e.target.files[0];
if (!file) return;
try {
const incoming = JSON.parse(await file.text());
if (!Array.isArray(incoming)) throw new Error("not a list");
const known = new Set(entries.map(x => x.id));
const added = incoming.filter(x => x && x.text && x.ts && !known.has(x.id));
entries = [...entries, ...added].sort((a, b) => b.ts - a.ts);
save(); render();
toast(`welcomed ${added.length} memories home π`);
} catch {
toast("hmm, that file didn't look right π«§");
}
e.target.value = "";
});
// βββββ toast βββββ
let toastTimer;
function toast(msg) {
const t = $("toast");
t.textContent = msg;
t.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => t.classList.remove("show"), 2400);
}
// βββββ parallax βββββ
const layers = [...document.querySelectorAll(".sky-layer")];
const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
let mx = 0, my = 0, ticking = false;
function applyParallax() {
ticking = false;
const sy = scrollY;
for (const layer of layers) {
const d = parseFloat(layer.dataset.depth || 0);
layer.style.transform =
`translate3d(${mx * d * 120}px, ${my * d * 120 - sy * d * 4}px, 0)`;
}
}
function requestParallax() {
if (!ticking) { ticking = true; requestAnimationFrame(applyParallax); }
}
if (!reduceMotion) {
addEventListener("mousemove", e => {
mx = e.clientX / innerWidth - 0.5;
my = e.clientY / innerHeight - 0.5;
requestParallax();
}, { passive: true });
addEventListener("scroll", requestParallax, { passive: true });
}
// floating motes
const motes = $("motes");
if (!reduceMotion) {
for (let i = 0; i < 16; i++) {
const m = document.createElement("span");
m.className = "mote";
const size = 3 + Math.random() * 6;
m.style.cssText = `left:${Math.random() * 100}%; top:${60 + Math.random() * 40}%;` +
`width:${size}px; height:${size}px;` +
`animation-duration:${14 + Math.random() * 18}s; animation-delay:${-Math.random() * 20}s;`;
motes.appendChild(m);
}
}
// βββββ sparkle burst on save βββββ
const canvas = $("sparkles"), ctx = canvas.getContext("2d");
let particles = [], animating = false;
function resize() { canvas.width = innerWidth; canvas.height = innerHeight; }
addEventListener("resize", resize, { passive: true });
resize();
function sparkleBurst() {
if (reduceMotion) return;
const rect = $("saveBtn").getBoundingClientRect();
const cx = rect.left + rect.width / 2, cy = rect.top + rect.height / 2;
const colors = ["#f5b942", "#f79cae", "#9ed0f5", "#cdb4f0", "#a8e6c9", "#ffffff"];
for (let i = 0; i < 42; i++) {
const a = Math.random() * Math.PI * 2, v = 2 + Math.random() * 5;
particles.push({
x: cx, y: cy,
vx: Math.cos(a) * v, vy: Math.sin(a) * v - 2,
r: 1.5 + Math.random() * 3,
life: 1,
color: colors[i % colors.length],
});
}
if (!animating) { animating = true; requestAnimationFrame(tick); }
}
function tick() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles = particles.filter(p => p.life > 0);
for (const p of particles) {
p.x += p.vx; p.y += p.vy;
p.vy += 0.12; // gravity
p.vx *= 0.985;
p.life -= 0.016;
ctx.globalAlpha = Math.max(p.life, 0);
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.x, p.y, p.r * p.life, 0, Math.PI * 2);
ctx.fill();
}
ctx.globalAlpha = 1;
if (particles.length) requestAnimationFrame(tick);
else animating = false;
}
// βββββ go βββββ
setGreeting();
nextPrompt();
render();
})();