Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
190 lines (153 loc) · 5.16 KB
/
Copy pathscript.js
File metadata and controls
190 lines (153 loc) · 5.16 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
// Do not open this page via file://. Use Live Server in VS Code or run it on localhost.
// EmailJS setup instructions:
// 1. Use Live Server in VS Code or serve the site from localhost or a deployed domain.
// 2. Replace YOUR_SERVICE_ID, YOUR_TEMPLATE_ID, and YOUR_PUBLIC_KEY with values from the EmailJS dashboard.
// 3. In your EmailJS template, make sure the variables match exactly: {{name}}, {{email}}, and {{message}}.
const navToggle = document.querySelector('.nav-toggle');
const navLinks = document.querySelector('.nav-links');
if (navToggle && navLinks) {
navToggle.addEventListener('click', () => {
const isOpen = navLinks.classList.toggle('open');
navToggle.setAttribute('aria-expanded', String(isOpen));
});
navLinks.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', () => {
navLinks.classList.remove('open');
navToggle.setAttribute('aria-expanded', 'false');
});
});
}
const revealItems = document.querySelectorAll('.section, .project-card, .skill-card, .timeline-item, .edu-card');
revealItems.forEach((item) => item.classList.add('reveal'));
document.querySelectorAll('[data-placeholder="true"]').forEach((link) => {
link.addEventListener('click', (event) => event.preventDefault());
});
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
observer.unobserve(entry.target);
}
});
},
{ threshold: 0.15 }
);
revealItems.forEach((item) => observer.observe(item));
const EMAILJS_CONFIG = {
serviceId: 'service_xppfscw',
templateId: 'template_kw7a97p',
publicKey: '8QNgTmWWXFyXROLyl'
};
const contactForm = document.querySelector('#contact-form');
const nameInput = document.querySelector('#name');
const emailInput = document.querySelector('#email');
const messageInput = document.querySelector('#message');
const submitButton = document.querySelector('#contact-submit');
const formAlert = document.querySelector('#form-alert');
function initializeEmailJS() {
if (!window.emailjs || window.location.protocol === 'file:' || !isConfiguredForEmailJS()) {
return false;
}
emailjs.init(EMAILJS_CONFIG.publicKey);
return true;
}
function setFieldValidity(field, isValid) {
if (field) {
field.setAttribute('aria-invalid', String(!isValid));
}
}
function resetFieldValidity() {
[nameInput, emailInput, messageInput].forEach((field) => setFieldValidity(field, true));
}
function showFormAlert(message, type) {
if (!formAlert) {
return;
}
formAlert.textContent = message;
formAlert.className = 'form-alert';
if (type) {
formAlert.classList.add(`is-${type}`, 'is-visible');
}
}
function setSubmittingState(isSubmitting) {
if (!submitButton) {
return;
}
submitButton.disabled = isSubmitting;
submitButton.textContent = isSubmitting ? 'Sending...' : 'Send Message';
}
function isConfiguredForEmailJS() {
return !Object.values(EMAILJS_CONFIG).some((value) => value.startsWith('YOUR_'));
}
function validateForm() {
const name = nameInput.value.trim();
const email = emailInput.value.trim();
const message = messageInput.value.trim();
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
resetFieldValidity();
if (!name) {
setFieldValidity(nameInput, false);
return { isValid: false, message: 'Please enter your name.' };
}
if (!email || !emailPattern.test(email)) {
setFieldValidity(emailInput, false);
return { isValid: false, message: 'Please enter a valid email address.' };
}
if (!message) {
setFieldValidity(messageInput, false);
return { isValid: false, message: 'Please enter your message.' };
}
return {
isValid: true,
data: { name, email, message }
};
}
function sendContactMessage() {
console.log('Sending EmailJS payload:', {
name: nameInput.value.trim(),
email: emailInput.value.trim(),
message: messageInput.value.trim()
});
return emailjs.send(EMAILJS_CONFIG.serviceId, EMAILJS_CONFIG.templateId, {
name: nameInput.value.trim(),
email: emailInput.value.trim(),
message: messageInput.value.trim()
});
}
async function handleContactSubmit(event) {
event.preventDefault();
if (!contactForm) {
return;
}
if (window.location.protocol === 'file:') {
showFormAlert('Open this site using Live Server or localhost. EmailJS will not work from file://.', 'error');
return;
}
const validation = validateForm();
if (!validation.isValid) {
showFormAlert(validation.message, 'error');
return;
}
if (!window.emailjs || !isConfiguredForEmailJS()) {
showFormAlert('Replace the EmailJS placeholder keys in script.js before sending messages.', 'error');
return;
}
setSubmittingState(true);
showFormAlert('', '');
try {
await sendContactMessage();
contactForm.reset();
resetFieldValidity();
showFormAlert('Message sent successfully!', 'success');
} catch (error) {
console.error('EmailJS Error:', error);
showFormAlert('Failed to send. Try again.', 'error');
} finally {
setSubmittingState(false);
}
}
if (contactForm) {
initializeEmailJS();
contactForm.addEventListener('submit', handleContactSubmit);
}