-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
184 lines (151 loc) · 5 KB
/
main.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
let formElement = document.querySelector("#add-item-form");
let selectAllItemsElement = document.querySelector("#choose-all-items");
let inputElement = document.querySelector("#todo-input");
let itemList = document.querySelector("#item-list");
let todoFooterElement = document.querySelector(".todo-footer"); // antar vi ska ha en till footer, därav namnet
let itemsLeft = document.querySelector("#item-count");
let clearButton = document.querySelector("#clear-button");
let activeFilter = false;
let completedFilter = false;
let itemsArray = [];
formElement.onsubmit = function (event) {
event.preventDefault();
let inputValue = inputElement.value.trim();
if (inputValue !== '') {
selectAllItemsElement.style.display = "flex";
selectAllItemsElement.textContent = "🔽";
let newItem = document.createElement("li");
let checkbox = document.createElement("input");
let itemText = document.createElement("p");
let removeButton = document.createElement("button");
checkbox.type = "checkbox";
checkbox.classList.add("checkbox");
itemText.textContent = inputValue;
removeButton.className = 'remove-button';
removeButton.textContent = '❌';
newItem.append(checkbox);
newItem.append(itemText);
newItem.append(removeButton);
itemList.append(newItem);
checkbox.addEventListener("change", () => {
itemCount();
//Added to update the filter when changing a checkbox.
if(activeFilter === true){
DisplayActive();
}
else if(completedFilter === true){
DisplayCompleted();
}
else{
DisplayAll();
}
});
removeButton.addEventListener("click", () => {
itemsArray.splice(itemsArray.indexOf(newItem), 1);
newItem.remove();
itemCount();
});
itemsArray.push(newItem);
itemCount();
labelBehaviour();
inputElement.value = '';
}
};
function itemCount() {
let count = 0;
let anyChecked = false;
itemsArray.forEach(item => {
let checkBox = item.querySelector(".checkbox");
if (checkBox.checked) {
anyChecked = true;
}
else {
count++;
}
})
clearButton.style.visibility = anyChecked ? "visible" : "hidden";
itemsLeft.textContent = count === 1 ? `${count} item left` : `${count} items left`;
footerDisplay();
};
// Check or uncheck all
let selectAllChecked = false;
selectAllItemsElement.addEventListener("click", () => {
selectAllChecked = !selectAllChecked;
selectAllItemsElement.style.boxShadow = "0 0 2px 2px #CF7D7D";
itemsArray.forEach(item => {
let checkbox = item.querySelector(".checkbox");
checkbox.checked = selectAllChecked;
});
itemCount();
});
// Remove connection label => input when items in array
function labelBehaviour() {
if (itemsArray.length > 0) {
selectAllItemsElement.removeAttribute("for");
}
else {
selectAllItemsElement.setAttribute("for", "todo-input");
selectAllItemsElement.style.display = "none";
}
};
// Remove label "border" when focus on input field
inputElement.addEventListener("focus", () => {
selectAllItemsElement.style.boxShadow = "none";
});
document.querySelector("#all-button").addEventListener("click", DisplayAll);
document.querySelector("#active-button").addEventListener("click", DisplayActive);
document.querySelector("#completed-button").addEventListener("click", DisplayCompleted);
clearButton.addEventListener("click", ClearCompleted);
function DisplayAll() {
activeFilter = false;
completedFilter = false;
itemsArray.forEach(item => {
item.style.display = "flex";
})
}
function DisplayActive() {
activeFilter = true;
completedFilter = false;
itemsArray.forEach(item => {
let checkBox = item.querySelector(".checkbox");
if (checkBox.checked) {
item.style.display = "none";
}
else {
item.style.display = "flex";
}
})
}
function DisplayCompleted() {
activeFilter = false;
completedFilter = true;
itemsArray.forEach(item => {
let checkBox = item.querySelector(".checkbox");
if (checkBox.checked) {
item.style.display = "flex";
}
else {
item.style.display = "none";
}
})
}
function ClearCompleted() {
itemsArray = itemsArray.filter(item => {
let checkBox = item.querySelector(".checkbox");
if (checkBox.checked) {
item.remove();
return false; // Ta bort item från array
}
return true; // behåll item i array
});
itemCount();
}
function footerDisplay() {
if (itemsArray.length > 0) {
todoFooterElement.style.display = "flex";
}
else {
todoFooterElement.style.display = "none";
selectAllItemsElement.style.display = "none";
}
}