-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo.js
608 lines (474 loc) · 18 KB
/
todo.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
todoMain();
function todoMain() {
const DEFAULT_OPTION = "Choose category";
let inputElem,
inputElem2,
dateInput,
timeInput,
addButton,
sortButton,
selectElem,
todoList = [],
calendar,
shortlistBtn,
changeBtn,
todoTable,
draggingElement;
getElements();
addListeners();
initCalendar();
load();
renderRows(todoList);
updateSelectOptions();
function getElements() {
inputElem = document.getElementsByTagName("input")[0];
inputElem2 = document.getElementsByTagName("input")[1];
dateInput = document.getElementById("dateInput");
timeInput = document.getElementById("timeInput");
addButton = document.getElementById("addBtn");
sortButton = document.getElementById("sortBtn");
selectElem = document.getElementById("categoryFilter");
shortlistBtn = document.getElementById("shortlistBtn");
changeBtn = document.getElementById("changeBtn");
todoTable = document.getElementById("todoTable");
}
function addListeners() {
addButton.addEventListener("click", addEntry, false);
sortButton.addEventListener("click", sortEntry, false);
selectElem.addEventListener("change", multipleFilter, false);
shortlistBtn.addEventListener("change", multipleFilter, false);
document.getElementById("todo-modal-close-btn").addEventListener("click", closeEditModalBox, false);
changeBtn.addEventListener("click", commitEdit, false);
todoTable.addEventListener("dragstart", onDragstart, false);
todoTable.addEventListener("drop", onDrop, false);
todoTable.addEventListener("dragover", onDragover, false);
}
function addEntry(event) {
let inputValue = inputElem.value;
inputElem.value = "";
let inputValue2 = inputElem2.value;
inputElem2.value = "";
let dateValue = dateInput.value;
dateInput.value = "";
let timeValue = timeInput.value;
timeInput.value = "";
let obj = {
id: _uuid(),
todo: inputValue,
category: inputValue2,
date: dateValue,
time: timeValue,
done: false,
};
renderRow(obj);
todoList.push(obj);
save();
updateSelectOptions();
}
function updateSelectOptions() {
let options = [];
todoList.forEach((obj) => {
options.push(obj.category);
});
let optionsSet = new Set(options);
// empty the select options
selectElem.innerHTML = "";
let newOptionElem = document.createElement('option');
newOptionElem.value = DEFAULT_OPTION;
newOptionElem.innerText = DEFAULT_OPTION;
selectElem.appendChild(newOptionElem);
for (let option of optionsSet) {
let newOptionElem = document.createElement('option');
newOptionElem.value = option;
newOptionElem.innerText = option;
selectElem.appendChild(newOptionElem);
}
}
function save() {
let stringified = JSON.stringify(todoList);
localStorage.setItem("todoList", stringified);
}
function load() {
calendar.today();
let retrieved = localStorage.getItem("todoList");
todoList = JSON.parse(retrieved);
//console.log(typeof todoList)
if (todoList == null)
todoList = [];
}
function renderRows(arr) {
arr.forEach(todoObj => {
renderRow(todoObj);
})
}
function renderRow({ todo: inputValue, category: inputValue2, id, date, time, done }) {
// add a new row
let table = document.getElementById("todoTable");
let trElem = document.createElement("tr");
table.appendChild(trElem);
trElem.draggable = "true";
trElem.dataset.id = id;
// checkbox cell
let checkboxElem = document.createElement("input");
checkboxElem.type = "checkbox";
checkboxElem.addEventListener("click", checkboxClickCallback, false);
checkboxElem.dataset.id = id;
let tdElem1 = document.createElement("td");
tdElem1.appendChild(checkboxElem);
trElem.appendChild(tdElem1);
// date cell
let dateElem = document.createElement("td");
dateElem.innerText = formatDate(date);
trElem.appendChild(dateElem);
// time cell
let timeElem = document.createElement("td");
timeElem.innerText = time;
trElem.appendChild(timeElem);
// to-do cell
let tdElem2 = document.createElement("td");
tdElem2.innerText = inputValue;
trElem.appendChild(tdElem2);
// category cell
let tdElem3 = document.createElement("td");
tdElem3.innerText = inputValue2;
tdElem3.className = "categoryCell";
trElem.appendChild(tdElem3);
// edit cell
let editSpan = document.createElement("span");
editSpan.innerText = "edit";
editSpan.className = "material-icons";
editSpan.addEventListener("click", toEditItem, false);
editSpan.dataset.id = id;
let editTd = document.createElement("td");
editTd.appendChild(editSpan);
trElem.appendChild(editTd);
// delete cell
let spanElem = document.createElement("span");
spanElem.innerText = "delete";
spanElem.className = "material-icons";
spanElem.addEventListener("click", deleteItem, false);
spanElem.dataset.id = id;
let tdElem4 = document.createElement("td");
tdElem4.appendChild(spanElem);
trElem.appendChild(tdElem4);
// done button
checkboxElem.type = "checkbox";
checkboxElem.checked = done;
if (done) {
trElem.classList.add("strike");
} else {
trElem.classList.remove("strike");
}
addEvent({
id: id,
title: inputValue,
start: date,
});
dateElem.dataset.type = "date";
dateElem.dataset.value = date;
timeElem.dataset.type = "time";
tdElem2.dataset.type = "todo";
tdElem3.dataset.type = "category";
dateElem.dataset.id = id;
timeElem.dataset.id = id;
tdElem2.dataset.id = id;
tdElem3.dataset.id = id;
function deleteItem() {
trElem.remove();
updateSelectOptions();
for (let i = 0; i < todoList.length; i++) {
if (todoList[i].id == this.dataset.id)
todoList.splice(i, 1);
}
save();
// remove from calendar
calendar.getEventById(this.dataset.id).remove();
}
function checkboxClickCallback() {
trElem.classList.toggle("strike");
for (let i = 0; i < todoList.length; i++) {
if (todoList[i].id == this.dataset.id)
todoList[i]["done"] = this.checked;
}
save();
}
}
function _uuid() {
var d = Date.now();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
function sortEntry() {
todoList.sort((a, b) => {
let aDate = Date.parse(a.date);
let bDate = Date.parse(b.date);
return aDate - bDate;
});
save();
clearTable();
renderRows(todoList);
}
function initCalendar() {
var calendarEl = document.getElementById('calendar');
calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth',
initialDate: '2020-07-07',
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay'
},
events: [],
eventClick: function (info) {
toEditItem(info.event);
},
eventBackgroundColor: "#a11e12",
eventBorderColor: "#ed6a5e",
editable: true,
eventDrop: function (info) {
calendarEventDragged(info.event);
}
});
calendar.render();
}
function addEvent(event) {
calendar.addEvent(event);
}
function clearTable() {
// Empty the table, keeping the first row
let trElems = document.getElementsByTagName("tr");
for (let i = trElems.length - 1; i > 0; i--) {
trElems[i].remove();
}
calendar.getEvents().forEach(event => event.remove());
}
function multipleFilter() {
clearTable();
// shortlistBtn.checked
let selection = selectElem.value;
if (selection == DEFAULT_OPTION) {
if (shortlistBtn.checked) {
let filteredIncompleteArray = todoList.filter(obj => obj.done == false);
renderRows(filteredIncompleteArray);
let filteredDoneArray = todoList.filter(obj => obj.done == true);
renderRows(filteredDoneArray);
} else {
renderRows(todoList);
}
} else {
let filteredCategoryArray = todoList.filter(obj => obj.category == selection);
if (shortlistBtn.checked) {
let filteredIncompleteArray = filteredCategoryArray.filter(obj => obj.done == false);
renderRows(filteredIncompleteArray);
let filteredDoneArray = filteredCategoryArray.filter(obj => obj.done == true);
renderRows(filteredDoneArray);
} else {
renderRows(filteredCategoryArray);
}
}
}
function onTableClicked(event) {
if (event.target.matches("td") && event.target.dataset.editable == "true") {
let tempInputElem;
switch (event.target.dataset.type) {
case "date":
tempInputElem = document.createElement("input");
tempInputElem.type = "date";
tempInputElem.value = event.target.dataset.value;
break;
case "time":
tempInputElem = document.createElement("input");
tempInputElem.type = "time";
tempInputElem.value = event.target.innerText;
break;
case "todo":
case "category":
tempInputElem = document.createElement("input");
tempInputElem.value = event.target.innerText;
break;
default:
}
event.target.innerText = "";
event.target.appendChild(tempInputElem);
tempInputElem.addEventListener("change", onChange, false);
}
function onChange(event) {
let changedValue = event.target.value;
let id = event.target.parentNode.dataset.id;
let type = event.target.parentNode.dataset.type;
// remove from calendar
calendar.getEventById(id).remove();
todoList.forEach(todoObj => {
if (todoObj.id == id) {
//todoObj.todo = changedValue;
todoObj[type] = changedValue;
addEvent({
id: id,
title: todoObj.todo,
start: todoObj.date,
});
}
});
save();
if (type == "date") {
event.target.parentNode.innerText = formatDate(changedValue);
} else {
event.target.parentNode.innerText = changedValue;
}
}
}
function formatDate(date) {
let dateObj = new Date(date);
let formattedDate = dateObj.toLocaleString("en-GB", {
month: "long",
day: "numeric",
year: "numeric",
});
return formattedDate;
}
function showEditModalBox(event) {
document.getElementById("todo-overlay").classList.add("slidedIntoView");
}
function closeEditModalBox(event) {
document.getElementById("todo-overlay").classList.remove("slidedIntoView");
}
function commitEdit(event) {
closeEditModalBox();
let id = event.target.dataset.id;
let todo = document.getElementById("todo-edit-todo").value;
let category = document.getElementById("todo-edit-category").value;
let date = document.getElementById("todo-edit-date").value;
let time = document.getElementById("todo-edit-time").value;
// remove from calendar
calendar.getEventById(id).remove();
for (let i = 0; i < todoList.length; i++) {
if (todoList[i].id == id) {
todoList[i] = {
id: id,
todo: todo,
category: category,
date: date,
time: time
};
addEvent({
id: id,
title: todoList[i].todo,
start: todoList[i].date,
});
}
}
save();
// Update the table
//let tdNodeList = todoTable.querySelectorAll("td");
//let tdNodeList = todoTable.querySelectorAll("td[data-id='" + id + "']");
let tdNodeList = todoTable.querySelectorAll(`td[data-id='${id}']`);
for (let i = 0; i < tdNodeList.length; i++) {
//if(tdNodeList[i].dataset.id == id){
let type = tdNodeList[i].dataset.type;
switch (type) {
case "date":
tdNodeList[i].innerText = formatDate(date);
break;
case "time":
tdNodeList[i].innerText = time;
break;
case "todo":
tdNodeList[i].innerText = todo;
break;
case "category":
tdNodeList[i].innerText = category;
break;
}
//}
}
}
function toEditItem(event) {
showEditModalBox();
let id;
if (event.target) // mouse event
id = event.target.dataset.id;
else // calendar event
id = event.id;
preFillEditForm(id);
}
function preFillEditForm(id) {
let result = todoList.find(todoObj => todoObj.id == id);
let { todo, category, date, time } = result;
document.getElementById("todo-edit-todo").value = todo;
document.getElementById("todo-edit-category").value = category;
document.getElementById("todo-edit-date").value = date;
document.getElementById("todo-edit-time").value = time;
changeBtn.dataset.id = id;
}
function onDragstart(event) {
draggingElement = event.target; //trElem
}
function onDrop(event) {
/* Handling visual drag and drop of the rows */
// prevent when target is table
if (event.target.matches("table"))
return;
let beforeTarget = event.target;
// to look through parent until it is tr
while (!beforeTarget.matches("tr"))
beforeTarget = beforeTarget.parentNode;
// to be implemented
//beforeTarget.style.paddingTop = "1rem";
// prevent when the tr is the first row
if (beforeTarget.matches(":first-child"))
return;
// visualise the drag and drop
todoTable.insertBefore(draggingElement, beforeTarget);
/* Handling the array */
let tempIndex;
// find the index of one to be taken out
todoList.forEach((todoObj, index) => {
if (todoObj.id == draggingElement.dataset.id)
tempIndex = index;
});
// pop the element
let [toInsertObj] = todoList.splice(tempIndex, 1);
// find the index of one to be inserted before
todoList.forEach((todoObj, index) => {
if (todoObj.id == beforeTarget.dataset.id)
tempIndex = index;
});
// insert the temp
todoList.splice(tempIndex, 0, toInsertObj);
// update storage
save();
}
function onDragover(event) {
event.preventDefault();
}
function calendarEventDragged(event) {
let id = event.id;
let dateObj = new Date(event.start);
let year = dateObj.getFullYear();
let month = dateObj.getMonth() + 1;
let date = dateObj.getDate();
let paddedMonth = month.toString();
if (paddedMonth.length < 2) {
paddedMonth = "0" + paddedMonth;
}
let paddedDate = date.toString();
if (paddedDate.length < 2) {
paddedDate = "0" + paddedDate;
}
let toStoreDate = `${year}-${paddedMonth}-${paddedDate}`;
console.log(toStoreDate);
todoList.forEach(todoObj => {
if (todoObj.id == id) {
todoObj.date = toStoreDate;
}
});
save();
multipleFilter();
}
}