-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite.js
More file actions
165 lines (165 loc) · 5.07 KB
/
Copy pathsite.js
File metadata and controls
165 lines (165 loc) · 5.07 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
async function loadData() {
const response = await fetch("data.json");
if (!response.ok) {
throw new Error("couldnt load data.json");
}
return response.json();
}
function projectPeople(project, peopleById) {
return project.people
.map((id) => {
const person = peopleById.get(id);
if (!person) {
return "";
}
return `<a href="#${person.id}" data-person-id="${person.id}">${person.name}</a>`;
})
.filter(Boolean)
.join(", ");
}
function personProjects(personId, projects) {
return projects.filter((project) => project.people.includes(personId));
}
function renderPersonDetail(detail, person, projects, options = {}) {
if (!person) {
detail.hidden = true;
return;
}
const games = personProjects(person.id, projects);
const links = [
person.github ? `<a href="${person.github}">github</a>` : "",
person.site ? `<a href="${person.site}">site</a>` : ""
].filter(Boolean);
const image = person.image
? `<img src="${person.image}" alt="${person.name}">`
: `<div class="person-initial">${person.name.slice(0, 1)}</div>`;
detail.hidden = false;
detail.innerHTML = `
${options.showBack ? `<button type="button" data-close-profile>close</button>` : ""}
<div class="person-top">
${image}
<div>
<h3>${person.name}</h3>
<p class="muted">${games.length} ports</p>
</div>
</div>
${person.bio ? `<p class="muted">${person.bio}</p>` : ""}
${links.length ? `<p class="person-links">${links.join(" ")}</p>` : "<p class=\"muted\">no links added yet.</p>"}
<div>
<h4>ports:</h4>
<ul>
${games.map((game) => `<li>${game.title}</li>`).join("") || "<li>none listed yet.</li>"}
</ul>
</div>
`;
}
function renderPeople(data) {
const grid = document.querySelector("[data-people]");
if (!grid) {
return;
}
const visiblePeople = data.people.filter((person) => person.image);
grid.innerHTML = visiblePeople
.map((person) => `
<a class="team-link" href="projects.html#${person.id}">
<img src="${person.image}" alt="${person.name}">
<span>${person.name}</span>
</a>
`)
.join("");
}
function renderPersonPage(container, person, projects) {
if (!container || !person) {
return;
}
const games = personProjects(person.id, projects);
const links = [
person.github ? `<a href="${person.github}">github</a>` : "",
person.site ? `<a href="${person.site}">site</a>` : ""
].filter(Boolean);
const image = person.image
? `<img src="${person.image}" alt="${person.name}">`
: `<div class="person-initial">${person.name.slice(0, 1)}</div>`;
container.hidden = false;
container.innerHTML = `
<a class="back-link" href="projects.html">all projects</a>
<div class="person-top">
${image}
<div>
<h2>${person.name}</h2>
<p class="muted">${games.length} ports</p>
</div>
</div>
${person.bio ? `<p class="muted">${person.bio}</p>` : ""}
${links.length ? `<p class="person-links">${links.join("")}</p>` : ""}
<h3 class="section-title">ports:</h3>
<div class="person-port-list">
${games.map((game) => {
const title = game.url
? `<a class="project-name" href="${game.url}">${game.title}</a>`
: `<span class="project-name">${game.title}</span>`;
return `<article class="person-port-row">${title}</article>`;
}).join("") || "<p class=\"muted\">none listed yet.</p>"}
</div>
`;
}
function renderProjects(data) {
const list = document.querySelector("[data-projects]");
if (!list) {
return;
}
const peopleById = new Map(data.people.map((person) => [person.id, person]));
list.innerHTML = data.projects
.map((project) => {
const title = project.url
? `<a href="${project.url}" class="project-name">${project.title}</a>`
: `<span class="project-name">${project.title}</span>`;
return `
<article class="project-row">
${title}
<p>${projectPeople(project, peopleById)}</p>
</article>
`;
})
.join("");
const layout = document.querySelector(".projects-layout");
const personPage = document.querySelector("[data-person-page]");
const hero = document.querySelector("[data-projects-hero]");
function showProjects() {
if (hero) {
hero.hidden = false;
}
if (personPage) {
personPage.hidden = true;
personPage.innerHTML = "";
}
if (layout) {
layout.hidden = false;
}
}
function showHashPerson() {
const id = decodeURIComponent(window.location.hash.replace("#", "")).toLowerCase();
const person = data.people.find((item) => item.id === id);
if (!id || !person || !personPage) {
showProjects();
return;
}
if (hero) {
hero.hidden = true;
}
if (layout) {
layout.hidden = true;
}
renderPersonPage(personPage, person, data.projects);
}
window.addEventListener("hashchange", showHashPerson);
showHashPerson();
}
loadData()
.then((data) => {
renderPeople(data);
renderProjects(data);
})
.catch((error) => {
console.error(error);
});