github.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var github = (function(){
  2. function escapeHtml(str) {
  3. return $('<div/>').text(str).html();
  4. }
  5. function render(target, repos){
  6. var i = 0, fragment = '', t = $(target)[0];
  7. fragment += '<ul class="list-group" id="github">';
  8. for(i = 0; i < repos.length; i++) {
  9. fragment += '<li class="list-group-item"><a href="'+repos[i].html_url+'">'+repos[i].name+'</a><p><small>'+escapeHtml(repos[i].description||'')+'</small></p></li>';
  10. }
  11. fragment += '</ul>';
  12. t.innerHTML = fragment;
  13. }
  14. return {
  15. showRepos: function(options){
  16. $.ajax({
  17. url: "https://api.github.com/users/"+options.user+"/repos?callback=?"
  18. , dataType: 'jsonp'
  19. , error: function (err) { $(options.target + ' li.loading').addClass('error').text("Error loading feed"); }
  20. , success: function(data) {
  21. var repos = [];
  22. if (!data || !data.data) { return; }
  23. for (var i = 0; i < data.data.length; i++) {
  24. if (options.skip_forks && data.data[i].fork) { continue; }
  25. repos.push(data.data[i]);
  26. }
  27. repos.sort(function(a, b) {
  28. var aDate = new Date(a.pushed_at).valueOf(),
  29. bDate = new Date(b.pushed_at).valueOf();
  30. if (aDate === bDate) { return 0; }
  31. return aDate > bDate ? -1 : 1;
  32. });
  33. if (options.count) { repos.splice(options.count); }
  34. render(options.target, repos);
  35. }
  36. });
  37. }
  38. };
  39. })();