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

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