-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
304 lines (263 loc) · 10.7 KB
/
scripts.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
let hoverTimeout;
document.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
document.querySelector(".preloader").classList.add("fade-out");
}, 1000);
setTimeout(function () {
document.querySelector(".fixed-header").style.display = "flex";
document.querySelector(".container").style.display = "inherit";
document.querySelector(".preloader").style.display = "none";
}, 1500);
});
document.addEventListener("DOMContentLoaded", () => {
const cookieModal = document.getElementById("cookieModal");
const acceptCookiesBtn = document.getElementById("acceptCookiesBtn");
const cookiesAccepted = localStorage.getItem("cookiesAccepted");
const resetBtn = document.getElementById('resetBtn');
const confirmResetBtn = document.getElementById('confirmResetBtn');
const resetConfirmationModal = document.getElementById('resetConfirmationModal');
if (!cookiesAccepted) {
$("#cookieModal").modal("show");
}
acceptCookiesBtn.addEventListener("click", () => {
localStorage.setItem("cookiesAccepted", true);
$("#cookieModal").modal("hide");
});
const noteInput = document.getElementById("noteInput");
const noteDate = document.getElementById("noteDate");
const noteTime = document.getElementById("noteTime");
const addNoteBtn = document.getElementById("addNoteBtn");
const noteList = document.getElementById("noteList");
let notes = JSON.parse(localStorage.getItem("notes")) || [];
let trash = JSON.parse(localStorage.getItem("trash")) || [];
function saveToLocalStorage() {
localStorage.setItem("notes", JSON.stringify(notes));
localStorage.setItem("trash", JSON.stringify(trash));
}
function renderNotes() {
if (noteList) {
noteList.innerHTML = "";
notes.forEach((note, index) => {
const li = document.createElement("li");
li.className = `list-group-item ${note.completed ? "completed" : ""}`;
li.dataset.index = index;
li.draggable = true;
let noteContent = `${note.content}`;
if (note.date || note.time) {
const date = note.date ? new Date(note.date) : null;
const dateString = date ? date.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) : '';
const dayOfWeek = date ? date.getDay() : null;
const dayColor = dayOfWeek === 0 || dayOfWeek === 6 ? 'color: red;' : ''; // Red color for Sunday (0) and Saturday (6)
const dayWord = dateString.split(',')[0]; // Extract the day word from the full date string
const formattedDate = dateString.replace(dayWord, `<span style="${dayColor}">${dayWord}</span>`); // Wrap the day word in a span with the specified color
noteContent += `<br><small>${formattedDate} ${note.time || ""}</small>`;
}
li.innerHTML = `
<div class="note-content">
${noteContent}
</div>
<div class="note-buttons">
<button class="btn btn-sm btn-primary float-right save-btn d-none">Save</button>
<button class="btn btn-sm btn-secondary float-right edit-btn">Edit</button>
<button class="btn btn-sm btn-success float-right done-btn">${note.completed ? 'Undone' : 'Done'}</button>
<button class="btn btn-sm btn-danger float-right delete-btn">Delete</button>
</div>
`;
noteList.appendChild(li);
li.querySelector(".done-btn").addEventListener("click", () => {
notes[index].completed = !notes[index].completed;
saveToLocalStorage();
renderNotes();
const doneButton = li.querySelector(".done-btn");
doneButton.textContent = notes[index].completed ? 'Undone' : 'Done';
});
li.querySelector(".edit-btn").addEventListener("click", () => {
const editContent = li.querySelector(".note-content");
const editButtons = li.querySelector(".note-buttons");
editContent.innerHTML = `
<input type="text" class="form-control edit-content" value="${notes[index].content}">
<input type="date" class="form-control edit-date" value="${notes[index].date}">
<input type="time" class="form-control edit-time" value="${notes[index].time}">
`;
editButtons.innerHTML = `
<button class="btn btn-sm btn-primary float-right save-btn">Save</button>
<button class="btn btn-sm btn-secondary float-right cancel-btn">Cancel</button>
`;
editButtons.querySelector(".save-btn").addEventListener("click", () => {
const newContent = editContent.querySelector(".edit-content").value;
const newDate = editContent.querySelector(".edit-date").value;
const newTime = editContent.querySelector(".edit-time").value;
notes[index].content = newContent;
notes[index].date = newDate;
notes[index].time = newTime;
saveToLocalStorage();
renderNotes();
});
editButtons.querySelector(".cancel-btn").addEventListener("click", () => {
renderNotes();
});
});
});
}
}
function showToast(message, type) {
Toastify({
text: message,
duration: 3000,
close: true,
gravity: "top",
position: "right",
backgroundColor: type === "error" ? "#FF0000" : "#5cb85c",
}).showToast();
}
hoverTimeout = setTimeout(() => {
Toastify({
text: "Tip: You can change the position of any notes by dragging them.",
duration: 5000,
gravity: "bottom",
position: "center",
backgroundColor: "linear-gradient(to right, #00bcd4, #03a9f4)",
style: {
margin: '20px',
},
}).showToast();
}, 2000);
noteList.addEventListener("mouseover", (e) => {
if (e.target.tagName === "LI") {
clearTimeout(hoverTimeout);
}
});
noteList.addEventListener("mouseout", (e) => {
if (e.target.tagName === "LI") {
hoverTimeout = setTimeout(() => {
Toastify({
text: "Tip: You can change the position of any notes by dragging them.",
duration: 5000,
gravity: "bottom",
position: "center",
backgroundColor: "linear-gradient(to right, #00bcd4, #03a9f4)",
style: {
margin: '20px',
},
}).showToast();
}, 1000);
}
});
if (addNoteBtn) {
addNoteBtn.addEventListener("click", () => {
const noteContent = noteInput.value.trim();
const noteDateValue = noteDate.value;
const noteTimeValue = noteTime.value;
if (!localStorage.getItem("cookiesAccepted")) {
showToast("Please accept cookies to save notes.", "error");
} else if (!noteContent) {
showToast("Note cannot be empty.", "error");
} else {
notes.push({
content: noteContent,
date: noteDateValue,
time: noteTimeValue,
completed: false,
});
noteInput.value = "";
noteDate.value = "";
noteTime.value = "";
saveToLocalStorage();
renderNotes();
}
});
}
if (noteList) {
noteList.addEventListener("click", (e) => {
const target = e.target;
if (target.classList.contains("delete-btn")) {
const index = target.parentElement.parentElement.dataset.index;
const note = notes.splice(index, 1)[0];
note.trashDate = new Date().toISOString();
trash.push(note);
saveToLocalStorage();
renderNotes();
}
if (e.target.classList.contains("done-btn")) {
notes[index].completed = !notes[index].
completed;
saveToLocalStorage();
renderNotes();
}
if (target.classList.contains("edit-btn")) {
const index = target.parentElement.parentElement.dataset.index;
const li = target.parentElement.parentElement;
li.innerHTML = `
<div class="note-content">
<input type="text" class="form-control edit-content" value="${notes[index].content}">
<input type="date" class="form-control edit-date" value="${notes[index].date}">
<input type="time" class="form-control edit-time" value="${notes[index].time}">
</div>
<div class="note-buttons">
<button class="btn btn-sm btn-primary float-right save-btn">Save</button>
<button class="btn btn-sm btn-secondary float-right cancel-btn">Cancel</button>
</div>
`;
const editDateInput = li.querySelector(".edit-date");
const editTimeInput = li.querySelector(".edit-time");
if (notes[index].date) {
const dateParts = notes[index].date.split("-");
const year = dateParts[0];
const month = dateParts[1].padStart(2, "0");
const day = dateParts[2].padStart(2, "0");
editDateInput.value = `${year}-${month}-${day}`;
}
if (notes[index].time) {
editTimeInput.value = notes[index].time;
}
editButtons.querySelector(".save-btn").addEventListener("click", () => {
const newContent = li.querySelector(".edit-content").value;
const newDate = li.querySelector(".edit-date").value;
const newTime = li.querySelector(".edit-time").value;
notes[index].content = newContent;
notes[index].date = newDate;
notes[index].time = newTime;
saveToLocalStorage();
renderNotes();
});
editButtons.querySelector(".cancel-btn").addEventListener("click", () => {
renderNotes();
});
}
if (target.classList.contains("save-btn")) {
const index = target.parentElement.parentElement.dataset.index;
const li = target.parentElement.parentElement;
const newContent = li.querySelector(".edit-content").value;
const newDate = li.querySelector(".edit-date").value;
const newTime = li.querySelector(".edit-time").value;
notes[index].content = newContent;
notes[index].date = newDate;
notes[index].time = newTime;
saveToLocalStorage();
renderNotes();
}
if (target.classList.contains("cancel-btn")) {
renderNotes();
}
});
new Sortable(noteList, {
animation: 150,
onEnd: () => {
notes = Array.from(noteList.children).map(
(li) => notes[li.dataset.index]
);
saveToLocalStorage();
},
});
}
renderNotes();
resetBtn.addEventListener('click', () => {
$('#resetConfirmationModal').modal('show');
});
confirmResetBtn.addEventListener('click', () => {
localStorage.removeItem('cookiesAccepted');
localStorage.removeItem('notes');
localStorage.removeItem('trash');
window.location.reload();
});
});