-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
432 lines (375 loc) · 13.8 KB
/
Copy pathscript.js
File metadata and controls
432 lines (375 loc) · 13.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
430
431
432
document.addEventListener('DOMContentLoaded', function() {
// --- Theme Toggle ---
initTheme();
// --- Smooth Scrolling ---
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const targetId = this.getAttribute('href');
if (targetId === '#') return;
e.preventDefault();
const target = document.querySelector(targetId);
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});
// --- Email Footer Button ---
const emailBtn = document.getElementById('email-button');
if (emailBtn) {
emailBtn.addEventListener('click', function(e) {
e.preventDefault();
const emailParts = {
username: 'ashoknimiwal1998',
domain: 'gmail.com'
};
const existing = this.parentElement.querySelector('.email-text');
if (existing) {
existing.remove();
} else {
const span = document.createElement('span');
span.className = 'email-text';
span.textContent = `username@${emailParts.domain}; username = ${emailParts.username}`;
this.parentElement.insertBefore(span, this.nextSibling);
}
});
}
// --- Home Link ---
const homeLink = document.getElementById('home-link');
if (homeLink) {
homeLink.addEventListener('click', function(e) {
e.preventDefault();
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
// --- Load All Sections ---
loadAllSections();
});
// ============================================
// THEME
// ============================================
function initTheme() {
const toggle = document.getElementById('theme-toggle');
const saved = localStorage.getItem('theme');
if (saved) {
document.documentElement.setAttribute('data-theme', saved);
} else {
document.documentElement.setAttribute('data-theme', 'dark');
}
updateThemeIcon();
if (toggle) {
toggle.addEventListener('click', () => {
const current = document.documentElement.getAttribute('data-theme');
const next = current === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', next);
localStorage.setItem('theme', next);
updateThemeIcon();
});
}
}
function updateThemeIcon() {
const toggle = document.getElementById('theme-toggle');
if (!toggle) return;
const theme = document.documentElement.getAttribute('data-theme');
toggle.innerHTML = theme === 'dark'
? '<i class="fas fa-sun"></i>'
: '<i class="fas fa-moon"></i>';
}
// ============================================
// DATA LOADING
// ============================================
async function loadAllSections() {
try {
const aboutResponse = await fetch('api/about.json');
const aboutData = await aboutResponse.json();
populateAbout(aboutData);
const sections = ['news', 'publications', 'talks', 'projects', 'blogPosts', 'miscellaneous'];
for (const section of sections) {
const response = await fetch(`api/${section}.json`);
const data = await response.json();
populateSection(section, data);
}
} catch (error) {
console.error('Error loading data:', error);
}
}
// ============================================
// ABOUT
// ============================================
function populateAbout(data) {
// Profile image
const profileContainer = document.getElementById('about-profile');
if (profileContainer) {
const img = document.createElement('img');
img.src = data.profileImage;
img.alt = data.name;
img.className = 'profile-pic';
img.id = 'profilePic';
img.style.opacity = '0';
profileContainer.appendChild(img);
setTimeout(() => {
img.style.opacity = '1';
img.style.animation = 'popOut 1.5s ease-out';
}, 200);
}
// Text content + social links
const contentContainer = document.getElementById('about-content');
if (contentContainer) {
let html = `<h2>About Me</h2>`;
if (data.greeting) {
html += `<p>${data.greeting}</p>`;
}
data.bio.forEach(para => {
html += `<p>${para}</p>`;
});
html += `<div class="social-links">`;
data.links.forEach(link => {
html += `<a href="${link.url}" target="_blank" aria-label="${link.label}"><i class="${link.icon}"></i></a>`;
});
html += `</div>`;
contentContainer.innerHTML = html;
}
}
function populateSection(section, data) {
const container = document.getElementById(`${section}-container`);
if (!container) return;
container.innerHTML = '';
// News gets special scroll treatment
if (section === 'news') {
populateNews(container, data);
return;
}
data.forEach(item => {
const element = createSectionElement(section, item);
if (element) {
container.appendChild(element);
}
});
}
function createSectionElement(section, item) {
switch (section) {
case 'publications': return createPublicationElement(item);
case 'talks': return createTalkElement(item);
case 'projects': return createProjectCard(item);
case 'blogPosts': return createBlogCard(item);
case 'miscellaneous': return createMiscellaneousElement(item);
default: return null;
}
}
// ============================================
// NEWS (with scroll/expand)
// ============================================
const NEWS_VISIBLE_COUNT = 10;
function populateNews(container, data) {
const wrapper = document.createElement('div');
wrapper.className = 'news-scroll-wrapper';
if (data.length > NEWS_VISIBLE_COUNT) {
wrapper.classList.add('scrollable');
}
const ul = document.createElement('ul');
data.forEach(item => {
const li = document.createElement('li');
li.innerHTML = `<strong class="news-date">${item.date}</strong><span class="sep-bullet">•</span><span class="news-text">${parseLinkedContent(item.content)}</span>`;
ul.appendChild(li);
});
wrapper.appendChild(ul);
container.appendChild(wrapper);
}
function parseLinkedContent(content) {
return content.replace(/<([^>]+)>/g, (match, p1) => {
const [text, url] = p1.split('|');
if (url) {
return `<a href="${url}" target="_blank">${text}</a>`;
}
return match;
});
}
// ============================================
// PUBLICATIONS
// ============================================
function createPublicationElement(publication) {
const element = document.createElement('div');
element.className = 'publication';
const yearDisplay = publication.year
? `<span class="separator">•</span>${publication.year}`
: '';
// Build buttons
let buttonsHtml = '';
buttonsHtml += createButton('abstract', 'fas fa-book-open', 'Abstract', publication.abstract);
if (publication.citation) {
buttonsHtml += createButton('citation', 'fas fa-quote-right', 'Cite', publication.citation);
}
buttonsHtml += createButton('doi', 'fas fa-link', 'DOI', publication.doi);
buttonsHtml += createButton('pdf', 'fas fa-file-pdf', 'PDF', publication.pdf);
element.innerHTML = `
<h4>${publication.title}</h4>
<p class="journal-year">${publication.journal}${yearDisplay}</p>
<div class="pub-buttons">
${buttonsHtml}
</div>
<div class="abstract">
<div class="abstract-inner">
<p>${publication.abstract || 'Abstract not available.'}</p>
</div>
</div>
${publication.citation ? `
<div class="citation">
<div class="citation-inner">
<p>${publication.citation}</p>
</div>
</div>
` : ''}
`;
// Wire up abstract toggle
const abstractButton = element.querySelector('.btn-abstract');
if (abstractButton && publication.abstract) {
abstractButton.addEventListener('click', function() {
toggleExpandable(this, '.abstract', 'fas fa-book-open', 'Abstract', 'fas fa-book', 'Hide Abstract');
});
}
// Wire up citation toggle
const citationButton = element.querySelector('.btn-citation');
if (citationButton && publication.citation) {
citationButton.addEventListener('click', function() {
toggleExpandable(this, '.citation', 'fas fa-quote-right', 'Cite', 'fas fa-quote-left', 'Hide');
});
}
return element;
}
function toggleExpandable(btn, selector, iconOpen, textOpen, iconClose, textClose) {
const parent = btn.closest('.publication') || btn.closest('.talk');
const target = parent.querySelector(selector);
if (!target) return;
const isOpen = target.classList.contains('show');
if (isOpen) {
target.classList.remove('show');
btn.innerHTML = `<i class="${iconOpen}"></i> ${textOpen}`;
} else {
target.classList.add('show');
btn.innerHTML = `<i class="${iconClose}"></i> ${textClose}`;
}
}
// ============================================
// TALKS
// ============================================
function createTalkElement(talk) {
const element = document.createElement('div');
element.className = 'talk';
// Tag (e.g., Forthcoming)
const tagHtml = talk.tag
? `<span class="talk-tag">${talk.tag}</span>`
: '';
// Audience
const audienceHtml = talk.audience
? `<span class="talk-audience">${talk.audience}</span>`
: '';
// Description button
const descBtnHtml = talk.description
? createButton('description', 'fas fa-align-left', 'Description', talk.description)
: createButton('description', 'fas fa-align-left', 'Description', '');
element.innerHTML = `
<h4>${talk.title}</h4>
<div class="talk-meta">
<p>${talk.date} <span class="sep-bullet">•</span> ${talk.event}, ${talk.location}</p>
${tagHtml}
${audienceHtml}
</div>
<div class="talk-buttons">
${descBtnHtml}
${createButton('code', 'fas fa-code', 'Code', talk.code)}
${createButton('slides', 'fas fa-desktop', 'Slides', talk.slides)}
${createButton('video', 'fas fa-video', 'Video', talk.video)}
</div>
${talk.description ? `
<div class="talk-description">
<div class="talk-description-inner">
<p>${talk.description}</p>
</div>
</div>
` : ''}
`;
// Wire up description toggle
const descBtn = element.querySelector('.btn-description');
if (descBtn && talk.description) {
descBtn.addEventListener('click', function() {
toggleExpandable(this, '.talk-description', 'fas fa-align-left', 'Description', 'fas fa-times', 'Hide');
});
}
return element;
}
// ============================================
// BUTTONS
// ============================================
function createButton(type, iconClass, text, content) {
if (!content) {
return `<button class="btn btn-${type} inactive" disabled><i class="${iconClass}"></i> ${text}</button>`;
}
// Expandable button types (handled via JS click, not links)
if (type === 'abstract' || type === 'citation' || type === 'description') {
return `<button class="btn btn-${type}"><i class="${iconClass}"></i> ${text}</button>`;
}
return `<a href="${content}" class="btn btn-${type}" target="_blank"><i class="${iconClass}"></i> ${text}</a>`;
}
// ============================================
// PROJECT CARDS
// ============================================
function createProjectCard(project) {
const card = document.createElement('a');
card.className = 'project-card';
if (project.slug) {
card.href = `projects/${project.slug}.html`;
} else {
card.href = project.link;
card.target = '_blank';
card.rel = 'noopener noreferrer';
}
const statusClass = project.status === 'Active' ? '' : 'wip';
const statusText = project.status || 'In Progress';
card.innerHTML = `
<img src="${project.image}" alt="${project.title}">
<div class="card-content">
<h3>${project.title}</h3>
<p>${project.description}</p>
<div class="card-status">
<span class="status-dot ${statusClass}"></span>
${statusText}
</div>
</div>
`;
return card;
}
// ============================================
// BLOG CARDS
// ============================================
function createBlogCard(post) {
const card = document.createElement('a');
card.className = 'blog-card';
if (post.slug) {
card.href = `blogs/post.html?post=${post.slug}`;
} else if (post.link) {
card.href = post.link;
card.target = '_blank';
card.rel = 'noopener noreferrer';
} else {
card.href = '#';
card.style.opacity = '0.5';
card.style.pointerEvents = 'none';
}
const tag = post.tag || 'Article';
card.innerHTML = `
<img src="${post.image}" alt="${post.title}">
<div class="card-content">
<h3>${post.title}</h3>
<p>${post.excerpt}</p>
<div class="card-tag">${tag}</div>
</div>
`;
return card;
}
// ============================================
// MISCELLANEOUS
// ============================================
function createMiscellaneousElement(item) {
const li = document.createElement('li');
li.innerHTML = parseLinkedContent(item.content);
return li;
}