-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtodo_script.js
83 lines (72 loc) · 2.81 KB
/
todo_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
const todo_list=document.getElementById('todo_list');
var i=0;
if(loadedTimer === null || records[loadedTimer] === undefined){ // New timer i.e. not a loaded from saved ones or record cannot be found
}
else{ // Loaded timer from saved ones
for(let todo of records[loadedTimer].todos){
let taskObject = addTask(todo.name);
console.log(taskObject);
if(todo.completed){
doneTask(taskObject.name)
taskObject.checkbox.setAttribute("checked",true)
}
}
}
function addTask(value="") { // This function is called when Add button in todo list is clicked
//Create a container div to contain controls for i-th task
//The controls will be created dynamically here
const newTask=document.createElement('div');
newTask.setAttribute('id','Task'+String(i));
//To that container, add textbox showing name of task
const taskName=document.createElement('input');
taskName.setAttribute('type','text');
taskName.setAttribute('placeholder','Enter TaskName');
taskName.setAttribute('class','taskName');
taskName.setAttribute('id','taskName'+String(i));
taskName.value = value;
//Add checkbox to container and call doneTask() when selected
const doneCheck=document.createElement('input');
doneCheck.setAttribute('type','checkbox');
doneCheck.setAttribute('id','doneCheck'+String(i));
doneCheck.setAttribute('class','checkButton');
doneCheck.addEventListener('change',function () {
if(doneCheck.checked){
doneTask(taskName);
}
else {
reviveTask(taskName);
}
});
//Create a delete button and call deleteTask() when clicked
const deleteButton=document.createElement('input');
deleteButton.setAttribute('type','button');
deleteButton.setAttribute('value','X');
deleteButton.setAttribute('class','deleteButton');
deleteButton.setAttribute('id','deleteButton'+String(i));
deleteButton.addEventListener('click',function () {
deleteTask(newTask);
});
newTask.appendChild(doneCheck);
newTask.appendChild(taskName);
newTask.appendChild(deleteButton);
todo_list.appendChild(newTask);
i++;
let returnObject = {task:newTask,name:taskName,delButton:deleteButton,checkbox:doneCheck};
return(returnObject);
}
function doneTask(elementTask) {
// Strike through the task name and disable its controls
if(elementTask.value){
elementTask.style.textDecoration = "line-through";
elementTask.readOnly = "true";
}
}
function reviveTask(elementTask) {
elementTask.style.textDecoration = "none";
elementTask.removeAttribute("readonly");
}
function deleteTask(elementTask) {
//Removes element from its parent
const task = document.getElementById(elementTask.id);
todo_list.removeChild(task);
}