-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhow-was-your-day.html
197 lines (175 loc) · 5.68 KB
/
how-was-your-day.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
---
title: How was your day?
categories: web
tags:
- privacy
- offline first
- offline only
- offline apps
- idea
- health tracking
- privacy
- experiment
---
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
/* Reset default margin */
background: rgb(21, 159, 193);
color: rgb(255, 250, 245);
font-family: Arial, Helvetica, sans-serif;
}
a {
color: rgb(255, 250, 245);
}
#redirect {
text-align: center;
padding: 5px;
}
#right {
float: right;
right: 0;
top: 0;
padding-right: 10px;
}
#about {
padding: 20px;
}
ul,
li {
display: inline;
}
.vis-item.good {
background-color: greenyellow;
border-color: green;
color: green;
}
.vis-item.bad {
background-color: red;
border-color: darkred;
color: white;
}
.vis-timeline {
background-color: gainsboro;
}
.vis-selected.bad,
.vis-selected.good {
color: brown;
box-shadow: 0 0 10px gray;
}
</style>
<link href="http://visjs.org/dist/vis.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pouchdb.min.js"></script>
<script src="http://visjs.org/dist/vis.min.js"></script>
<script>
var names = {
'good': { title: 'Good Days', 'response': 'glad' },
'bad': { title: 'Bad Days', response: 'sorry' }
};
// Create a DataSet (allows two way data-binding)
var items = new vis.DataSet([]);
function addTimelineDoc(doc) {
items.add({
id: items.length + 1,
content: doc.state,
start: doc._id,
group: 1 + Object.keys(names).indexOf(doc.state),
title: doc.log,
className: doc.state
});
}
function record(state) {
console.log('recording state: ' + state);
var db = new PouchDB('howwasyourday');
var date = new Date();
var doc = {
_id: date.toISOString().split('T')[0],
state: state,
log: document.getElementById("more").value
};
console.log(doc);
db.put(doc).then(function (resp) {
console.log(resp);
}).catch(function (err) {
console.log(err);
});
document.getElementById("about").innerHTML = "<h1>I'm " + names[state].response + " to hear that</h1>";
addTimelineDoc(doc);
};
function timelineFromDocs(db) {
var container = document.getElementById('visualization');
// Configuration for the Timeline
var options = {
tooltip: {
followMouse: true,
overflowMethod: 'cap'
},
showCurrentTime: false,
width: '90%',
zoomMin: 60 * 60 * 24 * 1000
};
var groups = new vis.DataSet();
Object.keys(names).forEach(function f(x, y) {
groups.add({ id: y + 1, content: names[x].title });
});
db.allDocs({ include_docs: true }).then(function f(data) {
console.log(data);
data.rows.forEach(function x(row) {
var doc = row.doc;
if (doc.state) {
addTimelineDoc(doc);
}
});
document.getElementById('loading').style.display = 'none';
var timeline = new vis.Timeline(container, items, groups, options);
});
};
function init() {
var db = new PouchDB('howwasyourday');
db.get('_design/info').catch(function (err) {
console.log(err);
var ddoc = {
_id: '_design/info',
views: {
by_site: {
map: function (doc) { emit(doc.date); }.toString(),
reduce: '_count'
}
}
};
db.put(ddoc).then(function () {
console.log('Created design doc');
}).catch(function (err) {
console.log('Error creating design doc - already exists?');
});
});
var date = new Date();
db.get(date.toISOString().split('T')[0]).then(function (resp) {
console.log(resp);
document.getElementById("about").innerHTML = "<h1>I'm " + names[resp.state].response + " to hear that you've had a " + resp.state + " day</h1>";
});
timelineFromDocs(db);
};
</script>
</head>
<body onload="init();">
<center>
<div id="about">
<h1>How was your day?</h1>
<p>Mood tracking in a one page, offline app.</p>
<form>
<button id="good" onclick="record('good')">Good</button>
<button id="bad" onclick="record('bad')">Bad</button>
<br />
<textarea id="more" placeholder="Any more?"></textarea>
</form>
</div>
<div id="visualization"></div>
<div id="loading">loading...</div>
<p>What you record never leaves your device.</p>
</center>
</body>
</html>