Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
254 lines (229 loc) · 9.26 KB
/
Copy pathscript.js
File metadata and controls
254 lines (229 loc) · 9.26 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"use strict";
/* ====== Configuração (edite aqui) ====== */
const CONFIG = {
githubUser: "danilotomassoni",
contactEmail: "daniloaugustotomassoni@gmail.com",
roles: ["crio sites responsivos.", "cuido da acessibilidade.", "gosto de aprender."],
maxRepos: 30,
};
const $ = (sel, ctx = document) => ctx.querySelector(sel);
const $$ = (sel, ctx = document) => [...ctx.querySelectorAll(sel)];
/* ====== Tema claro/escuro ====== */
(function theme() {
const root = document.documentElement;
const saved = localStorage.getItem("theme");
const prefersLight = window.matchMedia("(prefers-color-scheme: light)").matches;
root.dataset.theme = saved || (prefersLight ? "light" : "dark");
$("#themeToggle").addEventListener("click", () => {
const next = root.dataset.theme === "dark" ? "light" : "dark";
root.dataset.theme = next;
localStorage.setItem("theme", next);
const meta = $('meta[name="theme-color"]');
if (meta) meta.content = next === "dark" ? "#0b1020" : "#f6f8ff";
});
})();
/* ====== Menu mobile ====== */
(function menu() {
const burger = $("#burger");
const list = $("#navList");
const close = () => {
list.classList.remove("open");
burger.setAttribute("aria-expanded", "false");
};
burger.addEventListener("click", () => {
const open = list.classList.toggle("open");
burger.setAttribute("aria-expanded", String(open));
});
$$(".nav__link").forEach((a) => a.addEventListener("click", close));
document.addEventListener("keydown", (e) => e.key === "Escape" && close());
window.addEventListener("resize", () => window.innerWidth > 760 && close());
})();
/* ====== Rolagem: header, progresso, voltar ao topo ====== */
(function scrollFx() {
const header = $("#header");
const progress = $("#progress");
const toTop = $("#toTop");
let ticking = false;
const update = () => {
const y = window.scrollY;
const max = document.documentElement.scrollHeight - window.innerHeight;
header.classList.toggle("scrolled", y > 10);
progress.style.width = (max > 0 ? (y / max) * 100 : 0) + "%";
toTop.classList.toggle("show", y > 600);
ticking = false;
};
window.addEventListener("scroll", () => {
if (!ticking) { requestAnimationFrame(update); ticking = true; }
}, { passive: true });
update();
toTop.addEventListener("click", () => window.scrollTo({ top: 0, behavior: "smooth" }));
})();
/* ====== Link ativo no menu ====== */
(function activeLink() {
const links = $$(".nav__link");
const map = new Map(links.map((l) => [l.getAttribute("href").slice(1), l]));
const io = new IntersectionObserver((entries) => {
entries.forEach((en) => {
if (en.isIntersecting) {
links.forEach((l) => l.classList.remove("active"));
map.get(en.target.id)?.classList.add("active");
}
});
}, { rootMargin: "-45% 0px -50% 0px" });
$$("main section[id]").forEach((s) => io.observe(s));
})();
/* ====== Revelar elementos + barras + contadores ====== */
(function reveal() {
const animateCount = (el) => {
const target = +el.dataset.count;
const suffix = el.dataset.suffix || "";
const dur = 1400;
const start = performance.now();
const tick = (now) => {
const p = Math.min((now - start) / dur, 1);
el.textContent = Math.round(target * (1 - Math.pow(1 - p, 3))) + suffix;
if (p < 1) requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
};
const io = new IntersectionObserver((entries) => {
entries.forEach((en) => {
if (!en.isIntersecting) return;
const el = en.target;
el.classList.add("in");
$$(".bar__fill", el).forEach((b) => (b.style.width = b.dataset.level + "%"));
$$("[data-count]", el).forEach(animateCount);
io.unobserve(el);
});
}, { threshold: 0.15 });
$$(".reveal").forEach((el) => io.observe(el));
})();
/* ====== Efeito de digitação ====== */
(function typing() {
const el = $("#typed");
if (!el) return;
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
el.textContent = CONFIG.roles[0];
return;
}
let r = 0, c = 0, deleting = false;
const step = () => {
const word = CONFIG.roles[r];
el.textContent = word.slice(0, c);
let delay = deleting ? 35 : 75;
if (!deleting && c === word.length) { deleting = true; delay = 1600; }
else if (deleting && c === 0) { deleting = false; r = (r + 1) % CONFIG.roles.length; delay = 400; }
c += deleting ? -1 : 1;
setTimeout(step, delay);
};
step();
})();
/* ====== Projetos via API do GitHub ====== */
(async function repos() {
const grid = $("#repos");
const msg = $("#reposMsg");
const filtersEl = $("#filters");
const search = $("#search");
let all = [];
let lang = "Todos";
const colors = { JavaScript: "#f1e05a", HTML: "#e34c26", CSS: "#563d7c", TypeScript: "#3178c6", Python: "#3572a5", Java: "#b07219", PHP: "#4f5d95", C: "#555555", "C++": "#f34b7d", "C#": "#178600", Go: "#00add8", Shell: "#89e051" };
const esc = (s = "") => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
const render = () => {
const q = search.value.trim().toLowerCase();
const list = all.filter((r) =>
(lang === "Todos" || r.language === lang) &&
(!q || r.name.toLowerCase().includes(q) || (r.description || "").toLowerCase().includes(q))
);
grid.innerHTML = list.map((r, i) => `
<article class="card" style="animation-delay:${Math.min(i * 40, 400)}ms">
<h3>${esc(r.name)}</h3>
<p>${esc(r.description || "Sem descrição.")}</p>
<div class="card__meta">
${r.language ? `<span><i class="dot" style="background:${colors[r.language] || "var(--primary)"}"></i>${esc(r.language)}</span>` : ""}
<span>★ ${r.stargazers_count}</span>
<span>⑂ ${r.forks_count}</span>
</div>
<div class="card__links">
<a href="${r.html_url}" target="_blank" rel="noopener">Código →</a>
${r.homepage ? `<a href="${esc(r.homepage)}" target="_blank" rel="noopener">Demo →</a>` : ""}
</div>
</article>`).join("");
msg.hidden = list.length > 0;
if (!list.length) msg.textContent = "Nenhum projeto encontrado.";
};
const buildFilters = () => {
const langs = ["Todos", ...new Set(all.map((r) => r.language).filter(Boolean))];
filtersEl.innerHTML = langs.map((l) => `<button class="chip ${l === lang ? "active" : ""}" data-lang="${esc(l)}">${esc(l)}</button>`).join("");
};
filtersEl.addEventListener("click", (e) => {
const b = e.target.closest(".chip");
if (!b) return;
lang = b.dataset.lang;
$$(".chip", filtersEl).forEach((c) => c.classList.toggle("active", c === b));
render();
});
search.addEventListener("input", render);
try {
const cached = sessionStorage.getItem("repos");
let data = cached ? JSON.parse(cached) : null;
if (!data) {
const res = await fetch(`https://api.github.com/users/${CONFIG.githubUser}/repos?sort=updated&per_page=${CONFIG.maxRepos}`);
if (!res.ok) throw new Error("HTTP " + res.status);
data = await res.json();
sessionStorage.setItem("repos", JSON.stringify(data));
}
all = data.filter((r) => !r.fork && r.name.toLowerCase() !== `${CONFIG.githubUser}.github.io`);
if (!all.length) {
grid.innerHTML = "";
msg.hidden = false;
msg.textContent = "Ainda não há repositórios públicos para exibir.";
return;
}
buildFilters();
render();
} catch (err) {
grid.innerHTML = "";
msg.hidden = false;
msg.innerHTML = `Não foi possível carregar os projetos agora. Veja direto em <a href="https://github.com/${CONFIG.githubUser}" target="_blank" rel="noopener" style="color:var(--primary)">github.com/${CONFIG.githubUser}</a>.`;
}
})();
/* ====== Formulário de contato (abre o app de e-mail) ====== */
(function form() {
const f = $("#form");
const status = $("#formStatus");
const rules = {
nome: (v) => (v.trim().length >= 2 ? "" : "Informe seu nome."),
email: (v) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) ? "" : "Informe um e-mail válido."),
msg: (v) => (v.trim().length >= 10 ? "" : "Escreva ao menos 10 caracteres."),
};
const validate = (input) => {
const err = rules[input.name](input.value);
const field = input.closest(".field");
field.classList.toggle("invalid", !!err);
$(".error", field).textContent = err;
return !err;
};
$$("input, textarea", f).forEach((i) => i.addEventListener("blur", () => validate(i)));
f.addEventListener("submit", (e) => {
e.preventDefault();
const ok = $$("input, textarea", f).map(validate).every(Boolean);
if (!ok) return;
const d = new FormData(f);
const subject = encodeURIComponent(`Contato pelo site - ${d.get("nome")}`);
const body = encodeURIComponent(`${d.get("msg")}\n\n${d.get("nome")} (${d.get("email")})`);
window.location.href = `mailto:${CONFIG.contactEmail}?subject=${subject}&body=${body}`;
status.textContent = "Abrindo seu aplicativo de e-mail...";
toast("Mensagem preparada!");
f.reset();
});
})();
/* ====== Toast ====== */
function toast(text) {
const t = $("#toast");
t.textContent = text;
t.classList.add("show");
clearTimeout(toast._t);
toast._t = setTimeout(() => t.classList.remove("show"), 2800);
}
/* ====== Ano no rodapé ====== */
$("#year").textContent = new Date().getFullYear();