android.moparisthebest.org/source/javascripts/github.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2011-09-27 01:58:01 -04:00
var github = (function(){
2012-06-05 17:09:26 -04:00
function render(target, data){
var i = 0, repos = '';
2011-09-04 09:56:34 -04:00
2012-06-05 17:09:26 -04:00
for(i = 0; i < data.length; i++) {
repos += '<li><a href="'+data[i].html_url+'">'+data[i].name+'</a><p>'+data[i].description+'</p></li>';
2011-09-04 09:56:34 -04:00
}
2012-06-05 17:09:26 -04:00
target.html(repos);
2011-09-27 01:58:01 -04:00
}
return {
showRepos: function(options){
$.ajax({
2012-05-25 00:05:33 -04:00
url: "https://api.github.com/users/"+options.user+"/repos?callback=?"
, dataType: 'jsonp'
2012-06-05 17:09:26 -04:00
, error: function (err) { options.target.find('.loading').addClass('error').text("Error loading feed"); }
, success: function(data) {
2011-09-27 01:58:01 -04:00
var repos = [];
2012-05-25 00:05:33 -04:00
if (!data.data) { return; }
for (var i = 0; i < data.data.length; i++) {
if (options.skip_forks && data.data[i].fork) { continue; }
repos.push(data.data[i]);
2011-09-27 01:58:01 -04:00
}
repos.sort(function(a, b) {
var aDate = new Date(a.pushed_at).valueOf(),
bDate = new Date(b.pushed_at).valueOf();
2011-09-04 09:56:34 -04:00
2011-09-27 01:58:01 -04:00
if (aDate === bDate) { return 0; }
return aDate > bDate ? -1 : 1;
});
2011-09-04 09:56:34 -04:00
2011-09-27 01:58:01 -04:00
if (options.count) { repos.splice(options.count); }
render(options.target, repos);
2011-09-04 09:56:34 -04:00
}
});
2011-09-27 01:58:01 -04:00
}
};
})();
2012-06-05 17:09:26 -04:00
$(document).ready(function(){
g = $('#gh_repos');
github.showRepos({
user: g.attr('data-user')
, count: parseInt(g.attr('data-count'))
, skip_forks: g.attr('data-skip') == 'true'
, target: g
});
});