-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (68 loc) · 2.55 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
// GET https://cors-anywhere.herokuapp.com/https://api.scratch.mit.edu/proxy/featured
(function sendAPIreq() {
const url = " https://cors-anywhere.herokuapp.com/https://api.scratch.mit.edu/proxy/featured";
const tabs = document.getElementById("tabs");
const container = document.getElementById("result");
fetch(url)
.then(response => response.json())
.then(function(data) {
let projects = data;
for (var project in projects) {
let section = createNode("div");
section.className = "section open";
append(container, section);
let anchor = createNode("a");
anchor.name = project;
anchor.className = "anchor";
append(section, anchor);
let title = createNode("h3");
title.className = "title";
title.innerHTML = project.replace(/_/g, " ");
title.addEventListener('click', function() {
section.classList.toggle('open');
}, false);
append(section, title);
let ul = createNode("ul");
append(section, ul);
projects[project].map(function(data) {
let imageContainer = createNode("a");
if (title.innerHTML.search("studios")==-1){
imageContainer.href = "https://scratch.mit.edu/projects/" + data.id + "/";
} else {
imageContainer.href = "https://scratch.mit.edu/studios/" + data.id + "/";
}
imageContainer.className = "imageContainer";
let li = createNode("li"),
img = createNode("img"),
description = createNode("div");
img.className = "thumbnail";
img.src = "https:" + data.thumbnail_url;
description.className = "desc";
if (title.innerHTML.search("studios")==-1){
description.innerHTML = `${data.title} by ${data.creator}`;
} else {
description.innerHTML = `${data.title}`
}
append(imageContainer, img);
append(imageContainer, description);
append(li, imageContainer);
append(ul, li);
});
let tab = createNode("a");
tab.href = "#" + project;
tab.innerHTML = project === "scratch_design_studio" ? "SDS"
: project === "curator_top_projects" ? "curated"
: project.replace(/_/g, " ").replace(/community|projects/g, "").trim();
append(tabs, tab);
}
})
.catch(function(error) {
console.error(error);
});
})();
function createNode(element) {
return document.createElement(element);
}
function append(parent, el) {
return parent.appendChild(el);
}