You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

home.js 14 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. var token;
  2. if(isEmpty(token)){
  3. var meta = $("meta[name=_uid]");
  4. if(!isEmpty(meta)){
  5. token = meta.attr("content");
  6. }
  7. }
  8. var swiperNewMessage = new Swiper(".newslist", {
  9. direction: "vertical",
  10. slidesPerView: 10,
  11. loop: true,
  12. autoplay: {
  13. delay: 2500,
  14. disableOnInteraction: false,
  15. },
  16. });
  17. var swiperRepo = new Swiper(".homepro-list", {
  18. slidesPerView: 3,
  19. slidesPerColumn: 2,
  20. slidesPerColumnFill:'row',
  21. spaceBetween: 30,
  22. pagination: {
  23. el: ".swiper-pagination",
  24. clickable: true,
  25. },
  26. autoplay: {
  27. delay: 2500,
  28. disableOnInteraction: false,
  29. },
  30. });
  31. var output = document.getElementById("newmessage");
  32. var url = "ws://" + document.location.host + "/action/notification";
  33. if(document.location.host == "git.openi.org.cn" || document.URL.startsWith("https")){
  34. url = "wss://" + document.location.host + "/action/notification"
  35. }
  36. var socket = new WebSocket(url);
  37. socket.onopen = function () {
  38. messageQueue = [];
  39. console.log("message has connected.");
  40. };
  41. var maxSize = 20;
  42. var html =document.documentElement;
  43. var lang = html.attributes["lang"]
  44. var isZh = true;
  45. if(lang != null && lang.nodeValue =="en-US" ){
  46. isZh=false;
  47. }else{
  48. }
  49. socket.onmessage = function (e) {
  50. var data =JSON.parse(e.data)
  51. var html = "";
  52. if (data != null){
  53. if(messageQueue.length > maxSize){
  54. delete messageQueue[0];
  55. }else{
  56. messageQueue.push(data);
  57. }
  58. var currentTime = new Date().getTime();
  59. for(var i = 0; i < messageQueue.length; i++){
  60. var record = messageQueue[i];
  61. var recordPrefix = getMsg(record);
  62. var actionName = getAction(record.OpType,isZh);
  63. if(record.OpType == "6" || record.OpType == "10" || record.OpType == "12" || record.OpType == "13"){
  64. html += recordPrefix + actionName;
  65. html += " <a href=\"" + getIssueLink(record) + "\" rel=\"nofollow\">" + getIssueText(record) + "</a>"
  66. }
  67. else if(record.OpType == "7" || record.OpType == "11" || record.OpType == "14" || record.OpType == "15" || record.OpType == "22"
  68. || record.OpType == "23"){
  69. html += recordPrefix + actionName;
  70. html += " <a href=\"" + getPRLink(record) + "\" rel=\"nofollow\">" + getPRText(record) + "</a>"
  71. }
  72. else if(record.OpType == "1"){
  73. html += recordPrefix + actionName;
  74. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" +getRepotext(record) + "</a>"
  75. }
  76. else if(record.OpType == "9" || record.OpType == "5"){
  77. branch = "<a href=\"" + getRepoLink(record) + "/src/branch/" + record.RefName + "\" rel=\"nofollow\">" + record.RefName + "</a>"
  78. actionName = actionName.replace("{branch}",branch);
  79. html += recordPrefix + actionName;
  80. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepotext(record) + "</a>"
  81. }else if(record.OpType == "17"){
  82. actionName = actionName.replace("{deleteBranchName}",record.RefName);
  83. var repoLink = "<a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepotext(record) + "</a>"
  84. actionName = actionName.replace("{repoName}",repoLink);
  85. html += recordPrefix + actionName;
  86. }
  87. else if(record.OpType == "2"){
  88. actionName = actionName.replace("{oldRepoName}",record.Content);
  89. html += recordPrefix + actionName;
  90. html += " <a href=\"" + getRepoLink(record) + "\" rel=\"nofollow\">" + getRepotext(record) + "</a>"
  91. }
  92. else{
  93. continue;
  94. }
  95. if(record.Repo != null){
  96. var time = getTime(record.CreatedUnix,currentTime);
  97. html += " " + time;
  98. }
  99. html += "</div>";
  100. html += "</div>";
  101. }
  102. }
  103. output.innerHTML = html;
  104. swiperNewMessage.updateSlides();
  105. swiperNewMessage.updateProgress();
  106. };
  107. function getMsg(record){
  108. var html ="";
  109. html += "<div class=\"swiper-slide item\">";
  110. html += " <img class=\"ui avatar image\" src=\"/user/avatar/" + record.ActUser.Name + "/-1\" alt=\"\">"
  111. html += " <div class=\"middle aligned content nowrap\">"
  112. html += " <a href=\"/" + record.ActUser.Name + "\" title=\"\">" + record.ActUser.Name + "</a>"
  113. return html;
  114. }
  115. function getRepotext(record){
  116. if(record.Repo.Alias){
  117. return record.Repo.OwnerName + "/" + record.Repo.Alias;
  118. }else{
  119. return record.Repo.OwnerName + "/" + record.Repo.Name;
  120. }
  121. }
  122. function getRepoLink(record){
  123. return record.Repo.OwnerName + "/" + record.Repo.Name;
  124. }
  125. function getTime(UpdatedUnix,currentTime){
  126. UpdatedUnix = UpdatedUnix;
  127. currentTime = currentTime / 1000;
  128. var timeEscSecond = currentTime - UpdatedUnix;
  129. if( timeEscSecond < 0){
  130. timeEscSecond = 1;
  131. }
  132. var hours= Math.floor(timeEscSecond / 3600);
  133. //计算相差分钟数
  134. var leave2 = Math.floor(timeEscSecond % (3600)); //计算小时数后剩余的秒数
  135. var minutes= Math.floor(leave2 / 60);//计算相差分钟数
  136. var leave3=Math.floor(leave2 % 60); //计算分钟数后剩余的秒数
  137. var seconds= leave3;
  138. if(hours == 0 && minutes == 0){
  139. return seconds + getRepoOrOrg(6,isZh,seconds);
  140. }else{
  141. if(hours > 0){
  142. return hours + getRepoOrOrg(4,isZh,hours);
  143. }else{
  144. return minutes + getRepoOrOrg(5,isZh,minutes);
  145. }
  146. }
  147. }
  148. function getPRLink(record){
  149. return "/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/pulls/" + getIssueId(record);
  150. }
  151. function getPRText(record){
  152. if(record.Repo.Alias){
  153. return record.Repo.OwnerName + "/" + record.Repo.Alias + "#" + getIssueId(record);
  154. }else{
  155. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + getIssueId(record);
  156. }
  157. }
  158. function getIssueLink(record){
  159. return "/" + record.Repo.OwnerName + "/" + record.Repo.Name + "/issues/" + getIssueId(record);
  160. }
  161. function getIssueId(record){
  162. var Id = "1";
  163. if(!isEmpty(record.Comment) && !isEmpty(record.Comment.Issue)){
  164. Id = record.Comment.Issue.Index;
  165. }else{
  166. if(!isEmpty(record.Content)){
  167. var content = record.Content;
  168. var index = content.indexOf("|");
  169. if(index != -1){
  170. Id = content.substring(0,index);
  171. }
  172. }
  173. }
  174. return Id;
  175. }
  176. function getIssueText(record){
  177. if(record.Repo.Alias){
  178. return record.Repo.OwnerName + "/" + record.Repo.Alias + "#" + getIssueId(record);
  179. }else{
  180. return record.Repo.OwnerName + "/" + record.Repo.Name + "#" + getIssueId(record);
  181. }
  182. }
  183. /*
  184. ActionCreateRepo ActionType = iota + 1 // 1
  185. ActionRenameRepo // 2
  186. ActionStarRepo // 3
  187. ActionWatchRepo // 4
  188. ActionCommitRepo // 5
  189. ActionCreateIssue // 6
  190. ActionCreatePullRequest // 7
  191. ActionTransferRepo // 8
  192. ActionPushTag // 9
  193. ActionCommentIssue // 10
  194. ActionMergePullRequest // 11
  195. ActionCloseIssue // 12
  196. ActionReopenIssue // 13
  197. ActionClosePullRequest // 14
  198. ActionReopenPullRequest // 15
  199. ActionDeleteTag // 16
  200. ActionDeleteBranch // 17
  201. ActionMirrorSyncPush // 18
  202. ActionMirrorSyncCreate // 19
  203. ActionMirrorSyncDelete // 20
  204. ActionApprovePullRequest // 21
  205. ActionRejectPullRequest // 22
  206. ActionCommentPull // 23
  207. */
  208. var actionNameZH={
  209. "1":"创建了项目",
  210. "2":"重命名项目 {oldRepoName} 为",
  211. "5":"推送了 {branch} 分支的代码到",
  212. "6":"创建了任务",
  213. "7":"创建了合并请求",
  214. "9":"推送了标签 {branch} 到",
  215. "10":"评论了任务",
  216. "11":"合并了合并请求",
  217. "12":"关闭了任务",
  218. "13":"重新开启了任务",
  219. "14":"关闭了合并请求",
  220. "15":"重新开启了合并请求",
  221. "17":"从 {repoName} 删除分支 {deleteBranchName}",
  222. "22":"建议变更",
  223. "23":"评论了合并请求"
  224. };
  225. var actionNameEN={
  226. "1":" created repository",
  227. "2":" renamed repository from {oldRepoName} to ",
  228. "5":" pushed to {branch} at",
  229. "6":" opened issue",
  230. "7":" created pull request",
  231. "9":" pushed tag {branch} to ",
  232. "10":" commented on issue",
  233. "11":" merged pull request",
  234. "12":" closed issue",
  235. "13":" reopened issue",
  236. "14":" closed pull request",
  237. "15":" reopened pull request",
  238. "17":" deleted branch {deleteBranchName} from {repoName}",
  239. "22":" proposed changes",
  240. "23":" commented on pull request"
  241. };
  242. var repoAndOrgZH={
  243. "1":"项目",
  244. "2":"成员",
  245. "3":"团队",
  246. "11":"项目",
  247. "21":"成员",
  248. "31":"团队",
  249. "4":"小时前",
  250. "5":"分钟前",
  251. "6":"秒前",
  252. "41":"小时前",
  253. "51":"分钟前",
  254. "61":"秒前"
  255. };
  256. var repoAndOrgEN={
  257. "1":"Repository",
  258. "2":"Member ",
  259. "3":"Team",
  260. "11":"Repositories",
  261. "21":"Members ",
  262. "31":"Teams",
  263. "4":" hour ago",
  264. "5":" minute ago",
  265. "6":" second ago",
  266. "41":" hours ago",
  267. "51":" minutes ago",
  268. "61":" seconds ago"
  269. };
  270. function getAction(opType,isZh){
  271. if(isZh){
  272. return actionNameZH[opType]
  273. }else{
  274. return actionNameEN[opType]
  275. }
  276. }
  277. queryRecommendData();
  278. function queryRecommendData(){
  279. $.ajax({
  280. type:"GET",
  281. url:"/recommend/org",
  282. headers: {
  283. authorization:token,
  284. },
  285. dataType:"json",
  286. async:false,
  287. success:function(json){
  288. displayOrg(json);
  289. },
  290. error:function(response) {
  291. }
  292. });
  293. $.ajax({
  294. type:"GET",
  295. url:"/recommend/repo",
  296. headers: {
  297. authorization:token,
  298. },
  299. dataType:"json",
  300. async:false,
  301. success:function(json){
  302. displayRepo(json);
  303. },
  304. error:function(response) {
  305. }
  306. });
  307. }
  308. function displayRepo(json){
  309. var orgRepo = document.getElementById("recommendrepo");
  310. var html = "";
  311. if (json != null && json.length > 0){
  312. for(var i = 0; i < json.length;i++){
  313. var record = json[i]
  314. html += "<div class=\"swiper-slide\">";
  315. html += " <div class=\"ui fluid card\">";
  316. html += " <div class=\"content\">";
  317. html += " <span class=\"right floated meta\">";
  318. html += " <i class=\"ri-star-line\"></i>" + record["NumStars"] + "<i class=\"ri-git-branch-line am-ml-10\"></i>" + record["NumForks"];
  319. html += " </span>";
  320. html += " <img class=\"left floated mini ui image\" src=\"" + record["Avatar"] + "\">";
  321. html += " <a class=\"header nowrap\" href=\"/" + record["OwnerName"] + "/" + record["Name"] + "\">" + record["Alias"] +"</a>";
  322. html += " <div class=\"description nowrap-2\">" + record["Description"] + " </div>";
  323. html += " <div class=\"ui tags nowrap am-mt-10\">"
  324. if(record["Topics"] != null){
  325. for(var j = 0; j < record["Topics"].length; j++){
  326. topic = record["Topics"][j];
  327. url = "/explore/repos?q=" + (topic) + "&amp;topic="
  328. html += "<a class=\"ui small label topic\" href=\"" + url + "\">" + topic + "</a>";
  329. }
  330. }
  331. html += " </div>";
  332. html += " </div>";
  333. html += " </div>";
  334. html += "</div>";
  335. }
  336. }
  337. orgRepo.innerHTML = html;
  338. swiperRepo.updateSlides();
  339. swiperRepo.updateProgress();
  340. }
  341. function getRepoOrOrg(key,isZhLang,numbers=1){
  342. if(numbers > 1){
  343. key+="1";
  344. }
  345. if(isZhLang){
  346. return repoAndOrgZH[key];
  347. }else{
  348. return repoAndOrgEN[key];
  349. }
  350. }
  351. function displayOrg(json){
  352. var orgDiv = document.getElementById("recommendorg");
  353. var html = "";
  354. if (json != null && json.length > 0){
  355. for(var i = 0; i < json.length;i++){
  356. var record = json[i]
  357. html += "<div class=\"column\">";
  358. html += " <a href=\"/" + record["Name"] + "\" class=\"ui fluid card\">";
  359. html += " <div class=\"content\">";
  360. html += " <div class=\"ui small header\">";
  361. html += " <img class=\"ui image\" src=\"" + record["Avatar"] + "\">";
  362. html += " <div class=\"content nowrap\">";
  363. html += " <span class=\"ui blue\">" + record["Name"] + "</span> " + record["FullName"];
  364. html += " <div class=\"sub header\">" + record["NumRepos"] +" " + getRepoOrOrg(1,isZh,record["NumRepos"]) + " ・ " + record["NumMembers"] +" " + getRepoOrOrg(2,isZh,record["NumMembers"]) + " ・ " + record["NumTeams"] + " " + getRepoOrOrg(3,isZh,record["NumTeams"]) + "</div>";
  365. html += " </div>";
  366. html += " </div>";
  367. html += " </div>";
  368. html += " </a>";
  369. html += "</div>";
  370. }
  371. }
  372. orgDiv.innerHTML = html;
  373. }