-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
220 lines (185 loc) · 6.69 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
const backgroundColor = document.querySelector('div.div > .img');
const backgroundColor2 = document.querySelector('div.div > .img1')
const ulBackground = document.querySelector('div.div1');
const ul = document.querySelector('ul');
const select = document.querySelectorAll('div.secDiv')
let inputValue = document.getElementById('myInput');
let inputFocus = document.getElementById('myInput').focus();
const form = document.querySelector('#inputs')
const body = document.querySelector('body');
const nav = document.querySelector('nav');
const firstIcon = document.querySelector('.img');
const filterOption = document.querySelector('div.div1 > div.div2')
const secIcon = document.querySelector('.img1');
let span = document.querySelector('span.span')
backgroundColor.addEventListener('click', color1)
backgroundColor2.addEventListener('click', color2)
form.addEventListener('submit', addTodo)
ul.addEventListener('click', deleteTodo)
ul.addEventListener('click', iconChange)
function addTodo(e){
e.preventDefault()
let res = inputValue.value;
if(res !== ''){
let span1 = document.createElement('span')
let span2 = document.createElement('span')
const img = document.createElement('img');
img.classList.add('check')
img.src = 'images/icon-cross.svg'
let li = document.createElement('li')
let iTag = document.createElement('i')
li.setAttribute('draggable', 'true')
iTag.classList.add('far', 'fa-circle')
iTag.setAttribute('data', 'nil')
span1.innerHTML = `${res}`;
let spine = document.createElement('span')
spine.append(iTag, span1)
span2.appendChild(img)
li.append(spine, span2)
let lis = document.querySelectorAll('li')
let listNum = lis.length;
if(listNum === 0){
span.textContent = 1;
}else{
span.textContent = listNum + 1;
}
ul.appendChild(li);
}else{
alert('You aint inputted a todo yet')
}
inputValue.value = ''
}
function color2(){
body.classList.remove('body');
nav.classList.remove('nav');
firstIcon.classList.remove('display');
secIcon.classList.add('display')
inputValue.style.background = '#fff'
inputValue.style.borderColor = '#fff'
ulBackground.style.background = '#fff'
ulBackground.style.borderColor = '#fff'
ulBackground.style.color = '#333'
inputFocus.style.background = '#fff'
inputFocus.style.color = '#333'
select.style.borderColor = '#fff'
}
function color1(){
body.classList.add('body');
nav.classList.add('nav');
firstIcon.classList.add('display');
secIcon.classList.remove('display')
inputValue.style.background = '#333'
inputValue.style.borderColor = '#333'
ulBackground.style.background = '#333'
ulBackground.style.borderColor = '#333'
ulBackground.style.color = '#fff'
inputFocus.style.background = '#333'
inputFocus.style.color = '#fff'
select.style.borderColor = '#333'
}
function deleteTodo(e){
let t = e.target;
if(t.classList[0] === 'check'){
let parentElements = t.parentElement.parentElement
parentElements.remove()
let lis = document.querySelectorAll('li')
let listNum = lis.length + 1;
if(listNum === 0){
span.textContent = 1;
}else{
span.textContent = listNum - 1;
}
}
}
const check = e => {
let element = e.target.parentElement.parentElement
const draggableElements = element;
draggableElements.addEventListener('dragstart', () => {
draggableElements.setAttribute('id', 'dragging')
})
draggableElements.addEventListener('dragend', () => {
draggableElements.removeAttribute('id', 'dragging')
})
}
ul.addEventListener('click', check)
const dragOverEvent = e => {
e.preventDefault()
const afterElement = getDragAfterElement(ul, e.clientY)
const draggable = document.querySelector('#dragging');
if (afterElement == null) {
ul.appendChild(draggable)
} else {
ul.insertBefore(draggable, afterElement)
}
}
ul.addEventListener('dragover', dragOverEvent)
function getDragAfterElement(ul, y) {
const draggableElements = [...ul.querySelectorAll('li:not(.dragging)')]
return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect()
const offset = y - box.top - box.height / 2
if (offset < 0 && offset > closest.offset) {
return { offset: offset, element: child }
} else {
return closest
}
}, { offset: Number.NEGATIVE_INFINITY }).element
}
function iconChange(e){
let r = e.target;
let textCont = r.parentElement;
let comp = textCont.parentElement;
let parentUl = comp.parentElement;
if(r.hasAttribute('data')){
r.removeAttribute('data', 'nil');
r.classList.remove('fa-circle');
r.classList.add('fa-check-circle');
textCont.style.textDecoration = 'line-through'
comp.classList.add('completed')
filterOption.addEventListener('click', filterTodo)
const secondSelect = document.querySelectorAll('div.secDiv')
secondSelect.forEach(second => {
second.addEventListener('click', e => {
let a = e.target;
if (a.classList.contains('completed')) {
comp.style.backgroundColor = 'rgb(125, 149, 208)'
}
})
})
function filterTodo(p) {
let x = p.target;
if(x.classList.contains('completed')){
comp.style.backgroundColor = 'rgb(125, 149, 208)'
}
if (x.classList.contains('clearTodo')) {
if(comp.classList[0] === 'completed'){
comp.remove()
let lis = document.querySelectorAll('li')
let listNum = lis.length;
if(listNum === 0){
span.textContent = 0;
}else{
span.textContent = listNum --;
}
}else{
alert('Not done with any todo yet, get back to work please!!!');
}
}
}
} else{
r.setAttribute('data', 'nil')
r.classList.add('fa-circle');
r.classList.remove('fa-check-circle')
textCont.style.textDecoration = 'none'
textCont.removeAttribute('id')
comp.removeAttribute('class')
filterOption.addEventListener('click', filterTodo)
function filterTodo(p) {
let x = p.target;
if(x.classList[0] === 'completed'){
comp.style.backgroundColor = 'white'
}
}
comp.style.backgroundColor = 'white'
}
}