-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (120 loc) · 4.33 KB
/
script.js
File metadata and controls
158 lines (120 loc) · 4.33 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
let todoItemsContainer = document.getElementById("todoItemsContainer");
let addBtnEl = document.getElementById("addBtn");
let saveTodoButton = document.getElementById("saveTodoButton");
function getSavedTodoList() {
let stringifiedTodoList = localStorage.getItem("todoList");
let parseTodoList = JSON.parse(stringifiedTodoList);
if (parseTodoList === null) {
return [];
} else {
return parseTodoList;
}
}
let todoList = getSavedTodoList();
saveTodoButton.onclick = function() {
let stringifyTodoList = JSON.stringify(todoList);
localStorage.setItem("todoList", stringifyTodoList);
//console.log(localStorage.getItem("todoList"));
};
function onTodoStatusChange(checkboxId, labelId, todoId) {
let labelEle = document.getElementById(labelId);
//let checkboxStatusEl = document.getElementById(checkboxId);
labelEle.classList.toggle("checked");
let todoObjectIndex = todoList.findIndex(function(eachTodo) {
let eachTodoId = "todo" + eachTodo.uniqueNum;
if (eachTodoId === todoId) {
return true;
} else {
return false;
}
});
let todoObject = todoList[todoObjectIndex];
if (todoObject.isChecked === false) {
todoObject.isChecked = true;
} else {
todoObject.isChecked = false;
}
}
function deleteTodoEle(todoId) {
let deleteEle = document.getElementById(todoId);
todoItemsContainer.removeChild(deleteEle);
let deleteTodoIndex = todoList.findIndex(function(eachTodo) {
let eachTodoId = "todo" + eachTodo.uniqueNum;
if (eachTodoId === todoId) {
return true;
} else {
return false;
}
});
todoList.splice(deleteTodoIndex, 1);
}
function createAndAppend(todo) {
let checkboxId = "checkbox" + todo.uniqueNum;
let labelId = "label" + todo.uniqueNum;
let todoId = "todo" + todo.uniqueNum;
let todoContainer = document.createElement("li");
todoContainer.id = todoId;
todoContainer.classList.add("todo-item-container", "d-flex", "flex-row");
todoItemsContainer.appendChild(todoContainer);
let inputEle = document.createElement("input");
inputEle.classList.add("checkbox-input");
inputEle.type = "checkbox";
inputEle.checked = todo.isChecked;
inputEle.id = checkboxId;
inputEle.onclick = function() {
onTodoStatusChange(checkboxId, labelId, todoId);
};
todoContainer.appendChild(inputEle);
let labelContainer = document.createElement("div");
labelContainer.classList.add("label-container", "d-flex", "flex-row");
todoContainer.appendChild(labelContainer);
let labelEle = document.createElement("label");
labelEle.classList.add("checkbox-label");
labelEle.id = labelId;
labelEle.setAttribute("for", checkboxId);
labelEle.textContent = todo.text;
if (todo.isChecked === true) {
labelEle.classList.toggle("checked");
}
labelContainer.appendChild(labelEle);
let deleteContainer = document.createElement("div");
deleteContainer.classList.add("delete-icon-container");
labelContainer.appendChild(deleteContainer);
let deleteEle = document.createElement("i");
deleteEle.classList.add("far", "fa-trash-alt", "delete-icon");
deleteEle.onclick = function() {
deleteTodoEle(todoId);
};
deleteContainer.appendChild(deleteEle);
}
for (let todo of todoList) {
createAndAppend(todo);
}
let lengthTodo = todoList.length;
function onAddTodo() {
let todoUserInput = document.getElementById("todoUserInput");
let userInput = todoUserInput.value;
if (userInput === "") {
alert("Enter the valid input");
return;
}
lengthTodo = lengthTodo + 1;
let newTodo = {
text: userInput,
uniqueNum: lengthTodo,
isChecked: false
};
todoList.push(newTodo);
createAndAppend(newTodo);
todoUserInput.value = "";
}
addBtnEl.onclick = function() {
onAddTodo();
};
// theam change
let theamID = document.getElementById("theamID");
let mainContainer = document.getElementById("mainContainer");
theamID.onchange = function(event) {
console.log(event.target.checked);
mainContainer.classList.toggle("dark-theam");
}