Main.js 4.0 KB

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