-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
148 lines (116 loc) · 3.4 KB
/
engine.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
let toDoInput
let errorInfo
let addBtn
let ulList
let newToDo
let newDiv
let btn1
let btn2
let btn3
let info1
let info3
let popup
let popupInfo
let todoToEdit
let popupInput
let popupAddBtn
let popupCloseBtn
const main = () => {
prepareDOMElements()
prepareDOMEvents()
}
const prepareDOMElements = () => {
toDoInput = document.querySelector('.todo-input')
errorInfo = document.querySelector('.error-info')
addBtn = document.querySelector('.btn-add')
ulList = document.querySelector('.todolist ul')
popup = document.querySelector('.popup')
popupInfo = document.querySelector('.popup-info')
popupInput = document.querySelector('.popup-input')
popupAddBtn = document.querySelector('.accept')
popupCloseBtn = document.querySelector('.cancel')
}
const prepareDOMEvents = () => {
addBtn.addEventListener('click', addNewTask)
ulList.addEventListener('click', checkClick)
popupCloseBtn.addEventListener('click', closePopup)
popupAddBtn.addEventListener('click', changeTodoText)
toDoInput.addEventListener('keyup', enterKeyCheck)
}
const addNewTask = () =>{
if(toDoInput.value !== ''){
newToDo = document.createElement('li')
newToDo.textContent = toDoInput.value
ulList.append(newToDo)
createToolsArea(newToDo)
toDoInput.value = ''
errorInfo.textContent = ''
} else {
errorInfo.textContent = 'Wpisz treść zadania'
}
}
const createToolsArea = (newToDoArg) => {
newDiv = document.createElement('div')
btn1 = document.createElement('button')
btn2 = document.createElement('button')
btn3 = document.createElement('button')
info1 = document.createElement('i')
info3 = document.createElement('i')
newDiv.classList.add('tools')
info1.classList.add('fas')
info1.classList.add('fa-check')
info3.classList.add('fas')
info3.classList.add('fa-times')
btn1.classList.add('complete')
btn2.classList.add('edit')
btn3.classList.add('delete')
btn2.textContent = 'EDIT'
newDiv.append(btn1)
newDiv.append(btn2)
newDiv.append(btn3)
btn1.append(info1)
btn3.append(info3)
newToDoArg.append(newDiv)
}
const checkClick = e => {
if(e.target.matches('.complete')){
e.target.closest('li').classList.toggle('completed')
e.target.classList.toggle('completed')
} else if(e.target.matches('.edit')){
editTodo(e);
}else if (e.target.matches('.delete')){
deleteTodo(e)
}
}
const editTodo = (e) => {
todoToEdit = e.target.closest('li')
popupInput.value = todoToEdit.firstChild.textContent
popup.style.display = 'flex'
}
const closePopup = () => {
popup.style.display = 'none'
popupInfo.textContent = ""
}
const changeTodoText = () => {
if(popupInput.value !== '' ){
todoToEdit.firstChild.textContent = popupInput.value
popup.style.display = 'none'
popupInfo.textContent = ""
}else {
popupInfo.textContent = "Musisz wpisać treść zadania"
}
}
const deleteTodo = (e) => {
(e.target.closest('li')).remove()
const allTodos = ulList.querySelectorAll('li')
if (allTodos.length == 0 ){
errorInfo.textContent = 'Brak zadań na liście'
} else {
errorInfo.textContent = ''
}
}
const enterKeyCheck = e => {
if(e.key === "Enter")
{addNewTask()}
}
document.addEventListener('DOMContentLoaded', main)