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

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