/
script.js
439 lines (374 loc) · 11.7 KB
/
script.js
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
$(document).ready(function(){
init();
if(typeof(Storage)!=="undefined") {
agenda();
dailyAlert();
taskAlert();
}
else
{
$('#result').innerHTML="Sorry, your browser does not support web storage...";
}
$('div#agenda').on('click','li.js-clickable',function(){
var taskId = $(this).attr('data-id');
var tasks = JSON.parse(localStorage.display);
var $form = $('div#interaction form');
//Set form attribute 'data-id' to index number of item being edited
$form.attr('data-id',taskId);
//populate the input title
var title = tasks[taskId].title;
$form.find('input#title').val(title).focus();
//populate the day checkboxes
$form.find('input[type=checkbox]').prop('checked',false);
for(var i = 0; i < tasks[taskId].days.length; i++) {
$form.find('input[type=checkbox]').each(function() {
if($(this).val() == tasks[taskId].days[i]) {
$(this).prop('checked',true);
}
});
}
//populate the input times
$form.find('input#hours').val(tasks[taskId].timeStart.hour);
$form.find('input#minutes').val(tasks[taskId].timeStart.minute);
$form.find('input#hoursEnd').val(tasks[taskId].timeEnd.hour);
$form.find('input#minutesEnd').val(tasks[taskId].timeEnd.minute);
$form.find('input#repeat').val(tasks[taskId].repeat);
//changes text of "add" button to "edit"
$('#addOrEditButton').text('Edit');
$('#addOrEditButton').after(' <button type="button" onclick="deleteIt();" class="toggler" id="deleteButton">Delete</button>');
});
// toggler
$('#page').on('click','.toggler, li.js-clickable',function(){
var $interaction = $('div#interaction');
if($interaction.hasClass('visible')) {
$interaction.animate({top:'-460px'}, 300).removeClass('visible');
$('#addOrHideButton').text('Add');
//remove delete button after deletion
$('#deleteButton').remove();
}
else {
$interaction.animate({top:'0px'}, 300).addClass('visible');
$('#addOrHideButton').text('Hide');
$interaction.find('input#title').focus();
}
});
//Clears task alert div
$('div#alert').on('click',function(){
if($('#alert').hasClass('visible')) {
$('#alert').animate({height:'0px'}, 3000).removeClass('visible');
$('#alert').html('').fadeIn();
taskAlert();
}
});
});
function init(){
//check if the localStorage has been used or this is new
if(!localStorage.display) {
var tasks = [];
tasks.push({
title: 'My First Task',
days: ["3","5"],
timeStart: {
hour: "01",
minute: "27"
},
timeEnd: {
hour: "",
minute: ""
},
repeat: ""
},
{
title: 'Get up and walk',
days: ["2","3","4","5","6"],
timeStart: {
hour: "09",
minute: "00"
},
timeEnd: {
hour: "23",
minute: "59"
},
repeat: "15"
},
{
title: 'Guitar',
days: ["2","4","6"],
timeStart: {
hour: "18",
minute: "00"
},
timeEnd: {
hour: "19",
minute: "00"
},
repeat: "5"
},{
title: 'Pushups',
days: ["2","3","4","5","6"],
timeStart: {
hour: "18",
minute: "00"
},
timeEnd: {
hour: "20",
minute: "00"
},
repeat: "20"
});
localStorage.display=JSON.stringify(tasks);
}
}
function dailyAlert() {
setInterval(function() {
var d = new Date();
var currentDay = d.getDay()+1;
if(d.getHours() == '05' && d.getMinutes() == '30') {
var tasks = JSON.parse(localStorage.display);
var titleDisplay = '';
$.each(tasks, function(index, value) {
if(value.days.indexOf(currentDay.toString()) > -1) {
titleDisplay += value.title+'\n';
}
});
alert('You need to complete a task today!\n'+titleDisplay);
}
console.log('Checking for 0530, every 1 minute '+d.getHours()+d.getMinutes());
}, 60000);
}
function taskAlert() {
var countDisplayedTasks=0;
setInterval(function() {
var tasks = JSON.parse(localStorage.display);
var d = new Date();
var currentDay = d.getDay()+1;
var hour = d.getHours();
var minute = d.getMinutes();
var hourTimesSixty = hour*60;
var current = hourTimesSixty+minute;
var displayAlerts = '';
/*
var starting=parseInt(value.timeStart.hour+value.timeStart.minute);
var ending=parseInt(value.timeEnd.hour+value.timeEnd.minute);
var current=parseInt(hour.toString()+minuteString);
*/
var hourString=hour.toString();
if(hourString.length < 2)
hourString = '0'+hour.toString();
var minuteString=minute.toString();
if(minuteString.length < 2)
minuteString = '0'+minute.toString();
$.each(tasks, function(index, value) {
var starting = parseInt(value.timeStart.hour)*60 + parseInt(value.timeStart.minute);
var ending = parseInt(value.timeEnd.hour)*60 + parseInt(value.timeEnd.minute);
var repeatFreq = parseInt(value.repeat);
if(current == starting || (current > starting && current <= ending && (current-starting)%repeatFreq == 0)) {
console.log('Do this now:'+value.title);
//$('#alert').fadeIn(3000,function(){
displayAlerts += '<br><center>Time for your '+hourString+minuteString+' "'+value.title+'"</center>';
// $('#alert').append('<br><center>Time for your '+hourString+minuteString+' "'+value.title+'"</center>');
//});
countDisplayedTasks++;
}
console.log('Addition:'+starting+'-'+ending+'Current'+current);
});
if(displayAlerts && countDisplayedTasks < 4) {
displayAlerts += '<br><center>Click to dismiss.</center>';
document.getElementById('alertsound').play();
$('#alert').animate({height:'+=90px'}, 300).addClass('visible');
$('#alert').append(displayAlerts);
}
else if(displayAlerts && countDisplayedTasks >= 4) {
//displayAlerts += '<br><center>Click to dismiss.</center>';
document.getElementById('alertsound').play();
$('#alert').animate({height:'+=45px'}, 300).addClass('visible');
$('#alert').append('<br><center>Too many alerts! Click to see more.</center>');
return;
}
//alert('Time to:\n'+displayTasks);// Use this later to make only one alert show per minute, if more than one task is found... this code is incomplete
}, 5000);
}
function addIt() {
var title = $('input#title').val(); //Stores title from HTML
var days = [];
//resets text of "add" button to "Add"
$('#addOrEditButton').text('Add');
//Check for empty fields
if(title == '' || $('input:checked').length == 0) {
alert('No empty fields, boss!');
return;
}
//Reset title field to null
//$('input#title').val('');
//For each checked input box, push the value to the array "days", then uncheck box (the unchecking is no longer necessary as form reset is done at end of function).
$('input:checked').each(function(){
days.push($(this).val());
// $(this).prop('checked',false);
});
//Set object "tasks" to what was previously saved to local storage
var tasks = JSON.parse(localStorage.display);
var $form = $('div#interaction form');
//If user is editing an existing task, save over that task; 'data-id' was set in the main function upon a click event with '$form.attr('data-id',taskId);'
if($form.attr('data-id')) {
tasks[$form.attr('data-id')] = {
title: title,
days: days,
timeStart: {
hour: $('input#hours').val(),
minute: $('input#minutes').val()
},
timeEnd: {
hour: $('input#hoursEnd').val(),
minute: $('input#minutesEnd').val()
},
repeat: $('input#repeat').val()
};
}
else{
//Add to "tasks" new items user just submitted.
tasks.push({
title: title,
days: days,
timeStart: {
hour: $('input#hours').val(),
minute: $('input#minutes').val()
},
timeEnd: {
hour: $('input#hoursEnd').val(),
minute: $('input#minutesEnd').val()
},
repeat: $('input#repeat').val()
});
}
console.log('Added: '+title+' '+days+' '+$('input#hours').val()+':'+$('input#minutes').val()+'-'+$('input#hoursEnd').val()+':'+$('input#minutesEnd').val()+' R:'+$('input#repeat').val()+' Total tasks: '+Object.keys(tasks).length);
console.log(tasks);
//Reset form
$form[0].reset();
$form.attr('data-id','');
//Save to local storage
localStorage.display=JSON.stringify(tasks);
//Reload agenda
agenda();
}
function deleteIt() {
//Set object "tasks" to what was previously saved to local storage
var tasks = JSON.parse(localStorage.display);
var $form = $('div#interaction form');
var days = [];
//resets text of "add" button to "Add"
$('#addOrEditButton').text('Add');
//For each checked input box, push the value to the array "days". Currently this is only used to create a log of what was deleted.
$('input:checked').each(function(){
days.push($(this).val());
});
//If user is trying to delete existing task, remove that task, otherwise provide alert; 'data-id' was set in the main function upon a click event with '$form.attr('data-id',taskId);'
if($form.attr('data-id')) {
delete tasks[$form.attr('data-id')];
tasks.splice($form.attr('data-id'),1);
}
else{
//Add to "tasks" new items user just submitted.
alert('Nothing to delete!');
}
console.log('Removed: '+$('input#title').val()+' '+days+' '+$('input#hours').val()+':'+$('input#minutes').val()+'-'+$('input#hoursEnd').val()+':'+$('input#minutesEnd').val()+' R:'+$('input#repeat').val()+' Total tasks: '+Object.keys(tasks).length);
console.log(tasks);
//Reset form
$form[0].reset();
$form.attr('data-id','');
//Save to local storage
localStorage.display=JSON.stringify(tasks);
//Reload agenda
agenda();
}
function resetTasks() {
localStorage.clear();
init(); // :-)
agenda();
}
function agenda() {
var tasks = JSON.parse(localStorage.display);
var taskSetDisplay = '';
var titleDisplay = '';
var d = new Date();
var currentDay = d.getDay()+1;
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var weekdayAbr=new Array(7);
weekdayAbr[0]="Sun";
weekdayAbr[1]="Mon";
weekdayAbr[2]="Tues";
weekdayAbr[3]="Wed";
weekdayAbr[4]="Thur";
weekdayAbr[5]="Fri";
weekdayAbr[6]="Sat";
for(var i=0;i<7;i++){
if(currentDay > 7)
currentDay = currentDay - 7;
var dayName = weekday[currentDay-1];
$.each(tasks, function(index, value) {
if(value.days.indexOf(currentDay.toString()) > -1) {
titleDisplay += '<li class="js-clickable" data-id='+index+'> > '+value.title+': <span>';
$.each(value.days, function(index, valueTwo) {
var dayNameAbr = weekdayAbr[valueTwo-1];
titleDisplay += ' '+dayNameAbr+' ';
});
titleDisplay = titleDisplay+value.timeStart.hour+value.timeStart.minute+'-'+value.timeEnd.hour+value.timeEnd.minute+' R:'+value.repeat+'</span></li>';
}
});
taskSetDisplay += '<h3>'+dayName+'</h3><ul>'+titleDisplay+'</ul>';
titleDisplay = '';
currentDay++;
}
$('#agenda').fadeOut(function(){
$(this).html(taskSetDisplay).fadeIn();
});
}
/*
function updateThisOneTinyField() {
var tasks = JSON.parse(localStorage.display);
var taskSetDisplay = '';
$.each(tasks, function(index, value) {
// transform the days array to the names of the dates
var daysDisplay = makeEmWords(value);
taskSetDisplay += '<h3>'+value.title+'</h3><ul>'+daysDisplay+'</ul><ul>'+value.timeStart.hour+':'+value.timeStart.minute+'</ul>';
});
$('#result').html(taskSetDisplay);
}
function makeEmWords(value) {
var daysDisplay = '';
for(var i = 0; i < value.days.length; i++) {
day = value.days[i];
switch(day) {
case "1":
name = 'Sunday';
break;
case "2":
name = 'Monday';
break;
case "3":
name = 'Tuesday';
break;
case "4":
name = 'Wednesday';
break;
case "5":
name = 'Thursday';
break;
case "6":
name = 'Friday';
break;
case "7":
name = 'Saturday';
break;
}
daysDisplay += '<li><label><input type="checkbox" name="day" value='+day+'>'+name+'</label></li>';
}
return daysDisplay;
}
*/