-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
85 lines (68 loc) · 2.09 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
82
83
84
85
items = [];
selected = 0;
function addItem(name, isChecked) {
let item = {
name,
isChecked
}
items.push(item);
}
function submitField() {
addItem(document.getElementById("input_todo").value, false);
displayItems(items);
document.getElementById("input_todo").value = "";
return false;
}
function displayItems(items) {
const container = document.getElementById("todo-container");
container.innerHTML = "";
items.forEach((item, index) => {
let check = item.isChecked ? "✔" : "";
let html = `<div class="todo-item">
<div class="todo-check" onclick="checkItem(${index})">${check}</div>
<span>${item.name}</span>
<div class="todo-delete" onclick="deleteItem(${index})">✖</div>
</div>`;
if (selected == 0 || (selected == 1 && !item.isChecked) || (selected == 2 && item.isChecked)) {
container.innerHTML += html;
}
});
if (items.length >= 1) {
let html = `
<div class="bar">
<span class="bar-left">${items.filter((item) => item.isChecked == false).length} items left</span>
<span class="bar-buttons">
<span class="bar-button ${selected == 0 ? 'selected' : ''}" onclick="setAll()">All</span>
<span class="bar-button ${selected == 1 ? 'selected' : ''}" onclick="setActive()">Active</span>
<span class="bar-button ${selected == 2 ? 'selected' : ''}" onclick="setCompleted()">Completed</span>
</span>
<span class="bar-clear"><span class="span-clear" onclick="clearCompleted()">Clear completed</span></span>
</div>`;
container.innerHTML += html;
}
}
displayItems(items);
function checkItem(index) {
items[index].isChecked = !items[index].isChecked;
displayItems(items);
}
function deleteItem(index) {
items.splice(index, 1);
displayItems(items);
}
function clearCompleted() {
items = items.filter((item) => item.isChecked == false);
displayItems(items);
}
function setAll() {
selected = 0;
displayItems(items);
}
function setActive() {
selected = 1;
displayItems(items);
}
function setCompleted() {
selected = 2;
displayItems(items);
}