Skip to content

Commit c2e96cb

Browse files
committed
Fix: minimal code clean.
1 parent c8fef29 commit c2e96cb

File tree

2 files changed

+58
-64
lines changed

2 files changed

+58
-64
lines changed

script.js

Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ if (!responsiveDesign && window.innerWidth <= 768) {
1717
***********************/
1818

1919
// Get elements that change with the mode.
20+
const body = document.body;
2021
const toggleModeBtn = document.getElementById("toggle-mode-btn");
2122
const portfolioLink = document.getElementById("portfolio-link");
22-
const body = document.body;
23-
const allUls = document.querySelectorAll("ul");
2423
const simpleRemindercContainer = document.getElementById("simple-reminder-container");
25-
const remindersContainer = document.getElementById("reminders-container");
2624
const inputContainer = document.getElementById("input-container");
2725
const inputField = document.getElementById("input-field");
26+
const remindersContainer = document.getElementById("reminders-container");
27+
const allUls = document.querySelectorAll("ul");
2828

2929

3030
// Function to apply mode.
@@ -40,7 +40,7 @@ function applyMode(mode) {
4040
ul.style.borderColor = "rgb(245, 245, 245)";
4141
});
4242

43-
simpleRemindercContainer.style.backgroundColor = "rgb(2, 4, 8)";
43+
simpleRemindercContainer.style.backgroundColor = "rgb(15, 15, 20)";
4444
inputContainer.style.backgroundColor = "rgb(30, 30, 30)";
4545
inputField.style.color = "rgb(245, 245, 245)";
4646

@@ -54,14 +54,14 @@ function applyMode(mode) {
5454
});
5555

5656
simpleRemindercContainer.style.backgroundColor = "rgb(245, 245, 245)";
57-
inputContainer.style.backgroundColor = "rgb(240, 240, 240)";
57+
inputContainer.style.backgroundColor = "rgb(225, 225, 225)";
5858
inputField.style.color = "rgb(2, 4, 8)";
5959

6060
responsiveWarning.style.backgroundColor = "rgb(245, 245, 245)";
6161
}
6262
}
6363

64-
// Check and apply saved mode on page load
64+
// Check and apply saved mode on page load.
6565
let savedMode = localStorage.getItem("mode");
6666

6767
if (savedMode === null) {
@@ -90,81 +90,81 @@ toggleModeBtn.addEventListener("click", function () {
9090
* SIMPLE REMINDER *
9191
******************/
9292

93-
// Variable to keep track of the element being dragged
93+
// Variable to keep track of the element being dragged.
9494
let draggingElement = null;
95-
// Variable to store the reference to the current alert
95+
// Variable to store the reference to the current alert.
9696
let currentAlert = null;
9797

9898

99-
// Function to create a new reminder element
99+
// Function to create a new reminder element.
100100
function createReminderElement(text, checked = false) {
101-
// Create a new list item element and set its HTML content
101+
// Create a new list item element and set its HTML content.
102102
const li = document.createElement("li");
103103
li.innerHTML = `<p>${text}</p><span>\u00d7</span>`;
104104

105-
// If the reminder is checked, add a CSS class to mark it as checked
105+
// If the reminder is checked, add a CSS class to mark it as checked.
106106
if (checked) {
107107
li.classList.add("checked");
108108
}
109109

110-
// Add and return event listeners to the newly created reminder
110+
// Add and return event listeners to the newly created reminder.
111111
addEventListenersToReminder(li);
112112
return li;
113113
}
114114

115115

116-
// Function to add event listeners to a reminder element
116+
// Function to add event listeners to a reminder element.
117117
function addEventListenersToReminder(reminder) {
118118
// Enable the reminder to be draggable
119119
reminder.draggable = true;
120120

121-
// Event listener to handle the start of a drag operation
121+
// Event listener to handle the start of a drag operation.
122122
reminder.addEventListener("dragstart", function (event) {
123-
// Store the reference to the dragging element
123+
// Store the reference to the dragging element.
124124
draggingElement = event.currentTarget;
125125

126-
// Set data to be transferred during drag-and-drop operation
126+
// Set data to be transferred during drag-and-drop operation.
127127
event.dataTransfer.setData("text/plain", "");
128128

129-
// Add a CSS class to indicate that the element is being dragged
129+
// Add a CSS class to indicate that the element is being dragged.
130130
setTimeout(() => {
131131
event.target.classList.add("dragging");
132132
}, 0);
133133
});
134134

135-
// Event listener to handle the end of a drag operation
135+
// Event listener to handle the end of a drag operation.
136136
reminder.addEventListener("dragend", function () {
137137
if (draggingElement) {
138138
draggingElement.classList.remove("dragging");
139139
draggingElement.classList.remove("highlighted");
140140
draggingElement.style.backgroundColor = "";
141141
draggingElement = null;
142142

143-
// Save the current state of reminders
143+
// Save the current state of reminders.
144144
savedReminders();
145145
};
146146
});
147147

148-
// Event listener to handle drag-over events on the reminder
148+
// Event listener to handle drag-over events on the reminder.
149149
reminder.addEventListener("dragover", function (event) {
150150
// Prevent the default drag-over behavior
151151
event.preventDefault();
152152

153-
// Find the target list item element being dragged over
153+
// Find the target list item element being dragged over.
154154
const targetElement = event.target.closest("li");
155155

156156
if (!targetElement) {
157157
return;
158158
}
159159

160-
// Get the vertical position of the cursor relative to the target element
160+
// Get the vertical position of the cursor relative to the target element.
161161
const bounding = targetElement.getBoundingClientRect();
162162
const offset = bounding.y + bounding.height / 2;
163163

164-
// Determine if the cursor is above or below the center of the target element
164+
// Determine if the cursor is above or below the center of the target element.
165165
const isAfter = event.clientY > offset;
166166

167-
// Reorder the reminders based on the cursor position
167+
// Reorder the reminders based on the cursor position.
168168
const draggingElement = document.querySelector(".dragging");
169169

170170
if (isAfter) {
@@ -175,142 +175,135 @@ function addEventListenersToReminder(reminder) {
175175

176176
const mode = localStorage.getItem("mode") || "light-mode";
177177

178-
// Add a CSS class to highlight the drop position
178+
// Add a CSS class to highlight the drop position.
179179
draggingElement.classList.add("highlighted");
180180

181181
if (mode === "dark-mode") {
182182
draggingElement.style.backgroundColor = "rgb(30, 30, 30)";
183183
} else {
184-
draggingElement.style.backgroundColor = "rgb(240, 240, 240)";
184+
draggingElement.style.backgroundColor = "rgb(225, 225, 225)";
185185
}
186186
});
187187

188-
// Event listener to handle the click on the delete button
188+
// Event listener to handle the click on the delete button.
189189
reminder.querySelector("span").addEventListener("click", function () {
190-
// Remove the reminder from the DOM
190+
// Remove the reminder from the DOM.
191191
reminder.remove();
192192

193-
// Save the current state of reminders
193+
// Save the current state of reminders.
194194
savedReminders();
195195
});
196196

197-
// Event listener to handle the click on the reminder itself
197+
// Event listener to handle the click on the reminder itself.
198198
reminder.addEventListener("click", function (event) {
199-
// Toggle the checked state of the reminder when clicking on it
199+
// Toggle the checked state of the reminder when clicking on it.
200200
if (event.target.tagName === "LI" || event.target.tagName === "P") {
201201
reminder.classList.toggle("checked");
202202

203-
// Save the current state of reminders
203+
// Save the current state of reminders.
204204
savedReminders();
205205
}
206206
});
207207
}
208208

209-
210-
// Function to add a new reminder
209+
// Function to add a new reminder.
211210
function addReminder() {
212211
const inputValue = inputField.value.trim();
213212

214-
// If the input value is empty, show an alert and return
213+
// If the input value is empty, show an alert and return.
215214
if (inputValue === "") {
216215
showAlert("You must write something!");
217216
return;
218217
}
219218

220-
// Check if the input value contains only valid characters
219+
// Check if the input value contains only valid characters.
221220
const validInput = /^[a-zA-ZÀ-ÿ\s\d\.,'":!?()/-]*$/.test(inputValue);
222221

223-
// If the input value contains invalid characters, show an alert and clear the input field
222+
// If the input value contains invalid characters, show an alert and clear the input field.
224223
if (!validInput) {
225224
showAlert("This character is not allowed!");
226225

227-
// Clear the input field
226+
// Clear the input field.
228227
inputField.value = "";
229228
return;
230229
}
231230

232-
233-
// Create a new reminder element with the input value
231+
// Create a new reminder element with the input value.
234232
const li = createReminderElement(inputValue);
235-
// Append the reminder to the reminders container
233+
// Append the reminder to the reminders container.
236234
remindersContainer.appendChild(li);
237-
// Save the current state of reminders
235+
// Save the current state of reminders.
238236
savedReminders();
239237

240-
// Clear the input field after adding the reminder
238+
// Clear the input field after adding the reminder.
241239
inputField.value = "";
242240
}
243241

244-
245-
// Function to save the current state of reminders to local storage
242+
// Function to save the current state of reminders to local storage.
246243
function savedReminders() {
247244
localStorage.setItem("reminders", remindersContainer.innerHTML);
248245
}
249246

250-
251-
// Function to load reminders from local storage
247+
// Function to load reminders from local storage.
252248
function loadReminders() {
253-
// Retrieve saved reminders from local storage
249+
// Retrieve saved reminders from local storage.
254250
const savedReminders = localStorage.getItem("reminders");
255251

256-
// If there are saved reminders, populate the reminders container with them
252+
// If there are saved reminders, populate the reminders container with them.
257253
if (savedReminders) {
258254
remindersContainer.innerHTML = savedReminders;
259255

260-
// Add event listeners to each loaded reminder
256+
// Add event listeners to each loaded reminder.
261257
remindersContainer.querySelectorAll("li").forEach(li => {
262258
addEventListenersToReminder(li);
263259
});
264260
}
265261
}
266262

267-
268-
// Function to show a custom alert message
263+
// Function to show a custom alert message.
269264
function showAlert(message) {
270-
// If an alert is already displayed, remove it
265+
// If an alert is already displayed, remove it.
271266
if (currentAlert) {
272267
currentAlert.remove();
273268
currentAlert = null;
274269
}
275270

276-
// Create a new alert element
271+
// Create a new alert element.
277272
const alertElement = document.createElement("div");
278273
alertElement.classList.add("alert");
279274
alertElement.textContent = message;
280275

281-
// Append the alert to the body
276+
// Append the alert to the body.
282277
document.body.appendChild(alertElement);
283278

284-
// Remove the alert after a certain delay
279+
// Remove the alert after a certain delay.
285280
const delay = message === "clear" ? 500 : 3000;
286281
setTimeout(() => {
287282
alertElement.remove();
288283

289-
// Reset the reference to the current alert
284+
// Reset the reference to the current alert.
290285
if (alertElement === currentAlert) {
291286
currentAlert = null;
292287
}
293288
}, delay);
294289

295-
// Update the reference to the current alert
290+
// Update the reference to the current alert.
296291
currentAlert = alertElement;
297292
}
298293

299294

300-
// Event listener to add a reminder when the Enter key is pressed
295+
// Event listener to add a reminder when the Enter key is pressed.
301296
inputField.addEventListener("keypress", function (event) {
302297
if (event.key === "Enter") {
303298
addReminder();
304299
} else {
305-
// If an alert is displayed, remove it
300+
// If an alert is displayed, remove it.
306301
if (currentAlert) {
307302
currentAlert.remove();
308303
currentAlert = null;
309304
}
310305
}
311306
});
312307

313-
314-
315-
// Load reminders from local storage when the page is loaded
308+
// Load reminders from local storage when the page is loaded.
316309
loadReminders();

style.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ a {
7070

7171
h2 {
7272
margin-bottom: 25px;
73-
padding-left: 20px;
73+
padding-left: 17.5px;
7474
display: flex;
7575
align-items: center;
7676
font-family: "Roboto Black", sans-serif;
77+
font-weight: 500;
7778
}
7879

7980
/* MAIN */

0 commit comments

Comments
 (0)