forked from HeyImMatt/epicodus-todo-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
82 lines (72 loc) · 2.08 KB
/
scripts.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
function TodoList() {
this.todoList = [];
this.currentId = 0;
}
TodoList.prototype.addTodo = function(todo) {
todo.id = this.assignId();
this.todoList.push(todo)
}
TodoList.prototype.assignId = function() {
this.currentId += 1;
return this.currentId;
}
TodoList.prototype.findTodo = function(id) {
for (let i = 0; i < this.todoList.length; i++ ) {
if (this.todoList[i].id == id) {
return this.todoList[i];
}
}
return false;
}
TodoList.prototype.deleteTodo = function(id) {
console.log()
for (let i = 0; i < this.todoList.length; i++) {
if (this.todoList[i].id == id) {
this.todoList.splice(i, 1);
return true;
}
}
return false;
}
TodoList.prototype.displayTodos = function() {
this.todoList.forEach((todo) => {
if (todo.isCompleted) {
$("#todo-list").append(`
<p id=${todo.id} class="strikethrough"><input type="checkbox" checked> ${todo.description} <button class="btn btn-danger btn-sm">Remove</button></p>
`);
} else {
$("#todo-list").append(`
<p id=${todo.id}><input type="checkbox"> ${todo.description} <button class="btn btn-danger btn-sm">Remove</button></p>
`);
}
});
}
function TodoItem(description) {
this.description = description;
this.isCompleted = false;
}
$(document).ready(function(){
let todoList = new TodoList();
$("form#create-todo").submit(function(event) {
event.preventDefault();
let todoItem = new TodoItem($("input#description-input").val());
$("input#description-input").val('');
todoList.addTodo(todoItem);
$("#todo-list").empty();
todoList.displayTodos();
$("button.btn-danger").click(function(){
todoList.deleteTodo(this.parentElement.id);
$(this.parentElement).remove();
});
$("input:checkbox").change(function() {
let item = todoList.findTodo(this.parentElement.id);
if (!item.isCompleted) {
item.isCompleted = true;
$(this.parentElement).addClass("strikethrough")
} else {
item.isCompleted = false;
$(this.parentElement).removeClass("strikethrough")
}
});
});
});