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.

search.js 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 html =document.documentElement;
  10. var lang = html.attributes["lang"]
  11. var isZh = true;
  12. if(lang != null && lang.nodeValue =="en-US" ){
  13. console.log("the language is " + lang.nodeValue);
  14. isZh=false;
  15. }else{
  16. console.log("default lang=zh");
  17. }
  18. function isEmpty(str){
  19. if(typeof str == "undefined" || str == null || str == ""){
  20. return true;
  21. }
  22. return false;
  23. }
  24. var itemType={
  25. "1":"repository",
  26. "2":"issue",
  27. "3":"user",
  28. "4":"org",
  29. "5":"dataset",
  30. "6":"pr"
  31. };
  32. var sortBy={
  33. "11":"updated_unix.keyword",
  34. "12":"num_watches.keyword",
  35. "13":"num_stars.keyword",
  36. "14":"num_forks.keyword",
  37. "21":"updated_unix.keyword",
  38. "31":"created_unix.keyword",
  39. "32":"name",
  40. "41":"created_unix.keyword",
  41. "42":"name",
  42. "51":"download_times.keyword",
  43. "61":"updated_unix.keyword"
  44. };
  45. function searchItem(type,sortType){
  46. console.log("enter here.");
  47. var keyword = document.getElementById("keyword_input").value;
  48. var page =1;
  49. var pageSize=15;
  50. if(!isEmpty(keyword)){
  51. doSearch(itemType[type],keyword,page,pageSize,false,sortBy[sortType]);
  52. }
  53. }
  54. function search(){
  55. console.log("enter here.");
  56. var keyword = document.getElementById("keyword_input").value;
  57. var page =1;
  58. var pageSize=15;
  59. if(!isEmpty(keyword)){
  60. doSearch("repository",keyword,page,pageSize,false,sortBy[11]);
  61. doSearch("issue",keyword,page,pageSize,true);
  62. //doSearch("user",keyword,page,pageSize,true);
  63. //doSearch("org",keyword,page,pageSize,true);
  64. //doSearch("dataset",keyword,page,pageSize,true);
  65. //doSearch("pr",keyword,page,pageSize,true);
  66. }
  67. }
  68. function doSearch(tableName,keyword,page,pageSize=15,onlyReturnNum=true,sortBy){
  69. $.ajax({
  70. type:"GET",
  71. url:"/all/dosearch/",
  72. headers: {
  73. authorization:token,
  74. },
  75. dataType:"json",
  76. dataType:"json",
  77. data:{
  78. 'TableName': tableName,
  79. 'Key': keyword,
  80. 'Page': page,
  81. 'PageSize': pageSize,
  82. 'OnlyReturnNum':onlyReturnNum,
  83. 'SortBy':sortBy
  84. },
  85. async:true,
  86. success:function(json){
  87. console.log(json);
  88. displayResult(tableName,page,json,onlyReturnNum,keyword);
  89. },
  90. error:function(response) {
  91. console.log(response);
  92. }
  93. });
  94. }
  95. function displayResult(tableName,page,jsonResult,onlyReturnNum,keyword){
  96. if(tableName == "repository") {
  97. displayRepoResult(page,jsonResult,onlyReturnNum,keyword);
  98. } else if (tableName == "issue") {
  99. displayIssueResult(page,jsonResult,onlyReturnNum,keyword);
  100. } else if (tableName == "user") {
  101. displayUserResult(page,jsonResult,onlyReturnNum,keyword);
  102. } else if (tableName == "org") {
  103. displayOrgResult(page,jsonResult,onlyReturnNum,keyword);
  104. } else if (tableName == "dataset") {
  105. displayDataSetResult(page,jsonResult,onlyReturnNum,keyword);
  106. } else if (tableName == "pr") {
  107. displayPrResult(page,jsonResult,onlyReturnNum,keyword);
  108. }
  109. }
  110. function displayPrResult(page,jsonResult,onlyReturnNum,keyword){
  111. var data = jsonResult.Result;
  112. var total = jsonResult.Total;
  113. $('#pr_total').text(total);
  114. if(!onlyReturnNum){
  115. $('#keyword_desc').text(keyword);
  116. $('#obj_desc').text("合并请求");
  117. $('#child_total').text(total);
  118. }
  119. }
  120. function displayDataSetResult(page,jsonResult,onlyReturnNum,keyword){
  121. var data = jsonResult.Result;
  122. var total = jsonResult.Total;
  123. $('#dataset_total').text(total);
  124. if(!onlyReturnNum){
  125. $('#keyword_desc').text(keyword);
  126. $('#obj_desc').text("数据集");
  127. $('#child_total').text(total);
  128. }
  129. }
  130. function displayOrgResult(page,jsonResult,onlyReturnNum,keyword){
  131. var data = jsonResult.Result;
  132. var total = jsonResult.Total;
  133. $('#org_total').text(total);
  134. if(!onlyReturnNum){
  135. $('#keyword_desc').text(keyword);
  136. $('#obj_desc').text("组织");
  137. $('#child_total').text(total);
  138. }
  139. }
  140. function displayUserResult(page,jsonResult,onlyReturnNum,keyword){
  141. var data = jsonResult.Result;
  142. var total = jsonResult.Total;
  143. $('#user_total').text(total);
  144. if(!onlyReturnNum){
  145. $('#keyword_desc').text(keyword);
  146. $('#obj_desc').text("用户");
  147. $('#child_total').text(total);
  148. }
  149. }
  150. function displayIssueResult(page,jsonResult,onlyReturnNum,keyword){
  151. var data = jsonResult.Result;
  152. var total = jsonResult.Total;
  153. $('#issue_total').text(total);
  154. if(!onlyReturnNum){
  155. setActivate("issue_item");
  156. $('#keyword_desc').text(keyword);
  157. $('#obj_desc').text("任务");
  158. $('#child_total').text(total);
  159. var sortHtml = "";
  160. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(2,21);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">最近更新</a>"
  161. var html = "";
  162. var currentTime = new Date().getTime();
  163. for(var i = 0; i < data.length;i++){
  164. var recordMap = data[i];
  165. html += "<div class=\"item\">";
  166. html += " <div class=\"content\">";
  167. html += " <div class=\"ui header\">";
  168. html += " <a class=\"name\" href=\"/" + record["repoUrl"] +"\">" + record["name"] + "</a>";
  169. html +=" </div>";
  170. html += " <div class=\"description\">";
  171. html += " <p class=\"has-emoji\"> " + recordMap["description"] + "</p>";
  172. html +=" <p class=\"time\">";
  173. html +=" <i class=\"ri-code-box-line\"></i>";
  174. html +=" <a class=\"am-text grey\" href=\"/" + record["repoUrl"] +"/" + record["id"] + "\"> " + record["repoUrl"] +"#" + record["id"] + "</a>";
  175. html +=" <i class=\"ri-information-line am-ml-10\"></i> ";
  176. if(isEmpty(record["is_closed"]) || record["is_closed"]=="false"){
  177. html += "关闭";
  178. }else{
  179. html += "开启中";
  180. }
  181. html +=" <span class=\"am-ml-10\"></span> 最后更新于 <span class=\"time-since poping up\" title=\"\" data-content=\"\" data-variation=\"inverted tiny\">" + getTime(recordMap["updated_unix"],currentTime) +"</span>";
  182. html +=" </p>";
  183. html +=" </div>";
  184. html +=" </div>";
  185. html +="</div>";
  186. }
  187. }
  188. }
  189. function setActivate(name){
  190. $('#repo_item').removeClass("active");
  191. $('#user_item').removeClass("active");
  192. $('#issue_item').removeClass("active");
  193. $('#dataset_item').removeClass("active");
  194. $('#org_item').removeClass("active");
  195. $('#pr_item').removeClass("active");
  196. var tmp = "#" + name;
  197. $(tmp).addClass("active");
  198. }
  199. function displayRepoResult(page,jsonResult,onlyReturnNum,keyword){
  200. var data = jsonResult.Result;
  201. var total = jsonResult.Total;
  202. $('#repo_total').text(total);
  203. if(!onlyReturnNum){
  204. setActivate("repo_item");
  205. $('#keyword_desc').text(keyword);
  206. $('#obj_desc').text("项目");
  207. $('#child_total').text(total);
  208. var sortHtml = "";
  209. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,11);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">最近更新</a>"
  210. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,12);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">关注数</a>"
  211. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,13);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">点赞数</a>"
  212. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,14);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">Fork数</a>"
  213. document.getElementById("sort_type").innerHTML=sortHtml;
  214. var html = "";
  215. var currentTime = new Date().getTime();
  216. for(var i = 0; i < data.length;i++){
  217. var recordMap = data[i];
  218. html += "<div class=\"item\">";
  219. if(!isEmpty(recordMap['avatar'])){
  220. html += "<img class=\"ui avatar image\" src=\"" + recordMap['avatar'] + "\">";
  221. }
  222. html += " <div class=\"content\">";
  223. html += " <div class=\"ui header\">";
  224. html += " <a class=\"name\" href=\"/" + recordMap["owner_name"] + "/" + recordMap["real_name"] +"\"> <span class=\"highlight\">" + recordMap["owner_name"] +"</span> <span>/</span> <strong>" + recordMap["name"] + "</strong></a>";
  225. html +=" <span class=\"middle\"><svg class=\"svg octicon-repo-clone\" width=\"16\" height=\"16\" aria-hidden=\"true\"><use xlink:href=\"#octicon-repo-clone\"></use></svg></span>";
  226. html +=" </div>";
  227. html += " <div class=\"description\">";
  228. html += " <p class=\"has-emoji\"> " + recordMap["description"] + "</p>";
  229. html += " <div class=\"ui tags\">";
  230. if(!isEmpty(recordMap["topics"]) && recordMap["topics"] !="null"){
  231. for(var j = 0; j < recordMap["topics"].length;j++){
  232. html +=" <a href=\"/explore/repos?q=" + recordMap["topics"][j] + "&amp;topic=\"><div class=\"ui small label topic\">"+ recordMap["topics"][j] + "</div></a>";
  233. }
  234. }
  235. html +=" </div>";
  236. html +=" <p class=\"time\">";
  237. html +=" <i class=\"ri-star-line\"></i>" + recordMap["num_stars"] + "<i class=\"ri-git-branch-line am-ml-10\"></i>" + recordMap["num_forks"];
  238. html +=" 最后更新于 <span class=\"time-since poping up\" title=\"\" data-content=\"\" data-variation=\"inverted tiny\">" + getTime(recordMap["updated_unix"],currentTime) +"</span>";
  239. if(!isEmpty(recordMap["lang"])){
  240. html +=" <span class=\"text grey am-ml-10\"><i class=\"color-icon\" style=\"background-color: #3572A5\"></i>" + recordMap["lang"] + "</span>";
  241. }
  242. html +=" </p>";
  243. html +=" </div>";
  244. html +=" </div>";
  245. html +="</div>";
  246. }
  247. document.getElementById("child_search_item").innerHTML=html;
  248. }
  249. }
  250. function getTime(UpdatedUnix,currentTime){
  251. UpdatedUnix = UpdatedUnix;
  252. currentTime = currentTime / 1000;
  253. var timeEscSecond = currentTime - UpdatedUnix;
  254. if( timeEscSecond < 0){
  255. timeEscSecond = 1;
  256. }
  257. console.log("currentTime=" + currentTime + " updateUnix=" + UpdatedUnix);
  258. var hours= Math.floor(timeEscSecond / 3600);
  259. //计算相差分钟数
  260. var leave2 = Math.floor(timeEscSecond % (3600)); //计算小时数后剩余的秒数
  261. var minutes= Math.floor(leave2 / 60);//计算相差分钟数
  262. var leave3=Math.floor(leave2 % 60); //计算分钟数后剩余的秒数
  263. var seconds= leave3;
  264. if(hours == 0 && minutes == 0){
  265. return seconds + getRepoOrOrg(6,isZh);
  266. }else{
  267. if(hours > 0){
  268. if(hours >= 24){
  269. return Math.ceil(hours/24) + getRepoOrOrg(7,isZh);
  270. }else{
  271. return hours + getRepoOrOrg(4,isZh);
  272. }
  273. }else{
  274. return minutes + getRepoOrOrg(5,isZh);
  275. }
  276. }
  277. }
  278. function getRepoOrOrg(key,isZhLang){
  279. if(isZhLang){
  280. return repoAndOrgZH[key];
  281. }else{
  282. return repoAndOrgEN[key];
  283. }
  284. }
  285. var repoAndOrgZH={
  286. "1":"项目",
  287. "2":"成员",
  288. "3":"团队",
  289. "4":"小时前",
  290. "5":"分钟前",
  291. "6":"秒前",
  292. "7":"天前"
  293. };
  294. var repoAndOrgEN={
  295. "1":"repository",
  296. "2":"Members ",
  297. "3":"Teams",
  298. "4":" hours ago",
  299. "5":" minutes ago",
  300. "6":" seconds ago",
  301. "7":" days ago"
  302. };