-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (107 loc) · 3.96 KB
/
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
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
//replace elements inside a todo card
const replaceElements = (cardDiv, elements) => {
for(const element in elements){
cardDiv.querySelector(element).replaceWith(elements[element]);
}
}
//save items to local storage
const saveToStorage = () => {
const ul = document.querySelector("ul");
localStorage.setItem("cards", ul.innerHTML);
localStorage.setItem("itemCount", itemCount);
}
// create a button with specific classes and click handler
const createBtn = (classes, clickHandler) => {
const btn = document.createElement("button");
btn.classList.add(classes.shift());
const icon = document.createElement("i");
icon.classList.add(...classes);
btn.appendChild(icon);
btn.addEventListener("click", clickHandler);
return btn;
}
// remove a todo card
const removeCard = (event) => {
const btn = event.target;
const cardDiv = btn.closest(".item-card");
cardDiv.parentNode.remove();
itemCount--;
saveToStorage();
}
// edit a todo card
const editCard = (event) => {
const btn = event.target;
const cardDiv = btn.closest(".item-card");
const p = cardDiv.querySelector("p");
const cardValue = p.textContent;
const inputElement = document.createElement('input');
inputElement.setAttribute('type', 'text');
inputElement.value = cardValue;
const cancel = createBtn(["cancel", "fa-solid", "fa-x"], () => {
cancelEdit(cardValue, cardDiv);
});
const confirm = createBtn(["confirm", "fa-solid", "fa-check"], () => {
confirmEdit(cardValue, cardDiv);
});
replaceElements(cardDiv, {".edit": cancel, ".delete": confirm, "p": inputElement});
inputElement.focus();
}
// cancel editing a todo card
const cancelEdit = (cardValue, cardDiv) => {
const p = document.createElement('p');
p.textContent = cardValue;
const editBtn = createBtn(["edit", "fa-solid", "fa-pen-to-square"], editCard);
const deleteBtn = createBtn(["delete", "fa-solid", "fa-trash"], removeCard);
replaceElements(cardDiv, {"input": p, ".cancel": editBtn, ".confirm": deleteBtn});
}
// confirm editing a todo card
const confirmEdit = (cardValue, cardDiv) => {
const input = cardDiv.querySelector("input");
if(input.value.trim() === ""){
cancelEdit(cardValue, cardDiv);
return;
}
const p = document.createElement('p');
p.textContent = input.value.trim();
const editBtn = createBtn(["edit", "fa-solid", "fa-pen-to-square"], editCard);
const deleteBtn = createBtn(["delete", "fa-solid", "fa-trash"], removeCard);
replaceElements(cardDiv, {"input": p, ".cancel": editBtn, ".confirm": deleteBtn});
saveToStorage();
}
// add new todo card
const addTodo = () => {
const ul = document.querySelector("ul");
const todo = document.querySelector(".todo");
let inputValue = todo.value.trim();
if(inputValue === ""){
return;
}
const li = document.createElement("li");
const newCard = document.createElement("div");
newCard.classList.add("item-card", `item${itemCount}`);
const p = document.createElement("p");
p.textContent = inputValue;
const editBtn = createBtn(["edit", "fa-solid", "fa-pen-to-square"], editCard);
const deleteBtn = createBtn(["delete", "fa-solid", "fa-trash"], removeCard);
newCard.appendChild(p);
newCard.appendChild(editBtn);
newCard.appendChild(deleteBtn);
li.appendChild(newCard);
ul.appendChild(li);
todo.value = "";
itemCount++;
saveToStorage();
}
window.onload = () => {
itemCount = localStorage.getItem("itemCount") || 1;
const ul = document.querySelector("ul");
ul.innerHTML = localStorage.getItem("cards");
ul.querySelectorAll('.edit').forEach(editButton => {
editButton.addEventListener('click', editCard);
});
ul.querySelectorAll('.delete').forEach(deleteButton => {
deleteButton.addEventListener('click', removeCard);
});
};
let itemCount;
document.querySelector(".submit").addEventListener("click", addTodo);