forked from 1995YogeshSharma/Multi-Stop-Watch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopWatch.html
More file actions
327 lines (288 loc) · 11.5 KB
/
stopWatch.html
File metadata and controls
327 lines (288 loc) · 11.5 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
<!DOCTYPE html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Link for jquery required for bootstrap -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<title>Multi Watch</title>
<link rel="shortcut icon" href="favicon.ico" />
<script type='text/javascript'>
//globals
var listStopWatch = [];
var idCounter = 1; //gives id to the watch object
//given time in seconds returns a string in format hh:mm:ss
function GiveTimeString(remSeconds) {
var secs = remSeconds % 60;
remSeconds = remSeconds - secs;
var mins = (remSeconds/60) % 60;
remSeconds = remSeconds/60 - mins;
var hrs = (remSeconds/60);
if(hrs < 10) hrs = "0" + hrs;
if(mins < 10) mins = "0" + mins;
if(secs < 10) secs = "0" + secs;
return hrs + ":" + mins + ":" + secs;
}
//returns html body for the watch
function StopWatchBody(Watch) {
var pausePlayButtonStr = function(status) {
if(status == 1) {
return "<span class='btn btn-warning glyphicon glyphicon-pause' onclick='PausePlayToggle(this, " + Watch.id + ")'>Pause</span>";
}
else {
return "<span class='btn btn-success glyphicon glyphicon-play' onclick='PausePlayToggle(this, " + Watch.id + ")'>Play</span>"
}
}
var retStr = "<div class='col-md-6' id='" + Watch.id + "'>" +
"<div class='panel panel-default'>" +
"<div class='panel-heading'>" +
"<div class='row'>" +
"<div class='col-md-8'>" +
"<h3 class='panel-title'>" +
Watch.title +
"</h3>" +
"</div>" +
"<div class='col-md-4 text-center'>" +
"<button type='button' class='btn btn-default glyphicon glyphicon-pencil' data-toggle='modal' data-target='#notesModal' onclick='fillModal(" + Watch.id + ")'>" +
" Notes" +
"</button>" +
"</div>" +
"</div>" +
"</div>" +
"<div class='panel-body'>" +
"<h2 class='text-center' + id='watch" + Watch.id +"'>" +
GiveTimeString(Watch.curTime - Watch.startTime) +
"</h2>" +
"</div>" +
"<div class='panel-footer'>" +
"<div class='btn-group btn-group-justified'>" +
pausePlayButtonStr(Watch.status) +
"<span class='btn btn-info glyphicon glyphicon-refresh' onclick='RestartClock(" + Watch.id + ")'>Restart</span>" +
"<span class='btn btn-danger glyphicon glyphicon-remove' onclick='RemoveOne(" + Watch.id + ")'>Remove</span>" +
"<!/div>" +
"</div>" +
"</div>" +
"</div>";
return retStr;
}
//creates a watch object
function Watch(status = 1, title, startTime=Date.now(), curTime=Date.now(), notes=[]) {
if(title == null)
title = document.getElementById('title').value;
this.id = idCounter;
idCounter = idCounter + 1;
this.status = status; // 0 -> pause state 1 -> play state
this.title = title;
this.startTime = startTime;
this.curTime = curTime;
this.notes = notes;
}
//adds watch to DOM
function AddWatch() {
listStopWatch[listStopWatch.length] = new Watch();
$("#stopwatches").append(StopWatchBody(listStopWatch[listStopWatch.length - 1]));
document.getElementById('title').value = '';
}
function ShowList() {
console.log(listStopWatch);
// for(var i = 0; i < listStopWatch.length; i++) {
// console.log(listStopWatch[i].title);
// }
}
//removes a single clock when called
function RemoveOne(id) {
document.getElementById(id).outerHTML = '';
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].id == id) {
listStopWatch.splice(i, 1);
break;
}
}
}
//restarts the time for one clock
function RestartClock(id) {
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].id == id) {
listStopWatch[i].startTime = Date.now();
listStopWatch[i].curTime = Date.now();
document.getElementById('watch' + id).innerHTML = GiveTimeString(0);
}
}
}
//for pause play
function PausePlayToggle(elem, id) {
if(elem.innerHTML == 'Pause') {
elem.outerHTML = "<span class='btn btn-success glyphicon glyphicon-play' onclick='PausePlayToggle(this, " + id + ")'>Play</span>"
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].id == id) {
listStopWatch[i].status = 0;
}
}
}
else if(elem.innerHTML == 'Play') {
elem.outerHTML = "<span class='btn btn-warning glyphicon glyphicon-pause' onclick='PausePlayToggle(this, " + id + ")'>Pause</span>"
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].id == id) {
listStopWatch[i].status = 1;
}
}
}
}
//removes all clocks
function RemoveAll() {
listStopWatch = [];
idCounter = 1;
$("#stopwatches").html('');
}
//updates the time in clocks
function updateClocks() {
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].status !== 0) {
listStopWatch[i].curTime += 1;
var tempId = 'watch' + listStopWatch[i].id;
var tempModalId = 'note' + listStopWatch[i].id;
document.getElementById(tempId).innerHTML = GiveTimeString(listStopWatch[i].curTime - listStopWatch[i].startTime);
if(document.getElementById(tempModalId) !== null) {
document.getElementById(tempModalId).innerHTML = GiveTimeString(listStopWatch[i].curTime - listStopWatch[i].startTime);
}
}
}
}
//function to add note to the watch's list and reload the modal
function AddNote(id) {
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].id == id) {
listStopWatch[i].notes.push(document.getElementById('newNote').value);
fillModal(id);
break;
}
}
}
//function to remove note from list when cross button in list items is pressed
function RemoveNote(elem, id, noteIndex) {
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].id == id) {
listStopWatch[i].notes.splice(noteIndex, 1);
elem.outerHTML = '';
}
}
}
//function to fill the modal dynamically when a note button is called
function fillModal(id) {
var watch = null;
var retHtmlTitle = '';
var retHtmlBody = '';
var retHtmlFooter = '';
for(var i = 0; i < listStopWatch.length; i++) {
if(listStopWatch[i].id == id) {
watch = listStopWatch[i];
}
}
if(watch == null) {
console.log('error');
}
retHtmlTitle = watch.title;
retHtmlBody = retHtmlBody +
"<h2 class='text-center' + id='note" + watch.id +"'>" +
GiveTimeString(watch.curTime - watch.startTime) +
"</h2>" +
"<hr>";
retHtmlBody += "<ul class='list-group'>";
for(var i = 0; i < watch.notes.length; i++) {
retHtmlBody += "<li class='row list-group-item'>" +
"<p class='col-md-11 list-item'>" +
watch.notes[i] +
"</p>"+
"<span class='col-md-1 badge pull-right'>" +
"<span class='glyphicon glyphicon-remove' onclick='RemoveNote(this.parentNode.parentNode, " + watch.id + ", " + i + ")'></span>" +
"</span>" +
"</li>";
}
retHtmlBody += "</ul>";
retHtmlFooter += "<div class='input-group'>" +
"<input type='text' name='noteText' class='form-control' id='newNote' placeholder='Write Note' />" +
"<span class='input-group-btn'>" +
"<button onclick='AddNote(" + watch.id + ")' class='form-control btn btn-primary'><span class='glyphicon glyphicon-plus'></span> Add Note </button>" +
"</span>" +
"</div>";
$('#notesModalTitle').html(retHtmlTitle);
$('#notesModalBody').html(retHtmlBody);
$('#notesModalFooter').html(retHtmlFooter);
}
//clocks get updated each second because of this
setInterval(updateClocks, 1000);
//store the list of stopwatches in the cookie so that reloading the page does not cause data to loss
window.onbeforeunload = function(e){
e = e || window.event;
localStorage.setItem("myCookie", JSON.stringify(listStopWatch));
}
//load the list of stopwatches in listStopWatch and attach them to html is list is present in cookie.
window.onload = function(e) {
e = e || window.event;
var X = JSON.parse(localStorage.getItem("myCookie"));
for(var i = 0; i < X.length; i++) {
listStopWatch[listStopWatch.length] = new Watch(X[i].status, X[i].title, X[i].startTime, X[i].curTime, X[i].notes);
$("#stopwatches").append(StopWatchBody(listStopWatch[listStopWatch.length - 1])); }
}
function detectEnter(event) {
event = event || window.event;
if(event.keyCode == 13){
AddWatch();
}
}
</script>
<style>
li:hover {
background-color: pink;
}
li .glyphicon:hover {
color: red;
}
li .list-item {
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<!-- Modal for showing Notes-->
<div class="modal fade" id="notesModal" tabindex="-1" role="dialog" aria-labelledby="notesModalTitle">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="notesModalTitle">to be filled by function</h4>
</div>
<div class="modal-body" id="notesModalBody">
<p> to be filled by function</p>
</div>
<hr>
<div class="modal-footer" id="notesModalFooter">
<p> to be filled by function </p>
</div>
</div>
</div>
</div>
<body class='container'>
<div class="row">
<div class="container-fluid">
<h1 class="page-header text-center"> Multi Stop Watch </h1>
<div class="input-group">
<input id='title' type='text' class="form-control" onkeydown='detectEnter(event)' placeholder="Enter title for StopWatch"/>
<span class="input-group-btn">
<button onclick="AddWatch()" class="form-control btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Add stopwatch </button>
</span>
</div>
</div>
</div>
<hr>
<!--container for Stopwatches -->
<div class="row" id="stopwatches">
</div>
<hr>
<div class="row">
<p class="col-md-offset-5 btn btn-danger btn-lg" onclick="RemoveAll()"><span class="glyphicon glyphicon-remove"></span>REMOVE ALL</p>
</div>
</body>
</html>