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 17 kB

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