-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
143 lines (129 loc) · 4.24 KB
/
script.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
138
139
140
141
142
143
/* Global Declaration */
const itemCount = localStorage.length;
/* Add todo */
addTodo = () => {
const title = document.getElementById("todo-title");
const desc = document.getElementById("todo-desc");
if (title.value == "" || desc.value == "") {
const response = document.getElementById("response");
response.style.display = "block";
response.style.color = "red";
response.innerHTML = "Please fill out all the fields!";
setTimeout(() => {
response.innerHTML = "";
response.style.display = "none";
}, 2000);
} else {
localStorage.setItem(title.value, desc.value);
location.reload();
}
};
/* Get items from localStorage */
getAllItemsFromLocalStorage = () => {
let key = "";
let value = "";
for (let i = 0; i < itemCount; i++) {
key = localStorage.key(i);
value = localStorage.getItem(key);
}
};
/* Display todo as the page loads */
window.addEventListener("DOMContentLoaded", () => {
getAllItemsFromLocalStorage();
let tableRows = "";
for (let i = 0; i < itemCount; i++) {
let title = localStorage.key(i);
tableRows += `
<div class="todo-entry">
<div class="action-data">
<h4 style="font-weight:normal">${localStorage.key(i)}</h4>
</div>
<div class="action-data">
<p>${localStorage.getItem(title)}</p>
</div>
<div class="action">
<button class="edit" data-index="${i}">Edit</button>
<button class="delete" data-index="${i}">Delete</button>
</div>
</div>
`;
}
document.getElementById("todo-list").innerHTML = tableRows;
document.getElementById("todo-list").addEventListener("click", (event) => {
if (event.target.classList.contains("edit")) {
const dataIndex = event.target.getAttribute("data-index");
openEditModal(dataIndex);
} else if (event.target.classList.contains("delete")) {
const dataIndex = event.target.getAttribute("data-index");
deleteTodoEntry(dataIndex);
}
});
const searchInput = document.querySelector("#todo-search");
updateTable = () => {
let tableRows = "";
for (let i = 0; i < itemCount; i++) {
let title = localStorage.key(i);
let content = localStorage.getItem(title);
// Filter based on search input
const searchTerm = searchInput.value.toLowerCase();
if (
title.toLowerCase().includes(searchTerm) ||
content.toLowerCase().includes(searchTerm)
) {
tableRows += `
<div class="todo-entry">
<div class="action-data">
<h4 style="font-weight:normal">${title}</h4>
</div>
<div class="action-data">
<p>${content}</p>
</div>
<div class="action">
<button class="edit" data-index="${i}">Edit</button>
<button class="delete" data-index="${i}">Delete</button>
</div>
</div>
`;
}
}
document.getElementById("todo-list").innerHTML = tableRows;
};
searchInput.addEventListener("input", updateTable);
});
/* Function to delete a todo */
deleteTodoEntry = (index) => {
const title = localStorage.key(index);
localStorage.removeItem(title);
location.reload();
};
/* Function to edit a todo */
openEditModal = (index) => {
const title = localStorage.key(index);
const todoText = localStorage.getItem(title);
const modal = document.getElementById("editModal");
const editInput = document.getElementById("editInput");
const saveEditButton = document.getElementById("saveEdit");
editInput.value = todoText;
modal.style.display = "flex";
const windowHeight = window.innerHeight;
const modalHeight = modal.offsetHeight;
const topPosition = (windowHeight - modalHeight) / 2;
modal.style.top = `${topPosition}px`;
saveEditButton.addEventListener("click", () => {
const newData = editInput.value;
if (newData !== "") {
localStorage.setItem(title, newData);
modal.style.display = "none";
location.reload();
}
});
const closeModal = document.getElementsByClassName("close")[0];
closeModal.addEventListener("click", () => {
modal.style.display = "none";
});
window.addEventListener("click", (event) => {
if (event.target === modal) {
modal.style.display = "none";
}
});
};