-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
76 lines (61 loc) · 2.34 KB
/
main.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
let btnElement = document.querySelectorAll('main#app button');
let inputElement = document.querySelectorAll('main#app input');
let listElement = document.querySelectorAll('main#app ul');
let optionElement = document.querySelector('main#app select');
let animes = JSON.parse(localStorage.getItem('animes')) || [];
function isValidInput(input) {
if (input.value.length === 0 || !input.value.trim(' ')) {
return false;
}
return true;
}
function render(idList, dataStorage) {
if (dataStorage.length === 0) {
listElement[idList].innerHTML = 'Não há animes';
} else {
listElement[idList].innerHTML = ' ';
for (data of dataStorage) {
let itemElement = document.createElement('li');
let textElement = document.createTextNode(data);
let btnDeleteElement = document.createElement('a');
let textBtnDeleteElement = document.createTextNode('x');
let pos = animes.indexOf(data);
btnDeleteElement.appendChild(textBtnDeleteElement);
btnDeleteElement.setAttribute('href', '#');
btnDeleteElement.setAttribute('onclick', `remove(${idList}, ${pos})`);
itemElement.appendChild(textElement);
itemElement.appendChild(btnDeleteElement);
listElement[idList].appendChild(itemElement);
}
}
}
function add(idList, nameDataStorage, dataStorage, idInput) {
let itemText = inputElement[idInput].value + ' - ' + optionElement.value;
dataStorage.push(itemText);
inputElement[idInput].value = '';
render(idList, dataStorage);
saveToStorage(nameDataStorage, dataStorage);
}
function remove(idDataStorage, pos) {
if (idDataStorage === 0){
animes.splice(pos, 1);
render(0, animes)
saveToStorage('animes', animes);
} else if (idDataStorage === 1) {
links.splice(pos, 1);
render(1, links)
saveToStorage('links', links);
}
}
function saveToStorage(nameDataStorage, dataStorage) {
localStorage.setItem(nameDataStorage, JSON.stringify(dataStorage));
}
render(0, animes);
btnElement[0].onclick = () => {
if (!isValidInput(inputElement[0])) {
alert("O campo não pode ser vazio ou conter apenas espaços!!");
inputElement[0].value = '';
return;
}
add(0, 'animes', animes, 0);
};