Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
429 lines (381 loc) · 11.8 KB
/
Copy pathmain.js
File metadata and controls
429 lines (381 loc) · 11.8 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* Python Nepal Website - Main JavaScript
* Handles navigation, theme switching, search, and interactions
*/
document.addEventListener("DOMContentLoaded", async () => {
// Load shared components first
await loadComponents();
// Initialize all modules
initNavigation();
initThemeToggle();
initBackToTop();
initSearch();
initAnimations();
updateCurrentYear();
});
/**
* Determine the base path based on current page location
*/
function getBasePath() {
const path = window.location.pathname;
// Check if we're in the pages directory
if (path.includes("/pages/")) {
return "../";
}
return "./";
}
/**
* Get current page name from URL
*/
function getCurrentPage() {
const path = window.location.pathname;
const filename = path.split("/").pop().replace(".html", "");
return filename === "" || filename === "index" ? "index" : filename;
}
/**
* Load shared header and footer components
*/
async function loadComponents() {
const basePath = getBasePath();
const headerContainer = document.getElementById("header-container");
const footerContainer = document.getElementById("footer-container");
try {
// Load header
if (headerContainer) {
const headerResponse = await fetch(`${basePath}components/header.html`);
if (headerResponse.ok) {
const headerHtml = await headerResponse.text();
headerContainer.innerHTML = headerHtml;
updateHeaderLinks(basePath);
setActiveNavLink();
}
}
// Load footer
if (footerContainer) {
const footerResponse = await fetch(`${basePath}components/footer.html`);
if (footerResponse.ok) {
const footerHtml = await footerResponse.text();
footerContainer.innerHTML = footerHtml;
updateFooterLinks(basePath);
}
}
} catch (error) {
console.error("Error loading components:", error);
}
}
/**
* Update header links based on current page location
*/
function updateHeaderLinks(basePath) {
const isInPages = basePath === "../";
// Update logo
const logoLink = document.getElementById("nav-logo-link");
const logoImg = document.getElementById("nav-logo-img");
if (logoLink) logoLink.href = `${basePath}index.html`;
if (logoImg) logoImg.src = `${basePath}images/logo/python-ugn-white.png`;
// Update nav links
const navLinks = document.querySelectorAll(".nav-link[data-page]");
navLinks.forEach((link) => {
const page = link.getAttribute("data-page");
if (page === "index") {
link.href = `${basePath}index.html`;
} else {
// If we're in the pages directory, other pages are in the same directory
// If we're at root, pages are in the pages/ subdirectory
if (isInPages) {
link.href = `./${page}.html`;
} else {
link.href = `./pages/${page}.html`;
}
}
});
}
/**
* Update footer links based on current page location
*/
function updateFooterLinks(basePath) {
const isInPages = basePath === "../";
// Update logo
const logoLink = document.getElementById("footer-logo-link");
const logoImg = document.getElementById("footer-logo-img");
if (logoLink) logoLink.href = `${basePath}index.html`;
if (logoImg) logoImg.src = `${basePath}images/logo/python-ugn-white.png`;
// Update footer page links
const footerLinks = document.querySelectorAll(".footer-links a[data-page]");
footerLinks.forEach((link) => {
const page = link.getAttribute("data-page");
// If we're in the pages directory, other pages are in the same directory
// If we're at root, pages are in the pages/ subdirectory
if (isInPages) {
link.href = `./${page}.html`;
} else {
link.href = `./pages/${page}.html`;
}
});
}
/**
* Set active nav link based on current page
*/
function setActiveNavLink() {
const currentPage = getCurrentPage();
const navLinks = document.querySelectorAll(".nav-link[data-page]");
navLinks.forEach((link) => {
const page = link.getAttribute("data-page");
if (page === currentPage) {
link.classList.add("active");
} else {
link.classList.remove("active");
}
});
}
/**
* Navigation functionality
*/
function initNavigation() {
const navToggle = document.getElementById("nav-toggle");
const navMenu = document.getElementById("nav-menu");
const navbar = document.getElementById("navbar");
// Mobile menu toggle
if (navToggle && navMenu) {
navToggle.addEventListener("click", () => {
navMenu.classList.toggle("active");
navToggle.classList.toggle("active");
});
// Close menu when clicking outside
document.addEventListener("click", (e) => {
if (!navToggle.contains(e.target) && !navMenu.contains(e.target)) {
navMenu.classList.remove("active");
navToggle.classList.remove("active");
}
});
// Close menu when clicking a link
navMenu.querySelectorAll(".nav-link").forEach((link) => {
link.addEventListener("click", () => {
navMenu.classList.remove("active");
navToggle.classList.remove("active");
});
});
}
// Navbar scroll effect
if (navbar) {
window.addEventListener("scroll", () => {
if (window.scrollY > 50) {
navbar.classList.add("scrolled");
} else {
navbar.classList.remove("scrolled");
}
});
}
}
/**
* Theme toggle (Dark/Light mode)
*/
function initThemeToggle() {
const themeToggle = document.getElementById("theme-toggle");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
const savedTheme = localStorage.getItem("theme");
// Set initial theme
if (savedTheme) {
document.documentElement.setAttribute("data-theme", savedTheme);
updateThemeIcon(savedTheme);
} else if (prefersDark) {
document.documentElement.setAttribute("data-theme", "dark");
updateThemeIcon("dark");
}
if (themeToggle) {
themeToggle.addEventListener("click", () => {
const currentTheme = document.documentElement.getAttribute("data-theme");
const newTheme = currentTheme === "dark" ? "light" : "dark";
document.documentElement.setAttribute("data-theme", newTheme);
localStorage.setItem("theme", newTheme);
updateThemeIcon(newTheme);
});
}
}
function updateThemeIcon(theme) {
const themeToggle = document.getElementById("theme-toggle");
if (themeToggle) {
const icon = themeToggle.querySelector("i");
if (icon) {
icon.className = theme === "dark" ? "fas fa-sun" : "fas fa-moon";
}
}
}
/**
* Back to top button
*/
function initBackToTop() {
const backToTop = document.getElementById("back-to-top");
if (backToTop) {
window.addEventListener("scroll", () => {
if (window.pageYOffset > 300) {
backToTop.classList.add("visible");
} else {
backToTop.classList.remove("visible");
}
});
backToTop.addEventListener("click", () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
});
}
}
/**
* Search functionality
*/
function initSearch() {
const searchInputs = document.querySelectorAll(".search-box input");
searchInputs.forEach((input) => {
input.addEventListener("input", (e) => {
const searchTerm = e.target.value.toLowerCase().trim();
const container = input.closest(".page-content") || document;
// Determine what type of content to search
const developerCards = container.querySelectorAll(".developer-card");
const companyCards = container.querySelectorAll(".company-card");
const meetupDetails = container.querySelectorAll(".meetup-detail");
const resourceSections = container.querySelectorAll(".resources-section");
const videoCards = container.querySelectorAll(".video-card");
if (developerCards.length > 0) {
filterCards(developerCards, searchTerm, ["h3"]);
}
if (companyCards.length > 0) {
filterCards(companyCards, searchTerm, ["h3", ".company-info"]);
}
if (meetupDetails.length > 0) {
filterCards(meetupDetails, searchTerm, [
"h2",
".talk-item h4",
".talk-item p",
]);
}
if (resourceSections.length > 0) {
filterResourceSections(resourceSections, searchTerm);
}
if (videoCards.length > 0) {
filterCards(videoCards, searchTerm, ["h3", "p"]);
}
// Update results count if element exists
const resultsCount = document.getElementById("results-count");
if (resultsCount) {
const visibleCards = container.querySelectorAll(
".developer-card:not([style*='display: none']), .company-card:not([style*='display: none']), .video-card:not([style*='display: none'])"
);
if (searchTerm) {
resultsCount.textContent = `Found ${visibleCards.length} result${
visibleCards.length !== 1 ? "s" : ""
}`;
} else {
resultsCount.textContent = "";
}
}
});
});
}
function filterCards(cards, searchTerm, selectors) {
cards.forEach((card) => {
let text = "";
selectors.forEach((selector) => {
const elements = card.querySelectorAll(selector);
elements.forEach((el) => {
text += " " + el.textContent.toLowerCase();
});
});
if (text.includes(searchTerm) || searchTerm === "") {
card.style.display = "";
} else {
card.style.display = "none";
}
});
}
function filterResourceSections(sections, searchTerm) {
sections.forEach((section) => {
const items = section.querySelectorAll(".resource-item");
let hasVisibleItems = false;
items.forEach((item) => {
const text = item.textContent.toLowerCase();
if (text.includes(searchTerm) || searchTerm === "") {
item.style.display = "";
hasVisibleItems = true;
} else {
item.style.display = "none";
}
});
// Hide entire section if no items match
section.style.display = hasVisibleItems ? "" : "none";
});
}
/**
* Scroll animations using Intersection Observer
*/
function initAnimations() {
const animatedElements = document.querySelectorAll(
".feature-card, .meetup-card, .platform-card, .developer-card, .company-card, .video-card, .meetup-detail, .resources-section"
);
if ("IntersectionObserver" in window) {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.style.opacity = "1";
entry.target.style.transform = "translateY(0)";
observer.unobserve(entry.target);
}
});
},
{
threshold: 0.1,
rootMargin: "0px 0px -50px 0px",
}
);
animatedElements.forEach((el) => {
el.style.opacity = "0";
el.style.transform = "translateY(20px)";
el.style.transition = "opacity 0.5s ease, transform 0.5s ease";
observer.observe(el);
});
}
}
/**
* Update copyright year
*/
function updateCurrentYear() {
const yearElement = document.getElementById("current-year");
if (yearElement) {
yearElement.textContent = new Date().getFullYear();
}
}
/**
* Smooth scroll for anchor links
*/
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", function (e) {
const href = this.getAttribute("href");
if (href !== "#") {
e.preventDefault();
const target = document.querySelector(href);
if (target) {
target.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
}
});
});
/**
* Lazy loading for images (if needed)
*/
if ("loading" in HTMLImageElement.prototype) {
const images = document.querySelectorAll('img[loading="lazy"]');
images.forEach((img) => {
img.src = img.dataset.src;
});
} else {
// Fallback for browsers that don't support lazy loading
const script = document.createElement("script");
script.src =
"https://cdnjs.cloudflare.com/ajax/libs/lazysizes/5.3.2/lazysizes.min.js";
document.body.appendChild(script);
}