-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
399 lines (343 loc) · 12.5 KB
/
Copy pathmain.js
File metadata and controls
399 lines (343 loc) · 12.5 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* ============================================
RANDROID.DEV - Main Script
============================================ */
(function () {
'use strict';
// --- Navbar hide/show on scroll ---
const nav = document.getElementById('nav');
let lastScrollY = 0;
let ticking = false;
function updateNav() {
const currentY = window.scrollY;
if (currentY > lastScrollY && currentY > 100) {
nav.classList.add('hidden');
} else {
nav.classList.remove('hidden');
}
lastScrollY = currentY;
ticking = false;
}
window.addEventListener('scroll', function () {
if (!ticking) {
requestAnimationFrame(updateNav);
ticking = true;
}
});
// --- Active nav link tracking ---
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.nav-links a[data-section]');
function updateActiveLink() {
const scrollY = window.scrollY + 200;
sections.forEach(function (section) {
const top = section.offsetTop;
const height = section.offsetHeight;
const id = section.getAttribute('id');
if (scrollY >= top && scrollY < top + height) {
navLinks.forEach(function (link) {
link.classList.remove('active');
if (link.getAttribute('data-section') === id) {
link.classList.add('active');
}
});
}
});
}
window.addEventListener('scroll', updateActiveLink);
// --- Mobile menu ---
const menuToggle = document.getElementById('menuToggle');
const mobileMenu = document.getElementById('mobileMenu');
const menuClose = document.getElementById('menuClose');
const mobileLinks = document.querySelectorAll('.mobile-link');
menuToggle.addEventListener('click', function () {
mobileMenu.classList.add('open');
document.body.style.overflow = 'hidden';
});
menuClose.addEventListener('click', closeMobileMenu);
mobileLinks.forEach(function (link) {
link.addEventListener('click', closeMobileMenu);
});
function closeMobileMenu() {
mobileMenu.classList.remove('open');
document.body.style.overflow = '';
}
// --- Scroll-triggered fade-in animations ---
const observerOptions = {
root: null,
rootMargin: '0px 0px -60px 0px',
threshold: 0.1
};
const fadeObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
fadeObserver.unobserve(entry.target);
}
});
}, observerOptions);
document.querySelectorAll('.fade-in, .playable-card, .story-card, .blog-card').forEach(function (el) {
fadeObserver.observe(el);
});
// --- Hero stat counter animation ---
const statNumbers = document.querySelectorAll('.hero-stat-number[data-count]');
let statsAnimated = false;
function animateStats() {
if (statsAnimated) return;
statsAnimated = true;
statNumbers.forEach(function (el) {
const target = parseInt(el.getAttribute('data-count'), 10);
const duration = 1200;
const startTime = performance.now();
function tick(now) {
const elapsed = now - startTime;
const progress = Math.min(elapsed / duration, 1);
// Ease out cubic
const eased = 1 - Math.pow(1 - progress, 3);
el.textContent = Math.round(eased * target);
if (progress < 1) {
requestAnimationFrame(tick);
}
}
requestAnimationFrame(tick);
});
}
const heroObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
animateStats();
heroObserver.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
const heroStats = document.querySelector('.hero-stats');
if (heroStats) {
heroObserver.observe(heroStats);
}
// --- Stagger story card animations ---
const storyCards = document.querySelectorAll('.story-card');
const storyObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
// Find the index within currently visible batch
const card = entry.target;
const allCards = Array.from(storyCards);
const idx = allCards.indexOf(card);
const delay = (idx % 3) * 100; // stagger within rows of 3
card.style.transitionDelay = delay + 'ms';
card.classList.add('visible');
storyObserver.unobserve(card);
}
});
}, { rootMargin: '0px 0px -40px 0px', threshold: 0.05 });
storyCards.forEach(function (card) {
storyObserver.observe(card);
});
// --- Stagger playable card animations ---
const playableCards = document.querySelectorAll('.playable-card');
const playableObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
playableObserver.unobserve(entry.target);
}
});
}, { rootMargin: '0px 0px -40px 0px', threshold: 0.05 });
playableCards.forEach(function (card) {
playableObserver.observe(card);
});
// --- Blog reader overlay ---
var blogReader = document.getElementById('blogReader');
var blogReaderContent = document.getElementById('blogReaderContent');
var blogReaderBackdrop = document.getElementById('blogReaderBackdrop');
var blogReaderClose = document.getElementById('blogReaderClose');
var blogCards = document.querySelectorAll('.blog-card[data-post]');
function openBlogPost(postId) {
var template = document.getElementById(postId);
if (!template) return;
blogReaderContent.innerHTML = template.innerHTML;
blogReader.classList.add('open');
document.body.style.overflow = 'hidden';
// Scroll the reader panel back to top
var scrollEl = blogReader.querySelector('.blog-reader-scroll');
if (scrollEl) scrollEl.scrollTop = 0;
}
function closeBlogReaderUI() {
blogReader.classList.remove('open');
document.body.style.overflow = '';
// Wait for close animation to clear content
setTimeout(function () {
if (!blogReader.classList.contains('open')) {
blogReaderContent.innerHTML = '';
}
}, 500);
}
blogCards.forEach(function (card) {
card.addEventListener('click', function () {
var postId = card.getAttribute('data-post');
openBlogPost(postId);
history.pushState(null, '', '#blog/' + postId);
});
});
function closeBlogReader() {
if (location.hash.indexOf('#blog/') === 0) {
history.back();
} else {
closeBlogReaderUI();
}
}
blogReaderClose.addEventListener('click', closeBlogReader);
blogReaderBackdrop.addEventListener('click', closeBlogReader);
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && blogReader.classList.contains('open')) {
closeBlogReader();
}
});
// Handle browser back/forward for blog post deep links
window.addEventListener('popstate', function () {
var match = location.hash.match(/^#blog\/(post-\d+)$/);
if (match) {
openBlogPost(match[1]);
} else if (blogReader.classList.contains('open')) {
closeBlogReaderUI();
}
});
// Open blog post from URL hash on initial page load
var initialMatch = location.hash.match(/^#blog\/(post-\d+)$/);
if (initialMatch) {
openBlogPost(initialMatch[1]);
}
// --- Floating feedback button ---
var fab = document.getElementById('fab');
var feedbackPanel = document.getElementById('feedbackPanel');
var feedbackForm = document.getElementById('feedbackForm');
function toggleFeedback() {
var isOpen = fab.classList.toggle('open');
feedbackPanel.classList.toggle('open', isOpen);
if (isOpen) {
document.getElementById('feedbackMessage').focus();
}
}
fab.addEventListener('click', toggleFeedback);
// Close feedback panel when clicking outside
document.addEventListener('click', function (e) {
if (!fab.contains(e.target) && !feedbackPanel.contains(e.target) && feedbackPanel.classList.contains('open')) {
fab.classList.remove('open');
feedbackPanel.classList.remove('open');
}
});
// Close feedback panel on Escape
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && feedbackPanel.classList.contains('open')) {
fab.classList.remove('open');
feedbackPanel.classList.remove('open');
}
});
var feedbackSubmit = document.getElementById('feedbackSubmit');
var feedbackSuccess = document.getElementById('feedbackSuccess');
feedbackForm.addEventListener('submit', function (e) {
e.preventDefault();
var name = document.getElementById('feedbackName').value.trim();
var message = document.getElementById('feedbackMessage').value.trim();
if (!message) return;
var title = 'Site Feedback' + (name ? ' from ' + name : '');
var body = message + (name ? '\n\n- ' + name : '');
// Show sending state
feedbackSubmit.disabled = true;
feedbackSubmit.classList.add('sending');
fetch('/api/feedback', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: title, body: body })
})
.then(function (res) {
if (!res.ok) throw new Error('GitHub API returned ' + res.status);
return res.json();
})
.then(function () {
// Show success state
feedbackForm.style.display = 'none';
feedbackSuccess.classList.add('visible');
feedbackForm.reset();
// Reset after a delay
setTimeout(function () {
fab.classList.remove('open');
feedbackPanel.classList.remove('open');
// Reset states after close animation
setTimeout(function () {
feedbackForm.style.display = '';
feedbackSuccess.classList.remove('visible');
feedbackSubmit.disabled = false;
feedbackSubmit.classList.remove('sending');
}, 350);
}, 2000);
})
.catch(function () {
feedbackSubmit.disabled = false;
feedbackSubmit.classList.remove('sending');
feedbackSubmit.classList.add('error');
var label = feedbackSubmit.querySelector('.feedback-submit-label');
label.textContent = 'Failed - try again';
setTimeout(function () {
feedbackSubmit.classList.remove('error');
label.textContent = 'Send Feedback';
}, 3000);
});
});
// --- Mouse glow effect on story cards ---
storyCards.forEach(function (card) {
card.addEventListener('mousemove', function (e) {
const rect = card.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const glow = card.querySelector('.story-card-glow');
if (glow) {
glow.style.left = (x - rect.width) + 'px';
glow.style.top = (y - rect.height) + 'px';
glow.style.opacity = '0.06';
}
});
card.addEventListener('mouseleave', function () {
var glow = card.querySelector('.story-card-glow');
if (glow) glow.style.opacity = '0';
});
});
})();
// --- Mi Casa Es Su Casa - name-based embed loader ---
function loadMiCasa(event, form) {
event.preventDefault();
var input = form.querySelector('.mcesc-input');
var name = input.value.trim();
if (!name || !/^[A-Za-z0-9][A-Za-z0-9_-]{0,18}[A-Za-z0-9]$|^[A-Za-z0-9]$/.test(name)) {
input.style.borderColor = 'var(--accent)';
input.focus();
return;
}
var placeholder = form.closest('.embed-placeholder');
var card = placeholder.closest('.playable-card');
card.setAttribute('data-url', 'https://mi-casa-es-su-casa.vercel.app/' + encodeURIComponent(name.toLowerCase()));
loadEmbed(placeholder);
}
// --- Embed loader (global for onclick) ---
function loadEmbed(placeholder) {
var container = placeholder.parentElement;
var card = container.closest('.playable-card');
var url = card.getAttribute('data-url');
if (!url) return;
placeholder.style.opacity = '0';
placeholder.style.pointerEvents = 'none';
var iframe = document.createElement('iframe');
iframe.src = url;
iframe.setAttribute('allow', 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope');
iframe.setAttribute('allowfullscreen', '');
iframe.setAttribute('loading', 'lazy');
iframe.style.opacity = '0';
iframe.style.transition = 'opacity 0.5s ease';
container.appendChild(iframe);
iframe.addEventListener('load', function () {
iframe.style.opacity = '1';
placeholder.remove();
});
// Fallback: show iframe after 3s even if load event doesn't fire
setTimeout(function () {
iframe.style.opacity = '1';
}, 3000);
}