Spaces:
Running
Running
Commit
·
03c70ad
1
Parent(s):
47a077a
Create add.js
Browse files
add.js
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
let todoItemsContainer = document.getElementById("todoItemsContainer");
|
2 |
+
let addTodoButton = document.getElementById("addTodoButton");
|
3 |
+
let saveTodoButton = document.getElementById("saveTodoButton");
|
4 |
+
|
5 |
+
function getTodoListFromLocalStorage() {
|
6 |
+
let stringifiedTodoList = localStorage.getItem("todoList");
|
7 |
+
let parsedTodoList = JSON.parse(stringifiedTodoList);
|
8 |
+
if (parsedTodoList === null) {
|
9 |
+
return [];
|
10 |
+
} else {
|
11 |
+
return parsedTodoList;
|
12 |
+
}
|
13 |
+
}
|
14 |
+
|
15 |
+
let todoList = getTodoListFromLocalStorage();
|
16 |
+
let todosCount = todoList.length;
|
17 |
+
|
18 |
+
saveTodoButton.onclick = function() {
|
19 |
+
localStorage.setItem("todoList", JSON.stringify(todoList));
|
20 |
+
};
|
21 |
+
|
22 |
+
function onAddTodo() {
|
23 |
+
let userInputElement = document.getElementById("todoUserInput");
|
24 |
+
let userInputValue = userInputElement.value;
|
25 |
+
|
26 |
+
if (userInputValue === "") {
|
27 |
+
alert("Enter Valid Text");
|
28 |
+
return;
|
29 |
+
}
|
30 |
+
|
31 |
+
todosCount = todosCount + 1;
|
32 |
+
|
33 |
+
let newTodo = {
|
34 |
+
text: userInputValue,
|
35 |
+
uniqueNo: todosCount,
|
36 |
+
isChecked: false
|
37 |
+
};
|
38 |
+
todoList.push(newTodo);
|
39 |
+
createAndAppendTodo(newTodo);
|
40 |
+
userInputElement.value = "";
|
41 |
+
}
|
42 |
+
|
43 |
+
addTodoButton.onclick = function() {
|
44 |
+
onAddTodo();
|
45 |
+
};
|
46 |
+
|
47 |
+
function onTodoStatusChange(checkboxId, labelId, todoId) {
|
48 |
+
let checkboxElement = document.getElementById(checkboxId);
|
49 |
+
let labelElement = document.getElementById(labelId);
|
50 |
+
labelElement.classList.toggle("checked");
|
51 |
+
|
52 |
+
let todoObjectIndex = todoList.findIndex(function(eachTodo) {
|
53 |
+
let eachTodoId = "todo" + eachTodo.uniqueNo;
|
54 |
+
|
55 |
+
if (eachTodoId === todoId) {
|
56 |
+
return true;
|
57 |
+
} else {
|
58 |
+
return false;
|
59 |
+
}
|
60 |
+
});
|
61 |
+
|
62 |
+
let todoObject = todoList[todoObjectIndex];
|
63 |
+
|
64 |
+
if(todoObject.isChecked === true){
|
65 |
+
todoObject.isChecked = false;
|
66 |
+
} else {
|
67 |
+
todoObject.isChecked = true;
|
68 |
+
}
|
69 |
+
|
70 |
+
}
|
71 |
+
|
72 |
+
function onDeleteTodo(todoId) {
|
73 |
+
let todoElement = document.getElementById(todoId);
|
74 |
+
todoItemsContainer.removeChild(todoElement);
|
75 |
+
|
76 |
+
let deleteElementIndex = todoList.findIndex(function(eachTodo) {
|
77 |
+
let eachTodoId = "todo" + eachTodo.uniqueNo;
|
78 |
+
if (eachTodoId === todoId) {
|
79 |
+
return true;
|
80 |
+
} else {
|
81 |
+
return false;
|
82 |
+
}
|
83 |
+
});
|
84 |
+
|
85 |
+
todoList.splice(deleteElementIndex, 1);
|
86 |
+
}
|
87 |
+
|
88 |
+
function createAndAppendTodo(todo) {
|
89 |
+
let todoId = "todo" + todo.uniqueNo;
|
90 |
+
let checkboxId = "checkbox" + todo.uniqueNo;
|
91 |
+
let labelId = "label" + todo.uniqueNo;
|
92 |
+
|
93 |
+
let todoElement = document.createElement("li");
|
94 |
+
todoElement.classList.add("todo-item-container", "d-flex", "flex-row");
|
95 |
+
todoElement.id = todoId;
|
96 |
+
todoItemsContainer.appendChild(todoElement);
|
97 |
+
|
98 |
+
let inputElement = document.createElement("input");
|
99 |
+
inputElement.type = "checkbox";
|
100 |
+
inputElement.id = checkboxId;
|
101 |
+
inputElement.checked = todo.isChecked;
|
102 |
+
|
103 |
+
inputElement.onclick = function () {
|
104 |
+
onTodoStatusChange(checkboxId, labelId, todoId);
|
105 |
+
};
|
106 |
+
|
107 |
+
inputElement.classList.add("checkbox-input");
|
108 |
+
todoElement.appendChild(inputElement);
|
109 |
+
|
110 |
+
let labelContainer = document.createElement("div");
|
111 |
+
labelContainer.classList.add("label-container", "d-flex", "flex-row");
|
112 |
+
todoElement.appendChild(labelContainer);
|
113 |
+
|
114 |
+
let labelElement = document.createElement("label");
|
115 |
+
labelElement.setAttribute("for", checkboxId);
|
116 |
+
labelElement.id = labelId;
|
117 |
+
labelElement.classList.add("checkbox-label");
|
118 |
+
labelElement.textContent = todo.text;
|
119 |
+
if (todo.isChecked === true) {
|
120 |
+
labelElement.classList.add("checked");
|
121 |
+
}
|
122 |
+
labelContainer.appendChild(labelElement);
|
123 |
+
|
124 |
+
let deleteIconContainer = document.createElement("div");
|
125 |
+
deleteIconContainer.classList.add("delete-icon-container");
|
126 |
+
labelContainer.appendChild(deleteIconContainer);
|
127 |
+
|
128 |
+
let deleteIcon = document.createElement("i");
|
129 |
+
deleteIcon.classList.add("far", "fa-trash-alt", "delete-icon");
|
130 |
+
|
131 |
+
deleteIcon.onclick = function () {
|
132 |
+
onDeleteTodo(todoId);
|
133 |
+
};
|
134 |
+
|
135 |
+
deleteIconContainer.appendChild(deleteIcon);
|
136 |
+
}
|
137 |
+
|
138 |
+
for (let todo of todoList) {
|
139 |
+
createAndAppendTodo(todo);
|
140 |
+
}
|