|
315 | 315 | const sidebar = document.getElementById('sidebar'); |
316 | 316 | const toggleBtn = document.querySelector('.toggle-btn'); |
317 | 317 | const content = document.querySelector('.content'); |
| 318 | + if (!sidebar) { |
| 319 | + console.error('Sidebar element not found'); |
| 320 | + return; |
| 321 | + } |
318 | 322 | sidebar.classList.toggle('hidden', isHidden); |
319 | 323 | content.style.marginLeft = isHidden ? '0' : '210px'; // Force margin update |
320 | 324 | localStorage.setItem('sidebar-hidden', isHidden); |
321 | 325 | if (window.innerWidth > 768) { |
322 | 326 | toggleBtn.style.display = isHidden ? 'block' : 'none'; |
| 327 | + } else { |
| 328 | + toggleBtn.style.display = 'block'; |
323 | 329 | } |
324 | | - // Update active link after toggle |
325 | 330 | updateActiveLink(); |
326 | 331 | } |
327 | 332 |
|
328 | 333 | // Handle tap/click events |
329 | 334 | function handleTap(event) { |
330 | 335 | const sidebar = document.getElementById('sidebar'); |
331 | 336 | const target = event.target; |
| 337 | + if (!sidebar) { |
| 338 | + console.error('Sidebar not found during tap'); |
| 339 | + return; |
| 340 | + } |
332 | 341 |
|
333 | 342 | // Allow taps outside interactive elements to trigger hide/show |
334 | 343 | if ( |
|
364 | 373 | const isDark = body.classList.contains('dark-mode'); |
365 | 374 | localStorage.setItem('dark-mode', isDark); |
366 | 375 | const icon = document.querySelector('.dark-toggle i'); |
367 | | - icon.classList.remove('fa-moon', 'fa-sun'); |
368 | | - icon.classList.add(isDark ? 'fa-sun' : 'fa-moon'); |
| 376 | + if (icon) { |
| 377 | + icon.classList.remove('fa-moon', 'fa-sun'); |
| 378 | + icon.classList.add(isDark ? 'fa-sun' : 'fa-moon'); |
| 379 | + } else { |
| 380 | + console.error('Dark mode icon not found'); |
| 381 | + } |
| 382 | + // Force re-render of styled elements |
| 383 | + const sidebar = document.getElementById('sidebar'); |
| 384 | + const themeContainer = document.getElementById('theme-time-container'); |
| 385 | + if (sidebar) sidebar.style.display = 'none'; // Trigger reflow |
| 386 | + if (themeContainer) themeContainer.style.display = 'none'; |
| 387 | + setTimeout(() => { |
| 388 | + if (sidebar) sidebar.style.display = ''; |
| 389 | + if (themeContainer) themeContainer.style.display = ''; |
| 390 | + }, 0); |
369 | 391 | } |
370 | 392 |
|
371 | 393 | // Timezone toggle |
372 | 394 | function toggleTimezone() { |
373 | 395 | currentZone = currentZone === 'IST' ? 'GMT' : 'IST'; |
374 | | - document.getElementById('tz-toggle-btn').textContent = currentZone; |
375 | | - updateLiveTime(); |
| 396 | + const tzButton = document.getElementById('tz-toggle-btn'); |
| 397 | + if (tzButton) { |
| 398 | + tzButton.textContent = currentZone; |
| 399 | + updateLiveTime(); |
| 400 | + } else { |
| 401 | + console.error('Timezone button not found'); |
| 402 | + } |
376 | 403 | } |
377 | 404 |
|
378 | 405 | // Update live time |
379 | 406 | function updateLiveTime() { |
| 407 | + const liveTime = document.getElementById('live-time'); |
| 408 | + if (!liveTime) { |
| 409 | + console.error('Live time element not found'); |
| 410 | + return; |
| 411 | + } |
380 | 412 | const now = new Date(); |
381 | 413 | let date; |
382 | 414 | if (currentZone === 'GMT') { |
|
390 | 422 | const s = date.getSeconds().toString().padStart(2, '0'); |
391 | 423 | const ampm = h >= 12 ? 'PM' : 'AM'; |
392 | 424 | h = h % 12 || 12; |
393 | | - document.getElementById('live-time').textContent = |
394 | | - `Time (${currentZone}): ${h}:${m}:${s} ${ampm}`; |
| 425 | + liveTime.textContent = `Time (${currentZone}): ${h}:${m}:${s} ${ampm}`; |
395 | 426 | } |
396 | 427 |
|
397 | 428 | // Handle scroll for theme/time container |
398 | 429 | function handleScroll() { |
399 | 430 | const themeTimeContainer = document.getElementById('theme-time-container'); |
400 | | - if (window.scrollY > SCROLL_THRESHOLD) { |
401 | | - themeTimeContainer.classList.add('hidden'); |
| 431 | + if (themeTimeContainer) { |
| 432 | + if (window.scrollY > SCROLL_THRESHOLD) { |
| 433 | + themeTimeContainer.classList.add('hidden'); |
| 434 | + } else { |
| 435 | + themeTimeContainer.classList.remove('hidden'); |
| 436 | + } |
402 | 437 | } else { |
403 | | - themeTimeContainer.classList.remove('hidden'); |
| 438 | + console.error('Theme/time container not found'); |
404 | 439 | } |
405 | 440 | } |
406 | 441 |
|
|
409 | 444 | // Initialize dark mode |
410 | 445 | const isDark = localStorage.getItem('dark-mode') === 'true'; |
411 | 446 | const body = document.body; |
412 | | - if (isDark) { |
413 | | - body.classList.add('dark-mode'); |
414 | | - document.querySelector('.dark-toggle i').classList.replace('fa-moon', 'fa-sun'); |
| 447 | + if (body) { |
| 448 | + if (isDark) { |
| 449 | + body.classList.add('dark-mode'); |
| 450 | + const icon = document.querySelector('.dark-toggle i'); |
| 451 | + if (icon) icon.classList.replace('fa-moon', 'fa-sun'); |
| 452 | + } |
| 453 | + } else { |
| 454 | + console.error('Body element not found'); |
415 | 455 | } |
416 | 456 |
|
417 | 457 | // Initialize sidebar state |
418 | 458 | const sidebar = document.getElementById('sidebar'); |
419 | 459 | const toggleBtn = document.querySelector('.toggle-btn'); |
420 | 460 | const content = document.querySelector('.content'); |
421 | | - const isSidebarHidden = localStorage.getItem('sidebar-hidden') === 'true'; |
422 | | - sidebar.classList.toggle('hidden', isSidebarHidden); |
423 | | - content.style.marginLeft = isSidebarHidden ? '0' : '210px'; // Initial margin |
424 | | - if (window.innerWidth > 768) { |
425 | | - toggleBtn.style.display = isSidebarHidden ? 'block' : 'none'; |
| 461 | + if (sidebar && toggleBtn && content) { |
| 462 | + const isSidebarHidden = localStorage.getItem('sidebar-hidden') === 'true'; |
| 463 | + sidebar.classList.toggle('hidden', isSidebarHidden); |
| 464 | + content.style.marginLeft = isSidebarHidden ? '0' : '210px'; |
| 465 | + if (window.innerWidth > 768) { |
| 466 | + toggleBtn.style.display = isSidebarHidden ? 'block' : 'none'; |
| 467 | + } else { |
| 468 | + toggleBtn.style.display = 'block'; |
| 469 | + } |
426 | 470 | } else { |
427 | | - toggleBtn.style.display = 'block'; |
| 471 | + console.error('Sidebar, toggle button, or content not found'); |
428 | 472 | } |
429 | 473 |
|
430 | 474 | // Add tap/click and scroll listeners |
|
433 | 477 | window.addEventListener('scroll', handleScroll, { passive: true }); |
434 | 478 |
|
435 | 479 | // Show tooltip on first visit |
436 | | - if (!localStorage.getItem('tooltip-shown')) { |
| 480 | + const tooltip = document.getElementById('tooltip'); |
| 481 | + if (tooltip && !localStorage.getItem('tooltip-shown')) { |
437 | 482 | setTimeout(() => { |
438 | | - const tooltip = document.getElementById('tooltip'); |
439 | 483 | tooltip.classList.add('visible'); |
440 | 484 | setTimeout(() => { |
441 | 485 | tooltip.classList.remove('visible'); |
442 | 486 | localStorage.setItem('tooltip-shown', 'true'); |
443 | 487 | }, 5000); |
444 | 488 | }, 1000); |
| 489 | + } else if (!tooltip) { |
| 490 | + console.error('Tooltip element not found'); |
445 | 491 | } |
446 | 492 |
|
447 | 493 | // Initialize active link |
|
0 commit comments