-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
275 lines (228 loc) · 8.98 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
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
const leftSide = document.querySelector('#left-side');
const rightSide = document.querySelector('#right-side');
const inputTask = document.querySelector('#taskInput');
const addBtn = document.querySelector('#add-button');
const taskContainer = document.querySelector('#task-container');
const noTaskLogo = document.querySelector('#no-task-screen');
const initial = document.querySelector('#initial');
const completed = document.querySelector('#completed');
const slash = document.querySelector('#slash');
const total = document.querySelector('#total');
// sounds
const delSound = new Audio('Assets/deleteSound.mp3');
const comSound = new Audio('Assets/completeSound.wav');
// adding event listener to add button
addBtn.addEventListener('click', Add);
// adding event listener to enter key
document.addEventListener('keydown', (value) => {
if(value.key == 'Enter' && inputTask.value.trim() != '')
{
Add();
}
})
function Add() {
if(inputTask.value.trim() != '')
{
const task = inputTask.value;
inputTask.value = '';
addBtn.blur();
addTask(task);
}
}
function addTask(task) {
noTaskLogo.style.display = 'none';
const newTask = document.createElement('li');
newTask.className = 'task';
newTask.innerHTML =
`
<input class="check" type="checkbox">
<span><img class="check-icon" src="Assets/tickIcon.svg" alt=""></span>
<p class="task-txt">${task}</p>
<button class="delete-btn"><img class="delete-icon" src="Assets/deleteIcon.svg" alt=""></button>
`;
taskContainer.appendChild(newTask);
updateRightSide();
updateProgressSection();
updateCheeringLine();
// Check the number of remaining tasks after deletion
const remainingTasks = taskContainer.querySelectorAll('.task').length;
if(remainingTasks != 0) {
saveTasks(); // jb koi task ho tabhi save karo faltu me html mat store karo
}
// accessing delete and check button
const delBtn = document.querySelector('.task:last-child .delete-btn');
const checkBtn = document.querySelector('.task:last-child .check');
// adding event listener to delete button
delBtn.addEventListener('click', (event) => {
const taskElement = event.target.closest('.task');
delSound.currentTime = 0; // Reset playback to the start
delSound.play();
taskElement.remove();
updateRightSide();
updateProgressSection();
updateCheeringLine();
// Show noTaskLogo if there are no tasks left
if (remainingTasks === 0) {
noTaskLogo.style.display = 'flex'; // Show the no task screen again
}
saveTasks();
})
// adding event listener to check button
checkBtn.addEventListener('click', (event) => {
if (event.target.checked) {
comSound.currentTime = 0;
comSound.play();
const taskTextElement = event.target.closest('.task').querySelector('.task-txt');
taskTextElement.classList.add('strikethrough');
}
else {
console.log("Checkbox is unchecked");
const taskTextElement = event.target.closest('.task').querySelector('.task-txt');
taskTextElement.classList.remove('strikethrough');
}
updateRightSide();
updateCheeringLine();
saveTasks();
})
}
function updateProgressSection() {
const totalTasks = document.querySelectorAll('.task').length; // this will give the no. of elements having class name 'task'
if(totalTasks > 0) {
rightSide.style.cssText = 'width: 175px; height: 175px; backdrop-filter: blur(9000px); border: solid rgba(255, 255, 255, 0.203) 1px; border-radius: 50%; top: 50%; left: 80%; transform: translate(-50%, -50%)';
leftSide.style.cssText = 'display: flex; opacity: 1';
}
else {
leftSide.style.cssText = 'display: none; opacity: 0';
rightSide.style.cssText = 'width: 360px; height: 160px; top: 50%; left: 50%; transform: translate(-50%, -50%)';
}
}
function updateRightSide() {
const totalTasks = document.querySelectorAll('.task').length; // this will give the no. of elements having class name 'task'
const completedTasks = document.querySelectorAll('.task .check:checked').length; // this gives the number of elements with class 'check' inside class 'task' that are checked
if(totalTasks != 0)
{
// hide 0
initial.style.display = 'none';
// set the values
completed.textContent = completedTasks;
total.textContent = totalTasks;
// display them
completed.style.display = 'block';
slash.style.display = 'block';
total.style.display = 'block';
}
else
{
completed.style.display = 'none';
slash.style.display = 'none';
total.style.display = 'none';
initial.style.display = 'block';
}
}
function updateCheeringLine() {
const totalTasks = document.querySelectorAll('.task').length;
const completedTasks = document.querySelectorAll('.task .check:checked').length;
const cheeringLine = document.querySelector('#cheering-line');
const percentageTaskCompletion = (completedTasks/totalTasks)*100;
updateProgressBar(percentageTaskCompletion);
if(percentageTaskCompletion == 100)
{
cheeringLine.textContent = `CHAMPION!`;
}
else if(percentageTaskCompletion >= 85)
{
cheeringLine.textContent = `Almost there!`;
}
else if(percentageTaskCompletion >= 50)
{
cheeringLine.textContent = `Halfway there!`;
}
else if(percentageTaskCompletion >= 30)
{
cheeringLine.textContent = `You can do it!`;
}
else if(percentageTaskCompletion >= 20)
{
cheeringLine.textContent = `Good Start!`;
}
else if(percentageTaskCompletion >= 0)
{
cheeringLine.textContent = `Let's start the journey!`;
}
}
function updateProgressBar(percentage) {
const bar = document.querySelector('#progress');
bar.style.cssText = `width: ${percentage}%`;
}
// save tasks to local storage
function saveTasks() {
const allTasks = Array.from(document.querySelectorAll('.task')).map(task => {
const Checked = task.querySelector('.check').checked; // get the checkbox if it is checked
return {
taskHTML: task.outerHTML,
checkState: Checked // save that it is checked
}
});
localStorage.setItem('allTasks', JSON.stringify(allTasks)); // save all tasks to the local storage
console.log("saved")
}
// load tasks from local storage
function loadTasks() {
console.log("loaded");
const tasks = JSON.parse(localStorage.getItem('allTasks')) || []; // Get tasks from localStorage or an empty array if it was empty
tasks.forEach(taskData => {
addSavedTasks(taskData)
});
}
function addSavedTasks(taskData) {
console.log('adding')
console.log(taskData);
noTaskLogo.style.display = 'none';
// Instead of creating a new task, we are using the passed taskHTML
taskContainer.insertAdjacentHTML('beforeend', taskData.taskHTML); // Set the inner HTML to the saved task
updateRightSide();
updateProgressSection();
updateCheeringLine();
// accessing delete and check button
const delBtn = document.querySelector('.task:last-child .delete-btn');
const checkBtn = document.querySelector('.task:last-child .check');
// Restore the checked state from taskHTML
checkBtn.checked = taskData.checkState;
// adding event listener to delete button
delBtn.addEventListener('click', (event) => {
const taskElement = event.target.closest('.task');
delSound.currentTime = 0; // Reset playback to the start
delSound.play();
taskElement.remove();
updateRightSide();
updateProgressSection();
updateCheeringLine();
saveTasks();
// Check the number of remaining tasks after deletion
const remainingTasks = taskContainer.querySelectorAll('.task').length;
// Show noTaskLogo if there are no tasks left
if (remainingTasks === 0) {
noTaskLogo.style.display = 'flex'; // Show the no task screen again
}
})
// adding event listener to check button
checkBtn.addEventListener('click', (event) => {
if (event.target.checked) {
comSound.currentTime = 0;
comSound.play();
const taskTextElement = event.target.closest('.task').querySelector('.task-txt');
taskTextElement.classList.add('strikethrough');
}
else {
console.log("Checkbox is unchecked");
const taskTextElement = event.target.closest('.task').querySelector('.task-txt');
taskTextElement.classList.remove('strikethrough');
}
updateRightSide();
updateCheeringLine();
saveTasks();
})
}
// Call loadTasks when the page loads
window.onload = loadTasks;
// localStorage.clear();