-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
158 lines (120 loc) · 4 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
var reposTable = document.getElementById('repos');
var tbody = document.createElement('tbody');
reposTable.appendChild(tbody);
var tablesort;
var numOrgs = 0;
var searchInput = document.getElementById('search');
var forkCheckbox = document.getElementById('include-forks');
var update = debounce(function (e) {
var rows = tbody.getElementsByTagName('tr'),
value = searchInput.value;
for (var i = 0, len = rows.length; i < len; i++) {
rows[i].style.display =
(!rows[i].dataset.fork || forkCheckbox.checked) &&
(!value || rows[i].textContent.toLowerCase().indexOf(value.toLowerCase())) >= 0 ? 'table-row' : 'none';
}
}, 150);
searchInput.oninput = update;
forkCheckbox.onclick = update;
var orgInput = document.getElementById('org'),
orgForm = document.getElementById('org-form');
orgForm.onsubmit = loadFromInput;
function loadFromInput() {
numOrgs = orgInput.value.split(',').length;
orgInput.value.split(',').forEach(function(org) {
loadOrganization(org.trim());
});
return false;
}
function loadOrganization(org) {
tbody.innerHTML = '';
document.body.className = 'loading';
getRepos('https://github.com/gitapi/orgs/' + org + '/repos?type=public&per_page=100');
}
function getRepos(url) {
var xhr = new XMLHttpRequest();
xhr.onload = onResponse;
xhr.open('get', url, true);
xhr.send();
xhr.responseType = 'json';
}
function onResponse(e) {
var xhr = e.target;
if (xhr.response.message === 'Not Found') {
document.body.className = '';
return;
}
addRepos(xhr.response);
var links = getLinks(xhr.getResponseHeader('Link'));
if (links && links.next) getRepos(links.next);
document.body.className = 'loaded';
}
function getLinks(header) {
if (!header) return null;
var parts = header.split(','),
links = {};
for (var i = 0; i < parts.length; i++) {
var section = parts[i].split(';'),
url = section[0].match(/<(.*)>/)[1],
name = section[1].match(/rel="(.*)"/)[1];
links[name] = url;
}
return links;
}
function addRepos(repos) {
for (var i = 0; i < repos.length; i++) {
var repo = repos[i];
var repoName = numOrgs > 1 ? repo.full_name : repo.name
var createdDate = formatDate(repo.created_at);
var pushedDate = formatDate(repo.pushed_at);
addRow(repoName, repo.html_url, repo.language, repo.stargazers_count, repo.forks_count,
repo.open_issues_count, createdDate, pushedDate, repo.description, repo);
}
if (!tablesort) tablesort = new Tablesort(reposTable, {descending: true});
else tablesort.refresh();
}
function formatDate(str) {
return new Date(str).toDateString().substr(4);
}
function addRow(name, link, language, stars, forks, issues, created, pushed, description, repo) {
var tr = document.createElement('tr');
if (repo.fork) tr.dataset.fork = true;
// Create link to repository via the DOM
var td = document.createElement('td');
var a = document.createElement('a');
a.setAttribute('href', link)
a.setAttribute('target', '_blank')
name = document.createTextNode(name);
a.appendChild(name);
td.appendChild(a);
tr.appendChild(td);
// create the other TDs in the table
createCell(language, tr);
createCell(stars, tr);
createCell(forks, tr);
createCell(issues, tr);
createCell(created, tr);
createCell(pushed, tr);
createCell(description, tr);
tbody.appendChild(tr);
}
function createCell(content, row) {
var td = document.createElement('td');
content = document.createTextNode(content);
td.appendChild(content);
row.appendChild(td);
}
function debounce(fn, wait) {
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
fn.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}