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

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