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