-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
118 lines (101 loc) · 3.57 KB
/
main.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
let inputbox = document.getElementById('textbox')
let savebtn = document.getElementById('savebtn')
let deletebtn = document.getElementById('deletebtn')
let section = document.querySelector('section')
let taskArray = localStorage.getItem('tasks') ? JSON.parse(localStorage.getItem('tasks')) : [];
let emptyDiv = document.createElement('div')
let emptyTasks =()=>{
if(taskArray.length === 0){
emptyDiv.innerHTML = 'No Tasks, Add new Task!!!'
emptyDiv.style.fontSize = '2rem'
emptyDiv.style.marginTop = '2rem'
emptyDiv.style.textAlign = 'center'
section.appendChild(emptyDiv)
}else{
emptyDiv.style.display = 'none'
}
}
let saveItem = (text) => {
emptyTasks()
let card = document.createElement('div')
card.classList.add('card')
let taskText = document.createElement('div')
taskText.classList.add('taskText')
taskText.innerHTML = JSON.parse(text).new_task
card.appendChild(taskText)
section.append(card)
let boxes = document.createElement('div')
let complete = document.createElement('div')
let notStarted = document.createElement('div')
let deleteTask = document.createElement('div')
boxes.classList.add('boxes')
card.appendChild(boxes)
complete.classList.add('complete')
boxes.appendChild(complete)
notStarted.classList.add('notStarted')
boxes.appendChild(notStarted)
deleteTask.classList.add('deleteTask')
boxes.appendChild(deleteTask)
complete.addEventListener('click',()=>{
let taskobj = JSON.parse(text)
taskobj.current_status = 'complete'
let str_taskobj = JSON.stringify(taskobj)
taskArray[taskArray.indexOf(text)] = str_taskobj
localStorage.setItem('tasks',JSON.stringify(taskArray))
card.style.background = '#8ac926'
})
notStarted.addEventListener('click',()=>{
let taskobj = JSON.parse(text)
taskobj.current_status = 'not started'
let ns_str_taskobj = JSON.stringify(taskobj)
taskArray[taskArray.indexOf(text)] = ns_str_taskobj
localStorage.setItem('tasks',JSON.stringify(taskArray))
card.style.background = '#ff595e'
})
deleteTask.addEventListener('click',()=>{
section.removeChild(card)
let res = taskArray.indexOf(text)
if (res > -1) {
taskArray.splice(res, 1); // Remove the element
}
localStorage.setItem('tasks',JSON.stringify(taskArray))
location.reload()
emptyTasks()
})
let task_object = JSON.parse(text)
if(task_object.current_status === 'not started'){
card.style.background = '#ff595e'
}else if(task_object.current_status === 'complete'){
card.style.background = '#8ac926'
}
}
taskArray.forEach(saveItem);
let addTask = () => {
if (inputbox.value === '') {
alert('TaskBox should not be Empty!!')
} else {
let task = {
current_status: 'not started',
new_task: inputbox.value
}
taskArray.push(JSON.stringify(task))
localStorage.setItem('tasks', JSON.stringify(taskArray))
saveItem(JSON.stringify(task))
inputbox.value = ''
}
}
let deleteAllTasks = () => {
if(taskArray.length === 0){
alert('No Tasks to Delete')
}else{
localStorage.clear()
taskArray.length = 0;
while (section.firstChild) {
section.removeChild(section.firstChild);
}
location.reload()
}
}
savebtn.addEventListener('click', addTask)
deletebtn.addEventListener('click', deleteAllTasks)
emptyTasks()