This repository has been archived on 2019-04-23. You can view files and clone it, but cannot push or open issues or pull requests.
GitHub-Widget/dist/widget.js
2016-07-28 23:35:15 +05:30

9 lines
4.0 KiB
JavaScript

$(document).ready(function(){$("body").append("<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}</style>");
$(".github-widget").append('<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 gh-widget-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 a=$(".github-widget").data("username");fetchRepos(a);fetchUserDetails(a)});function fetchRepos(a){$.get("https://api.github.com/users/"+a+"/repos",function(a){updateRepoDetails(topRepos(a));updateLastPush(lastPushedDay(a))})}function fetchUserDetails(a){$.get("https://api.github.com/users/"+a,function(a){updateUserDetails(a)})}function updateLastPush(a){$(".gh-widget-active-time").append("Last active: "+(a?a+" day(s) ago":"Today"))}
function lastPushedDay(a){for(var b=new Date,c,d=9999999999999,e=0;e<a.length;e++){var f=new Date(a[e].pushed_at);b-f<d&&(c=f,d=b-f)}return Math.floor((b-c)/864E5)}
function updateUserDetails(a){var b=$(".gh-widget-personal-details"),c=$(".gh-widget-stats");b.append('<div class="full-name">'+a.name+"</div>");a.bio&&b.append('<div class="bio">'+a.bio+"</div>");a.location&&b.append('<div class="location">&#9906; '+a.location+"</div>");c.append(' <div class="gh-widget-item"><div class="count">'+a.followers+'</div><div class="stat-name">gh-widget-followers</div></div>');c.append(' <div class="gh-widget-item"><div class="count">'+a.following+'</div><div class="stat-name">gh-widget-following</div></div>');
c.append(' <div class="gh-widget-item"><div class="count">'+a.public_repos+'</div><div class="stat-name">gh-widget-repositories</div></div>');$(".gh-widget-photo").append('<img src="'+a.avatar_url+'">');$(".gh-widget-follow").append('<button><a class="gh-widget-link" target="new" href="'+a.html_url+'">Follow</a></button>')}
function updateRepoDetails(a){for(var b=0;b<a.length;b++){var c=a[b].language?a[b].language:"Unknown";$(".gh-widget-repositories").append('<div class="gh-widget-container"><div class="gh-widget-item names"><div><a class="gh-widget-link" href="'+a[b].repoUrl+'">'+a[b].name+'</a></div></div><div class="gh-widget-item language"><div>'+c+'</div></div><div class="gh-widget-item stars"><div>&#9733;'+a[b].stars+"</div></div></div>")}}
function topRepos(a){a.sort(function(a,b){return a.stargazers_count===b.stargazers_count?0:a.stargazers_count>b.stargazers_count?-1:1});a=a.slice(0,3);var b=[],c;for(c in a){var d=a[c];b.push({name:d.name,stars:d.stargazers_count,language:d.language,repoUrl:d.html_url})}return b};