-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (130 loc) · 5.27 KB
/
Copy pathindex.js
File metadata and controls
139 lines (130 loc) · 5.27 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
let todoList = [];
const generateTodoItem = (itemName) => {
const timeStamp = Date.now();
return {
id: "todo-item-" + timeStamp,
name: itemName,
timeStamp,
isDone: false,
};
};
const todoElement = (item, i) => {
const elem = document.createElement("div");
elem.id = item.id;
elem.innerHTML = `
<div class="row py-2 text-center">
<div class="col-3 text-center">${i + 1}</div>
<div class="col-3" id=${item.id}>
<input type="checkbox" onchange="markItem(event)" ${
item.isDone ? "checked" : ""
}/>
</div>
<div class="col-3">${item.name}</div>
<div class="col-3" id=${item.id}>
<button class="btn btn-outline-danger" onclick="deleteItem(event)">Delete Item</button>
</div>
</div> `;
return elem;
};
const renderList = () => {
const listElement = document.getElementById("todo-list");
listElement.innerHTML = "";
for (let i = 0; i < todoList.length; i++) {
const todoItem = todoElement(todoList[i], i);
listElement.appendChild(todoItem);
}
};
document.getElementById("todo-form").addEventListener("submit", (event) => {
event.preventDefault();
const itemName = event.target["todo-input"].value;
if (itemName) {
todoList.push(generateTodoItem(itemName));
renderList();
saveTodo();
const alert = `
<div class="text-center alert alert-success alert-dismissible fade show shadow border-0" role="alert" data-aos="fade-down">
<strong>Congratulations, your item has been added.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#confirmation").html(alert);
event.target.reset();
}
});
const markItem = (event) => {
const itemId = event.target.parentElement.id; // getting the id of the parent element to search in the array
todoList.some((todoItem) => {
if (todoItem.id === itemId) {
todoItem.isDone = !todoItem.isDone;
renderList();
saveTodo();
return true;
}
});
};
const deleteItem = (event) => {
const itemId = event.target.parentElement.id; // getting the id of the parent element to search in the array
todoList.some((todoItem, i) => {
if (todoItem.id === itemId) {
const approveDelete = confirm(
"Delete this item. If not then press cancel to undo"
); //taking input from user if they sure want to delete
if (approveDelete) {
todoList.splice(i, 1);
renderList();
saveTodo();
const alert = `
<div class="text-center alert alert-danger alert-dismissible fade show shadow border-0" role="alert" data-aos="fade-down">
<strong>Your item has been removed.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#confirmation").html(alert);
}
return true;
}
});
};
const saveTodo = () => {
localStorage.setItem("todoList", JSON.stringify(todoList)); // key and its value
const saveInfo = `
<div class="text-center alert alert-primary alert-dismissible fade show shadow border-0" role="alert" data-aos="zoom-in-down">
<strong>${
todoList.length > 0
? "Your changes have been successfully saved."
: "Please add any item."
}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#save-info").html(saveInfo);
};
const clearTodo = () => {
if (todoList.length){
const approveClear = confirm("Are you sure you want to delete your all items");
if(approveClear){
`${localStorage.removeItem("todoList")}
${(todoList = [])}
${renderList()}`
const clearInfo = `
<div class="text-center alert alert-primary alert-dismissible fade show shadow border-0" role="alert" data-aos="zoom-in-down">
<strong>Your items have been successfully removed.</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#save-info").html(clearInfo);
}
}
else{
const clearInfo = `
<div class="text-center alert alert-primary alert-dismissible fade show shadow border-0" role="alert" data-aos="zoom-in-down">
<strong>Please add any item</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#save-info").html(clearInfo);
}
};
try {
if (localStorage.getItem("todoList")) {
todoList = JSON.parse(localStorage.getItem("todoList"));
renderList();
}
} catch (e) {
console.log(e);
}