-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (128 loc) · 4.04 KB
/
script.js
File metadata and controls
156 lines (128 loc) · 4.04 KB
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
const listaTarefa = document.getElementById('lista-tarefas');
const todosLi = document.getElementsByTagName('li');
const btnUp = document.getElementById('mover-cima');
const btnDown = document.getElementById('mover-baixo');
const meuModal = new bootstrap.Modal('#alertaModal');
let qtLista;
function posicaoBotoesNav() {
const imgLapis = document.getElementById('imgLapis');
btnUp.style.transform = `translateY(${-380 + 26 * qtLista}%)`;
btnDown.style.transform = `translateY(${-270 + 26 * qtLista}%)`;
imgLapis.style.transform = `translateY(${-1 + qtLista}%)`;
imgLapis.style.height = `${qtLista * 10 + 150}px`;
}
function destacaTarefa(evento) {
const alvo = evento.target;
for (let index = 0; index < todosLi.length; index += 1) {
todosLi[index].className = todosLi[index].className
.replace('marcado', '')
.replace(' ', '');
}
alvo.className += ' marcado';
}
function marcaTarefaCompleta(evento) {
const alvo = evento.target;
if (!alvo.className.includes('completed')) {
alvo.className = 'completed';
} else {
alvo.className = '';
}
}
function addTarefa() {
const txtTarefa = document.getElementById('texto-tarefa');
const tagLi = document.createElement('li');
tagLi.addEventListener('click', destacaTarefa);
tagLi.addEventListener('dblclick', marcaTarefaCompleta);
tagLi.innerText = txtTarefa.value;
txtTarefa.value = '';
listaTarefa.appendChild(tagLi);
qtLista += 1;
posicaoBotoesNav();
}
function apagaTodasLi() {
while (todosLi.length > 0) {
listaTarefa.removeChild(todosLi[0]);
qtLista -= 1;
}
posicaoBotoesNav();
}
function apagaLiRiscadas() {
for (let index = 0; index < todosLi.length; index += 1) {
if (todosLi[index].className === 'completed') {
listaTarefa.removeChild(todosLi[index]);
qtLista -= 1;
index -= 1;
}
}
posicaoBotoesNav();
}
function exibeModalSave() {
meuModal.show();
}
function salvaLista() {
localStorage.clear();
for (let index = 0; index < todosLi.length; index += 1) {
localStorage.setItem(`txtLi${index}`, todosLi[index].innerText);
localStorage.setItem(`classeLi${index}`, todosLi[index].className);
}
meuModal.hide();
}
function recuperaLi() {
qtLista = 0;
if (localStorage.length > 0) {
for (let index = 0; index < localStorage.length / 2; index += 1) {
const tagLi = document.createElement('li');
tagLi.innerHTML = localStorage.getItem(`txtLi${index}`);
tagLi.className = localStorage.getItem(`classeLi${index}`);
tagLi.addEventListener('click', destacaTarefa);
tagLi.addEventListener('dblclick', marcaTarefaCompleta);
listaTarefa.appendChild(tagLi);
qtLista += 1;
}
}
posicaoBotoesNav();
}
function sobeLi() {
const tagTemp = document.querySelector('.marcado');
if (tagTemp) {
const tagInsercao = tagTemp.previousElementSibling;
if (tagInsercao) {
listaTarefa.insertBefore(tagTemp, tagInsercao);
}
}
}
function desceLi() {
const tagTemp = document.querySelector('.marcado');
if (tagTemp) {
const tagInsercao = tagTemp.nextElementSibling;
if (tagInsercao) {
listaTarefa.insertBefore(tagTemp, tagInsercao.nextElementSibling);
} else {
listaTarefa.appendChild(tagTemp);
}
}
}
function removeMarcado() {
const tagTemp = document.querySelector('.marcado');
if (tagTemp) {
listaTarefa.removeChild(tagTemp);
qtLista -= 1;
posicaoBotoesNav();
}
}
window.onload = function setaPagina() {
qtLista = 0;
recuperaLi();
};
document.getElementById('criar-tarefa').addEventListener('click', addTarefa);
document.getElementById('apaga-tudo').addEventListener('click', apagaTodasLi);
document
.getElementById('remover-finalizados')
.addEventListener('click', apagaLiRiscadas);
document.getElementById('salvar-tarefas').addEventListener('click', exibeModalSave);
document.getElementById('btnSave').addEventListener('click', salvaLista);
btnUp.addEventListener('click', sobeLi);
btnDown.addEventListener('click', desceLi);
document
.getElementById('remover-selecionado')
.addEventListener('click', removeMarcado);