-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (68 loc) · 1.73 KB
/
index.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
const texto = document.querySelector('input')
const btnInsert = document.querySelector('.divInsert button')
const btnDeleteAll = document.querySelector('.header button')
const ul = document.querySelector('ul')
var itensDB = []
btnDeleteAll.onclick = () => {
itensDB = []
updateDB()
}
texto.addEventListener('keypress', e => {
if (e.key == 'Enter' && texto.value != '') {
setItemDB()
}
})
btnInsert.onclick = () => {
if (texto.value != '') {
setItemDB()
}
}
function setItemDB() {
if (itensDB.length >= 20) {
alert('Limite máximo de 20 itens atingido!')
return
}
itensDB.push({ 'item': texto.value, 'status': '' })
updateDB()
}
function updateDB() {
localStorage.setItem('todolist', JSON.stringify(itensDB))
loadItens()
}
function loadItens() {
ul.innerHTML = "";
itensDB = JSON.parse(localStorage.getItem('todolist')) ?? []
itensDB.forEach((item, i) => {
insertItemTela(item.item, item.status, i)
})
}
function insertItemTela(text, status, i) {
const li = document.createElement('li')
li.innerHTML = `
<div class="divLi">
<input type="checkbox" ${status} data-i=${i} onchange="done(this, ${i});" />
<span data-si=${i}>${text}</span>
<button onclick="removeItem(${i})" data-i=${i}><i class='bx bx-trash'></i></button>
</div>
`
ul.appendChild(li)
if (status) {
document.querySelector(`[data-si="${i}"]`).classList.add('line-through')
} else {
document.querySelector(`[data-si="${i}"]`).classList.remove('line-through')
}
texto.value = ''
}
function done(chk, i) {
if (chk.checked) {
itensDB[i].status = 'checked'
} else {
itensDB[i].status = ''
}
updateDB()
}
function removeItem(i) {
itensDB.splice(i, 1)
updateDB()
}
loadItens()