-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
160 lines (138 loc) · 4.95 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
var todo = {
tasks: [],
taskLocalStorage: function(task) {
if (task === "backup") {
localStorage["tasks"] = JSON.stringify(todo.tasks);
} else if (task === "reinitialize") {
todo.tasks = JSON.parse(localStorage["tasks"]);
todo.listTasks("all", false); todo.listTasks("all", true);
}
},
initialise: function() {
},
listTask: function(id, task, ULid) {
// Create li element and get UL
var ul = document.getElementById(ULid),
li = document.createElement("li");
li.setAttribute("id", id);
// Create span element for task text
var taskSpan = document.createElement("span"),
taskContent = document.createTextNode(task);
taskSpan.appendChild(taskContent);
taskSpan.setAttribute("class", "task-span");
// Create span element for buttons to go inside.
var buttonSpan = document.createElement("span"),
deleteButton = document.createElement("button"),
doneButton = document.createElement("button"),
doneButtonContent = "";
buttonSpan.setAttribute("class", "button-span");
deleteButton.appendChild(document.createTextNode("X"));
deleteButton.setAttribute("title", "Delete task.");
deleteButton.setAttribute("class", "delete-button");
// if task is not done
if (ULid === "todo-list") {
doneButtonContent = document.createTextNode("\u2713"); // Check mark.
doneButton.appendChild(doneButtonContent);
doneButton.setAttribute("title", "Complete task.");
doneButton.setAttribute("class", "do-button");
} else if (ULid === "history-list") { // Task is done
doneButtonContent = document.createTextNode("\u00F7"); // Check mark.
doneButton.appendChild(doneButtonContent);
doneButton.setAttribute("title", "Task not yet completed.");
doneButton.setAttribute("class", "undo-button");
}
// Add buttons to span element
buttonSpan.appendChild(deleteButton); buttonSpan.appendChild(doneButton);
// Add span element to list
li.appendChild(taskSpan); li.appendChild(buttonSpan);
// Insert LI element in UL
ul.insertBefore(li, ul.firstChild);
// Add delete event-listener
deleteButton.addEventListener("click", function() { todo.deleteTask(id); }, false); //
if (ULid === "todo-list") {
doneButton.addEventListener("click", function() { todo.doneTask(id); }, false);
} else if (ULid === "history-list") {
doneButton.addEventListener("click", function() { todo.undoneTask(id); }, false);
}
},
listTasks: function(amount, done, taskID) { // List tasks
if (amount === "all") {
if (done === false) {
for (var i = 0; i < todo.tasks.length; i++) {
if (todo.tasks[i].done === false) {
todo.listTask(todo.tasks[i].id, todo.tasks[i].task, "todo-list");
}
}
} else if (done === true) {
for (var x = 0; x < todo.tasks.length; x++) {
if (todo.tasks[x].done === true) {
todo.listTask(todo.tasks[x].id, todo.tasks[x].task, "history-list");
}
}
}
} else if (amount === "one") {
for (var z = 0; z < todo.tasks.length; z++) {
var task = todo.tasks[z];
if (task.id === taskID && done === false) {
todo.listTask(task.id, task.task, "todo-list");
} else if (task.id === taskID && done === true) {
todo.listTask(task.id, task.task, "history-list");
}
}
}
},
addTask: function(newTask) { // Adds new task to tasks array
var newId = 0;
for (var i = 0; i < todo.tasks.length; i ++) {
if (newId < todo.tasks[i].id) {
newId = todo.tasks[i].id;
} else if (!todo.tasks[i].id) {
newId = 0;
}
}
newId = newId + 1;
todo.tasks.push({ task: newTask, done: false, id: newId }); // Add new task to array
todo.listTasks("one", false, newId); // Print on screen
todo.taskLocalStorage("backup");
},
doneTask: function(taskID) { // Finds and marks task done (done = true)
for (var i = 0; i < todo.tasks.length; i++) {
if (todo.tasks[i].id === taskID) {
todo.tasks[i].done = true;
var task = document.getElementById(todo.tasks[i].id);
task.parentNode.removeChild(task);
todo.listTasks("one", true, todo.tasks[i].id);
}
}
todo.taskLocalStorage("backup");
},
undoneTask: function(taskID) { // Finds and marks task !done (done = false)
for (var i = 0; i < todo.tasks.length; i++) {
if (todo.tasks[i].id === taskID) {
todo.tasks[i].done = false;
var task = document.getElementById(todo.tasks[i].id);
task.parentNode.removeChild(task);
todo.listTasks("one", false, todo.tasks[i].id);
}
}
todo.taskLocalStorage("backup");
},
deleteTask: function(taskID) { // Finds and deletes task
for (var i = 0; i < todo.tasks.length; i++) {
if (todo.tasks[i].id === taskID) {
var task = document.getElementById(todo.tasks[i].id);
todo.tasks.splice(i, 1);
task.parentNode.removeChild(task);
}
}
todo.taskLocalStorage("backup");
}
};
var submitForm = document.getElementById("submit-form");
submitForm.addEventListener("submit", function(event) {
event.preventDefault();
var input = document.getElementById("task-input");
todo.addTask(input.value);
input.value = "";
return false;
}, false);