-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
131 lines (118 loc) · 4.48 KB
/
script.js
File metadata and controls
131 lines (118 loc) · 4.48 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
// Theme toggle functionality
const themeToggle = document.getElementById('theme-toggle');
const themeIcon = document.querySelector('.theme-icon');
// Function to get system theme preference
function getSystemTheme() {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
// Function to set theme
function setTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
updateThemeIcon(theme);
}
// Function to update theme icon
function updateThemeIcon(theme) {
themeIcon.textContent = theme === 'dark' ? '☀️' : '🌙';
}
// Function to toggle theme
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
setTheme(newTheme);
}
// Initialize theme
function initTheme() {
const savedTheme = localStorage.getItem('theme');
const systemTheme = getSystemTheme();
const initialTheme = savedTheme || systemTheme;
setTheme(initialTheme);
}
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
setTheme(e.matches ? 'dark' : 'light');
}
});
// Event listener for theme toggle button
if (themeToggle) {
themeToggle.addEventListener('click', toggleTheme);
}
// Initialize on page load
initTheme();
// Search functionality
function initSearch() {
const searchForm = document.querySelector('.search-form');
if (searchForm) {
searchForm.addEventListener('submit', function(e) {
e.preventDefault();
const query = this.querySelector('.search-input').value.trim().toLowerCase();
if (query) {
// For now, redirect to a search results page or show alert
// In a full implementation, this would search through content
alert(`Searching for: "${query}"\n\nThis is a placeholder. In a full implementation, this would search through all articles and display results.`);
// You could redirect to a search page: window.location.href = `search.html?q=${encodeURIComponent(query)}`;
}
});
}
}
// Randomize category cards
function randomizeCategoryCards() {
const categoriesGrid = document.querySelector('.categories-grid');
if (categoriesGrid) {
const cards = Array.from(categoriesGrid.children);
// Shuffle array using Fisher-Yates algorithm
for (let i = cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[cards[i], cards[j]] = [cards[j], cards[i]];
}
// Re-append shuffled cards
cards.forEach(card => categoriesGrid.appendChild(card));
}
}
// Random article functionality
function initRandomArticle() {
const randomLinks = document.querySelectorAll('a[href="#"]');
randomLinks.forEach(link => {
if (link.textContent.toLowerCase().includes('random article')) {
link.addEventListener('click', function(e) {
e.preventDefault();
// List of available articles (expand this as you add more)
const articles = [
'science/origin-of-the-universe.html'
// Add more article paths here
];
if (articles.length > 0) {
const randomIndex = Math.floor(Math.random() * articles.length);
window.location.href = articles[randomIndex];
} else {
alert('No articles available yet. Check back soon!');
}
});
}
});
}
// Table of Contents functionality
function initTableOfContents() {
const tocList = document.querySelector('.toc-list');
if (tocList) {
// Smooth scrolling for TOC links
tocList.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
e.preventDefault();
const targetId = e.target.getAttribute('href').substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
}
}
});
}
}
// Initialize all features
document.addEventListener('DOMContentLoaded', function() {
initSearch();
randomizeCategoryCards();
initRandomArticle();
initTableOfContents();
});