-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003c.js
257 lines (225 loc) · 9.17 KB
/
003c.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Get necessary elements
const inputBox = document.getElementById('inputBox');
const addBtn = document.getElementById('addBtn');
const todoList = document.getElementById('todoList');
const notesList = document.getElementById('notes-list');
let editTodo = null;
// Section Navigation Logic
document.getElementById('todo-section-btn').addEventListener('click', () => {
document.getElementById('main-screen').classList.add('hidden');
document.getElementById('todo-section').classList.remove('hidden');
closePopup(); // Ensure popups are closed when switching
});
document.getElementById('notes-section-btn').addEventListener('click', () => {
document.getElementById('main-screen').classList.add('hidden');
document.getElementById('notes-section').classList.remove('hidden');
closePopup(); // Ensure popups are closed when switching
});
/*************************************************************************
* To-Do List Logic
**************************************************************************/
// Function to add todo
const addTodo = () => {
const inputText = inputBox.value.trim();
if (inputText.length <= 0) {
alert("You must write something in your to-do");
return false;
}
if (addBtn.value === "Edit") {
editLocalTodos(editTodo.target.previousElementSibling.innerHTML);
editTodo.target.previousElementSibling.innerHTML = inputText;
addBtn.value = "Add";
inputBox.value = "";
} else {
const li = document.createElement("li");
const p = document.createElement("p");
p.innerHTML = inputText;
li.appendChild(p);
const editBtn = document.createElement("button");
editBtn.innerText = "Edit";
editBtn.classList.add("btn", "editBtn");
li.appendChild(editBtn);
const deleteBtn = document.createElement("button");
deleteBtn.innerText = "Remove";
deleteBtn.classList.add("btn", "deleteBtn");
li.appendChild(deleteBtn);
todoList.appendChild(li);
inputBox.value = "";
saveLocalTodos(inputText);
}
};
// Function to update (Edit/Delete) todo
const updateTodo = (e) => {
if (e.target.innerHTML === "Remove") {
todoList.removeChild(e.target.parentElement);
deleteLocalTodos(e.target.parentElement);
}
if (e.target.innerHTML === "Edit") {
inputBox.value = e.target.previousElementSibling.innerHTML;
inputBox.focus();
addBtn.value = "Edit";
editTodo = e;
}
};
// Function to save todos in localStorage
const saveLocalTodos = (todo) => {
let todos = JSON.parse(localStorage.getItem("todos")) || [];
todos.push(todo);
localStorage.setItem("todos", JSON.stringify(todos));
};
// Function to get todos from localStorage
const getLocalTodos = () => {
const todos = JSON.parse(localStorage.getItem("todos")) || [];
todos.forEach(todo => {
const li = document.createElement("li");
const p = document.createElement("p");
p.innerHTML = todo;
li.appendChild(p);
const editBtn = document.createElement("button");
editBtn.innerText = "Edit";
editBtn.classList.add("btn", "editBtn");
li.appendChild(editBtn);
const deleteBtn = document.createElement("button");
deleteBtn.innerText = "Remove";
deleteBtn.classList.add("btn", "deleteBtn");
li.appendChild(deleteBtn);
todoList.appendChild(li);
});
};
// Function to delete todos from localStorage
const deleteLocalTodos = (todo) => {
const todos = JSON.parse(localStorage.getItem("todos")) || [];
const todoText = todo.children[0].innerHTML;
const updatedTodos = todos.filter(t => t !== todoText);
localStorage.setItem("todos", JSON.stringify(updatedTodos));
};
// Function to edit todos in localStorage
const editLocalTodos = (todo) => {
const todos = JSON.parse(localStorage.getItem("todos")) || [];
const todoIndex = todos.indexOf(todo);
todos[todoIndex] = inputBox.value;
localStorage.setItem("todos", JSON.stringify(todos));
};
// Initialize To-Do List
addBtn.addEventListener('click', addTodo);
todoList.addEventListener('click', updateTodo);
// Make To-Do List Sortable
const sortable = new Sortable(todoList, {
animation: 150,
onEnd: () => {
const items = Array.from(todoList.children).map(li => li.textContent.trim());
localStorage.setItem('todos', JSON.stringify(items));
}
});
document.addEventListener('DOMContentLoaded', getLocalTodos);
/*************************************************************************
* Notes Logic
**************************************************************************/
// Function to open the popup for creating a new note
function popup() {
closePopup(); // Ensure only one popup is active
const popupContainer = document.createElement("div");
popupContainer.setAttribute("id", "popupContainer");
popupContainer.innerHTML = `
<div id="popupContent">
<h1>New Note</h1>
<textarea id="note-text" placeholder="Enter your note..."></textarea>
<div id="btn-container">
<button id="submitBtn" onclick="createNote()">Create Note</button>
<button id="closeBtn" onclick="closePopup()">Close</button>
</div>
</div>
`;
document.body.appendChild(popupContainer);
}
// Function to close the popup
function closePopup() {
const popupContainer = document.getElementById("popupContainer");
if (popupContainer) {
popupContainer.remove();
}
}
// Function to create a new note
function createNote() {
const noteText = document.getElementById('note-text').value.trim();
if (noteText !== '') {
const note = { id: Date.now(), text: noteText };
const existingNotes = JSON.parse(localStorage.getItem('notes')) || [];
existingNotes.push(note);
localStorage.setItem('notes', JSON.stringify(existingNotes));
closePopup();
displayNotes();
}
}
// Function to display all notes
function displayNotes() {
notesList.innerHTML = '';
const notes = JSON.parse(localStorage.getItem('notes')) || [];
notes.forEach(note => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<span>${note.text}</span>
<div id="noteBtns-container">
<button id="editBtn" onclick="editNote(${note.id})"><i class="fa-solid fa-pen"></i></button>
<button id="deleteBtn" onclick="deleteNote(${note.id})"><i class="fa-solid fa-trash"></i></button>
</div>
`;
notesList.appendChild(listItem);
});
}
// Function to open the edit popup for a note
function editNote(noteId) {
closeEditPopup(); // Ensure only one popup is active
const notes = JSON.parse(localStorage.getItem('notes')) || [];
const noteToEdit = notes.find(note => note.id === noteId);
const noteText = noteToEdit ? noteToEdit.text : '';
const editingPopup = document.createElement("div");
editingPopup.setAttribute("id", "editing-container");
editingPopup.setAttribute("data-note-id", noteId);
editingPopup.innerHTML = `
<div id="popupContent">
<h1>Edit Note</h1>
<textarea id="note-text">${noteText}</textarea>
<div id="btn-container">
<button id="submitBtn" onclick="updateNote()">Done</button>
<button id="closeBtn" onclick="closeEditPopup()">Cancel</button>
</div>
</div>
`;
document.body.appendChild(editingPopup);
}
// Function to close the edit popup
function closeEditPopup() {
const editingPopup = document.getElementById("editing-container");
if (editingPopup) {
editingPopup.remove();
}
}
// Function to update a note
function updateNote() {
const noteText = document.getElementById('note-text').value.trim();
const editingPopup = document.getElementById('editing-container');
if (noteText !== '') {
const noteId = Number(editingPopup.getAttribute('data-note-id'));
const notes = JSON.parse(localStorage.getItem('notes')) || [];
const updatedNotes = notes.map(note => (note.id === noteId ? { id: note.id, text: noteText } : note));
localStorage.setItem('notes', JSON.stringify(updatedNotes));
closeEditPopup();
displayNotes();
}
}
// Function to delete a note
function deleteNote(noteId) {
const notes = JSON.parse(localStorage.getItem('notes')) || [];
const updatedNotes = notes.filter(note => note.id !== noteId);
localStorage.setItem('notes', JSON.stringify(updatedNotes));
displayNotes();
}
// Initialize Notes Display
document.addEventListener('DOMContentLoaded', displayNotes);
function goBackToMain() {
document.getElementById('todo-section').classList.add('hidden');
document.getElementById('notes-section').classList.add('hidden');
document.getElementById('main-screen').classList.remove('hidden');
closePopup(); // Ensure no popups remain open
}