forked from VAIBHAVBABELE/vaibhavbabele.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
201 lines (166 loc) · 6.04 KB
/
Copy pathindex.js
File metadata and controls
201 lines (166 loc) · 6.04 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
// ===== Navbar & Scroll Handling =====
const element = document.getElementById('scroll-hide');
const load = document.getElementById("onload");
function checkWindowSize() {
if (!element) return;
if (window.innerWidth < 1000) {
window.addEventListener('scroll', function () {
if (window.scrollY > 10) {
element.style.display = 'none';
}
});
} else {
element.style.display = 'flex';
}
}
window.addEventListener('load', checkWindowSize);
window.addEventListener('resize', checkWindowSize);
// ===== Back Button Function =====
function goBack() {
if (window.history.length > 1) {
window.history.back();
} else {
window.location.href = '/';
}
}
// ===== Dark Mode Toggle =====
let darkModeInitialized = false;
function initDarkMode() {
// Prevent multiple initializations
if (darkModeInitialized) return;
const mode = document.getElementById("mode");
if (!mode) {
console.error('Mode element not found!');
return;
}
// Initialize dark mode state
const isDark = localStorage.getItem('mode') === 'true';
document.body.classList.toggle('dark-mode', isDark);
updateModeIcon(mode);
// Remove any existing event listeners
mode.onclick = null;
// Add click event listener
mode.addEventListener('click', function () {
const wasDarkmode = localStorage.getItem('mode') === 'true';
const newMode = !wasDarkmode;
localStorage.setItem('mode', newMode);
document.body.classList.toggle("dark-mode", newMode);
updateModeIcon(mode);
console.log('Theme toggled to:', newMode ? 'dark' : 'light');
});
darkModeInitialized = true;
console.log('Dark mode toggle initialized successfully');
}
function updateModeIcon(mode) {
if (!mode) return;
const path = window.location.pathname;
const isDeepPage = ['/summary', '/assistant', '/pr-contribution', '/certificate'].some(subpath =>
path.includes('/pages' + subpath)
);
let imgPathPrefix = '';
if (isDeepPage) {
imgPathPrefix = '../../images/';
} else if (path.includes('/pages') || path.includes('/games')) {
imgPathPrefix = '../images/';
} else {
imgPathPrefix = 'images/';
}
mode.src = document.body.classList.contains("dark-mode") ? imgPathPrefix + "sun.png" : imgPathPrefix + "moon.png";
}
// Test function for debugging (can be called from browser console)
function testThemeToggle() {
const mode = document.getElementById("mode");
console.log('Mode element:', mode);
console.log('Current dark mode state:', document.body.classList.contains('dark-mode'));
console.log('LocalStorage mode:', localStorage.getItem('mode'));
if (mode) {
mode.click();
} else {
console.error('Mode element not found!');
}
}
// Initialize dark mode when DOM is ready
document.addEventListener('DOMContentLoaded', function() {
// Small delay to ensure all elements are loaded
setTimeout(initDarkMode, 100);
});
// Also try on window load as backup
window.addEventListener('load', function() {
if (!darkModeInitialized) {
initDarkMode();
}
});
// ===== Copyright Year =====
document.addEventListener("DOMContentLoaded", function () {
const year = new Date().getFullYear();
const copyright = document.getElementById("copyright");
if (copyright) {
copyright.textContent = `© ${year} nitramitra | All Rights Reserved`;
}
});
// ===== Google Translate Toggle =====
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en',
includedLanguages: 'en,hi',
layout: google.translate.TranslateElement.InlineLayout.SIMPLE
}, 'google_translate_element');
}
// Dynamically load Google Translate script once
if (!document.querySelector('script[src*="translate.google.com"]')) {
const translateScript = document.createElement('script');
translateScript.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
document.body.appendChild(translateScript);
}
function waitForTranslate(callback) {
const interval = setInterval(() => {
const select = document.querySelector(".goog-te-combo");
if (select) {
clearInterval(interval);
callback(select);
}
}, 500);
}
function initLanguageToggle() {
const langToggle = document.getElementById("lang-toggle");
if (!langToggle) return;
waitForTranslate((select) => {
// Apply saved language immediately
const savedLang = localStorage.getItem("lang") || "en";
select.value = savedLang;
select.dispatchEvent(new Event("change"));
langToggle.innerText = savedLang === "en" ? "हिंदी" : "English";
// Toggle click
langToggle.addEventListener("click", function () {
const newLang = select.value === "en" ? "hi" : "en";
select.value = newLang;
select.dispatchEvent(new Event("change"));
langToggle.innerText = newLang === "en" ? "हिंदी" : "English";
localStorage.setItem("lang", newLang);
});
});
}
// Run language toggle initialization
document.addEventListener("DOMContentLoaded", function() {
setTimeout(initLanguageToggle, 200);
});
document.addEventListener("DOMContentLoaded", () => {
const sections = document.querySelectorAll("section[id]");
const navLinks = document.querySelectorAll("nav ul li a");
window.addEventListener("scroll", () => {
let current = "";
sections.forEach(section => {
const sectionTop = section.offsetTop - 100; // adjust offset for header
const sectionHeight = section.clientHeight;
if (scrollY >= sectionTop && scrollY < sectionTop + sectionHeight) {
current = section.getAttribute("id");
}
});
navLinks.forEach(link => {
link.classList.remove("active");
if (link.getAttribute("href") === `#${current}`) {
link.classList.add("active");
}
});
});
});