forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
200 lines (180 loc) · 5.36 KB
/
Copy pathindex.js
File metadata and controls
200 lines (180 loc) · 5.36 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
{
function fetchJSON(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onload = () => {
resolve(xhr.response);
};
xhr.onerror = () => {
reject(new Error('Network request failed'));
};
xhr.onabort = () => {
reject(new Error('Network request aborted'));
};
xhr.send();
});
}
function createAndAppend(name, parent, options = {}) {
const elem = document.createElement(name);
parent.appendChild(elem);
Object.keys(options).forEach(key => {
const value = options[key];
if (key === 'text') {
elem.textContent = value;
} else {
elem.setAttribute(key, value);
}
});
return elem;
}
function createHeader(root) {
const header = createAndAppend('header', root, {
class: 'header',
});
createAndAppend('p', header, {
text: 'HYF Repositories',
});
return header;
}
function createOptions(repositoryList, header) {
const select = createAndAppend('select', header, {
id: 'selector',
});
for (let i = 0; i < repositoryList.length; i++) {
const repository = repositoryList[i];
createAndAppend('option', select, {
text: repository.name,
value: i,
});
}
}
function createMainContainer(root) {
const mainContainer = createAndAppend('div', root, {
id: 'container',
});
return mainContainer;
}
function containerListContributor(mainContainer) {
const leftContainer = createAndAppend('div', mainContainer, {
class: 'left-block frame',
});
const table = createAndAppend('table', leftContainer);
const listContributor = createAndAppend('tbody', table);
return listContributor;
}
function createContributorContainer(mainContainer) {
const rightContainer = createAndAppend('div', mainContainer, {
class: 'right-block frame',
});
createAndAppend('p', rightContainer, {
text: 'Contributions',
class: 'contributor',
});
return createAndAppend('ul', rightContainer, {
class: 'list1',
});
}
function renderContributors(contributorList, contributors) {
contributorList.innerHTML = '';
for (let i = 0; i < contributors.length; i++) {
const li = createAndAppend('li', contributorList, {
class: 'contributor-item',
});
const linkFor = createAndAppend('a', li, {
target: '_blank',
href: contributors[i].html_url,
});
createAndAppend('img', linkFor, {
src: contributors[i].avatar_url,
class: 'avatar',
height: 48,
});
const divCont = createAndAppend('div', linkFor, {
class: 'contributor-data',
});
createAndAppend('div', divCont, {
text: contributors[i].login,
});
createAndAppend('div', divCont, {
text: contributors[i].contributions,
class: 'badge',
});
}
}
function renderRep(listContributors, repo, root, contributorList) {
const content = [
{
title: 'Repository',
attribute: 'name',
},
{
title: 'Description',
attribute: 'description',
},
{
title: 'Forks',
attribute: 'forks',
},
{
title: 'Updated',
attribute: 'updated_at',
},
];
listContributors.innerHTML = '';
for (let i = 0; i < content.length; i++) {
const headTitle = createAndAppend('tr', listContributors);
createAndAppend('td', headTitle, {
text: `${content[i].title} :`,
class: 'label',
});
const cellContent = createAndAppend('td', headTitle);
if (content[i].attribute === 'name') {
createAndAppend('a', cellContent, {
href: repo.html_url,
text: repo.name,
target: '_blank',
});
} else {
cellContent.textContent = repo[content[i].attribute];
}
}
fetchJSON(repo.contributors_url)
.then(contributors => renderContributors(contributorList, contributors))
.catch(err => {
createAndAppend('div', root, {
text: err.message,
class: 'alert-error',
});
});
}
function main(url) {
const root = document.getElementById('root');
const header = createHeader(root);
const mainContainer = createMainContainer(root);
const listContributor = containerListContributor(mainContainer);
const contributorList = createContributorContainer(mainContainer);
fetchJSON(url)
.then(data => {
let repositories = data;
repositories = repositories.sort((a, b) => a.name.localeCompare(b.name));
createOptions(repositories, header);
const selectedValue = document.getElementById('selector');
selectedValue.addEventListener('change', event => {
const selectedRepo = event.target.value;
renderRep(listContributor, repositories[selectedRepo], root, contributorList);
});
renderRep(listContributor, repositories[0], root, contributorList);
})
.catch(err => {
createAndAppend('div', root, {
text: err.message,
class: 'alert-error',
});
});
}
const HYF_REPS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => main(HYF_REPS_URL);
}