-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
225 lines (184 loc) · 6.13 KB
/
main.js
File metadata and controls
225 lines (184 loc) · 6.13 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const form = document.getElementById("formulario");
const ulCardsList = document.getElementById("UlCardsList");
const inTitle = document.getElementById("input-title");
const inAbout = document.getElementById("input-about"); // SOBRE/ ABOUT = LINGUAGEM --- TODO
const inCategory = document.getElementById("input-category");
const inDescription = document.getElementById("input-description");
const inLink = document.getElementById("input-link");
const inSearch = document.getElementById("input-search");
const btnSearch = document.getElementById("btn-search");
const btnClearSearch = document.getElementById("btn-clear-search");
const nTotalCards = document.getElementById("n-total-cards");
const nFrontend = document.getElementById("n-frontend");
const nBackend = document.getElementById("n-backend");
const nFullStack = document.getElementById("n-fullstack");
const nSoftSkills = document.getElementById("n-softskills");
/*************************/
// CRIA VETOR DE CONTROLE
let cardsList = [];
//CONTROLA ESTADO DE EDIÇÃO DO FORM
let editingItemId = null;
// ***************** FUNÇÂO SALVAR LISTA ***************
function saveList() {
const CardsJson = JSON.stringify(cardsList);
localStorage.setItem("recipes", CardsJson);
}
// ***************** FUNÇÂO CARREGAR LISTA ***********
function recoverList() {
const CardsJson = localStorage.getItem("recipes");
if (CardsJson) {
cardsList = JSON.parse(CardsJson);
}
updateScreen(cardsList);
}
// ***************** FUNÇÂO CRIAR CARD ***************
function createItem() {
if (editingItemId) {
//SALVA ITEM SENDO EDITADO
const item = cardsList.find((item) => item.id === editingItemId);
item.title = inTitle.value;
item.about = inAbout.value;
item.category = inCategory.value;
item.description = inDescription.value;
item.link = inLink.value;
editingItemId = null;
alert("Card alterado com sucesso!");
} else {
//CRIA NOVO CARD
const newItem = {
id: Date.now(),
title: inTitle.value,
about: inAbout.value,
category: inCategory.value,
description: inDescription.value,
link: inLink.value,
};
cardsList.push(newItem);
alert("Card cadastrado com sucesso!");
}
form.reset();
updateScreen(cardsList);
saveList();
}
// FUNÇÂO EDITAR ITENS
function editItem(itemToEdit) {
const { title, about, category, description, link } = itemToEdit;
inTitle.value = title;
inAbout.value = about;
inCategory.value = category;
inDescription.value = description;
inLink.value = link;
editingItemId = itemToEdit.id;
}
function removeItem(itemId) {
const response = confirm("Deseja realmente excluir o card?");
if (response) {
cardsList = cardsList.filter((item) => item.id !== itemId);
updateScreen(cardsList);
saveList();
}
}
//CRIAR ELEMENTO HTML
function createItemElement(item) {
const li = document.createElement("li");
li.innerHTML = `
<div class="Card-QuickTip">
<h3><strong>${item.title}</strong></h3>
<p><strong>Linguagem:</strong> ${item.about}</p>
<p><strong>Categoria:</strong> ${item.category}</p>
<p><strong>Descrição:</strong>
${item.description}</p>
<iframe src="${item.link // autoplay;
}" class="iframe" width="350" height="315" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div class="div-card-btn">
<button class="remove btn btn-outline-danger btn-card">🗑️ Excluir</button>
<a href="#formulario"><button class="edit btn btn-outline-warning btn-card">📝 Editar</button><a>
${item.link
? `<button class="link btn btn-outline-primary btn-card">📹 Link</button>`
: ""
}
</div>
</div>
`;
li.querySelector("button.remove").addEventListener("click", () => {
console.log("REMOVE", item.id);
removeItem(item.id);
});
li.querySelector("button.edit").addEventListener("click", () => {
console.log("EDITA", item);
editItem(item);
});
const btnLink = li.querySelector("button.link");
if (btnLink) {
btnLink.addEventListener("click", () => {
window.open(item.link, "_blank").focus();
});
}
return li;
}
// FUNÇÂO UPDATE CATEGORY
function updateCategories() {
const totalFrontend = cardsList.reduce((acc, item) => {
if (item.category === "Front-end") {
return acc + 1;
} else {
return acc;
}
}, 0);
const totalBackend = cardsList.reduce((acc, item) => {
if (item.category === "Back-end") {
return acc + 1;
} else {
return acc;
}
}, 0);
const totalFullStack = cardsList.reduce((acc, item) => {
if (item.category === "FullStack") {
return acc + 1;
} else {
return acc;
}
}, 0);
const totalSoftSkills = cardsList.reduce((acc, item) => {
if (item.category === "SoftSkills") {
return acc + 1;
} else {
return acc;
}
}, 0);
nFrontend.innerText = totalFrontend;
nBackend.innerText = totalBackend;
nFullStack.innerText = totalFullStack;
nSoftSkills.innerText = totalSoftSkills;
nTotalCards.innerText =
totalFrontend + totalBackend + totalFullStack + totalSoftSkills;
}
// ATUALIZA HTML DA LISTA
function updateScreen(list) {
ulCardsList.innerHTML = "";
list.forEach((item) => {
const cards = createItemElement(item);
ulCardsList.appendChild(cards);
});
updateCategories();
}
form.addEventListener("submit", (event) => {
//evita o comportamento padrão do navegador
event.preventDefault();
//chama função de criação do novo Item
createItem();
});
form.addEventListener("reset", (event) => {
editingItemId = null;
});
btnSearch.addEventListener("click", () => {
const filteredList = cardsList.filter((item) =>
item.title.toLocaleLowerCase().includes(inSearch.value.toLocaleLowerCase())
);
updateScreen(filteredList);
});
btnClearSearch.addEventListener("click", () => {
inSearch.value = "";
updateScreen(cardsList);
});
recoverList();