-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
287 lines (247 loc) · 8.2 KB
/
script.js
File metadata and controls
287 lines (247 loc) · 8.2 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Getting a raw json previously stored on local storage
let list = JSON.parse(localStorage.getItem('list'));
const toDo = document.querySelector("#to-do");
const done = document.querySelector("#done");
const form = document.querySelector("#form");
const defaultList = {
"to_do_list": [
{
"title": "🛒 Buy groceries",
"description": "Milk, bread, eggs, cheese, fruits and vegetables.",
"completed": false
},
{
"title": "📝 Finish project",
"description": "Complete the final report and submit it by Friday.",
"completed": true
},
{
"title": "🧼 Clean the house",
"description": "Vacuum, dust, and clean the bathrooms.",
"completed": false
},
{
"title": "💰 Pay bills",
"description": "Electricity, water, and internet bills.",
"completed": false
},
{
"title": "📞 Call mom",
"description": "Check in and see how she's doing.",
"completed": true
},
{
"title": "📅 Schedule appointment",
"description": "Make an appointment with the dentist.",
"completed": true
},
{
"title": "🏃♂️ Go for a run",
"description": "Jog for 30 minutes.",
"completed": true
},
{
"title": "📖 Read a book",
"description": "Finish reading 'The Great Gatsby'.",
"completed": true
},
{
"title": "✍️ Write a blog post",
"description": "Come up with a topic and write a 500-word blog post.",
"completed": true
},
{
"title": "🧹 Organize closet",
"description": "Sort clothes by category and donate items no longer needed.",
"completed": true
}
]
}
// This is in case our local storage is empty
if (!list) {
localStorage.setItem('list', JSON.stringify(defaultList));
list = JSON.parse(localStorage.getItem('list'));
}
// Handles the list of items
function getListData() {
toDo.innerHTML = "";
done.innerHTML = "";
// A handy var that will help us to render
// the list properly
let lastItem;
list.to_do_list.forEach((itm, index) => {
renderToDoItem(itm, index);
// This var always will hold the index
// of the last rendered item
lastItem = index;
});
if (toDo.childElementCount === 0) {
renderCompletedImg();
}
lastItem ++;
createNewItemFields(lastItem);
}
// Renders items
function renderToDoItem(itm, index) {
const title = document.createElement("textarea");
title.name = `n${index}-title`;
title.value = itm.title;
title.maxLength = 30;
title.addEventListener("change", handleItemsSubmit);
const desc = document.createElement("textarea");
desc.name = `n${index}-desc`;
desc.value = itm.description;
desc.style.height = "1.5rem";
desc.style.height = this.scrollHeight + "px";
desc.addEventListener("input", autoResize, false);
desc.addEventListener("click", autoResize, false);
desc.addEventListener("change", handleItemsSubmit);
const completed = document.createElement("input");
completed.type = "hidden";
completed.name = `n${index}-completed`;
completed.value = itm.completed;
// We need to create those two divs to respect
// our CSS structure
const wrapper = document.createElement("div");
const itmData = document.createElement("div");
itmData.classList.add("itm-data");
itmData.appendChild(title);
itmData.appendChild(desc);
itmData.appendChild(completed);
const checkBtn = document.createElement("span");
checkBtn.classList.add("material-symbols-outlined");
checkBtn.title = "Hold to delete this item";
checkBtn.addEventListener("long-press", removeItem);
checkBtn.addEventListener("click", checkItem);
checkBtn.setAttribute("data-long-press-delay", "100");
checkBtn.id = index;
wrapper.appendChild(itmData);
wrapper.appendChild(checkBtn);
if (itm.completed) {
checkBtn.innerText = "check_box";
done.appendChild(wrapper);
} else {
checkBtn.innerText = "check_box_outline_blank";
toDo.appendChild(wrapper);
}
}
// Expands textareas when clicked
function autoResize() {
this.style.height = "1.5rem";
this.style.height = this.scrollHeight + "px";
}
// Creates the fields for new items
function createNewItemFields(index) {
const newItmWrapper = document.createElement("div");
const newItmData = document.createElement("div");
const newTitle = document.createElement("textarea");
newTitle.name = `n${index}-title`;
newTitle.maxLength = 30;
newTitle.placeholder = "✨ Create a new note...";
newTitle.addEventListener("change", handleItemsSubmit);
const newDesc = document.createElement("textarea");
newDesc.name = `n${index}-desc`;
newDesc.style.height = "1.5rem";
newDesc.style.height = this.scrollHeight + "px";
newDesc.placeholder = "Note description";
newDesc.addEventListener("input", autoResize, false);
newDesc.addEventListener("change", autoResize, false);
newDesc.addEventListener("change", handleItemsSubmit);
const newCompleted = document.createElement("input");
newCompleted.type = "hidden";
newCompleted.name = `n${index}-completed`;
newItmData.classList.add("itm-data");
newItmData.appendChild(newTitle);
newItmData.appendChild(newDesc);
newItmData.appendChild(newCompleted);
newItmWrapper.appendChild(newItmData);
toDo.appendChild(newItmWrapper);
}
// Handles a new item
function handleItemsSubmit(e) {
const formData = new FormData(form);
// Creating an empty to-do list
const todoObject = {
"to_do_list": []
}
const formValues = formData.values();
for (const itx of formValues) {
let title = itx;
let description = formValues.next().value;
let completed = formValues.next().value;
if (
title.trim() !== "" ||
description.trim() !== ""
) {
todoObject.to_do_list.push({
title,
description,
completed: completed === 'true'
});
}
}
let listSizes = [
list.to_do_list.length,
todoObject.to_do_list.length
];
list = todoObject;
saveItemsStorage();
// This means that we'll only render
// the list again if an items was actually
// added
if (listSizes[0] < listSizes[1]) {
getListData();
}
}
// Saves the current list to the storage
function saveItemsStorage() {
localStorage.setItem("list", JSON.stringify(list));
}
// Removes an item from the list
function removeItem(e) {
let idToRemove = e.target.id;
const filteredList = list.to_do_list.filter((itm, index) => {
return index != idToRemove;
})
list.to_do_list = filteredList;
anime({
targets: e.target.parentElement,
duration: 500,
translateX: [0, 50],
opacity: [1, 0],
easing: "easeInExpo",
complete: function(anim) {
getListData();
saveItemsStorage();
}
});
}
// Checks/Unchecks an item
function checkItem(e) {
let isCompleted = list.to_do_list[e.target.id].completed;
list.to_do_list[e.target.id].completed = !isCompleted;
anime({
targets: e.target.parentElement,
duration: 500,
translateX: [0, 50],
opacity: [1, 0],
easing: "easeInExpo",
complete: function(anim) {
getListData();
saveItemsStorage();
}
});
}
// Render a cool image when all items are done
function renderCompletedImg() {
const upToDateImg = document.createElement("img");
const upToDateText = document.createElement("h3");
upToDateImg.src = "./images/upToDate.svg";
upToDateImg.classList.add("up-to-date-img");
upToDateText.innerText =
"Nice! You have finished all of your tasks.";
upToDateText.classList.add("up-to-date-txt");
toDo.appendChild(upToDateImg);
toDo.appendChild(upToDateText);
}
// Nice, now, some styling