-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
486 lines (425 loc) · 19.3 KB
/
script.js
File metadata and controls
486 lines (425 loc) · 19.3 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
document.addEventListener("DOMContentLoaded", () => {
const debounce = (func, wait) => {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), wait);
};
};
const initPreloader = () => {
const preloader = document.getElementById("preloader");
if (!preloader) return;
const hidePreloader = () => preloader.classList.add("hidden");
window.addEventListener("load", hidePreloader);
setTimeout(hidePreloader, 3000); // Fallback
};
const initMobileMenu = () => {
const menuToggle = document.querySelector(".menu-toggle");
const navbar = document.getElementById("navbar");
if (!menuToggle || !navbar) return;
if (!menuToggle.querySelector(".fa-times")) {
menuToggle.appendChild(Object.assign(document.createElement("i"), {
className: "fas fa-times"
}));
}
menuToggle.addEventListener("click", (e) => {
e.stopPropagation();
navbar.classList.toggle("active");
menuToggle.classList.toggle("open");
});
document.addEventListener("click", (e) => {
if (navbar.classList.contains("active") &&
!navbar.contains(e.target) &&
!menuToggle.contains(e.target)) {
navbar.classList.remove("active");
menuToggle.classList.remove("open");
}
});
navbar.addEventListener("click", (e) => {
if (e.target.tagName === "A" && navbar.classList.contains("active")) {
navbar.classList.remove("active");
menuToggle.classList.remove("open");
}
});
};
const initHeaderScroll = () => {
const header = document.querySelector("header");
if (!header) return;
const updateHeader = debounce(() => {
header.classList.toggle("scrolled", window.scrollY > 50);
}, 50);
window.addEventListener("scroll", updateHeader);
};
const initNavHighlight = () => {
const sections = document.querySelectorAll("section[id]");
const navLinks = document.querySelectorAll("#navbar a");
const header = document.querySelector("header");
if (sections.length === 0 || navLinks.length === 0) return;
const headerHeight = header?.offsetHeight || 70;
const updateActiveLink = debounce(() => {
const scrollY = window.scrollY || document.documentElement.scrollTop;
let current = "";
sections.forEach((section) => {
if (scrollY >= section.offsetTop - headerHeight - 20) {
current = section.id;
}
});
if (window.innerHeight + scrollY + 50 >= document.body.scrollHeight) {
current = sections[sections.length - 1].id;
}
navLinks.forEach((link) => {
link.classList.toggle("active", link.getAttribute("href") === `#${current}`);
});
}, 50);
window.addEventListener("scroll", updateActiveLink);
window.addEventListener("load", updateActiveLink);
};
const initThemeToggle = () => {
const themeToggle = document.querySelector(".theme-toggle");
const htmlElement = document.documentElement;
if (!themeToggle) return;
const savedTheme = localStorage.getItem("theme") ||
(window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
htmlElement.setAttribute("data-theme", savedTheme);
themeToggle.addEventListener("click", () => {
const newTheme = htmlElement.getAttribute("data-theme") === "light" ? "dark" : "light";
htmlElement.setAttribute("data-theme", newTheme);
localStorage.setItem("theme", newTheme);
});
};
const initCategoryDisplay = () => {
const categoryItems = document.querySelectorAll(".category-item");
const contentSections = document.querySelectorAll("#content-display > .content-section");
const contentPlaceholder = document.querySelector("#content-display-area .content-placeholder");
const contentDisplayArea = document.getElementById("content-display-area");
const header = document.querySelector("header");
if (categoryItems.length === 0 || !contentDisplayArea) {
return;
}
const headerHeight = header?.offsetHeight || 70;
const showCategory = (categoryId) => {
categoryItems.forEach((item) => item.classList.remove("active"));
const activeItem = document.querySelector(`.category-item[data-category="${categoryId}"]`);
if (activeItem) activeItem.classList.add("active");
contentSections.forEach((section) => section.style.display = "none");
if (contentPlaceholder) contentPlaceholder.style.display = "none";
const selectedSection = document.getElementById(categoryId);
if (selectedSection) {
selectedSection.style.display = "block";
try {
const rect = selectedSection.getBoundingClientRect();
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const targetPosition = rect.top + scrollTop - headerHeight - 20;
window.scrollTo({ top: targetPosition, behavior: "smooth" });
setTimeout(() => {
const currentPosition = window.scrollY;
if (Math.abs(currentPosition - targetPosition) > 5) {
selectedSection.scrollIntoView({ behavior: "smooth", block: "start" });
}
}, 500);
} catch (error) {
selectedSection.scrollIntoView({ behavior: "smooth", block: "start" });
}
} else {
if (contentPlaceholder) contentPlaceholder.style.display = "flex";
}
};
categoryItems.forEach((item) => {
item.addEventListener("click", () => {
const categoryId = item.getAttribute("data-category");
if (item.classList.contains("active")) {
item.classList.remove("active");
document.getElementById(categoryId).style.display = "none";
if (contentPlaceholder) contentPlaceholder.style.display = "flex";
} else {
showCategory(categoryId);
}
});
});
if (contentPlaceholder) contentPlaceholder.style.display = "flex";
contentSections.forEach((section) => section.style.display = "none");
};
const initScrollButtons = () => {
const scrollDownArrow = document.querySelector(".scroll-down a");
const scrollTopContainer = document.querySelector(".scroll-top");
const header = document.querySelector("header");
if (scrollDownArrow && header) {
scrollDownArrow.addEventListener("click", (e) => {
e.preventDefault();
const targetSection = document.querySelector(scrollDownArrow.getAttribute("href"));
if (targetSection) {
window.scrollTo({
top: targetSection.offsetTop - header.offsetHeight,
behavior: "smooth"
});
}
});
}
if (scrollTopContainer) {
scrollTopContainer.querySelector("a").addEventListener("click", (e) => {
e.preventDefault();
window.scrollTo({ top: 0, behavior: "smooth" });
});
window.addEventListener("scroll", debounce(() => {
scrollTopContainer.classList.toggle("visible", window.scrollY > 300);
}, 50));
}
};
const initHeroParallax = () => {
const heroSection = document.querySelector(".hero");
if (!heroSection) return;
window.addEventListener("scroll", debounce(() => {
heroSection.style.backgroundPositionY = `${window.scrollY * 0.3}px`;
}, 10));
};
// Global IntersectionObserver for scroll animations
let globalScrollObserver;
const initScrollReveal = () => {
if (globalScrollObserver) { // Return existing observer if already initialized
return { observer: globalScrollObserver };
}
globalScrollObserver = new IntersectionObserver((entries, obs) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
obs.unobserve(entry.target);
}
});
}, { threshold: 0.1, rootMargin: "0px 0px -5% 0px" });
document.querySelectorAll(
".animate-on-scroll, .book-item, .semester-item, .resource-item, .category-item, .blog-post-preview, .profile-bio-container, .typewriter-code, .education, .skills-overview, .social-links, .contact-form"
).forEach((el) => {
if (!el.classList.contains("animate-on-scroll")) { // Add class if not present
el.classList.add("animate-on-scroll");
}
globalScrollObserver.observe(el);
});
return { observer: globalScrollObserver }; // Return the observer for specific use cases if needed
};
const initTypewriter = () => {
const heroTitle = document.querySelector(".hero-content h1");
if (!heroTitle || !heroTitle.textContent.trim()) return;
const text = heroTitle.textContent.trim();
heroTitle.textContent = "";
heroTitle.style.opacity = "1";
let i = 0;
const typeWriter = () => {
if (i < text.length) {
heroTitle.textContent += text[i++];
setTimeout(typeWriter, 70);
}
};
setTimeout(typeWriter, 1200);
};
// Blog section - UPDATED
const initBlogSection = (moreText = "Show More Posts", lessText = "Show Less Posts") => {
const blogPostsData = [
{
id: "blog1",
title: "Essential Software for Chemical Engineers",
bloggerUrl: "https://eramrit.blogspot.com/2025/05/essential-software-for-chemical.html",
previewImage: "https://bit.ly/amritkblog1",
snippet: "Check out the best tools for chemical engineers! Use Aspen Plus and HYSYS to test ideas, AutoCAD and SolidWorks to draw designs, MATLAB, Python, and Minitab to study data, and Simulink, LabVIEW, and DeltaV to control processes. These make work easier and smarter! Great for students and experts."
},
{
id: "blog2",
title: "Chemical Engineering in Nepal: Opportunities and Challenges",
bloggerUrl: "https://eramrit.blogspot.com/2025/05/chemical-engineering-in-nepal.html",
previewImage: "https://bit.ly/amritkblog2",
snippet: "The history of chemical engineering in Nepal may be short, but its development has been promising. Originating after the Industrial Revolution, this field can significantly contribute to Nepal's pharmaceutical, food processing, cement, environmental protection, and renewable energy sectors."
},/*
{
id: "blog3",
title: "Mastering Remote Work: Tips for Productivity",
bloggerUrl: "https://eramritkhanal.blogspot.com/your-remote-work-link-here",
previewImage: "Images/blog-placeholder-3.jpg",
snippet: "Practical advice and strategies to stay focused, organized, and maintain a healthy work-life balance while working from home effectively."
},
{
id: "blog4",
title: "Essential Software for Chemical Engineers (Copy)",
bloggerUrl: "https://eramrit.blogspot.com/2025/05/essential-software-for-chemical.html",
previewImage: "https://bit.ly/amritkblog1",
snippet: "Check out the best tools for chemical engineers! Use Aspen Plus and HYSYS to test ideas, AutoCAD and SolidWorks to draw designs, MATLAB, Python, and Minitab to study data, and Simulink, LabVIEW, and DeltaV to control processes. These make work easier and smarter! Great for students and experts."
},
{
id: "blog5",
title: "Chemical Engineering in Nepal (Copy)",
bloggerUrl: "https://eramrit.blogspot.com/2025/05/chemical-engineering-in-nepal.html",
previewImage: "https://bit.ly/amritkblog2",
snippet: "The history of chemical engineering in Nepal may be short, but its development has been promising. Originating after the Industrial Revolution, this field can significantly contribute to Nepal's pharmaceutical, food processing, cement, environmental protection, and renewable energy sectors."
},
{
id: "blog6",
title: "Mastering Remote Work (Copy)",
bloggerUrl: "https://eramritkhanal.blogspot.com/your-remote-work-link-here",
previewImage: "Images/blog-placeholder-3.jpg",
snippet: "Practical advice and strategies to stay focused, organized, and maintain a healthy work-life balance while working from home effectively."
}*/
];
const blogPostsContainer = document.querySelector(".blog-posts-container");
const blogModal = document.getElementById("blogModal");
if (!blogPostsContainer) {
console.error(".blog-posts-container not found. Blog section will not initialize.");
return;
}
const modalBlogTitle = document.getElementById("modalBlogTitle");
const blogIframe = document.getElementById("blogIframe");
const viewOnBloggerLinkModal = document.getElementById("viewOnBloggerLink");
const closeModalButtons = document.querySelectorAll("#blogModal .close-button, #blogModal .close-modal-footer-btn");
const initialVisibleCount = 3;
let visibleCount = initialVisibleCount;
let isAllVisible = false;
const toggleBtn = document.querySelector("#toggleBtn");
if (!toggleBtn) {
console.warn("Toggle button #toggleBtn not found! Show More/Less functionality will be disabled.");
}
const displayBlogPreviews = () => {
blogPostsContainer.innerHTML = blogPostsData.length === 0
? '<p style="text-align: center; color: var(--text-light);">No blog posts available yet. Check back soon!</p>'
: blogPostsData.slice(0, visibleCount).map(post => `
<article class="blog-post-preview animate-on-scroll">
${post.previewImage ? `<img src="${post.previewImage}" alt="${post.title} preview" class="preview-image">` : ""}
<h3>${post.title}</h3>
<p class="snippet">${post.snippet}</p>
<div class="actions">
<button class="btn primary-btn read-more-btn" data-id="${post.id}" aria-label="Read more about ${post.title}">Read More</button>
<a href="${post.bloggerUrl}" target="_blank" rel="noopener noreferrer" class="btn secondary-btn view-on-blogger-preview" aria-label="View ${post.title} on Blogger">View on Blogger</a>
</div>
</article>
`).join("");
// Re-apply scroll reveal to newly added blog post previews
const scrollRevealInstance = initScrollReveal(); // Get the global observer instance
if (scrollRevealInstance && scrollRevealInstance.observer) {
document.querySelectorAll(".blog-posts-container .blog-post-preview.animate-on-scroll").forEach((el) => {
scrollRevealInstance.observer.observe(el);
});
}
if (toggleBtn) {
toggleBtn.textContent = isAllVisible ? lessText : moreText;
toggleBtn.classList.toggle("view-less", isAllVisible);
if (blogPostsData.length <= initialVisibleCount) {
toggleBtn.style.display = 'none';
} else {
toggleBtn.style.display = 'inline-flex';
}
}
};
const openModalWithPost = ({ id, title, bloggerUrl }) => {
if (!blogModal || !modalBlogTitle || !blogIframe || !viewOnBloggerLinkModal) {
if (bloggerUrl) window.open(bloggerUrl, '_blank'); // Fallback to open in new tab
return;
}
modalBlogTitle.textContent = title;
blogIframe.src = bloggerUrl;
viewOnBloggerLinkModal.href = bloggerUrl;
blogModal.setAttribute("aria-hidden", "false");
document.body.classList.add("modal-open");
const firstFocusable = blogModal.querySelector(".close-button, a.btn, button.btn, [tabindex]:not([tabindex='-1'])");
if (firstFocusable) firstFocusable.focus();
};
const closeBlogModal = () => {
if (!blogModal || !blogIframe) return;
blogModal.setAttribute("aria-hidden", "true");
document.body.classList.remove("modal-open");
setTimeout(() => { if (blogIframe) blogIframe.src = "about:blank"; }, 300);
};
blogPostsContainer.addEventListener("click", (e) => {
const readMoreBtn = e.target.closest(".read-more-btn");
if (readMoreBtn) {
const postId = readMoreBtn.dataset.id;
const postData = blogPostsData.find((p) => p.id === postId);
if (postData) openModalWithPost(postData);
}
});
if (blogModal) {
closeModalButtons.forEach((btn) => btn.addEventListener("click", closeBlogModal));
blogModal.addEventListener("click", (e) => {
if (e.target === blogModal) closeBlogModal();
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && blogModal.getAttribute("aria-hidden") === "false") {
closeBlogModal();
}
});
}
if (toggleBtn) {
toggleBtn.addEventListener("click", () => {
const wasAllVisible = isAllVisible;
isAllVisible = !isAllVisible;
visibleCount = isAllVisible ? blogPostsData.length : initialVisibleCount;
displayBlogPreviews();
if (wasAllVisible && !isAllVisible) {
const blogSectionElement = document.getElementById("blogs");
if (blogSectionElement) {
blogSectionElement.scrollIntoView({ behavior: "smooth", block: "start" });
} else if (blogPostsContainer) {
blogPostsContainer.scrollIntoView({ behavior: "smooth", block: "start" });
}
}
});
}
displayBlogPreviews();
};
const scriptURL = 'https://script.google.com/macros/s/AKfycbzrrvgJcSa_MrmnSaCW4aiwXzuwVpdEjXZSbXQGY8-uKyif71reDBk_G590OMXOFPZ6Rg/exec';
const contactForm = document.getElementById('contactForm');
const initContactForm = () => {
if (contactForm) {
contactForm.addEventListener('submit', function (e) {
e.preventDefault();
const name = contactForm.name.value.trim();
const email = contactForm.email.value.trim();
const subject = contactForm.subject.value.trim();
const message = contactForm.message.value.trim();
let isValid = true;
if (!name) {
isValid = false; alert('Name is required.'); contactForm.name.focus();
} else if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
isValid = false; alert('Valid email is required.'); contactForm.email.focus();
} else if (!subject) {
isValid = false; alert('Subject is required.'); contactForm.subject.focus();
} else if (!message) {
isValid = false; alert('Message is required.'); contactForm.message.focus();
}
if (!isValid) return;
const formData = new FormData();
formData.append('name', name);
formData.append('email', email);
formData.append('subject', subject);
formData.append('message', message);
fetch(scriptURL, { method: 'POST', body: formData })
.then(response => response.json())
.then(data => {
if (data.result === 'success') {
alert(`Thank you, ${name} ! Your message has been sent successfully. It will be reviewed shortly.`);
contactForm.reset();
} else {
alert('Error submitting form. Please try again.');
}
})
.catch(error => {
alert('Error submitting form. Please try again.');
});
});
}
};
const initFooterYear = () => { // Wrapped your footer year logic in a function
const yearSpan = document.getElementById('currentYear');
if (yearSpan) {
yearSpan.textContent = new Date().getFullYear();
}
};
initPreloader();
initMobileMenu();
initHeaderScroll();
initNavHighlight();
initThemeToggle();
initCategoryDisplay();
initScrollButtons();
initHeroParallax();
initScrollReveal(); // Initialize the global observer
initTypewriter();
initBlogSection("View More Blogs", "Back to previous"); // Text parameters for the button
initContactForm();
initFooterYear();
});