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