-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (45 loc) · 1.79 KB
/
script.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
const bearFile = 'bears.json'
const cols = document.getElementsByClassName('col')
const duplicate = document.querySelector('.duplicate')
fetch(bearFile).then(function(response) {
return response.json()
}).then(function(bearJSON) {
bearJSON = bearJSON.reverse();
addBears(bearJSON)
});
function addBears(bearJSON) {
for (var i = 0; i < bearJSON.length; i++) {
var currentTemplate = createBearElement(bearJSON[i], bearJSON.length - i - 1)
cols[i % cols.length].append(currentTemplate.cloneNode(true))
duplicate.append(currentTemplate)
}
}
function createBearElement(bearInfo, bearNumber) {
/*
bearInfo: {
bearImage,
bearDescription,
bearLink,
bearAuthor,
bearAuthorLink
}
*/
var template = document.getElementById('bear-template').cloneNode(true)
template = template.content
template.querySelector('.bear').id = "bear-" + (bearNumber + 1)
template.querySelector('.bear-pic').src = bearInfo.bearImage
template.querySelector('.bear-description').textContent = bearInfo.bearDescription || ""
template.querySelector('.bear-link').href = bearInfo.bearLink || "#"
template.querySelector('.bear-number').textContent = (bearNumber + 1)
if (bearInfo.bearAuthor) {
template.querySelector('.by').textContent = 'By '
template.querySelector('.bear-author').textContent = bearInfo.bearAuthor
template.querySelector('.bear-author').href = bearInfo.bearAuthorLink
}
if (bearInfo.bearEvent) {
template.querySelector('.for').textContent = 'Seen at '
template.querySelector('.bear-event').textContent = bearInfo.bearEvent
template.querySelector('.bear-event').href = bearInfo.bearEventLink
}
return template
}