-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
92 lines (66 loc) · 2.17 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
'use strict';
/*let banco = [
{'tarefa' : 'Estudar JS', 'status' : ''},
{'tarefa' : 'netflix', 'status' : 'checked'},
{'tarefa' : 'comer', 'status' : 'checked'}
]
*/
let banco = []
const getBanco = () => JSON.parse(localStorage.getItem ('todoList')) ?? []
const setBanco = (banco) => localStorage.setItem('todoList', JSON.stringify(banco))
const criarItem = (tarefa, status, indice) => {
const item = document.createElement('label')
item.classList.add('todo__item')
item.innerHTML =
`<input type="checkbox" ${status} data-indice = ${indice}>
<div>${tarefa}</div>
<input type="button" value="X" data-indice = ${indice}>`
document.getElementById('todoList').appendChild(item)
}
const limparTarefas = () => {
const todoList = document.getElementById('todoList')
while (todoList.firstChild){
todoList.removeChild(todoList.lastChild)
}
}
const atualizarTela = ()=>{
limparTarefas()
const banco = getBanco()
banco.forEach ((item, indice) => criarItem (item.tarefa, item.status, indice))
}
const inserirItem = (evento) => {
const tecla = evento.key
const texto = evento.target.value
if (tecla == 'Enter'){
const banco = getBanco()
banco.push({'tarefa' : texto, 'status' : '' })
setBanco(banco)
atualizarTela()
evento.target.value = '' //limpar caixa escrita
}
}
const removerItem = (indice) => {
const banco = getBanco()
banco.splice(indice, 1)
setBanco(banco)
atualizarTela()
}
const atualizarItem = (indice) => {
const banco = getBanco()
banco[indice].status = banco[indice].status === '' ? 'checked' : ''
setBanco(banco)
atualizarTela()
}
const clickItem = (evento) => {
const elemento = evento.target
if (elemento.type === 'button') {
const indice = elemento.dataset.indice
removerItem(indice)
} else if (elemento.type === 'checkbox') {
const indice = elemento.dataset.indice
atualizarItem (indice)
}
}
document.getElementById('newItem').addEventListener('keypress', inserirItem)
document.getElementById('todoList').addEventListener('click', clickItem)
atualizarTela()