-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
138 lines (108 loc) · 3.26 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// selector
const projectsArea = document.querySelector(".project-list-area");
function getProjectImg(project) {
const imgParent = document.createElement("div");
imgParent.className = "project-img-cont";
const imgElm = document.createElement("img");
imgElm.src = project.image;
imgElm.setAttribute("alt", project.name);
imgParent.appendChild(imgElm);
return imgParent;
}
function createAnchorElm(href, value, className, attrName, attrValue) {
const anchorElm = document.createElement("a");
anchorElm.innerText = value
anchorElm.href = href;
anchorElm.className = className;
anchorElm.setAttribute(attrName, attrValue);
return anchorElm;
}
function getProjectLinks(project) {
const linkContainer = document.createElement("div");
linkContainer.className = "links";
const websiteLink = createAnchorElm(project.url, 'Live',"btn", "target", "_blank");
const githubLink = createAnchorElm(project.github,'Github', "btn", "target", "_blank");
linkContainer.append(websiteLink, githubLink);
return linkContainer;
}
function getRandomColor() {
// Array of most used colors in hex format
const colors = [
"#FF5733",
"#FFC300",
"#4CAF50",
"#3498DB",
"#9B59B6",
"#E74C3C",
"#9b2226",
"#6a040f",
"#F39C12",
"#fb8500",
"#2ECC71",
"#2980B9",
"#8E44AD",
"#5B6D92",
"#800080",
"#E9967A",
"#4682B4",
"#0F1035",
"#163020",
"#264653",
"#4a5759",
];
const randomIndex = Math.floor(Math.random() * colors.length);
return colors[randomIndex]
}
function getProjectTags(project) {
const tagsContainer = document.createElement("div");
tagsContainer.className = 'tags'
if (!project?.tags.length) {
return null;
}
project.tags.forEach((tag) => {
const tagItem = document.createElement("span");
tagItem.innerText = tag;
tagItem.className = "tags-item";
tagItem.style.color = getRandomColor();
tagsContainer.appendChild(tagItem);
});
return tagsContainer;
}
function getProjectContent(project) {
const contentContainer = document.createElement("div");
contentContainer.className = "project-detail";
const contentElm = document.createElement("div");
contentElm.className = "project-content";
const projectName = document.createElement("h2");
projectName.innerText = project.name;
const projectTags = getProjectTags(project);
const projectDescription = document.createElement("p");
projectDescription.innerText = project.description;
contentElm.append(projectName, projectTags, projectDescription);
contentContainer.appendChild(contentElm);
return contentContainer;
}
function renderProjectList(projects) {
projects.forEach((project) => {
const projectCard = document.createElement("div");
projectCard.className = "project-card";
const projectImg = getProjectImg(project);
const projectContent = getProjectContent(project);
const projectLinks = getProjectLinks(project);
projectCard.append(projectImg, projectContent, projectLinks);
projectsArea.appendChild(projectCard);
});
}
function fetchProjects() {
fetch("./data.json")
.then((res) => {
return res.json();
})
.then((projects) => {
renderProjectList(projects);
})
.catch((err) => {
console.log(err);
});
}
fetchProjects();