-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetracker.html
255 lines (228 loc) · 8.51 KB
/
timetracker.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
<!DOCTYPE html>
<html>
<head>
<title>Time tracker</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="jquery.storageapi.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
var interval;
function checkNotificationPermission() {
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
} else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission == "granted") {
$("#notification-alert").hide();
}
});
}
}
function notifyMe() {
var options = {
body: "What are you doing?\nClick here to enter your input."
};
var notification = new Notification("Time tracker", options);
setTimeout(notification.close.bind(notification), 5000);
notification.onclick = function () {
window.focus();
focusInput();
};
}
function addEntry(e) {
e.preventDefault();
var data = $("#add");
if ("" != data.val().trim()) {
var date = new Date();
prependDOMElement(date, data.val());
var map = $.localStorage.get("entries") || {};
map[date.getTime()] = data.val();
$.localStorage.set("entries", map);
data.val("");
} else {
showAlertSection();
}
}
function showAlertSection() {
$(".alert-warning").slideDown();
$(".alert-warning").delay(1000).slideUp();
}
function hideAlertSection() {
$(".alert-warning").hide();
}
function focusInput() {
document.getElementById("add").focus();
}
function prependDOMElement(date, value) {
$(".list-group").prepend("<li class='list-group-item' data-timestamp='" + date.getTime() + "'>"
+ "<span class='badge'>" + date.toDateString() + "</span>"
+ "<span class='badge diff'></span>"
+ "<span class='badge'>" + date.getHours() + ":" + (date.getMinutes() < 10 ? '0' : '') + date.getMinutes() + "h</span>" + value + "</li>");
setDiffInMinutes($('*[data-timestamp="' + date.getTime() + '"]'), date);
}
function setDiffInMinutes(current, date) {
var nextTimeStamp = current.next().data("timestamp");
if (nextTimeStamp != undefined) {
var nextIsDifferentDay = date.toDateString() != new Date(nextTimeStamp).toDateString();
if (!nextIsDifferentDay) {
var diff = Math.floor((date.getTime() - parseInt(nextTimeStamp)) / 60000);
current.find(".diff").text("+" + diff + " min");
}
}
}
function fillTable() {
var map = $.localStorage.get("entries") || {};
var keys = Object.keys(map);
keys.sort();
for(count = 0; count < keys.length; count++) {
prependDOMElement(new Date(parseInt(keys[count])), map[keys[count]]);
}
}
function confirmClear() {
$(".clearButton").hide();
$(".clearConfirmationYes").show();
$(".clearConfirmationCancel").show();
}
function cancelClear() {
$(".clearButton").show();
$(".clearConfirmationYes").hide();
$(".clearConfirmationCancel").hide();
}
function performClear() {
$(".list-group").slideUp(1000);
$(".list-group").delay(1000).empty();
$(".list-group").slideDown();
$.localStorage.remove("entries");
cancelClear();
}
function setReminderButtonSelection(id) {
if (id == undefined) { // init load
var storedInterval = $.localStorage.get("interval");
if (storedInterval == null) {
storedInterval = 30; // default 30
}
id = "remind-" + storedInterval;
$("." + id + "-label").button("toggle");
console.log(id);
}
if (id != undefined && id == "remind-60") {
var intervalSelection = 60;
} else if (id != undefined && id == "remind-1") {
var intervalSelection = 1;
} else if (id != undefined && id == "remind-0") {
var intervalSelection = 0;
} else {
var intervalSelection = 30; // default 30
}
$.localStorage.set("interval", intervalSelection);
console.log("Init loop with delay " + intervalSelection);
initInterval();
}
function initInterval() {
if (interval != undefined) {
clearInterval(interval);
console.log("cleared interval, setting new one");
}
interval = setInterval(function(){
intervalSelection = $.localStorage.get("interval");
if ((new Date().getMinutes() === 0 && intervalSelection != 0)
|| (new Date().getMinutes() === 30 && intervalSelection == 30)
|| (intervalSelection == 1)) {
notifyMe();
}
}, 60 * 1000);
}
$(function() { //shorthand document.ready function
$('[data-toggle="tooltip"]').tooltip();
$('#timetrackerform').on('submit', function(e) {
addEntry(e);
});
$("#clearConfirm").click(function(e) {
confirmClear();
});
$("#clearCancel").click(function(e) {
cancelClear();
});
$("#clearYes").click(function(e) {
performClear();
});
$('.reminder-button').change(function (e) {
setReminderButtonSelection(e.target.id);
});
checkNotificationPermission();
hideAlertSection();
cancelClear();
fillTable();
focusInput();
setReminderButtonSelection();
initInterval();
});
</script>
</head>
<body style="margin:20px; padding-bottom: 50px;">
<form id="timetrackerform">
<div class="alert alert-danger alert-dismissible" id="notification-alert" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Caution:</strong> Notifications are not enabled.
</div>
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">Time tracker
<div data-toggle="buttons" class="btn-group btn-group-xs navbar-right" data-toggle="button" style="margin-right:0.2em" role="group">
<label class="btn btn-default remind-0-label">
<input class="reminder-button" type="radio" name="timer" id="remind-0"/>Off
</label>
<label class="btn btn-default remind-1-label">
<input class="reminder-button" type="radio" name="timer" id="remind-1"/>1m
</label>
<label class="btn btn-default remind-30-label">
<input class="reminder-button" type="radio" name="timer" id="remind-30"/>30m
</label>
<label class="btn btn-default remind-60-label">
<input class="reminder-button" type="radio" name="timer" id="remind-60"/>1h
</label>
<span class="glyphicon glyphicon-refresh" aria-hidden="true" style="margin-left:0.4em"></span>
</div>
</div>
<div class="panel-body">
<div class="input-group">
<input type="text" class="form-control" id="add" placeholder="What are you doing?">
<span class="input-group-btn">
<button class="btn btn-success" type="submit">Add</button>
</span>
</div>
<div class="alert alert-warning" role="alert">Please enter a description of your tasks.</div>
</div>
<ul class="list-group">
</ul>
</div>
<div class="btn-group btn-group-justified" id="bottom" role="group">
<div class="btn-group clearButton" role="group">
<button type="button" id="clearConfirm" class="btn btn-danger">Clear</button>
</div>
<div class="btn-group clearConfirmationYes" role="group">
<button type="button" id="clearYes" class="btn btn-success">Yes, clear!</button>
</div>
<div class="btn-group clearConfirmationCancel" role="group">
<button type="button" id="clearCancel" class="btn btn-danger">Cancel</button>
</div>
</div>
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<ul class="nav nav-pills">
<li role="presentation"><a target="_blank" href="http://www.lienemann.net">About</a></li>
<li role="presentation"><a target="_blank" href="https://github.com/dominik-lienemann/timetracker/">Github</a></li>
<li role="presentation"><a href="#bottom">Scroll to the bottom</a></li>
<li class="navbar-right" role="presentation"><a target="_blank" href="https://www.paypal.me/lienemann">
<img alt="donate" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif">
</a></li>
</ul>
</div>
</nav>
</form>
</body>
</html>