-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (110 loc) · 4.03 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
let todoItemsContainer = document.getElementById("todoItemsContainer");
let addTodoButton = document.getElementById("addTodoButton");
let saveTodoButton = document.getElementById("saveTodoButton");
function getTodoListFromLocalStorage() {
let stringifiedTodoList = localStorage.getItem("todoList");
let parsedTodoList = JSON.parse(stringifiedTodoList);
if (parsedTodoList === null) {
return [];
} else {
return parsedTodoList;
}
}
let todoList = getTodoListFromLocalStorage();
let todosCount = todoList.length;
saveTodoButton.onclick = function () {
localStorage.setItem("todoList", JSON.stringify(todoList));
};
function onAddTodo() {
let userInputElement = document.getElementById("todoUserInput");
let userInputValue = userInputElement.value;
if (userInputValue === "") {
alert("Enter Valid Text");
return;
}
todosCount = todosCount + 1;
let newTodo = {
text: userInputValue,
uniqueNo: todosCount,
isChecked: false
};
todoList.push(newTodo);
createAndAppendTodo(newTodo);
userInputElement.value = "";
}
addTodoButton.onclick = function () {
onAddTodo();
};
function onTodoStatusChange(checkboxId, labelId, todoId) {
let labelElement = document.getElementById(labelId);
labelElement.classList.toggle("checked");
let todoObjectIndex = todoList.findIndex(function (eachTodo) {
let eachTodoId = "todo" + eachTodo.uniqueNo;
if (eachTodoId === todoId) {
return true;
} else {
return false;
}
});
let todoObject = todoList[todoObjectIndex];
if (todoObject.isChecked === true) {
todoObject.isChecked = false;
} else {
todoObject.isChecked = true;
}
}
function onDeleteTodo(todoId) {
let todoElement = document.getElementById(todoId);
todoItemsContainer.removeChild(todoElement);
let deleteElementIndex = todoList.findIndex(function (eachTodo) {
let eachTodoId = "todo" + eachTodo.uniqueNo;
if (eachTodoId === todoId) {
return true;
} else {
return false;
}
});
todoList.splice(deleteElementIndex, 1);
}
function createAndAppendTodo(todo) {
let todoId = "todo" + todo.uniqueNo;
let checkboxId = "checkbox" + todo.uniqueNo;
let labelId = "label" + todo.uniqueNo;
let todoElement = document.createElement("li");
todoElement.classList.add("todo-item-container", "d-flex", "flex-row");
todoElement.id = todoId;
todoItemsContainer.appendChild(todoElement);
let inputElement = document.createElement("input");
inputElement.type = "checkbox";
inputElement.id = checkboxId;
inputElement.checked = todo.isChecked;
inputElement.onclick = function () {
onTodoStatusChange(checkboxId, labelId, todoId);
};
inputElement.classList.add("checkbox-input");
todoElement.appendChild(inputElement);
let labelContainer = document.createElement("div");
labelContainer.classList.add("label-container", "d-flex", "flex-row");
todoElement.appendChild(labelContainer);
let labelElement = document.createElement("label");
labelElement.setAttribute("for", checkboxId);
labelElement.id = labelId;
labelElement.classList.add("checkbox-label");
labelElement.textContent = todo.text;
if (todo.isChecked === true) {
labelElement.classList.add("checked");
}
labelContainer.appendChild(labelElement);
let deleteIconContainer = document.createElement("div");
deleteIconContainer.classList.add("delete-icon-container");
labelContainer.appendChild(deleteIconContainer);
let deleteIcon = document.createElement("i");
deleteIcon.classList.add("far", "fa-trash-alt", "delete-icon");
deleteIcon.onclick = function () {
onDeleteTodo(todoId);
};
deleteIconContainer.appendChild(deleteIcon);
}
for (let todo of todoList) {
createAndAppendTodo(todo);
}