-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
118 lines (91 loc) · 3.04 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
const $inputName = document.querySelector('#inputName');
const $btnAddTask = document.querySelector('#btnAdd');
const $listView = document.querySelector('#listView');
const $checkTasks = document.getElementsByClassName('check-task');
const $wrapperAlert = document.querySelector('.wrapper-alert');
const $btnDesfTaskAlert = document.querySelector('.btn-desf-task-alert');
const $btnCloseAlert = document.querySelector('.btn-close-alert');
const Data = JSON.parse(localStorage.getItem('Data')) || [{
id: Date.now(),
name: 'Seja Bem vindo!',
checked: false
}];
//salvar no localStorage
function saveLocal() {
let dataStrJson = JSON.stringify(Data);
localStorage.setItem('Data', dataStrJson);
}
$btnAddTask.addEventListener('click', saveTask);
//salva na array
function saveTask() {
if ($inputName.value != '') {
Data.push({
id: Date.now(),
name: $inputName.value,
checked: false,
});
$inputName.value = '';
saveLocal();
setTaskHTML();
}
}
//insere html
function setTaskHTML() {
$listView.innerHTML = '';
Data.forEach(task => {
const {id,name,checked} = task;
let taskDiv = document.createElement('div');
taskDiv.setAttribute('class', 'taskList');
const controllsTasks = document.createElement('div');
controllsTasks.setAttribute('class', 'controlls-task');
const checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
checkbox.setAttribute('class', 'check-task');
checkbox.checked = checked;
checkbox.addEventListener('click', () => { checkTask(id) });
const btnDelTask = document.createElement('button');
btnDelTask.setAttribute('class', 'btn-del-task');
btnDelTask.addEventListener('click', () => { removeTask(id) });
const nameTask = document.createElement('p');
nameTask.setAttribute('class', 'name-task');
nameTask.innerText = name;
controllsTasks.append(checkbox, btnDelTask);
taskDiv.append(controllsTasks, nameTask);
$listView.append(taskDiv);
});
}
function removeTask(id) {
Data.forEach((task, index) => {
if (task.id === id) {
DataDeleteds = Data.splice(index, 1);
// wrapper-alert
$wrapperAlert.classList.add('open');
setTimeout(() => {
$wrapperAlert.classList.remove('open');
}, 10000);
}
});
saveLocal();
setTaskHTML();
}
function checkTask(id) {
Data.forEach(task => {
if (task.id == id) {
task.checked = !task.checked;
saveLocal();
}
});
}
//armazena a tarefa apagada
let DataDeleteds = new Array;
// wrapper-alert
$btnCloseAlert.addEventListener('click', () => {
$wrapperAlert.classList.remove('open');
});
$btnDesfTaskAlert.addEventListener('click', () => {
$wrapperAlert.classList.remove('open');
Data.push(DataDeleteds[0]);
saveLocal();
setTaskHTML();
});
window.onload = () => setTaskHTML();