-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (110 loc) · 4.16 KB
/
index.js
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
/* Gets a JSON file and passes the result to a callback function. */
function getJSON(url, callback) {
const xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = () => {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
try {
var data = JSON.parse(xmlhttp.responseText);
} catch (err) {
console.log(`${err.message} in ${xmlhttp.responseText}`);
return;
}
callback(data);
}
}
xmlhttp.open('GET', url, true);
xmlhttp.send();
}
/* Adds the article's image to an element with a link to the article. */
function addImage(element, article) {
const image = new Image();
image.src = article.image;
image.classList.add('image');
const link = document.createElement('a');
link.href = article.link;
link.target = '_blank';
link.appendChild(image);
element.appendChild(link);
}
/* Adds a row containing the article's date and section. */
function addDateSection(element, article) {
const container = document.createElement('div');
container.classList.add('row', 'date-section');
const dateContainer = document.createElement('div');
dateContainer.classList.add('col-6', 'text-right');
const date = document.createElement('p');
date.innerHTML = article.date;
dateContainer.appendChild(date);
const sectionContainer = document.createElement('div');
sectionContainer.classList.add('col-6');
const section = document.createElement('p');
section.innerHTML = article.section;
sectionContainer.appendChild(section);
container.appendChild(sectionContainer);
container.appendChild(dateContainer);
element.appendChild(container);
}
/* Using a given heading, adds the article's title and link to an element. */
function addTitle(element, article, heading) {
const title = document.createElement(heading);
title.innerHTML = article.title;
const link = document.createElement('a');
link.href = article.link;
link.classList.add('article-link');
link.target = '_blank';
link.appendChild(title);
element.appendChild(link);
}
/* Adds a paragraph to the given element. Used for descriptions and credits. */
function addDescription(element, article, credit = false) {
const desc = document.createElement('p');
if (credit) desc.classList.add('credit');
desc.innerHTML = credit ? article.author : article.description;
element.appendChild(desc);
}
/* Builds the text elements in a set order. Takes in an optional heading. */
function buildText(element, article, heading = 'h2') {
addDateSection(element, article);
addTitle(element, article, heading);
addDescription(element, article);
addDescription(element, article, true);
}
function render(articles) {
/* CENTERPIECE */
addImage(document.getElementById('centerpiece-image'), articles.featured[0]);
buildText(document.getElementById('centerpiece-text'), articles.featured[0]);
/* FEATURED */
for (let i = 1; i < articles.featured.length; i++) {
// there should be no more than three projects in the featured array
if (i > 2) break;
const element = document.getElementById(`featured-${i}`);
addImage(element, articles.featured[i]);
buildText(element, articles.featured[i], 'h3');
// for the first featured project, add an hr to separate the two on mobile
if (i === 1) {
const hr = document.createElement('hr');
hr.classList.add('d-block', 'd-md-none');
element.appendChild(hr);
}
}
/* ALL PROJECTS */
const all = document.getElementById('all-projects');
// don't show this section if there are no projects in the array
if (articles.articles.length) all.style.display = 'block';
for (const article of articles.articles) {
const element = document.createElement('div');
element.classList.add('row');
const image = document.createElement('div');
image.classList.add('col-md-6', 'col-lg-4');
const text = document.createElement('div');
text.classList.add('col-md-6', 'col-lg-8', 'article-text');
addImage(image, article);
buildText(text, article, 'h4');
element.appendChild(image);
element.appendChild(text);
all.appendChild(element);
// add an hr between each project
all.appendChild(document.createElement('hr'));
}
}
getJSON('projects.json', render);