generated from ICEI-PUC-Minas-PMV-ADS/WebApplicationProject-Template-v2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
205 lines (157 loc) · 7.14 KB
/
index.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
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
const email = localStorage.getItem('loggedInUserEmail');
if (email) {
localStorage.removeItem('loggedInUserEmail');
}
document.addEventListener('DOMContentLoaded', () => {
const quemSomosBtn = document.querySelector('#quemSomosBtn');
quemSomosBtn.addEventListener('click', () => {
window.location.href = 'pagQuemSomos.html';
});
function fetchData(url) {
return fetch(url)
.then(response => response.json())
.catch(error => {
console.error('Erro ao carregar o JSON:', error);
throw error;
});
}
function createSliderCards(artigos) {
const sliderListDiv = document.querySelector('.sliderWrapper .sliderList');
const fragment = document.createDocumentFragment();
const maxArtigos = 4;
for (let index = 0; index < maxArtigos && index < artigos.length; index++) {
const artigo = artigos[index];
const sliderItem = document.createElement('div');
sliderItem.className = 'sliderItem';
sliderItem.id = `sliderItem${artigo.id}`;
const sliderImgDiv = document.createElement('div');
sliderImgDiv.className = 'sliderImg';
const sliderImg = document.createElement('img');
sliderImg.src = artigo.capa;
sliderImg.alt = artigo.titulo;
sliderImgDiv.appendChild(sliderImg);
const sliderTitleDiv = document.createElement('div');
sliderTitleDiv.className = 'sliderTitle';
const sliderTitle = document.createElement('h6');
sliderTitle.innerHTML = artigo.titulo;
sliderTitleDiv.appendChild(sliderTitle);
sliderItem.appendChild(sliderImgDiv);
sliderItem.appendChild(sliderTitleDiv);
sliderItem.addEventListener('click', () => {
window.location.href = `pagArtigos.html?artigoIndex=${index}`;
});
fragment.appendChild(sliderItem);
};
sliderListDiv.appendChild(fragment);
}
function createPerguntasFreqTitle(perguntasFrequentes) {
const perguntasFreqTitle = document.querySelector('.perguntasMainTitle');
const H2 = document.createElement('h2');
H2.innerHTML = perguntasFrequentes.titulo;
perguntasFreqTitle.appendChild(H2);
}
function createPerguntasItem(perguntasFrequentes) {
if (perguntasFrequentes && perguntasFrequentes.perguntas) {
perguntasFrequentes.perguntas.forEach((pergunta) => {
const perguntasList = document.querySelector('.perguntasList');
const perguntasItem = document.createElement('div');
perguntasItem.className = 'perguntasItem';
const perguntasImgDiv = document.createElement('div');
perguntasImgDiv.className = 'perguntasImg';
const perguntasImg = document.createElement('img');
perguntasImg.src = pergunta.imagem;
perguntasImg.id = `pergunta${pergunta.id}`;
perguntasImg.alt = `Pergunta ${pergunta.id}: ${pergunta.pergunta}`;
perguntasImgDiv.appendChild(perguntasImg);
const perguntasTxt = document.createElement('div');
perguntasTxt.className = 'perguntasText';
const H6 = document.createElement('h6');
H6.innerHTML = pergunta.pergunta;
perguntasTxt.appendChild(H6);
perguntasItem.appendChild(perguntasImgDiv);
perguntasItem.appendChild(perguntasTxt);
perguntasItem.addEventListener('click', () => {
showModal(pergunta);
});
perguntasList.appendChild(perguntasItem);
createModal(pergunta);
});
} else {
console.error('A propriedade perguntas está indefinida ou perguntasFrequentes não é um objeto.');
}
}
function createModal(pergunta) {
const modalDiv = document.createElement('div');
modalDiv.className = 'modal fade';
modalDiv.id = `modal${pergunta.id}`;
modalDiv.tabIndex = -1;
modalDiv.setAttribute('aria-labelledby', `modalLabel${pergunta.id}`);
modalDiv.setAttribute('aria-hidden', 'true');
modalDiv.innerHTML = `
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" id="modalLabel${pergunta.id}">${pergunta.pergunta}</h6>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="modal-text${pergunta.id}">
<p>${pergunta.resposta}</p>
</div>
</div>
<div class="modal-footer">
<button type="button" class="button" data-bs-dismiss="modal">Fechar</button>
</div>
</div>
</div>
`;
document.body.appendChild(modalDiv);
const closeButtonInsideModal = modalDiv.querySelector('.modal-header .btn-close');
closeButtonInsideModal.addEventListener('click', () => {
closeModal(modalDiv);
});
const closeButtonFooter = modalDiv.querySelector('.modal-footer button');
closeButtonFooter.addEventListener('click', () => {
closeModal(modalDiv);
});
}
function closeModal(modalDiv) {
modalDiv.classList.remove('show');
modalDiv.setAttribute('aria-hidden', 'true');
modalDiv.setAttribute('style', 'display: none');
const modalBackdrop = document.querySelector('.modal-backdrop');
modalBackdrop.parentNode.removeChild(modalBackdrop);
}
function showModal(pergunta) {
const modal = new bootstrap.Modal(document.getElementById(`modal${pergunta.id}`), {});
modal.show();
}
function init() {
fetchData('../Gersons/artigos.json')
.then(data => {
const artigos = data.artigo;
createSliderCards(artigos);
});
}
function initQuestions() {
fetchData('../Gersons/perguntasFrequentes.json')
.then(data => {
const perguntasFrequentes = data.perguntasFrequentes;
createPerguntasFreqTitle(perguntasFrequentes);
createPerguntasItem(perguntasFrequentes);
});
}
function initSlider() {
const sliderList = document.querySelector(".sliderWrapper .sliderList");
const sliderButtons = document.querySelectorAll(".sliderWrapper .sliderBtn");
sliderButtons.forEach(button => {
button.addEventListener("click", () => {
const direction = button.id === "prevSlide" ? -150 : 150;
sliderList.scrollBy({ left: direction, behavior: "smooth" });
});
});
}
init();
initSlider();
initQuestions();
});