-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
483 lines (416 loc) · 12.8 KB
/
index.html
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>pomo-mood-o</title>
<link
rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🍅</text></svg>"
/>
<link
href="https://fonts.googleapis.com/css2?family=Space+Mono&display=swap"
rel="stylesheet"
/>
<script
src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"
defer
></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-array.v2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
<style>
body {
background-color: #282c34;
color: #abb2bf;
}
button {
background-color: #61afef;
border: none;
font-size: 1rem;
padding: 0.2em 1em;
color: #fff;
border-radius: 0.2em;
font-weight: 700;
}
body,
button,
text {
font-family: 'Space Mono', monospace;
}
.secondary {
background-color: #abb2bf;
}
h1 {
color: #c678dd;
}
.app {
text-align: center;
}
.countdown {
font-size: 4rem;
margin-top: 5rem;
margin-bottom: 1rem;
color: #fff;
}
.summary {
margin-top: 5rem;
}
.emotion-picker {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 8rem;
}
.emotion-option {
opacity: 0;
position: absolute;
}
.emotion-label {
display: inline-block;
margin: 0.2rem;
font-size: 3rem;
transition: font-size 100ms ease-in-out;
position: relative;
color: #e5c07b;
}
.emotion-text {
font-size: 1rem;
position: absolute;
width: 10rem;
top: -0.75rem;
left: -2.5rem;
opacity: 0;
transition: opacity 100ms ease-in-out;
}
.emotion-label:hover,
.emotion-option:checked ~ .emotion-label {
font-size: 5rem;
}
.emotion-label:hover .emotion-text,
.emotion-option:checked ~ .emotion-label .emotion-text {
opacity: 1;
}
.data {
margin-top: 10rem;
margin-bottom: 5rem;
}
table {
display: inline-block;
margin-top: 5rem;
text-align: left;
}
th,
td {
padding: 0.1rem 0.5rem;
}
</style>
</head>
<body>
<div x-data="app()" class="app">
<header>
<h1>🍅 pomo-mood-o</h1>
</header>
<section class="countdown">
<div x-text="countDisplay"></div>
</section>
<section x-show="!isDone" class="actions">
<button x-show="!isRunning" @click="start()">Start</button>
<button x-show="isRunning" @click="stop()">Stop</button>
</section>
<section x-show="isDone" class="summary">
<div>
<h2>How did you feel?</h2>
<div class="emotion-picker">
<template x-for="e in emotions">
<div>
<input
class="emotion-option"
x-bind:id="e.value"
type="radio"
name="emotion"
x-bind:value="e.value"
x-model="emotion"
/>
<label class="emotion-label" x-bind:for="e.value">
<div x-text="e.value" class="emotion-text"></div>
<div x-text="e.label"></div>
</label>
</div>
</template>
</div>
</div>
<button @click="save()">Save</button>
<button class="secondary" @click="cancel()">Cancel</button>
</section>
<section x-show="pomodoros.length" class="data">
<div id="chart"></div>
<div>
<button @click="showRaw = true" x-show="!showRaw" class="secondary">
Show Raw Data
</button>
<button @click="showRaw = false" x-show="showRaw" class="secondary">
Hide Raw Data
</button>
</div>
<table x-show="showRaw">
<thead>
<tr>
<th>date</th>
<th>start</th>
<th>end</th>
<th>how did you feel?</th>
</tr>
</thead>
<tbody>
<template x-for="p in pomodoros">
<tr>
<td x-text="formatDate(p.startDate)"></td>
<td x-text="formatTime(p.startDate)"></td>
<td x-text="formatTime(p.endDate)"></td>
<td x-text="p.emotion"></td>
</tr>
</template>
</tbody>
</table>
</section>
</div>
<script>
// TODO
// - responsiveness
// - a11y for the emoji?
// - chart filtering?
// - offline/pwa
// - keep it running if you close the tab
const emotions = [
{value: 'great', label: '🤓', color: '#9ac379'},
{value: 'good', label: '😀', color: '#9ac379'},
{value: 'ok', label: '🙂', color: '#9ac379'},
{value: 'depressed', label: '😞', color: '#e5c07b'},
{value: 'tired', label: '😴', color: '#e5c07b'},
{value: 'frustrated', label: '😖', color: '#e06c75'},
{value: 'distracted', label: '🤪', color: '#e06c75'},
];
const DATE_FORMAT = 'ddd, MMM D';
const TIME_FORMAT = 'h:mm a';
const formatDate = raw => {
return moment(raw).format(DATE_FORMAT);
};
const formatTime = raw => {
return moment(raw).format(TIME_FORMAT);
};
const pad = i => (i.toString().length === 1 ? `0${i}` : i);
function app() {
const rawData = localStorage.getItem('data');
const pomodoros = rawData ? JSON.parse(rawData) : [];
drawChart(pomodoros);
return {
pomodoros,
emotions,
formatDate,
formatTime,
startDate: undefined,
endDarte: undefined,
emotion: '',
countDisplay: '25:00',
isRunning: false,
isDone: false,
showRaw: false,
start() {
this.startDate = new Date();
this.endDate = new Date(this.startDate.getTime() + 1500000); // 25 min in milliseconds
this.emotion = '';
this.isRunning = true;
this.tick();
},
stop() {
this.endDate = new Date();
},
cancel() {
this.startDate = undefined;
this.endDate = undefined;
this.emotion = '';
this.countDisplay = '25:00';
document.title = 'pomo-mood-o';
this.isDone = false;
this.isRunning = false;
},
save() {
this.pomodoros.push({
startDate: this.startDate.getTime(),
endDate: this.endDate.getTime(),
emotion: this.emotion,
});
localStorage.setItem('data', JSON.stringify(this.pomodoros));
this.cancel(); // just resets everything
drawChart(this.pomodoros);
},
tick() {
setTimeout(() => {
const now = Date.now();
const diffMs = this.endDate.getTime() - now;
if (diffMs <= 0) {
this.countDisplay = '00:00';
document.title = '00:00 - pomo-mood-o';
this.isRunning = false;
this.isDone = true;
return;
}
const diff = new Date(diffMs);
const displayMin = pad(diff.getMinutes());
const displaySec = pad(diff.getSeconds());
this.countDisplay = `${displayMin}:${displaySec}`;
document.title = `${this.countDisplay} - pomo-mood-o`;
this.tick();
}, 200);
},
};
}
// treats every start/end time like it happened on the earliest day
// so that we can display them as stacked bars
function normalize(data) {
const min = d3.min(data, d => d.startDate);
const normalized = data
.sort((a, b) => b.startDate - a.startDate)
.map(d => ({
date: formatDate(d.startDate),
start: normalizeTime(min, d.startDate),
end: normalizeTime(min, d.endDate),
emotion: emotions.find(e => e.value === d.emotion).label,
color: emotions.find(e => e.value === d.emotion).color,
emotionText: d.emotion,
}));
return normalized;
}
function normalizeTime(base, date) {
const m1 = moment(date);
const d2 = moment(base)
.startOf('day')
.hours(m1.hours())
.minutes(m1.minutes())
.seconds(m1.seconds());
return d2.valueOf();
}
function parseDayOfWeek(date) {
return date.substring(0, 3).toLocaleLowerCase();
}
function drawChart(data) {
const normalized = normalize(data);
const grouped = d3.group(normalized, d => d.date);
const days = Array.from(grouped.keys());
const margin = {top: 20, right: 20, bottom: 30, left: 80},
width = 960 - margin.left - margin.right,
height = days.length * 50 - margin.top - margin.bottom;
const x = d3
.scaleTime()
.range([width, 0])
.domain([
d3.max(normalized, d => d.end),
d3.min(normalized, d => d.start),
]);
const y = d3
.scaleBand()
.range([0, height])
.padding(0.1)
.domain(days);
d3.select('#chart svg').remove();
const svg = d3
.select('#chart')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr(
'transform',
'translate(' + margin.left + ',' + margin.top + ')',
);
// background bars
svg
.append('g')
.selectAll('.background-bar')
.data(days)
.enter()
.append('rect')
.attr(
'class',
d => `background-bar background-bar__${parseDayOfWeek(d)}`,
)
.attr('x', 0)
.attr('width', width)
.attr('y', (d, i) => y(d))
.attr('height', y.bandwidth())
.attr('opacity', 0.1)
.attr('fill', '#abb2bf')
.on('mouseover', d => {
d3.selectAll(`.background-bar__${parseDayOfWeek(d)}`)
.transition()
.duration(200)
.attr('opacity', 0.5);
})
.on('mouseout', d => {
d3.selectAll(`.background-bar__${parseDayOfWeek(d)}`)
.transition()
.duration(200)
.attr('opacity', 0.1);
});
// verticals
svg
.append('g')
.selectAll('vertical')
.data(x.ticks())
.enter()
.append('rect')
.attr('x', d => x(d) - 1)
.attr('width', 3)
.attr('y', 0)
.attr('height', height)
.attr('fill', '#282c34');
// bars
svg
.append('g')
.selectAll('.bar')
.data(normalized)
.enter()
.append('rect')
.attr('class', d => `bar bar__${d.emotionText}`)
.attr('x', d => x(d.start))
.attr('width', d => x(d.end) - x(d.start))
.attr('y', (d, i) => y(d.date))
.attr('height', y.bandwidth())
.attr('fill', d => d.color);
// emoji
svg
.selectAll('.emo')
.data(normalized)
.enter()
.append('text')
.attr('class', d => `emo emo__${d.emotionText}`)
.attr('x', d => x(d.start) + 2)
.attr('y', (d, i) => y(d.date) + 25)
.attr('font-size', '24px')
.text(d => d.emotion)
.on('mouseover', d => {
d3.selectAll(
`.bar:not(.bar__${d.emotionText}), .emo:not(.emo__${d.emotionText})`,
)
.transition()
.duration(200)
.attr('opacity', 0.2);
})
.on('mouseout', d => {
d3.selectAll(
`.bar:not(.bar__${d.emotionText}), .emo:not(.emo__${d.emotionText})`,
)
.transition()
.duration(200)
.attr('opacity', 1);
});
svg
.append('g')
.attr('transform', 'translate(0,' + height + ')')
.call(d3.axisBottom(x).tickFormat(formatTime));
svg.append('g').call(d3.axisLeft(y));
}
</script>
</body>
</html>