Main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import CacheUtils from "./CacheUtils.js";
  2. import GithubUtils from "./GithubUtils.js";
  3. import OpenCollectiveUtils from "./OpenCollectiveUtils.js";
  4. class OpenCollectiveService{
  5. static async init(){
  6. const collective="jmonkeyengine";
  7. this.backers=await OpenCollectiveUtils.getBackersFlat(collective,this.backers,(res)=>window.shuffleArray(res));
  8. console.assert(this.backers,"Backers can't be undefined");
  9. this.backersWithMessage = this.backers.filter(el=>el.message);
  10. this.cycleBackers();
  11. this.cycleMessage();
  12. setInterval(() =>{
  13. this.cycleBackers();
  14. this.cycleMessage();
  15. }, 5000);
  16. }
  17. static async cycleBackers () {
  18. this.shownBacker=CacheUtils.cachedCycle("backers",0,this.backers.length-1,10000);
  19. const backer = this.backers[this.shownBacker];
  20. document.querySelectorAll("#backerName").forEach(el=>{
  21. el.setAttribute("href", backer.accountUrl);
  22. el.innerHTML = backer.name;
  23. });
  24. }
  25. static async cycleMessage () {
  26. this.shownBackerMessage=CacheUtils.cachedCycle("backersMex",0,this.backersWithMessage.length-1,10000);
  27. const backer = this.backersWithMessage[this.shownBackerMessage];
  28. document.querySelectorAll("#backerMessage").forEach(el=>{
  29. el.innerHTML = backer.message?backer.message:" ";
  30. });
  31. document.querySelectorAll("#backerMessageName").forEach(el=>{
  32. el.innerText = "- "+backer.name;
  33. el.setAttribute("href", backer.accountUrl);
  34. });
  35. }
  36. }
  37. class GithubService{
  38. static async init(){
  39. this.contributors=[];
  40. this.contributors.push(...await GithubUtils.getTop100Contributors("jMonkeyEngine/jmonkeyengine"),(res)=>window.shuffleArray(res));
  41. this.contributors.push(...await GithubUtils.getTop100Contributors("jMonkeyEngine/sdk"),(res)=>window.shuffleArray(res));
  42. this.contributors.push(...await GithubUtils.getTop100Contributors("jMonkeyEngine/wiki"),(res)=>window.shuffleArray(res));
  43. this.resolveUsers();
  44. this.cycleContributors();
  45. setInterval(() =>{
  46. this.cycleContributors();
  47. }, 5000);
  48. }
  49. static async cycleContributors () {
  50. this.shownContributor=CacheUtils.cachedCycle("contribs",0,this.contributors.length-1,10000);
  51. let contributor = this.contributors[this.shownContributor];
  52. contributor=await GithubUtils.resolveUser(contributor.login);
  53. document.querySelectorAll("#contributorName").forEach(contributorNameEl=>{
  54. contributorNameEl.innerText = contributor.name?contributor.name:contributor.login;
  55. contributorNameEl.setAttribute("href", contributor.html_url);
  56. });
  57. }
  58. static async resolveUsers(){
  59. console.log("Look for github users to resolve...")
  60. document.querySelectorAll("[github-user]").forEach(el=>{
  61. const user=el.getAttribute("github-user");
  62. console.log("Found user needing resolution",el,user)
  63. GithubUtils.resolveUser(user).then(user=>{
  64. console.log(user,"resolved!");
  65. const properties={};
  66. properties.name=user.name;
  67. if(!properties.name)properties.name=user.login;
  68. properties.link=user.html_url;
  69. properties.bio=user.bio;
  70. properties.twitter_username=user.twitter_username;
  71. properties.twitter_link=user.twitter_username?"https://twitter.com/"+user.twitter_username:undefined;
  72. properties.public_repos=user.public_repos;
  73. properties.hireable=user.hireable;
  74. properties.email=user.email;
  75. properties.blog=user.blog;
  76. properties.avatar_url=user.avatar_url;
  77. for(let k in properties){
  78. const v=properties[k];
  79. if(!v)continue;
  80. el.querySelectorAll(".gh"+k).forEach(eel=>eel.style.display="inline-block");
  81. el.querySelectorAll("a.gh"+k).forEach(eel=>eel.setAttribute("href",v));
  82. el.querySelectorAll("span.gh"+k).forEach(eel=>eel.innerText=v);
  83. }
  84. });
  85. });
  86. }
  87. }
  88. window.addEventListener("load", function () {
  89. OpenCollectiveService.init();
  90. GithubService.init();
  91. });