-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
119 lines (101 loc) · 2.78 KB
/
script.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
let tasks = [];
const tableList = document.getElementById("table");
const addTaskInput = document.getElementById("add");
const tasksCounter = document.getElementById("tasks-counter");
const buttonToAddInput = document.getElementById("add-task-button");
// Function to add task in a list DOM
function addTaskToDom(task) {
// Created a row for table
let row = document.createElement("tr");
// Cells for a particular row
let c1 = document.createElement("td");
let c2 = document.createElement("td");
let c3 = document.createElement("td");
let c4 = document.createElement("td");
// Insert data to cells
c1.innerHTML = `
<input type="checkbox" id="${task.id}" class="custom-checkbox">
`;
c2.innerText = task.text;
c3.innerText = task.done;
c4.innerHTML = `
<img src="bin.png" class="delete" id="${task.id}"/>
`;
// Append cells to row
row.appendChild(c1);
row.appendChild(c2);
row.appendChild(c3);
row.appendChild(c4);
// Append row to table
tableList.appendChild(row);
}
// Function to render list
function renderList() {
tableList.innerHTML = "";
for (let i = 0; i < tasks.length; i++) {
addTaskToDom(tasks[i]);
}
tasksCounter.innerHTML = tasks.length;
}
// Function to toggle task
function toggleTask(taskId) {
const task = tasks.filter((task) => task.id === taskId);
if (task.length > 0) {
const currentTask = task[0];
currentTask.done = !currentTask.done;
renderList();
showNotification("Task toggled successfully");
return;
}
showNotification("Could not toggle the task");
}
// Function to perfom delete task
function deleteTask(taskId) {
const newTasks = tasks.filter((task) => task.id !== taskId);
tasks = newTasks;
renderList();
showNotification("Task deleted successfully");
}
// Function to add task in tasks array
function addTask(task) {
if (task) {
tasks.push(task);
renderList();
showNotification("Task added successfully");
return;
}
showNotification("Task cannot be added");
}
// Function create alerts
function showNotification(text) {
alert(text);
}
// Function to implement functionality when someone clicks on add button
function handleInputKeypress() {
const text = addTaskInput.value;
if (!text) {
showNotification("Task text cannot be empty");
return;
}
const task = {
text,
id: Date.now().toString(),
done: false,
};
addTaskInput.value = "";
addTask(task);
}
// function to handle click listeners
function handleClickEventListener(e) {
const target = e.target;
if (target.className === "delete") {
const taskId = target.id;
deleteTask(taskId);
return;
} else if (target.className === "custom-checkbox") {
const taskId = target.id;
toggleTask(taskId);
return;
}
}
document.addEventListener("click", handleClickEventListener);