-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (87 loc) · 3.69 KB
/
script.js
File metadata and controls
103 lines (87 loc) · 3.69 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
document.addEventListener('DOMContentLoaded', async () => {
const projectsContainer = document.getElementById('projects-container');
try {
// Fetch the list of pages from pages.json
const pagesResponse = await fetch('pages.json');
if (!pagesResponse.ok) {
throw new Error(`HTTP error! status: ${pagesResponse.status}`);
}
const pages = await pagesResponse.json();
// Fetch the metadata for each project from its respective folder
const projectDataPromises = pages.map(async (projectName) => {
try {
const metaResponse = await fetch(`./${projectName}/meta.json`);
if (!metaResponse.ok) {
throw new Error(`HTTP error! status: ${metaResponse.status}`);
}
const projectMeta = await metaResponse.json();
return {
name: projectName,
meta: projectMeta
};
} catch (error) {
console.log(`Error fetching or parsing meta.json for project: ${projectName}`, error);
return {
name: projectName,
meta: null
};
}
});
const projectData = (await Promise.all(projectDataPromises)).filter(Boolean);
projectData.forEach(project => {
const card = document.createElement('div');
card.className = 'card';
card.dataset.project = project.name;
const imageContainer = document.createElement('div');
imageContainer.className = 'card-image-container';
const img = new Image();
try {
img.src = `./${project.name}/${project.meta.image}`;
} catch (e) {
console.log(`No image found for project: ${project.name}, using default image.`);
try {
img.src = './default-image.png';
} catch (e) {}
}
img.className = 'card-image';
try {
img.alt = project.meta.title;
} catch (e) {
img.alt = project.name || "Project Image";
}
img.onload = () => {
imageContainer.appendChild(img);
};
img.onerror = () => {
console.log(`Failed to load image for project: ${project.name}`);
};
card.appendChild(imageContainer);
const content = document.createElement('div');
content.className = 'card-content';
const title = document.createElement('h3');
title.className = 'card-title';
try {
title.textContent = project.meta.title;
} catch (e) {
title.textContent = project.name || "Untitled Project";
}
const description = document.createElement('p');
description.className = 'card-description';
try {
description.textContent = project.meta.description;
} catch (e) {
description.textContent = "No description available.";
}
content.appendChild(title);
content.appendChild(description);
card.appendChild(content);
card.addEventListener('click', () => {
window.location.href = `./${project.name}/index.html`;
});
projectsContainer.appendChild(card);
});
} catch (error) {
console.error('Failed to load project data:', error);
projectsContainer.innerHTML = '<p class="text-red-500">Failed to load projects. Please check your data files.</p>';
}
});