/
github.js
47 lines (45 loc) · 1.4 KB
/
github.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
var github = (function(){
function render(repos){
var i = 0, fragment = '';
var t = document.getElementById('gh-repos');
for(i = 0; i < repos.length; i++) {
fragment += '<li><a href="'+repos[i].html_url+'">'+repos[i].name+'</a><p>'+repos[i].description+'</p></li>';
}
t.innerHTML = fragment;
}
return {
options: {},
parseResult: function(result) {
if (!result || !result.data) {
return;
}
var data = result.data;
var repos = [];
for (var i = 0; i < data.length; i++) {
if (this.options.skip_forks && data[i].fork) {
continue;
}
repos.push(data[i]);
}
repos.sort(function(a, b) {
var aDate = new Date(a.pushed_at).valueOf(),
bDate = new Date(b.pushed_at).valueOf();
if (aDate === bDate) { return 0; }
return aDate > bDate ? -1 : 1;
});
if (this.options.count) {
repos.splice(this.options.count);
}
render(repos);
},
showRepos: function(options){
var req = "https://api.github.com/users/"+options.user+"/repos?callback=github.parseResult";
var head = document.getElementsByTagName("head").item(0);
var script = document.createElement("script");
this.options = options;
script.setAttribute("type", "text/javascript");
script.setAttribute("src", req);
head.appendChild(script);
}
};
})();