-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (136 loc) · 4.45 KB
/
script.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
/* eslint-disable complexity */
const botaoCriarTarefa = document.getElementById('criar-tarefa');
const input = document.getElementById('texto-tarefa');
const listaDeTarefa = document.getElementById('lista-tarefas');
const tarefa = document.getElementsByTagName('li');
const botaoApagaTudo = document.getElementById('apaga-tudo');
const botaoApagaFinalizados = document.getElementById('remover-finalizados');
const botaoSalvarLista = document.getElementById('salvar-tarefas');
const botaoRemoveSelecionado = document.getElementById('remover-selecionado');
const botaoMoverCima = document.getElementById('mover-cima');
const botaoMoverBaixo = document.getElementById('mover-baixo');
const arrayDeTarefas = [];
// Requisito 7 e 8
function selecionarTarefa(event) {
// eslint-disable-next-line no-restricted-syntax
for (const valor of tarefa) {
valor.style.removeProperty('background-color');
}
event.target.style.backgroundColor = 'rgb(128,128,128)';
// eslint-disable-next-line no-restricted-syntax
for (const valor of arrayDeTarefas) {
if (valor.tarefa === event.target.innerText) {
valor.selecionada = true;
} else {
valor.selecionada = false;
}
}
}
// Requisito 9
function riscarTarefa(event) {
if (event.target.className === '') {
event.target.className = 'completed';
} else if (event.target.className === 'completed') {
event.target.classList.remove('completed');
}
// eslint-disable-next-line no-restricted-syntax
for (const valor of arrayDeTarefas) {
if (valor.tarefa === event.target.innerText) {
if (valor.riscada === true) {
valor.riscada = false;
} else {
valor.riscada = true;
}
}
}
}
// Requisito 5 e 6
function inserirTarefa() {
const linha = document.createElement('li');
linha.innerText = input.value;
arrayDeTarefas.push({
tarefa: input.value,
selecionada: false,
riscada: false,
});
linha.addEventListener('click', selecionarTarefa);
linha.addEventListener('dblclick', riscarTarefa);
listaDeTarefa.appendChild(linha);
input.value = '';
}
botaoCriarTarefa.addEventListener('click', inserirTarefa);
// Requisito 10
function apagarTudo() {
for (let i = listaDeTarefa.children.length; i >= 0; i -= 1) {
if (listaDeTarefa.children.length > 0) {
listaDeTarefa.firstElementChild.remove();
} else {
break;
}
}
}
botaoApagaTudo.addEventListener('click', apagarTudo);
// Requisito 11
function removerFinalizados() {
for (let i = tarefa.length - 1; i >= 0; i -= 1) {
if (tarefa[i].className === 'completed') {
listaDeTarefa.removeChild(tarefa[i]);
}
}
}
botaoApagaFinalizados.addEventListener('click', removerFinalizados);
// Requisito 12
function salvarLista() {
localStorage.setItem('tarefa', JSON.stringify(arrayDeTarefas));
}
botaoSalvarLista.addEventListener('click', salvarLista);
function recuperarLista() {
const arraySalvo = JSON.parse(localStorage.getItem('tarefa'));
for (let i = 0; i < arraySalvo.length; i += 1) {
const linha = document.createElement('li');
linha.innerText = arraySalvo[i].tarefa;
arrayDeTarefas.push(arraySalvo[i]);
linha.addEventListener('click', selecionarTarefa);
linha.addEventListener('dblclick', riscarTarefa);
if (arraySalvo[i].selecionada) {
linha.style.backgroundColor = 'rgb(128,128,128)';
}
if (arraySalvo[i].riscada) {
linha.classList.add('completed');
}
listaDeTarefa.appendChild(linha);
}
}
window.onload = function onLoad() {
if (localStorage.length > 0) {
recuperarLista();
}
};
// Requisito 13
function moverParaCima() {
for (let i = 0; i < tarefa.length; i += 1) {
if (tarefa[i].style.backgroundColor === 'rgb(128, 128, 128)' && i > 0) {
listaDeTarefa.insertBefore(tarefa[i], tarefa[i].previousSibling);
}
}
}
function moverParaBaixo() {
for (let i = 0; i < tarefa.length; i += 1) {
if (tarefa[i].style.backgroundColor === 'rgb(128, 128, 128)' && i < tarefa.length) {
const tarefaReferencia = tarefa[i + 2];
listaDeTarefa.insertBefore(tarefa[i], tarefaReferencia);
break;
}
}
}
botaoMoverCima.addEventListener('click', moverParaCima);
botaoMoverBaixo.addEventListener('click', moverParaBaixo);
// Requisito 14
function removerSelecionado() {
for (let i = 0; i < tarefa.length; i += 1) {
if (tarefa[i].style.backgroundColor === 'rgb(128, 128, 128)') {
listaDeTarefa.removeChild(tarefa[i]);
}
}
}
botaoRemoveSelecionado.addEventListener('click', removerSelecionado);