forked from vaarunh/lets-do-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
344 lines (269 loc) · 9.82 KB
/
app.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
let todo = localStorage.getItem('todo') ? JSON.parse(localStorage.getItem('todo')) : new Array();
let done = localStorage.getItem('done') ? JSON.parse(localStorage.getItem('done')) : new Array();
let imp = localStorage.getItem('imp') ? JSON.parse(localStorage.getItem('imp')) : new Array();
window.onload = () => {
const form1 = document.querySelector("#addForm");
let items = document.getElementById("items");
let submit = document.getElementById("submit");
let completedItems = document.getElementById("completedItems");
let editItem = null;
form1.addEventListener("submit", addItem);
items.addEventListener("click", handleToDoButtonClick);
completedItems.addEventListener("click", handleToDoButtonClickForCompleted);
// restore in-progress
if(todo.length !== 0){
for(let i=0; i<todo.length; i++){
item = todo[i];
let li = createMainLiTaskElement(item);
li.appendChild(createTaskButtons());
items.appendChild(li);
// handle imp task
if(imp.includes(item)){
li.classList.add('important');
}
}
}
// restore done
if(done.length !== 0){
for(let i=0; i<done.length; i++){
item = done[i];
let li = createMainLiTaskElement(item)
li.appendChild(createButtonForCompletedTasks());
completedItems.appendChild(li);
}
}
};
function operationCompleted(msg){
document.getElementById("lblsuccess").innerHTML = msg;
document.getElementById("lblsuccess")
.style.display = "block";
setTimeout(function() {
document.getElementById("lblsuccess")
.style.display = "none";
}, 3000);
}
function addItem(e) {
e.preventDefault();
let newItem = document.getElementById("item").value;
if (submit.value != "Add Task") {
console.log("Hello");
if (newItem.trim() == "" || newItem.trim() == null) {
operationCompleted("Please enter some data!")
}
else{
editItem.target.parentNode.parentNode.childNodes[0].data =
document.getElementById("item").value;
submit.value = "Add Task";
document.getElementById("item").value = "";
document.getElementById("lblsuccess").innerHTML =
"Text edited successfully";
document.getElementById("lblsuccess").style.display = "block";
setTimeout(function () {
document.getElementById("lblsuccess").style.display = "none";
}, 3000);
return false;
}
}
if (newItem.trim() == "" || newItem.trim() == null) {
document.getElementById("lblsuccess").innerHTML = "Please enter some data!";
document.getElementById("lblsuccess").style.display = "block";
setTimeout(function () {
document.getElementById("lblsuccess").style.display = "none";
}, 3000);
return false;
} else document.getElementById("item").value = "";
let li = createMainLiTaskElement(newItem);
li.appendChild(createTaskButtons());
items.appendChild(li);
addlocaltodo(newItem);
}
function handleToDoButtonClick(e) {
e.preventDefault();
// Handle delete todo button click
if (e.target.classList.contains("delete")) {
if (confirm("Are you Sure?")) {
let li = e.target.parentNode.parentNode;
items.removeChild(li);
removelocaltodo(li.childNodes[0].data);
document.getElementById("lblsuccess").innerHTML =
"Text deleted successfully";
document.getElementById("lblsuccess").style.display = "block";
setTimeout(function () {
document.getElementById("lblsuccess").style.display = "none";
}, 3000);
}
}
// Handle complete todo button click
if (e.target.classList.contains("comp")) {
if (confirm("Are you Sure?")) {
let li = e.target.parentNode.parentNode;
li.removeChild(li.childNodes[1]);
completeTask(li.innerText);
items.removeChild(li);
removelocaltodo(li.childNodes[0].data);
addlocaldone(li.childNodes[0].data)
document.getElementById("lblsuccess").innerHTML =
"Successfully Marked as Completed!";
document.getElementById("lblsuccess").style.display = "block";
setTimeout(function () {
document.getElementById("lblsuccess").style.display = "none";
}, 3000);
}
}
// Handle edit task button click
if (e.target.classList.contains("edit")) {
document.getElementById("item").value =
e.target.parentNode.parentNode.childNodes[0].data;
submit.value = "EDIT";
editItem = e;
}
// Handle important todo button click
if(e.target.classList.contains("imp")) {
let button = e.target;
let li = button.parentNode.parentNode;
if(li.classList.contains("important")) {
button.innerText = "Mark as important";
li.classList.remove("important");
removelocalimp(li.childNodes[0].data);
} else {
button.innerText = "Mark as normal";
li.classList.add("important");
items.insertBefore(li, items.childNodes[0]);
addlocalimp(li.childNodes[0].data);
}
}
}
function toggleButton(ref, btnID) {
document.getElementById(btnID).disabled = false;
}
function completeTask(innerText){
let li = createMainLiTaskElement(innerText)
li.appendChild(createButtonForCompletedTasks());
completedItems.appendChild(li);
}
function createTaskButtons() {
let actions = document.createElement("div");
actions.className = "actions flex-grow-0 align-self-start ml-2";
let deleteButton = document.createElement("button");
deleteButton.className = "btn-danger btn btn-sm mr-2 mb-2 delete";
deleteButton.appendChild(document.createTextNode("Delete Task "));
//icon for delete button
var img = document.createElement('img');
img.src = 'icons/delete.png';
deleteButton.appendChild(img);
let compButton = document.createElement("button");
compButton.className = "btn-success btn btn-sm mr-2 mb-2 comp";
compButton.appendChild(document.createTextNode("Mark as Complete "));
//icon for mark as done button
var doneImg = document.createElement('img');
doneImg.src = 'icons/done.png';
compButton.appendChild(doneImg);
let editButton = document.createElement("button");
editButton.className = "btn-warning btn btn-sm mr-2 mb-2 edit";
editButton.appendChild(document.createTextNode("Edit Task "));
//icon for edit button
var editImg = document.createElement('img');
editImg.src = 'icons/edit.png';
editButton.appendChild(editImg);
//Adding important button
let importantBtn = document.createElement("button");
importantBtn.className = "btn btn-warning btn-sm mr-2 mb-2 imp";
importantBtn.appendChild(document.createTextNode("Mark as important "));
//icon for mark as important button
var impImg = document.createElement('img');
impImg.src = 'icons/important.png';
importantBtn.appendChild(impImg);
actions.appendChild(deleteButton);
actions.appendChild(editButton);
actions.appendChild(compButton);
actions.appendChild(importantBtn);
return actions;
}
function handleToDoButtonClickForCompleted(e){
e.preventDefault();
//Handle inProgress task button click
if(e.target.classList.contains("inpro")) {
if (confirm("Are you Sure?")) {
let completedTaskLi = e.target.parentNode.parentNode;
completedTaskLi.removeChild(completedTaskLi.childNodes[1])
let li = createMainLiTaskElement(completedTaskLi.innerText)
li.appendChild(createTaskButtons());
items.appendChild(li);
completedItems.removeChild(completedTaskLi);
removelocaldone(li.childNodes[0].data);
addlocaltodo(li.childNodes[0].data);
operationCompleted("Successfully Marked as In-Progress!")
}
}
//Handle Remove Item After Completion click
if(e.target.classList.contains("remove")) {
if (confirm("Are you Sure? This Item will be Permanently Removed.")) {
let completedTaskLi = e.target.parentNode.parentNode;
completedTaskLi.removeChild(completedTaskLi.childNodes[1])
completedItems.removeChild(completedTaskLi);
removelocaldone(completedTaskLi.innerText);
operationCompleted("Item Successfully Removed from List.")
}
}
}
function createButtonForCompletedTasks(){
let actions = document.createElement("div");
actions.className = "actions flex-grow-0 align-self-start ml-2";
let moveToInprogressButton = document.createElement("button");
moveToInprogressButton.className = "btn-success btn btn-sm mr-2 mb-2 inpro";
moveToInprogressButton.appendChild(document.createTextNode("Mark as In-Progress"));
actions.appendChild(moveToInprogressButton);
let removeCompletedButton = document.createElement("button");
removeCompletedButton.className = "btn-danger btn btn-sm mr-2 mb-2 remove";
removeCompletedButton.appendChild(document.createTextNode("Remove"));
actions.appendChild(removeCompletedButton);
return actions;
}
function createMainLiTaskElement(taskText) {
let li = document.createElement("li");
li.className =
"list-group-item d-flex flex-row align-items-center justify-content-between flex-sm-wrap";
li.appendChild(document.createTextNode(taskText));
return li;
}
function addlocaltodo(task){
todo.push(task);
localStorage.setItem('todo', JSON.stringify(todo));
}
function removelocaltodo(task){
// console.log(task);
for(let i=0; i<todo.length; i++){
if(todo[i] === task){
todo.splice(i,1);
break;
}
}
localStorage.setItem('todo', JSON.stringify(todo));
}
function addlocaldone(task){
done.push(task);
localStorage.setItem('done', JSON.stringify(done));
}
function removelocaldone(task){
// console.log(task);
for(let i=0; i<done.length; i++){
if(done[i] === task){
done.splice(i,1);
break;
}
}
localStorage.setItem('done', JSON.stringify(done));
}
function addlocalimp(task){
imp.push(task);
localStorage.setItem('imp', JSON.stringify(imp));
}
function removelocalimp(task){
for(let i=0; i< imp.length; i++){
if(imp[i] === task){
imp.splice(i, 1);
break;
}
}
localStorage.setItem('imp', JSON.stringify(imp));
}