-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (143 loc) · 5.25 KB
/
Copy pathscript.js
File metadata and controls
172 lines (143 loc) · 5.25 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
const contentEl = document.getElementById('content');
const siteDescriptionEl = document.getElementById('site-description');
let globalDataCache;
/* utility: build a card, optionally with full-bleed background image */
function createCard({ title, description = "", image = null, level = "category", onClick }) {
const card = document.createElement('div');
card.className = 'card';
// Adjust height for subcategory cards
if (level === 'subcategory') {
card.style.height = '240px'; // 40% taller than default
}
if (image) {
card.classList.add('bg');
// use CSS var for ::before background-image
card.style.setProperty('--bg', `url('${image}')`);
}
const inner = document.createElement('div');
inner.className = 'card-content';
inner.innerHTML = `
${level === 'category' ? `<h2>${title}</h2>` : `<h3>${title}</h3>`}
${description ? `<p>${description}</p>` : ''}
`;
card.appendChild(inner);
if (typeof onClick === 'function') card.onclick = onClick;
return card;
}
/* utility: create page banner for category/subcategory */
function createBanner(title, image) {
const banner = document.createElement('div');
banner.className = 'page-banner';
if (image) banner.style.backgroundImage = `url('${image}')`;
const h2 = document.createElement('h2');
h2.textContent = title;
banner.appendChild(h2);
return banner;
}
async function loadData() {
const res = await fetch('data.json');
const data = await res.json();
globalDataCache = data.global;
renderHome(globalDataCache);
}
function makeBackButton(callback) {
const btn = document.createElement('div');
btn.className = 'back-btn';
btn.textContent = '← Back';
btn.onclick = callback;
contentEl.appendChild(btn);
}
function applyStaggeredAnimation() {
const cards = document.querySelectorAll('.card');
cards.forEach((card, index) => {
card.style.animationDelay = `${index * 0.08}s`; // 80ms stagger
});
}
/* HOME: show main categories with full background images */
function renderHome(globalData) {
siteDescriptionEl.textContent = globalData.description;
contentEl.innerHTML = '';
for (const [categoryName, categoryData] of Object.entries(globalData.categories)) {
const card = createCard({
title: categoryName,
description: categoryData.description,
image: categoryData.image || null, // full-bleed background
level: 'category',
onClick: () => renderCategory(categoryName, categoryData),
});
contentEl.appendChild(card);
}
applyStaggeredAnimation();
}
/* CATEGORY: show subcategories, with banner */
function renderCategory(name, data) {
contentEl.innerHTML = '';
makeBackButton(() => renderHome(globalDataCache));
// Banner
contentEl.appendChild(createBanner(name, data.image));
// Description
const desc = document.createElement('p');
desc.id = 'site-description';
desc.textContent = data.description;
contentEl.appendChild(desc);
// Subcategory cards
for (const [subName, subData] of Object.entries(data.subcategories)) {
const card = createCard({
title: subName,
description: '', // cleaner look for subcards
image: subData.image || null, // subcategory banner if available
level: 'subcategory',
onClick: () => renderSubcategory(name, subName, subData),
});
contentEl.appendChild(card);
}
applyStaggeredAnimation();
}
/* SUBCATEGORY: list articles, with banner */
function renderSubcategory(categoryName, subName, subData) {
contentEl.innerHTML = '';
makeBackButton(() => renderCategory(categoryName, globalDataCache.categories[categoryName]));
// Banner with subcategory image
const bannerImage = subData.image || '';
contentEl.appendChild(createBanner(`${categoryName} / ${subName}`, bannerImage));
// Article cards
subData.articles.forEach(article => {
const card = createCard({
title: article.title,
description: article.abstract || '',
image: article.image || null, // article card background if desired
level: 'article',
onClick: () => renderArticle(categoryName, subName, article),
});
contentEl.appendChild(card);
});
applyStaggeredAnimation();
}
/* ARTICLE: render mixed content blocks */
function renderArticle(categoryName, subName, article) {
contentEl.innerHTML = '';
makeBackButton(() => renderSubcategory(categoryName, subName, globalDataCache.categories[categoryName].subcategories[subName]));
const title = document.createElement('h2');
title.textContent = article.title;
contentEl.appendChild(title);
const articleContainer = document.createElement('div');
articleContainer.className = 'article-content';
article.content.forEach(block => {
if (block.type === 'heading') {
const h3 = document.createElement('h3');
h3.textContent = block.text;
articleContainer.appendChild(h3);
} else if (block.type === 'paragraph') {
const p = document.createElement('p');
p.textContent = block.text;
articleContainer.appendChild(p);
} else if (block.type === 'image') {
const img = document.createElement('img');
img.src = block.src;
img.alt = block.alt || '';
articleContainer.appendChild(img);
}
});
contentEl.appendChild(articleContainer);
}
loadData();