-
Notifications
You must be signed in to change notification settings - Fork 36
/
widget.js
140 lines (110 loc) · 6.31 KB
/
widget.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
function appendToWidget(parentSelector, tag, classes, html) {
var parentNode = document.querySelector(parentSelector);
var childNode = document.createElement(tag);
childNode.innerHTML = html;
childNode.className += classes;
parentNode.appendChild(childNode);
}
function getJSON(url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (request.status === 200) {
var data = JSON.parse(request.responseText);
callback(data);
}
};
request.send();
}
function ready(fn) {
if (document.readyState != 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
function start() {
appendToWidget("body", "style", "", "@import url(https://fonts.googleapis.com/css?family=Noto+Sans:400,700);.gh-widget-link,.gh-widget-link:hover{text-decoration:none}.gh-widget-container{display:flex;flex-direction:row;flex-wrap:no-wrap;align-items:center;justify-content:center;color:#333;font-family:'Noto Sans',sans-serif}.gh-widget-personal-details .bio,.gh-widget-stats .count{color:#4078C0}.github-widget{border:1px solid #DDD;max-width:350px}.gh-widget-item{flex:1;text-align:center;padding:10px}.gh-widget-repositories .language{text-align:left}.gh-widget-repositories .language div,.gh-widget-repositories .stars div{padding:5px 0}.gh-widget-photo{flex:2}.gh-widget-photo img{border-radius:100%;max-width:90px}.gh-widget-personal-details{flex:6}.gh-widget-personal-details .full-name{font-size:1.5em;line-height:1.5em}.gh-widget-personal-details .location{font-size:.8em}.gh-widget-stats .count{font-size:1.2em;font-weight:700}.gh-widget-repositories .names{flex:2;text-align:left}.gh-widget-repositories .names div{padding:5px 0;text-overflow:ellipsis}.gh-widget-follow{flex:2}.gh-widget-active-time{flex:4;font-size:.8em}.gh-widget-heading{font-weight:400;color:#666}.gh-widget-hr{border:1px solid #DDD}.gh-widget-link{color:#4078C0}.gh-widget-follow button{width:100%;height:2em;border:none;background:#ddd}");
var widgets = document.querySelectorAll('.github-widget');
for (var i = 0; i < widgets.length; i++) {
var parentNode = widgets[i];
parentNode.setAttribute("id", "widget" + i);
appendToWidget("#widget" + i, "div", "", '<div class="gh-widget-container"><div class="gh-widget-item gh-widget-photo"></div><div class="gh-widget-item gh-widget-personal-details"></div></div><div class="gh-widget-container gh-widget-stats"></div><hr class="gh-widget-hr"><div class="gh-widget-container"><div class="gh-widget-item gh-widget-heading">Top repositories</div></div><div class="gh-widget-repositories"></div><div class="gh-widget-container"><div class="gh-widget-item gh-widget-follow"></div><div class="gh-widget-item gh-widget-active-time"></div></div>')
var username = parentNode.dataset.username;
fetchRepos(username, "#widget" + i);
fetchUserDetails(username, "#widget" + i);
}
}
ready(start);
function fetchRepos(username, widgetId) {
var url = "https://api.github.com/users/" + username + "/repos?per_page=1000";
getJSON(url, function(response) {
updateRepoDetails(topRepos(response), widgetId);
updateLastPush(lastPushedDay(response), widgetId);
});
}
function fetchUserDetails(username, widgetId) {
var url = "https://api.github.com/users/" + username;
getJSON(url, function(response) {
updateUserDetails(response, widgetId);
});
}
function updateLastPush(lastDay, widgetId) {
appendToWidget(widgetId + " .gh-widget-active-time", "span", "", 'Last active: ' + (lastDay ? lastDay + ' day(s) ago' : 'Today'));
}
function lastPushedDay(repos) {
var now = new Date();
var latestDate;
var difference = 9999999999999;
for (var i = 0; i < repos.length; i++) {
var pushedDate = new Date(repos[i].pushed_at)
if (now - pushedDate < difference) {
latestDate = pushedDate;
difference = now - pushedDate;
}
}
return Math.floor((now - latestDate) / (1000 * 3600 * 24));
}
function updateUserDetails(user, widgetId) {
appendToWidget(widgetId + " .gh-widget-personal-details", "div", "full-name", user.name);
if (user.bio) {
appendToWidget(widgetId + " .gh-widget-personal-details", "div", "bio", user.bio);
}
if (user.location) {
appendToWidget(widgetId + " .gh-widget-personal-details", "div", "location", '⚲ ' + user.location);
}
appendToWidget(widgetId + " .gh-widget-stats", "div", "gh-widget-item", '<div class="count">' + user.followers + '</div><div class="stat-name">Followers</div>');
appendToWidget(widgetId + " .gh-widget-stats", "div", "gh-widget-item", '<div class="count">' + user.following + '</div><div class="stat-name">Following</div>');
appendToWidget(widgetId + " .gh-widget-stats", "div", "gh-widget-item", '<div class="count">' + user.public_repos + '</div><div class="stat-name">Repositories</div>');
appendToWidget(widgetId + " .gh-widget-photo", "span", "", '<img src="' + user.avatar_url + '">');
appendToWidget(widgetId + " .gh-widget-follow", "button", "", '<a class="gh-widget-link" target="new" href="' + user.html_url + '">Follow</a>')
}
function updateRepoDetails(repos, widgetId) {
for (var i = 0; i < repos.length; i++) {
var language = repos[i].language ? repos[i].language : "Unknown";
appendToWidget(widgetId + " .gh-widget-repositories", "div", "gh-widget-container", '<div class="gh-widget-item names"><div><a class="gh-widget-link" href="' + repos[i].repoUrl + '">' + repos[i].name + '</a></div></div><div class="gh-widget-item language"><div>' + language + '</div></div><div class="gh-widget-item stars"><div>★' + repos[i].stars + '</div></div>');
}
}
function topRepos(repos) {
repos.sort(function(a, b) {
if (a.stargazers_count === b.stargazers_count) {
return 0;
} else if (a.stargazers_count > b.stargazers_count) {
return -1;
} else {
return 1;
}
})
repos = repos.slice(0, 3);
var result = [];
for (var i in repos) {
var repo = repos[i];
result.push({
name: repo.name,
stars: repo.stargazers_count,
language: repo.language,
repoUrl: repo.html_url
});
}
return result;
}