@@ -17,14 +17,14 @@ if (!responsiveDesign && window.innerWidth <= 768) {
17
17
***********************/
18
18
19
19
// Get elements that change with the mode.
20
+ const body = document . body ;
20
21
const toggleModeBtn = document . getElementById ( "toggle-mode-btn" ) ;
21
22
const portfolioLink = document . getElementById ( "portfolio-link" ) ;
22
- const body = document . body ;
23
- const allUls = document . querySelectorAll ( "ul" ) ;
24
23
const simpleRemindercContainer = document . getElementById ( "simple-reminder-container" ) ;
25
- const remindersContainer = document . getElementById ( "reminders-container" ) ;
26
24
const inputContainer = document . getElementById ( "input-container" ) ;
27
25
const inputField = document . getElementById ( "input-field" ) ;
26
+ const remindersContainer = document . getElementById ( "reminders-container" ) ;
27
+ const allUls = document . querySelectorAll ( "ul" ) ;
28
28
29
29
30
30
// Function to apply mode.
@@ -40,7 +40,7 @@ function applyMode(mode) {
40
40
ul . style . borderColor = "rgb(245, 245, 245)" ;
41
41
} ) ;
42
42
43
- simpleRemindercContainer . style . backgroundColor = "rgb(2, 4, 8 )" ;
43
+ simpleRemindercContainer . style . backgroundColor = "rgb(15, 15, 20 )" ;
44
44
inputContainer . style . backgroundColor = "rgb(30, 30, 30)" ;
45
45
inputField . style . color = "rgb(245, 245, 245)" ;
46
46
@@ -54,14 +54,14 @@ function applyMode(mode) {
54
54
} ) ;
55
55
56
56
simpleRemindercContainer . style . backgroundColor = "rgb(245, 245, 245)" ;
57
- inputContainer . style . backgroundColor = "rgb(240, 240, 240 )" ;
57
+ inputContainer . style . backgroundColor = "rgb(225, 225, 225 )" ;
58
58
inputField . style . color = "rgb(2, 4, 8)" ;
59
59
60
60
responsiveWarning . style . backgroundColor = "rgb(245, 245, 245)" ;
61
61
}
62
62
}
63
63
64
- // Check and apply saved mode on page load
64
+ // Check and apply saved mode on page load.
65
65
let savedMode = localStorage . getItem ( "mode" ) ;
66
66
67
67
if ( savedMode === null ) {
@@ -90,81 +90,81 @@ toggleModeBtn.addEventListener("click", function () {
90
90
* SIMPLE REMINDER *
91
91
******************/
92
92
93
- // Variable to keep track of the element being dragged
93
+ // Variable to keep track of the element being dragged.
94
94
let draggingElement = null ;
95
- // Variable to store the reference to the current alert
95
+ // Variable to store the reference to the current alert.
96
96
let currentAlert = null ;
97
97
98
98
99
- // Function to create a new reminder element
99
+ // Function to create a new reminder element.
100
100
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.
102
102
const li = document . createElement ( "li" ) ;
103
103
li . innerHTML = `<p>${ text } </p><span>\u00d7</span>` ;
104
104
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.
106
106
if ( checked ) {
107
107
li . classList . add ( "checked" ) ;
108
108
}
109
109
110
- // Add and return event listeners to the newly created reminder
110
+ // Add and return event listeners to the newly created reminder.
111
111
addEventListenersToReminder ( li ) ;
112
112
return li ;
113
113
}
114
114
115
115
116
- // Function to add event listeners to a reminder element
116
+ // Function to add event listeners to a reminder element.
117
117
function addEventListenersToReminder ( reminder ) {
118
118
// Enable the reminder to be draggable
119
119
reminder . draggable = true ;
120
120
121
- // Event listener to handle the start of a drag operation
121
+ // Event listener to handle the start of a drag operation.
122
122
reminder . addEventListener ( "dragstart" , function ( event ) {
123
- // Store the reference to the dragging element
123
+ // Store the reference to the dragging element.
124
124
draggingElement = event . currentTarget ;
125
125
126
- // Set data to be transferred during drag-and-drop operation
126
+ // Set data to be transferred during drag-and-drop operation.
127
127
event . dataTransfer . setData ( "text/plain" , "" ) ;
128
128
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.
130
130
setTimeout ( ( ) => {
131
131
event . target . classList . add ( "dragging" ) ;
132
132
} , 0 ) ;
133
133
} ) ;
134
134
135
- // Event listener to handle the end of a drag operation
135
+ // Event listener to handle the end of a drag operation.
136
136
reminder . addEventListener ( "dragend" , function ( ) {
137
137
if ( draggingElement ) {
138
138
draggingElement . classList . remove ( "dragging" ) ;
139
139
draggingElement . classList . remove ( "highlighted" ) ;
140
140
draggingElement . style . backgroundColor = "" ;
141
141
draggingElement = null ;
142
142
143
- // Save the current state of reminders
143
+ // Save the current state of reminders.
144
144
savedReminders ( ) ;
145
145
} ;
146
146
} ) ;
147
147
148
- // Event listener to handle drag-over events on the reminder
148
+ // Event listener to handle drag-over events on the reminder.
149
149
reminder . addEventListener ( "dragover" , function ( event ) {
150
150
// Prevent the default drag-over behavior
151
151
event . preventDefault ( ) ;
152
152
153
- // Find the target list item element being dragged over
153
+ // Find the target list item element being dragged over.
154
154
const targetElement = event . target . closest ( "li" ) ;
155
155
156
156
if ( ! targetElement ) {
157
157
return ;
158
158
}
159
159
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.
161
161
const bounding = targetElement . getBoundingClientRect ( ) ;
162
162
const offset = bounding . y + bounding . height / 2 ;
163
163
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.
165
165
const isAfter = event . clientY > offset ;
166
166
167
- // Reorder the reminders based on the cursor position
167
+ // Reorder the reminders based on the cursor position.
168
168
const draggingElement = document . querySelector ( ".dragging" ) ;
169
169
170
170
if ( isAfter ) {
@@ -175,142 +175,135 @@ function addEventListenersToReminder(reminder) {
175
175
176
176
const mode = localStorage . getItem ( "mode" ) || "light-mode" ;
177
177
178
- // Add a CSS class to highlight the drop position
178
+ // Add a CSS class to highlight the drop position.
179
179
draggingElement . classList . add ( "highlighted" ) ;
180
180
181
181
if ( mode === "dark-mode" ) {
182
182
draggingElement . style . backgroundColor = "rgb(30, 30, 30)" ;
183
183
} else {
184
- draggingElement . style . backgroundColor = "rgb(240, 240, 240 )" ;
184
+ draggingElement . style . backgroundColor = "rgb(225, 225, 225 )" ;
185
185
}
186
186
} ) ;
187
187
188
- // Event listener to handle the click on the delete button
188
+ // Event listener to handle the click on the delete button.
189
189
reminder . querySelector ( "span" ) . addEventListener ( "click" , function ( ) {
190
- // Remove the reminder from the DOM
190
+ // Remove the reminder from the DOM.
191
191
reminder . remove ( ) ;
192
192
193
- // Save the current state of reminders
193
+ // Save the current state of reminders.
194
194
savedReminders ( ) ;
195
195
} ) ;
196
196
197
- // Event listener to handle the click on the reminder itself
197
+ // Event listener to handle the click on the reminder itself.
198
198
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.
200
200
if ( event . target . tagName === "LI" || event . target . tagName === "P" ) {
201
201
reminder . classList . toggle ( "checked" ) ;
202
202
203
- // Save the current state of reminders
203
+ // Save the current state of reminders.
204
204
savedReminders ( ) ;
205
205
}
206
206
} ) ;
207
207
}
208
208
209
-
210
- // Function to add a new reminder
209
+ // Function to add a new reminder.
211
210
function addReminder ( ) {
212
211
const inputValue = inputField . value . trim ( ) ;
213
212
214
- // If the input value is empty, show an alert and return
213
+ // If the input value is empty, show an alert and return.
215
214
if ( inputValue === "" ) {
216
215
showAlert ( "You must write something!" ) ;
217
216
return ;
218
217
}
219
218
220
- // Check if the input value contains only valid characters
219
+ // Check if the input value contains only valid characters.
221
220
const validInput = / ^ [ a - z A - Z À - ÿ \s \d \. , ' " : ! ? ( ) / - ] * $ / . test ( inputValue ) ;
222
221
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.
224
223
if ( ! validInput ) {
225
224
showAlert ( "This character is not allowed!" ) ;
226
225
227
- // Clear the input field
226
+ // Clear the input field.
228
227
inputField . value = "" ;
229
228
return ;
230
229
}
231
230
232
-
233
- // Create a new reminder element with the input value
231
+ // Create a new reminder element with the input value.
234
232
const li = createReminderElement ( inputValue ) ;
235
- // Append the reminder to the reminders container
233
+ // Append the reminder to the reminders container.
236
234
remindersContainer . appendChild ( li ) ;
237
- // Save the current state of reminders
235
+ // Save the current state of reminders.
238
236
savedReminders ( ) ;
239
237
240
- // Clear the input field after adding the reminder
238
+ // Clear the input field after adding the reminder.
241
239
inputField . value = "" ;
242
240
}
243
241
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.
246
243
function savedReminders ( ) {
247
244
localStorage . setItem ( "reminders" , remindersContainer . innerHTML ) ;
248
245
}
249
246
250
-
251
- // Function to load reminders from local storage
247
+ // Function to load reminders from local storage.
252
248
function loadReminders ( ) {
253
- // Retrieve saved reminders from local storage
249
+ // Retrieve saved reminders from local storage.
254
250
const savedReminders = localStorage . getItem ( "reminders" ) ;
255
251
256
- // If there are saved reminders, populate the reminders container with them
252
+ // If there are saved reminders, populate the reminders container with them.
257
253
if ( savedReminders ) {
258
254
remindersContainer . innerHTML = savedReminders ;
259
255
260
- // Add event listeners to each loaded reminder
256
+ // Add event listeners to each loaded reminder.
261
257
remindersContainer . querySelectorAll ( "li" ) . forEach ( li => {
262
258
addEventListenersToReminder ( li ) ;
263
259
} ) ;
264
260
}
265
261
}
266
262
267
-
268
- // Function to show a custom alert message
263
+ // Function to show a custom alert message.
269
264
function showAlert ( message ) {
270
- // If an alert is already displayed, remove it
265
+ // If an alert is already displayed, remove it.
271
266
if ( currentAlert ) {
272
267
currentAlert . remove ( ) ;
273
268
currentAlert = null ;
274
269
}
275
270
276
- // Create a new alert element
271
+ // Create a new alert element.
277
272
const alertElement = document . createElement ( "div" ) ;
278
273
alertElement . classList . add ( "alert" ) ;
279
274
alertElement . textContent = message ;
280
275
281
- // Append the alert to the body
276
+ // Append the alert to the body.
282
277
document . body . appendChild ( alertElement ) ;
283
278
284
- // Remove the alert after a certain delay
279
+ // Remove the alert after a certain delay.
285
280
const delay = message === "clear" ? 500 : 3000 ;
286
281
setTimeout ( ( ) => {
287
282
alertElement . remove ( ) ;
288
283
289
- // Reset the reference to the current alert
284
+ // Reset the reference to the current alert.
290
285
if ( alertElement === currentAlert ) {
291
286
currentAlert = null ;
292
287
}
293
288
} , delay ) ;
294
289
295
- // Update the reference to the current alert
290
+ // Update the reference to the current alert.
296
291
currentAlert = alertElement ;
297
292
}
298
293
299
294
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.
301
296
inputField . addEventListener ( "keypress" , function ( event ) {
302
297
if ( event . key === "Enter" ) {
303
298
addReminder ( ) ;
304
299
} else {
305
- // If an alert is displayed, remove it
300
+ // If an alert is displayed, remove it.
306
301
if ( currentAlert ) {
307
302
currentAlert . remove ( ) ;
308
303
currentAlert = null ;
309
304
}
310
305
}
311
306
} ) ;
312
307
313
-
314
-
315
- // Load reminders from local storage when the page is loaded
308
+ // Load reminders from local storage when the page is loaded.
316
309
loadReminders ( ) ;
0 commit comments