Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
342 lines (288 loc) · 12.1 KB
/
Copy pathscript.js
File metadata and controls
342 lines (288 loc) · 12.1 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
// script.js (Updated)
// --- Element Selection ---
const menuToggle = document.getElementById('menuToggle');
const mobileMenuToggle = document.getElementById('mobileMenuToggle');
const sidebar = document.getElementById('sidebar');
const overlay = document.getElementById('overlay');
const mainContent = document.querySelector('.main-content');
const searchBox = document.getElementById('searchBox');
const resourceCards = document.querySelectorAll('.resource-card');
// --- Functions to Open/Close Menu ---
function openMobileMenu() {
if (sidebar) sidebar.classList.add('active');
if (overlay) overlay.classList.add('active');
// Send button to the back so it can't be clicked when menu is open
if (mobileMenuToggle) mobileMenuToggle.style.zIndex = '998';
}
function closeMobileMenu() {
if (sidebar) sidebar.classList.remove('active');
if (overlay) overlay.classList.remove('active');
// Bring button back to the front
if (mobileMenuToggle) mobileMenuToggle.style.zIndex = '1001';
}
// --- Event Listeners ---
// Desktop menu toggle (if exists)
if (menuToggle) {
menuToggle.addEventListener('click', () => {
if (sidebar) sidebar.classList.toggle('active');
if (overlay) overlay.classList.toggle('active');
});
}
// Mobile menu toggle
if (mobileMenuToggle) {
mobileMenuToggle.addEventListener('click', () => {
// Check if menu is already open before toggling
const isMenuOpen = sidebar && sidebar.classList.contains('active');
if (isMenuOpen) {
closeMobileMenu();
} else {
openMobileMenu();
}
});
}
// Close menu when clicking the overlay
if (overlay) {
overlay.addEventListener('click', closeMobileMenu);
}
// Smooth scrolling for navigation links / hash routing compatibility
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', (e) => {
const href = link.getAttribute('href');
// Check if it's an anchor link for scrolling
if (href.startsWith('#')) {
const targetId = href.substring(1);
const scrollTargets = ['featured', 'about', 'platforms', 'download-qr'];
if (scrollTargets.includes(targetId)) {
e.preventDefault();
// Go to home view first if in a sub-view
if (window.location.hash !== '') {
window.location.hash = '';
// Delay scroll slightly to allow home view to render
setTimeout(() => {
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}, 400);
} else {
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
}
}
// Always close the mobile menu after a link is clicked
if (window.innerWidth <= 768) {
closeMobileMenu();
}
});
});
// Search functionality (if search box exists)
if (searchBox) {
searchBox.addEventListener('input', (e) => {
const searchTerm = e.target.value.toLowerCase();
resourceCards.forEach(card => {
const title = card.querySelector('h3')?.textContent?.toLowerCase() || '';
const description = card.querySelector('p')?.textContent?.toLowerCase() || '';
if (title.includes(searchTerm) || description.includes(searchTerm)) {
card.style.display = 'block';
} else {
card.style.display = 'none';
}
});
});
}
// Handle window resize
window.addEventListener('resize', () => {
if (window.innerWidth > 768) {
// Ensure mobile-specific states are cleared on resize to desktop
closeMobileMenu();
}
});
// --- PWA Service Worker Registration ---
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
// Resolve service-worker.js relative to the current page's base path.
// This works whether the site is hosted at / (local/Netlify) or /portal/ (GitHub Pages).
const basePath = window.location.pathname.replace(/\/[^/]*$/, '/');
navigator.serviceWorker.register(basePath + 'service-worker.js', { scope: basePath })
.then((reg) => console.log('Service Worker Registered', reg))
.catch((err) => console.log('Service Worker Registration Failed', err));
});
}
// ==========================================================================
// TYPEWRITER ANIMATION FOR TOP-LEFT BRAND SUBTITLE
// ==========================================================================
function startBrandTypewriter() {
const typewriterEl = document.getElementById('brandTypewriter');
const subTagsEl = document.getElementById('brandSubTags');
if (!typewriterEl) return;
if (typewriterEl.dataset.typingStarted === 'true') return;
typewriterEl.dataset.typingStarted = 'true';
const fullText = 'Projects for and by the students of UITS.';
typewriterEl.textContent = '';
let charIndex = 0;
const typingSpeed = 38; // ms per char
function typeChar() {
if (charIndex < fullText.length) {
typewriterEl.textContent += fullText.charAt(charIndex);
charIndex++;
setTimeout(typeChar, typingSpeed);
} else {
typewriterEl.classList.add('done-typing');
if (subTagsEl) {
subTagsEl.classList.add('visible');
}
}
}
setTimeout(typeChar, 200);
}
// ==========================================================================
// PRELOADER CONTROLLER (Centered -> Minimize Directly to Top-Left Site Brand)
// ==========================================================================
function initPreloader() {
const preloader = document.getElementById('portalPreloader');
const preloaderTitle = document.getElementById('preloaderTitle');
const preloaderTagline = document.getElementById('preloaderTagline');
const preloaderLine = document.getElementById('preloaderLine');
const siteBrand = document.getElementById('siteBrand');
if (!preloader || !preloaderTitle) {
startBrandTypewriter();
return;
}
// Initially hide destination brand text so it doesn't duplicate while preloader is centered
if (siteBrand) {
siteBrand.style.opacity = '0';
}
// Step 1: Hold centered for 850ms
setTimeout(() => {
// Fade out tagline and progress line smoothly
if (preloaderTagline) preloaderTagline.style.opacity = '0';
if (preloaderLine) preloaderLine.style.opacity = '0';
// Step 2: Calculate exact pixel trajectory to top-left siteBrand position
if (siteBrand) {
const brandRect = siteBrand.getBoundingClientRect();
const titleRect = preloaderTitle.getBoundingClientRect();
// Center-to-center delta
const currentCenterX = titleRect.left + titleRect.width / 2;
const currentCenterY = titleRect.top + titleRect.height / 2;
const targetCenterX = brandRect.left + brandRect.width / 2;
const targetCenterY = brandRect.top + brandRect.height / 2;
const deltaX = targetCenterX - currentCenterX;
const deltaY = targetCenterY - currentCenterY;
// Target scale matching brand font height
const targetScale = Math.min(1, Math.max(0.35, brandRect.height / titleRect.height));
// Smoothly glide title directly to the top-left corner
preloaderTitle.style.transition = 'transform 0.85s cubic-bezier(0.16, 1, 0.3, 1)';
preloaderTitle.style.transform = `translate(${deltaX}px, ${deltaY}px) scale(${targetScale})`;
}
// Fade out dark preloader backdrop
preloader.classList.add('fade-out');
// Step 3: Seamlessly dock into top-left siteBrand and remove preloader
setTimeout(() => {
if (siteBrand) {
siteBrand.style.opacity = '1';
siteBrand.style.transition = 'opacity 0.25s ease';
}
preloader.classList.add('hidden');
// Trigger typewriter animation as brand docks
startBrandTypewriter();
}, 850);
}, 850);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initPreloader);
} else {
initPreloader();
}
// ==========================================================================
// IDLE AUTO-SCROLL TO EXPLORE HUB RESOURCES (Silky Smooth Animation)
// ==========================================================================
function smoothScrollTo(targetY, duration = 1400) {
const startY = window.pageYOffset || document.documentElement.scrollTop;
const distance = targetY - startY;
if (Math.abs(distance) < 5) return;
let startTime = null;
let isCancelled = false;
function cancelScroll() {
isCancelled = true;
removeCancelListeners();
}
function removeCancelListeners() {
['wheel', 'touchstart', 'mousedown', 'keydown'].forEach(evt => {
window.removeEventListener(evt, cancelScroll, { passive: true });
});
}
['wheel', 'touchstart', 'mousedown', 'keydown'].forEach(evt => {
window.addEventListener(evt, cancelScroll, { passive: true });
});
// EaseInOutCubic for organic, fluid motion
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function step(timestamp) {
if (isCancelled) return;
if (!startTime) startTime = timestamp;
const elapsed = timestamp - startTime;
const progress = Math.min(elapsed / duration, 1);
const ease = easeInOutCubic(progress);
window.scrollTo(0, startY + (distance * ease));
if (progress < 1) {
requestAnimationFrame(step);
} else {
removeCancelListeners();
}
}
requestAnimationFrame(step);
}
function initIdleAutoScroll() {
let idleTimer = null;
let hasAutoScrolled = false;
const idleTimeLimit = 7000; // 7s idle threshold
function cleanup() {
if (idleTimer) {
clearTimeout(idleTimer);
idleTimer = null;
}
['mousemove', 'mousedown', 'keydown', 'touchstart', 'wheel', 'scroll'].forEach(evt => {
window.removeEventListener(evt, handleUserActivity, { passive: true });
});
}
function handleUserActivity() {
if (hasAutoScrolled) return;
if (idleTimer) clearTimeout(idleTimer);
idleTimer = setTimeout(() => {
if (hasAutoScrolled) return;
// Trigger only when on home view, near top of page, and only once
if (!document.body.classList.contains('in-subview') && window.scrollY < 200) {
const target = document.querySelector('.floating-nav-container') || document.getElementById('floatingCanvas');
if (target) {
hasAutoScrolled = true;
cleanup(); // Run for the first time only
const rect = target.getBoundingClientRect();
const currentScrollY = window.pageYOffset || document.documentElement.scrollTop;
const targetY = Math.max(0, currentScrollY + rect.top);
smoothScrollTo(targetY, 1400);
}
}
}, idleTimeLimit);
}
['mousemove', 'mousedown', 'keydown', 'touchstart', 'wheel', 'scroll'].forEach(evt => {
window.addEventListener(evt, handleUserActivity, { passive: true });
});
handleUserActivity();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initIdleAutoScroll);
} else {
initIdleAutoScroll();
}