-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (145 loc) · 5.53 KB
/
Copy pathscript.js
File metadata and controls
166 lines (145 loc) · 5.53 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
// ===== Utilities =====
const $ = (sel, ctx = document) => ctx.querySelector(sel);
const $$ = (sel, ctx = document) => Array.from(ctx.querySelectorAll(sel));
// ===== Theme Toggle (with localStorage) =====
(function themeInit() {
const root = document.documentElement;
const saved = localStorage.getItem("theme");
if (saved === "light" || saved === "dark") {
root.classList.toggle("theme-light", saved === "light");
root.classList.toggle("theme-dark", saved === "dark");
}
})();
$('#themeToggle').addEventListener('click', () => {
const root = document.documentElement;
const isDark = root.classList.contains('theme-dark');
const next = isDark ? 'light' : 'dark';
root.classList.toggle('theme-dark', next === 'dark');
root.classList.toggle('theme-light', next === 'light');
localStorage.setItem('theme', next);
});
// ===== Mobile nav =====
const navToggle = $('.nav__toggle');
const navMenu = $('#navMenu');
navToggle.addEventListener('click', () => {
const open = navMenu.classList.toggle('is-open');
navToggle.setAttribute('aria-expanded', String(open));
});
// Close menu on link click (mobile)
$$('#navMenu a').forEach(a => a.addEventListener('click', () => navMenu.classList.remove('is-open')));
// ===== Typing effect =====
const phrases = [
'useful ML tools.',
'interpretable models.',
'snappy data apps.',
'clean visualizations.'
];
const typingEl = $('.typing');
let pi = 0, ci = 0, deleting = false;
function typeLoop() {
const phrase = phrases[pi];
typingEl.textContent = phrase.slice(0, ci) + (ci === phrase.length && !deleting ? '|' : '');
if (!deleting && ci < phrase.length) ci++;
else if (!deleting && ci === phrase.length) deleting = true;
else if (deleting && ci > 0) ci--;
else { deleting = false; pi = (pi + 1) % phrases.length; }
setTimeout(typeLoop, deleting ? 50 : 90);
}
if (typingEl) typeLoop();
// ===== Reveal on scroll =====
const io = new IntersectionObserver((entries) => {
entries.forEach(e => {
if (e.isIntersecting) {
e.target.classList.add('is-visible');
io.unobserve(e.target);
}
})
}, { threshold: 0.12 });
$$('.reveal').forEach(el => io.observe(el));
// ===== Project filters & modal =====
const filterButtons = $$('.filters .chip');
const cards = $$('.card');
filterButtons.forEach(btn => btn.addEventListener('click', () => {
filterButtons.forEach(b => b.classList.remove('is-active'));
btn.classList.add('is-active');
const tag = btn.dataset.filter;
cards.forEach(card => {
const tags = card.dataset.tags.split(' ');
const show = tag === 'all' || tags.includes(tag);
card.style.display = show ? '' : 'none';
});
}));
// Modal logic
const modal = $('#projectModal');
const modalTitle = $('#modalTitle');
const modalDesc = $('#modalDesc');
const modalLinks = $('#modalLinks');
let lastFocused = null;
function openModal(card) {
lastFocused = document.activeElement;
modalTitle.textContent = card.dataset.title || card.querySelector('h3').textContent;
modalDesc.textContent = card.dataset.desc || '';
modalLinks.innerHTML = '';
try {
const links = JSON.parse(card.dataset.links || '[]');
links.forEach(l => {
const a = document.createElement('a');
a.className = 'btn btn--primary';
a.href = l.href; a.textContent = l.label;
a.target = '_blank'; a.rel = 'noopener noreferrer';
modalLinks.appendChild(a);
})
} catch {}
modal.setAttribute('aria-hidden', 'false');
// focus trap
$('.modal__close').focus();
document.addEventListener('keydown', escClose);
}
function closeModal() {
modal.setAttribute('aria-hidden', 'true');
document.removeEventListener('keydown', escClose);
if (lastFocused) lastFocused.focus();
}
function escClose(e){ if (e.key === 'Escape') closeModal(); }
cards.forEach(card => {
card.addEventListener('click', () => openModal(card));
card.addEventListener('keypress', (e) => { if (e.key === 'Enter') openModal(card); });
});
$$('[data-close]').forEach(el => el.addEventListener('click', closeModal));
// ===== Contact form (client-side validate + fake submit) =====
const form = $('#contactForm');
const statusEl = $('#formStatus');
function validateInput(input) {
const errorEl = input.parentElement.querySelector('.error');
let msg = '';
if (input.validity.valueMissing) msg = 'This field is required.';
else if (input.type === 'email' && input.validity.typeMismatch) msg = 'Enter a valid email address.';
else if (input.name === 'message' && input.value.trim().length < 10) msg = 'Please write at least 10 characters.';
errorEl.textContent = msg;
return !msg;
}
$$('#contactForm input, #contactForm textarea').forEach(i => {
i.addEventListener('blur', () => validateInput(i));
i.addEventListener('input', () => validateInput(i));
});
form.addEventListener('submit', (e) => {
e.preventDefault();
const inputs = [$('#name'), $('#email'), $('#message')];
const ok = inputs.every(validateInput);
if (!ok) { statusEl.textContent = 'Fix the errors above and try again.'; statusEl.style.color = 'var(--warn)'; return; }
statusEl.textContent = 'Sending…';
statusEl.style.color = '';
setTimeout(() => {
statusEl.textContent = 'Thanks! Your message has been sent.';
statusEl.style.color = 'var(--ok)';
form.reset();
}, 600);
});
// ===== Back to top =====
const backToTop = $('#backToTop');
window.addEventListener('scroll', () => {
backToTop.classList.toggle('is-visible', window.scrollY > 600);
});
backToTop.addEventListener('click', () => window.scrollTo({ top: 0, behavior: 'smooth' }));
// ===== Footer year =====
$('#year').textContent = new Date().getFullYear();