-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
216 lines (190 loc) · 7.57 KB
/
Copy pathscript.js
File metadata and controls
216 lines (190 loc) · 7.57 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
const state = {
games: [],
classes: [],
turma: new URLSearchParams(window.location.search).get("turma") || "all",
search: ""
};
const elements = {
totalGames: document.querySelector("#total-games"),
gamesGrid: document.querySelector("#games-grid"),
resultCount: document.querySelector("#result-count"),
emptyState: document.querySelector("#empty-state"),
classContext: document.querySelector("#class-context"),
searchInput: document.querySelector("#search-input"),
filterButtons: document.querySelectorAll(".filter-button"),
dialog: document.querySelector("#game-dialog"),
dialogClose: document.querySelector(".dialog-close"),
dialogMedia: document.querySelector("#dialog-media"),
dialogMeta: document.querySelector("#dialog-meta"),
dialogTitle: document.querySelector("#dialog-title"),
dialogDescription: document.querySelector("#dialog-description"),
dialogTeachers: document.querySelector("#dialog-teachers"),
dialogAuthors: document.querySelector("#dialog-authors"),
dialogTech: document.querySelector("#dialog-tech"),
dialogType: document.querySelector("#dialog-type"),
dialogTheme: document.querySelector("#dialog-theme"),
dialogTags: document.querySelector("#dialog-tags"),
dialogPlay: document.querySelector("#dialog-play")
};
async function loadGames() {
try {
const [gamesResponse, classesResponse] = await Promise.all([
fetch("data/jogos.json"),
fetch("data/turmas.json")
]);
if (!gamesResponse.ok || !classesResponse.ok) {
throw new Error("Nao foi possivel carregar os dados.");
}
state.games = await gamesResponse.json();
state.classes = await classesResponse.json();
render();
} catch (error) {
elements.resultCount.textContent = "Nao foi possivel carregar a galeria.";
elements.emptyState.hidden = false;
elements.emptyState.textContent = "Confira se o arquivo data/jogos.json esta disponivel.";
}
}
function render() {
const visibleGames = state.games.filter((game) => game.status !== "rascunho");
const filteredGames = getFilteredGames(visibleGames);
elements.totalGames.textContent = `${visibleGames.length} jogos cadastrados`;
elements.gamesGrid.innerHTML = filteredGames.map(createGameCard).join("");
elements.resultCount.textContent = `${filteredGames.length} disponiveis`;
elements.emptyState.hidden = filteredGames.length > 0;
elements.classContext.innerHTML = createClassContext(filteredGames);
bindCardActions();
}
function getFilteredGames(games) {
return games.filter((game) => {
const matchesTurma = state.turma === "all" || game.turma === state.turma;
const searchText = `${game.titulo} ${game.turma} ${game.categoria} ${game.tipoProjeto || ""} ${game.tags.join(" ")}`.toLowerCase();
const matchesSearch = searchText.includes(state.search.toLowerCase());
return matchesTurma && matchesSearch;
});
}
function createClassContext(games) {
if (state.turma !== "all") {
const classInfo = getClassInfo(state.turma);
return `
<div class="context-card">
<div>
<p class="eyebrow">${classInfo.nome}</p>
<h2>${classInfo.temaTitulo}</h2>
<strong><p>${classInfo.temaDescricao}</p></strong>
<p>${classInfo.descricao}</p>
</div>
<span class="context-badge">${classInfo.proposta}</span>
</div>
`;
}
return "";
}
function getClassInfo(classId) {
return state.classes.find((classInfo) => classInfo.id === classId) || {
id: classId,
nome: classId,
temaTitulo: "Tema gerador a cadastrar",
temaDescricao: "Adicione aqui uma descricao curta do tema gerador.",
descricao: "Contexto da turma ainda nao cadastrado.",
proposta: "Proposta a cadastrar"
};
}
function createGameCard(game) {
const cover = game.capa
? `<img src="${getAssetUrl(game.capa)}" alt="Capa do jogo ${game.titulo}" loading="lazy" onerror="this.parentElement.textContent='${game.turma}'">`
: game.turma;
const playUrl = `jogar.html?jogo=${encodeURIComponent(game.id)}`;
const canPlay = game.status === "publicado" && game.linkJogo;
const playControl = canPlay
? `<a class="button primary" href="${playUrl}">Jogar</a>`
: `<span class="button disabled">Em ajuste</span>`;
return `
<article class="game-card">
<div class="game-cover">${cover}</div>
<div class="game-body">
<div class="game-meta">
<span class="pill primary">${game.turma}</span>
<span class="pill">${game.ano}</span>
<span class="pill">${game.tipoProjeto || "Projeto"}</span>
<span class="pill">${game.categoria}</span>
${game.status === "em-ajuste" ? '<span class="pill warning">Em ajuste</span>' : ""}
</div>
<h3>${game.titulo}</h3>
<p>${game.descricaoCurta}</p>
<div class="tag-list">
${game.tags.slice(0, 3).map((tag) => `<span class="tag">${tag}</span>`).join("")}
</div>
<div class="card-actions">
${playControl}
<button class="button details-button" type="button" data-game-id="${game.id}" aria-label="Ver detalhes de ${game.titulo}">i</button>
</div>
</div>
</article>
`;
}
function bindCardActions() {
document.querySelectorAll("[data-game-id]").forEach((button) => {
button.addEventListener("click", () => {
const game = state.games.find((item) => item.id === button.dataset.gameId);
openDialog(game);
});
});
}
function openDialog(game) {
if (!game) {
return;
}
elements.dialogMedia.innerHTML = game.capa
? `<img src="${getAssetUrl(game.capa)}" alt="Capa do jogo ${game.titulo}" onerror="this.parentElement.textContent='${game.turma}'">`
: game.turma;
elements.dialogMeta.textContent = `${game.turma} - ${game.ano} - ${game.categoria}`;
elements.dialogTitle.textContent = game.titulo;
elements.dialogDescription.textContent = game.descricaoCompleta || game.descricaoCurta;
elements.dialogTeachers.textContent = game.docente || "Nao informado";
elements.dialogAuthors.textContent = game.autores.join(", ");
elements.dialogTech.textContent = game.tecnologias.join(", ");
elements.dialogType.textContent = game.tipoProjeto || "Projeto";
elements.dialogTheme.textContent = getClassInfo(game.turma).temaTitulo;
elements.dialogTags.innerHTML = game.tags.map((tag) => `<span class="tag">${tag}</span>`).join("");
if (game.status === "publicado" && game.linkJogo) {
elements.dialogPlay.href = `jogar.html?jogo=${encodeURIComponent(game.id)}`;
elements.dialogPlay.textContent = "Jogar";
elements.dialogPlay.classList.remove("disabled");
} else {
elements.dialogPlay.removeAttribute("href");
elements.dialogPlay.textContent = "Jogo em ajuste";
elements.dialogPlay.classList.add("disabled");
}
elements.dialog.showModal();
}
function getAssetUrl(path) {
return path
.split("/")
.map((part) => encodeURIComponent(part))
.join("/");
}
elements.filterButtons.forEach((button) => {
if (button.dataset.filter === state.turma) {
elements.filterButtons.forEach((item) => item.classList.remove("active"));
button.classList.add("active");
}
button.addEventListener("click", () => {
elements.filterButtons.forEach((item) => item.classList.remove("active"));
button.classList.add("active");
state.turma = button.dataset.filter;
render();
});
});
elements.searchInput.addEventListener("input", (event) => {
state.search = event.target.value;
render();
});
elements.dialogClose.addEventListener("click", () => {
elements.dialog.close();
});
elements.dialog.addEventListener("click", (event) => {
if (event.target === elements.dialog) {
elements.dialog.close();
}
});
loadGames();