-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
256 lines (186 loc) · 7.31 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
//grab all element into variable where functionality need to be ddded
const inputTask = document.getElementById("input");
const addButton = document.getElementById("addButton");
const taskList = document.getElementById("taskList");
//whenevr the site loads it loads task from local storage
document.addEventListener("DOMContentLoaded", loadTasks());
inputTask.focus()
//save task to local storage
function saveTasks(task){
//calling getTask() function into tasks variable where we're getting tasks from local storage
const tasks = getTasks()
//pushing argumneted task into tasks variable via getTask
tasks.push(task);
//setting item into local storage in form of JSON
localStorage.setItem('tasks',JSON.stringify(tasks))
}
//get task from local storage
function getTasks(){
//getting task from local storage
const tasks = localStorage.getItem('tasks')
//clearing the input field. Making it blank
inputTask.value='';
//returning json tasks if Tasks has a value
return tasks ? JSON.parse(tasks) : [];
}
//load task function for loading task from local storage
function loadTasks() {
//getting all the tasks before loading into tasks variable
const tasks = getTasks();
// loooping each task and styling it as per the listelement after loading
tasks.forEach(task => {
//creating the list item
const listItem = document.createElement("li");
//creating the current time event
const currentTime = new Date().toLocaleDateString();
// inputText / inputTask is not available inside this function so the task element is used where all the tasks are loaded
listItem.innerHTML = `<span> • ${task}</span><span class="time">${currentTime}</span>`;
//adding event listener to the list item to check the completion and pending activity
listItem.addEventListener("click", function() {
if (listItem.style.textDecoration === "line-through") {
//if the list item is pending
listItem.style.textDecoration = "none";
listItem.style.color = "black"
} else {
//if the list item is completed
listItem.style.textDecoration = "line-through";
listItem.style.color = "black"
}
});
//appending the child element we created the innerhtml of the list item to the task list
taskList.appendChild(listItem);
});
}
//adding task function
function addTask(){
//grabbing the entered value into a variable
const inputText = inputTask.value.trim();
//checking the input value existance and alerting the user
if (inputText){
//creating the list item
const listItem = document.createElement("li");
//creating the current time event
const currentTime = new Date().toLocaleTimeString();
//creating innerhtml of element created in name of listItem to append on the task list
listItem.innerHTML = `<span> • ${inputText}</span><span class="time">${currentTime}</span>`;
//adding event listener to the list item to check the completion and pending activity
listItem.addEventListener("click", function() {
if (listItem.style.textDecoration === "line-through") {
//if the list item is pending
listItem.style.textDecoration = "none";
listItem.style.color = "black"
} else {
//if the list item is completed
listItem.style.textDecoration = "line-through";
listItem.style.color = "#black"
}
});
//appending the child element we created the innerhtml of the list item to the task list
taskList.appendChild(listItem);
//saving task intro local storage
saveTasks(inputText);
//scrolls to the bottom
scrollBottom()
//clearing the input field. Making it blank
inputTask.value='';
//if the input field is empty
} else {
//alerting the user
alert("Please add a Task first");
}
}
//function for scrolling the task list to the bottom
function scrollBottom(){
taskList.scrollTop = taskList.scrollHeight;
}
//reset function
function reset(){
if (taskList.childElementCount === 0){
//alerts user when no task is there
alert("Please enter a Task first before resetting")
}else{
//resets all tasks
taskList.innerHTML = "";
// resets all tasks in storage
localStorage.removeItem('tasks')
}
}
//KEY EVENTS
//adding event listener to the add button as we click on enter key the addTask() function executes
inputTask.addEventListener('keypress',function(e){
if (e.key === 'Enter') {
addTask();
}
})
//whenever we click on insert key it focuses on the input
document.addEventListener("keydown", (e) => {
if (e.key === "Insert" || "TAB" || "Enter" || "Shift") {
inputTask.focus(); // Set focus to the input field
}
});
//when we click on delete-key it clears all the tasks
document.addEventListener('keydown',function(e){
if (e.key === 'Delete') {
reset();
}
})
//popup toggles
//opens sidebar toggle
document.getElementById("hamburger").addEventListener("click", () => {
const sidebar = document.getElementById("sidebar");
if (sidebar.style.display === "flex") {
sidebar.style.display = "none";
} else {
sidebar.style.display = "flex";
}
});
//close sidebar toggles
document.getElementById("closeSidebar").addEventListener("click", () => {
document.getElementById("sidebar").style.display = "none";
});
//click anywhere to close the sidebar
document.getElementById("container").addEventListener("click", () => {
if (document.getElementById("sidebar").style.display === "flex"){
document.getElementById("sidebar").style.display = "none";
}else{
document.getElementById("sidebar").style.display = "none";
}
});
//opens Guide toggle
document.getElementById("guideButton").addEventListener("click", () => {
const guidepopup = document.getElementById("guidepopup");
if (guidepopup.style.display === "block") {
guidepopup.style.display = "none";
} else {
guidepopup.style.display = "block";
}
});
//close Guide toggles
document.getElementById("guideCloseButton").addEventListener("click", () => {
document.getElementById("guidepopup").style.display = "none";
});
//opens Guide toggle
document.getElementById("guideButton").addEventListener("touchstart", () => {
if (guidepopup.style.display === "block") {
guidepopup.style.display = "none";
} else {
guidepopup.style.display = "block";
}
});
//close Guide toggles for mobile
document.getElementById("guideCloseButton").addEventListener("touchstart", () => {
document.getElementById("guidepopup").style.display = "none";
});
//opens Creator toggle
document.getElementById("creatorButton").addEventListener("click", () => {
const creatorpopup = document.getElementById("creatorpopup");
if (creatorpopup.style.display === "block") {
creatorpopup.style.display = "none";
} else {
creatorpopup.style.display = "block";
}
});
//close Creator toggles
document.getElementById("creatorCloseButton").addEventListener("click", () => {
document.getElementById("creatorpopup").style.display = "none";
});