github.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var github = (function(){
  2. function render(repos){
  3. var i = 0, fragment = '';
  4. var t = document.getElementById('gh-repos');
  5. for(i = 0; i < repos.length; i++) {
  6. fragment += '<li><a href="'+repos[i].html_url+'">'+repos[i].name+'</a><p>'+repos[i].description+'</p></li>';
  7. }
  8. t.innerHTML = fragment;
  9. }
  10. return {
  11. options: {},
  12. parseResult: function(result) {
  13. if (!result || !result.data) {
  14. return;
  15. }
  16. var data = result.data;
  17. var repos = [];
  18. for (var i = 0; i < data.length; i++) {
  19. if (this.options.skip_forks && data[i].fork) {
  20. continue;
  21. }
  22. repos.push(data[i]);
  23. }
  24. repos.sort(function(a, b) {
  25. var aDate = new Date(a.pushed_at).valueOf(),
  26. bDate = new Date(b.pushed_at).valueOf();
  27. if (aDate === bDate) { return 0; }
  28. return aDate > bDate ? -1 : 1;
  29. });
  30. if (this.options.count) {
  31. repos.splice(this.options.count);
  32. }
  33. render(repos);
  34. },
  35. showRepos: function(options){
  36. var req = "https://api.github.com/users/"+options.user+"/repos?callback=github.parseResult";
  37. var head = document.getElementsByTagName("head").item(0);
  38. var script = document.createElement("script");
  39. this.options = options;
  40. script.setAttribute("type", "text/javascript");
  41. script.setAttribute("src", req);
  42. head.appendChild(script);
  43. }
  44. };
  45. })();