forked from Kritika30032002/To-Do-List-Application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
691 lines (684 loc) · 26.9 KB
/
index.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Elements reference
const taskList = document.getElementById("taskList");
const dueDateInput = document.getElementById("dueDate");
const priorityInput = document.getElementById("priority");
const submitBtn = document.getElementById("submitBtn");
const editTaskBtn = document.getElementById("editTask");
const tasksHeading = document.getElementById("heading-tasks");
const modeToggleBtn = document.getElementById("modeToggle");
const checkboxes = document.querySelectorAll(".form-check-input");
let editItem = null;
const tasksWithPriority = [];
const priorityColors = {
High: "task-priority-High",
Medium: "task-priority-Medium",
Low: "task-priority-Low",
};
const priorityValues = {
High: 3,
Medium: 2,
Low: 1,
};
// Adding Event Listeners
editTaskBtn.addEventListener("click", (e) => {
handleEditClick(e);
});
submitBtn.addEventListener("click", (e) => {
addItem(e);
});
taskList.addEventListener("click", handleItemClick);
modeToggleBtn.addEventListener("click", toggleMode);
checkboxes.forEach((checkbox) => {
checkbox.addEventListener("change", markAsComplete);
});
flatpickr(dueDateInput, {
enableTime: false, // If you want to enable time selection as well
dateFormat: "Y-m-d", // Adjust the date format as needed
});
function init() {
const body = document.getElementsByTagName("body")[0];
body.classList.add("light-mode");
const searchBar = document.getElementById("searchBar");
searchBar.addEventListener("input", handleSearch);
loadTasksFromLocalStorage();
tasksCheck();
}
function handleSearch() {
const searchTerm = searchBar.value.toLowerCase();
const tasks = document.querySelectorAll(".list-group-item");
tasks.forEach((task) => {
const taskTitle = task.childNodes[1].textContent.trim().toLowerCase();
if (taskTitle.includes(searchTerm)) {
task.style.display = "block";
} else {
task.style.display = "none";
}
});
}
function tasksCheck() {
const tasks = taskList.children;
if (tasks.length === 0) {
tasksHeading.classList.toggle("hidden");
document.querySelector(".clear_btn").style.display = "none";
document.querySelector(".dropdown").style.display = "none";
}
}
function handleEditItem(e) {
e.preventDefault();
editTaskBtn.style.display = "inline";
submitBtn.style.display = "none";
const taskTitle = e.target.parentElement.childNodes[1].textContent.trim();
document.getElementById("item").value = taskTitle;
editItem = e.target;
}
function handleEditClick(e) {
e.preventDefault();
const itemInput = document.getElementById("item");
const dueDateInput = document.getElementById("dueDate");
const editedItemText = itemInput.value;
const editedDueDate = new Date(dueDateInput.value);
const currentDate = new Date().toISOString().split("T")[0];
if (editedDueDate < new Date(currentDate)) {
displayErrorMessage("Due date has already passed");
return false;
}
const listItem = editItem.parentElement;
listItem.childNodes[1].textContent = editedItemText;
if (editedDueDate >= new Date(currentDate)) {
listItem.childNodes[5].textContent = `Due Date:${dueDateInput.value}
`;
}
displaySuccessMessage("Task edited successfully");
editItem = null;
itemInput.value = "";
dueDateInput.value = "";
editTaskBtn.style.display = "none";
submitBtn.style.display = "inline";
saveTasksToLocalStorage();
}
document.addEventListener("DOMContentLoaded", function () {
const recognition = new (window.SpeechRecognition ||
window.webkitSpeechRecognition)();
recognition.lang = "en-US";
recognition.interimResults = false;
let isListening = false;
const voiceCommandButton = document.getElementById("voice-command-button");
voiceCommandButton.addEventListener("click", function () {
if (isListening) {
recognition.stop();
isListening = false;
voiceCommandButton.textContent = "start voice command";
} else {
recognition.start();
isListening = true;
voiceCommandButton.textContent = "stop voice command";
}
});
recognition.onresult = function (event) {
const transcript = event.results[0][0].transcript;
handleVoiceCommand(transcript);
};
recognition.onend = function () {
isListening = false;
voiceCommandButton.textContent = "Start Voice Command";
};
function handleEditClick(e) {
e.preventDefault();
const itemInput = document.getElementById("item");
const dueDateInput = document.getElementById("dueDate");
const editedItemText = itemInput.value;
const editedDueDate = new Date(dueDateInput.value);
const currentDate = new Date().toISOString().split("T")[0];
if (editedDueDate < new Date(currentDate)) {
displayErrorMessage("Due date has already passed");
return false;
}
const listItem = editItem.parentElement;
listItem.childNodes[1].textContent = editedItemText;
if (editedDueDate >= new Date(currentDate)) {
listItem.childNodes[5].textContent = `Due Date:${dueDateInput.value}`;
}
displaySuccessMessage("Task edited successfully");
editItem = null;
itemInput.value = "";
dueDateInput.value = "";
editTaskBtn.style.display = "none";
submitBtn.style.display = "inline";
// Call displayTaskDetails with the appropriate argument
displayTaskDetails(listItem);
saveTasksToLocalStorage();
}
function handleVoiceCommand(command) {
console.log("Recognized Command:", command);
const commandParts = command.split(" ");
if (command.length >= 4) {
if (command.toLowerCase().includes("add")) {
const titleIndex = commandParts.indexOf("add") + 1;
const dueIndex = commandParts.indexOf("due");
const dateIndex = commandParts.indexOf("date");
const priorityIndex = commandParts.indexOf("priority");
if (
titleIndex < dueIndex &&
dueIndex < dateIndex &&
dateIndex < priorityIndex
) {
const taskTitle = commandParts.slice(titleIndex, dueIndex).join(" ");
const dueDate = commandParts
.slice(dateIndex + 1, priorityIndex)
.join(" ");
const priority = commandParts[priorityIndex + 1];
addTask(taskTitle, dueDate, priority);
return;
}
} else if (
command.toLowerCase().includes("edit") &&
command.toLowerCase().includes("task")
) {
// const editIndex = commandParts.indexOf('edit');
const editIndex = commandParts.indexOf("edit");
const taskIndex = commandParts.indexOf("task");
const toIndex = commandParts.indexOf("to");
const dueDateIndex = commandParts.indexOf("due");
const priorityIndex = commandParts.indexOf("priority");
if (
editIndex !== -1 &&
taskIndex !== -1 &&
toIndex !== -1 &&
dueDateIndex !== -1 &&
priorityIndex !== -1 &&
toIndex > taskIndex &&
dueDateIndex > toIndex &&
priorityIndex > dueDateIndex &&
priorityIndex < commandParts.length - 1
) {
const oldTitle = commandParts.slice(taskIndex + 1, toIndex).join(" ");
const newTitle = commandParts
.slice(toIndex + 1, dueDateIndex)
.join(" ");
const newdueDate = commandParts.slice(
dueDateIndex + 2,
dueDateIndex + 4
);
const newpriority = capitalizeFirstLetter(
commandParts[priorityIndex + 1]
);
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
console.log("Old Title:", oldTitle);
console.log("New Title:", newTitle);
console.log("Due Date:", newdueDate);
console.log("Priority:", newpriority);
editTask(oldTitle, newTitle, newdueDate, newpriority);
return;
}
} else if (command.toLowerCase().includes("delete")) {
const titleIndex = commandParts.indexOf("task") + 1;
const taskTitle = commandParts.slice(titleIndex).join(" ");
deleteTask(taskTitle);
} else {
displayErrorMessage("Invalid voice command format.");
}
}
}
function deleteTask(taskTitle) {
const taskElement = findTaskElement(taskTitle);
if (taskElement) {
taskElement.remove();
saveTasksToLocalStorage();
displaySuccessMessage(`Task "${taskTitle}" deleted successfully.`);
} else {
displayErrorMessage(`Task "${taskTitle}" not found.`);
}
}
function editTask(oldTitle, newTitle, newdueDate, newpriority) {
const taskElement = findTaskElement(oldTitle);
if (taskElement) {
const dueDateElement = taskElement.querySelector("#task-dueDate");
const priorityElement = taskElement.querySelector("#task-priority");
const titleTextNode = taskElement.childNodes[1];
titleTextNode.textContent = titleTextNode.textContent.replace(
oldTitle,
newTitle
);
console.log("New Due Date:", newdueDate);
if (dueDateElement) {
dueDateElement.textContent = `Due Date: ${newdueDate}`;
dueDateElement.id = "task-dueDate";
}
if (priorityElement) {
priorityElement.textContent = newpriority;
priorityElement.id = "task-priority";
}
// Call displayTaskDaetails with the appropriate argument
displayTaskDetails(taskElement);
saveTasksToLocalStorage();
displaySuccessMessage(`Task "${oldTitle}" edited successfully.`);
} else {
displayErrorMessage(`Task "${oldTitle}" not found.`);
}
}
function findTaskElement(taskTitle) {
const tasks = document.querySelectorAll(".list-group-item");
for (const task of tasks) {
const title = task.childNodes[1].textContent.trim().toLowerCase();
if (title === taskTitle.toLowerCase()) {
return task;
}
}
return null;
}
function addTask(taskTitle, dueDate, priority) {
const todoList = document.getElementById("taskList");
const existingTasks = todoList.querySelectorAll("li");
// Use taskTitle instead of task in the comparison
console.log("Existing tasks: ");
existingTasks.forEach((item) =>
console.log(item.textContent.trim().toLowerCase())
);
const taskExists = Array.from(existingTasks).some(
(item) =>
item.textContent.trim().toLowerCase() === taskTitle.trim().toLowerCase()
);
if (taskExists) {
displayErrorMessage("Task already exists");
return;
}
const li = document.createElement("li");
const capitalizedPriority =
priority.charAt(0).toUpperCase() + priority.slice(1).toLowerCase();
console.log("Priority:", priority);
console.log("Priority Class:", priorityColors[capitalizedPriority]);
li.className = `list-group-item card shadow mb-4 bg-transparent ${priorityColors[capitalizedPriority]}`;
const completeCheckbox = document.createElement("input");
completeCheckbox.type = "checkbox";
completeCheckbox.className = "form-check-input task-completed";
completeCheckbox.addEventListener("change", markAsComplete);
const deleteButton = document.createElement("button");
deleteButton.type = "button";
deleteButton.className = "btn btn-danger float-right delete";
deleteButton.innerHTML =
'<ion-icon name="trash-outline" style="font-size: 20px"></ion-icon>';
const editButton = document.createElement("button");
editButton.className = "btn btn-success btn-sm float-right edit";
editButton.innerHTML =
'<ion-icon name="create-outline" style="font-size: 20px"></ion-icon>';
editButton.style.marginRight = "8px";
editButton.addEventListener("click", handleEditItem);
// const titleParagraph = document.createElement("p");
// titleParagraph.style.fontSize = "18px";
// titleParagraph.style.margin = "0 19px";
// titleParagraph.appendChild(document.createTextNode(taskTitle));
const dateTimeParagraph = document.createElement("p");
dateTimeParagraph.className = "text-muted";
dateTimeParagraph.id = "created-at";
dateTimeParagraph.style.fontSize = "15px";
dateTimeParagraph.style.margin = "0 19px";
dateTimeParagraph.appendChild(
document.createTextNode("Created:" + new Date().toLocaleString())
);
const dueDateParagraph = document.createElement("p");
dueDateParagraph.className = "text-muted";
dueDateParagraph.id = "task-dueDate";
dueDateParagraph.style.fontSize = "15px";
dueDateParagraph.style.margin = "0 19px";
dueDateParagraph.appendChild(
document.createTextNode("Due Date:" + dueDate)
);
const priorityParagraph = document.createElement("p");
priorityParagraph.className = "text-muted";
priorityParagraph.id = "task-priority";
priorityParagraph.style.fontSize = "15px";
priorityParagraph.style.margin = "0 19px";
priorityParagraph.appendChild(document.createTextNode(capitalizedPriority));
li.appendChild(completeCheckbox);
li.appendChild(document.createTextNode(taskTitle));
li.appendChild(deleteButton);
li.appendChild(editButton);
li.appendChild(dateTimeParagraph);
li.appendChild(dueDateParagraph);
li.appendChild(priorityParagraph);
todoList.appendChild(li);
saveTasksToLocalStorage();
// Display task details after adding
displayTaskDetails(li);
}
// recognition.start();
});
function displayTaskDetails(taskElement) {
if (taskElement) {
const dueDateElement = taskElement.querySelector("#task-dueDate");
const priorityElement = taskElement.querySelector("#task-priority");
const dueDate = dueDateElement
? dueDateElement.textContent.split(":")[1].trim()
: null;
const priority = priorityElement
? priorityElement.textContent.trim()
: null;
console.log(`Task Details - Due Date: ${dueDate}, Priority: ${priority}`);
}
}
function addItem(e) {
e.preventDefault();
tasksCheck();
const newTaskTitle = document.getElementById("item").value;
let dueDate = document.getElementById("dueDate").value;
const priority = document.getElementById("priority").value;
// Check if the due date has already passed
const currentDate = new Date();
const dueDateObj = new Date(dueDate);
const tasks = taskList.children;
console.log(newTaskTitle);
// if (dueDateObj < currentDate && tasks.length === 0) {
// displayErrorMessage("Due date has already passed");
// tasksHeading.classList.add("hidden");
// return false;
// } else if (dueDateObj < currentDate && tasks.length > 0) {
// displayErrorMessage("Due date has already passed");
// return false;
// } else {
// tasksHeading.classList.remove("hidden");
// }
// Added new logic to check conditions whether Task and Date are entered
if (!newTaskTitle) {
displayErrorMessage("Task not entered");
tasksHeading.classList.add("hidden");
return false;
} else if (!dueDate) {
displayErrorMessage("Please specify a due date!!!"); // Check if the due date is specified or not
return false;
} else if (dueDateObj < currentDate) {
displayErrorMessage("Due date has already passed");
return false;
} else {
tasksHeading.classList.remove("hidden");
}
if (newTaskTitle.trim() === "") return false;
else {
document.getElementById("item").value = "";
document.querySelector(".clear_btn").style.display = "inline";
document.querySelector(".dropdown").style.display = "inline";
}
const creationDateTime = new Date().toLocaleString();
createNewTask(newTaskTitle, creationDateTime, dueDate, priority);
saveTasksToLocalStorage();
document.getElementById("dueDate").value = "";
document.getElementById("priority").value = "";
}
function handleItemClick(e) {
if (e.target.classList.contains("delete")) {
e.preventDefault();
const li = e.target.parentElement;
const confirmationBox = document.getElementById("custom-confirm");
document.getElementById("confirm-msg").style.backgroundColor = "White";
document.getElementById("confirm-msg").style.color = "Black";
document.getElementById("confirm-msg").innerText =
"Are you sure you want to delete this task?";
const confirmYesButton = document.getElementById("confirm-yes");
const confirmNoButton = document.getElementById("confirm-no");
const confirmCancelButton = document.getElementById("confirm-cancel");
const handleYesClick = () => {
confirmationBox.style.display = "none";
li.parentElement.removeChild(li);
tasksCheck();
displaySuccessMessage("Task deleted successfully");
saveTasksToLocalStorage();
confirmYesButton.removeEventListener("click", handleYesClick);
confirmNoButton.removeEventListener("click", handleNoClick);
confirmCancelButton.removeEventListener("click", handleCancelClick);
};
const handleNoClick = () => {
confirmationBox.style.display = "none";
confirmYesButton.removeEventListener("click", handleYesClick);
confirmNoButton.removeEventListener("click", handleNoClick);
confirmCancelButton.removeEventListener("click", handleCancelClick);
};
const handleCancelClick = () => {
confirmationBox.style.display = "none";
confirmYesButton.removeEventListener("click", handleYesClick);
confirmNoButton.removeEventListener("click", handleNoClick);
confirmCancelButton.removeEventListener("click", handleCancelClick);
};
confirmYesButton.addEventListener("click", handleYesClick);
confirmNoButton.addEventListener("click", handleNoClick);
confirmCancelButton.addEventListener("click", handleCancelClick);
confirmationBox.style.display = "flex";
}
saveTasksToLocalStorage();
}
// function DefaultDate() {
// const today = new Date();
// const year = today.getFullYear();
// const month = today.getMonth() + 1;
// const day = today.getDate();
// return `${year}-${month}-${day}`;
// }
function markAsComplete(e) {
const li = e.target.parentElement;
li.classList.toggle("completed");
}
function displaySuccessMessage(message) {
document.getElementById("lblsuccess").innerHTML = message;
document.getElementById("lblsuccess").style.display = "block";
setTimeout(function () {
document.getElementById("lblsuccess").style.display = "none";
}, 3000);
}
function displayErrorMessage(message) {
document.getElementById("lblerror").innerHTML = message;
document.getElementById("lblerror").style.display = "block";
setTimeout(function () {
document.getElementById("lblerror").style.display = "none";
}, 3000);
}
// Function to save tasks to local storage
function saveTasksToLocalStorage() {
const tasks = document.querySelectorAll(".list-group-item");
const tasksArray = [];
tasks.forEach((task) => {
const taskText = task.childNodes[1].textContent;
const isCompleted = task.classList.contains("completed");
const createdAt = task.querySelector("#created-at").textContent;
const dueDate = task.querySelector("#task-dueDate").textContent;
const priority = task.querySelector("#task-priority").textContent;
const taskObj = {
text: taskText,
completed: isCompleted,
createdAt: createdAt,
dueDate: dueDate,
priority: priority,
};
tasksArray.push(taskObj);
});
localStorage.setItem("tasks", JSON.stringify(tasksArray));
}
// Function to retrieve tasks from local storage and display them
function loadTasksFromLocalStorage() {
const tasks = JSON.parse(localStorage.getItem("tasks"));
if (tasks && tasks.length > 0) {
const tasksHeading = document.getElementById("heading-tasks");
tasksHeading.classList.remove("hidden");
document.querySelector(".clear_btn").style.display = "inline";
document.querySelector(".dropdown").style.display = "inline";
tasks.forEach((task) => {
console.log(task.text, "taskText");
console.log(task.createdAt.slice(8), "taskCreateAt");
console.log(task.dueDate.split(":")[1], "testDuedate");
createNewTask(
task.text,
task.createdAt.slice(8),
task.dueDate.split(":")[1],
task.priority
);
});
}
}
function enableSubmit(ref, btnID) {
document.getElementById(btnID).disabled = false;
}
function toggleMode() {
const body = document.body;
body.classList.toggle("dark-mode");
body.classList.toggle("light-mode");
}
function clearAllTasks() {
// Removes all tasks from the task list
//changing confirmation message
const confirmationBoxAll = document.getElementById("custom-confirm-all");
document.getElementById("confirm-msg-all").style.backgroundColor = "Red";
document.getElementById("confirm-msg-all").style.color = "White";
document.getElementById("confirm-msg-all").innerText =
"Are you sure you want to delete all tasks?";
const confirmYesButtonAll = document.getElementById("confirm-yes-all");
const confirmNoButtonAll = document.getElementById("confirm-no-all");
const confirmCancelButtonAll = document.getElementById("confirm-cancel-all");
confirmYesButtonAll.addEventListener("click", () => {
confirmationBoxAll.style.display = "none";
while (taskList.firstChild) {
taskList.removeChild(taskList.firstChild);
}
// Hide the button after the task list is cleared
document.querySelector(".clear_btn").style.display = "none";
document.querySelector(".dropdown").style.display = "none";
// Hide the tasks heading since there are no tasks left
tasksHeading.classList.add("hidden");
saveTasksToLocalStorage();
});
confirmNoButtonAll.addEventListener("click", () => {
confirmationBoxAll.style.display = "none";
});
confirmCancelButtonAll.addEventListener("click", () => {
confirmationBoxAll.style.display = "none";
});
confirmationBoxAll.style.display = "flex";
}
//Function to sort task list by due date
function sortByDueDate(order) {
const sortTaskList = JSON.parse(localStorage.getItem("tasks"));
if (order === "early") {
sortTaskList.sort((a, b) => {
return new Date(a.dueDate) - new Date(b.dueDate);
});
} else if (order === "late") {
sortTaskList.sort((a, b) => {
return new Date(b.dueDate) - new Date(a.dueDate);
});
}
while (taskList.firstChild) {
taskList.removeChild(taskList.firstChild);
}
tasksHeading.classList.add("hidden");
localStorage.setItem("tasks", JSON.stringify(sortTaskList));
loadTasksFromLocalStorage();
}
function sortByPriority(order) {
const sortTaskList = JSON.parse(localStorage.getItem("tasks"));
sortTaskList.sort((a, b) => {
if (order === "highToLow") {
// Sort tasks from high priority to low priority
return priorityValues[b.priority] - priorityValues[a.priority];
} else if (order === "lowToHigh") {
// Sort tasks from low priority to high priority
return priorityValues[a.priority] - priorityValues[b.priority];
} else {
// Default sorting (e.g., no sorting)
return 0;
}
});
while (taskList.firstChild) {
taskList.removeChild(taskList.firstChild);
}
tasksHeading.classList.add("hidden");
localStorage.setItem("tasks", JSON.stringify(sortTaskList));
loadTasksFromLocalStorage();
}
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
console.log("func called");
document.getElementById("myDropdown").classList.toggle("show");
}
// function sortTasks(order){
// if (order === 'highToLow'){
// sortByPriority('highToLow');
// }
// else if (order === 'lowToHigh'){
// sortByPriority('lowToHigh');
// }
// document.getElementById("myDropdown").classList.remove("show");
// }
// Close the dropdown menu if the user clicks outside of it
window.onclick = function (event) {
if (!event.target.matches(".dropbtn")) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains("show")) {
openDropdown.classList.remove("show");
}
}
}
};
function createNewTask(taskTitle, createdDate, dueDate, priority) {
const li = document.createElement("li");
li.className = `list-group-item card shadow mb-4 bg-transparent ${priorityColors[priority]}`;
console.log(priority);
const completeCheckbox = document.createElement("input");
completeCheckbox.type = "checkbox";
completeCheckbox.className = "form-check-input task-completed";
completeCheckbox.addEventListener("change", markAsComplete);
const deleteButton = document.createElement("button");
deleteButton.type = "button";
deleteButton.className = "btn btn-danger float-right delete";
// deleteButton.appendChild(document.createTextNode("Delete"));
deleteButton.innerHTML =
'<ion-icon name="trash-outline" style="font-size: 20px"></ion-icon>';
const editButton = document.createElement("button");
editButton.className = "btn btn-success btn-sm float-right edit";
// editButton.appendChild(document.createTextNode("Edit"));
editButton.innerHTML =
'<ion-icon name="create-outline" style="font-size: 20px"></ion-icon>';
editButton.style.marginRight = "8px";
// Create a click event listener for the edit button
editButton.addEventListener("click", function (e) {
handleEditItem(e);
});
const dateTimeParagraph = document.createElement("p");
dateTimeParagraph.className = "text-muted";
dateTimeParagraph.id = "created-at";
dateTimeParagraph.style.fontSize = "15px";
dateTimeParagraph.style.margin = "0 19px";
dateTimeParagraph.appendChild(
document.createTextNode("Created:" + createdDate)
);
// Create a paragraph element for the due date
const dueDateParagraph = document.createElement("p");
dueDateParagraph.className = "text-muted";
dueDateParagraph.id = "task-dueDate";
dueDateParagraph.style.fontSize = "15px";
dueDateParagraph.style.margin = "0 19px";
dueDateParagraph.appendChild(document.createTextNode("Due Date:" + dueDate));
const priorityParagraph = document.createElement("p");
priorityParagraph.className = "text-muted";
priorityParagraph.id = "task-priority";
priorityParagraph.style.fontSize = "15px";
priorityParagraph.style.margin = "0 19px";
priorityParagraph.appendChild(document.createTextNode(priority));
li.appendChild(completeCheckbox);
li.appendChild(document.createTextNode(taskTitle));
li.appendChild(deleteButton);
li.appendChild(editButton);
li.appendChild(dateTimeParagraph);
li.appendChild(dueDateParagraph);
li.appendChild(priorityParagraph);
taskList.appendChild(li);
displayTaskDetails(li);
}
init();
// Preloader function
document.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
document.querySelector(".preloader").style.display = "none";
}, 2000);
});