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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. setActivate("pr_item");
  116. $('#keyword_desc').text(keyword);
  117. $('#obj_desc').text("合并请求");
  118. $('#child_total').text(total);
  119. setIssueOrPrInnerHtml(data);
  120. }
  121. }
  122. function displayDataSetResult(page,jsonResult,onlyReturnNum,keyword){
  123. var data = jsonResult.Result;
  124. var total = jsonResult.Total;
  125. $('#dataset_total').text(total);
  126. if(!onlyReturnNum){
  127. setActivate("dataset_item");
  128. $('#keyword_desc').text(keyword);
  129. $('#obj_desc').text("数据集");
  130. $('#child_total').text(total);
  131. var sortHtml = "";
  132. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(4,41);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">最近更新</a>";
  133. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(4,42);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_2\">名称自然序</a>";
  134. document.getElementById("sort_type").innerHTML=sortHtml;
  135. var html = "";
  136. var currentTime = new Date().getTime();
  137. for(var i = 0; i < data.length;i++){
  138. var recordMap = data[i];
  139. html += "<div class=\"item\">";
  140. html += " <div class=\"content\">";
  141. html += " <div class=\"ui right metas\">" ;
  142. html += " <span class=\"text grey\"><i class=\"ri-fire-line\"></i> " +recordMap["download_times"] + "</span> ";
  143. html +=" </div>";
  144. html += " <div class=\"ui header\">";
  145. html += " <a class=\"name\" href=\"/" +recordMap["repoUrl"] +"/datasets?type=0\">" + recordMap["repoUrl"] + "</a>";
  146. 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>";
  147. html +=" </div>";
  148. html += " <div class=\"description\">";
  149. html += " <p class=\"has-emoji\"> " + recordMap["description"] + "</p>";
  150. html += " <p class=\"has-emoji\"> " + recordMap["file_name"] + "</p>";
  151. html +=" <p class=\"time\">";
  152. html +=" <span class=\"am-ml-10\"></span> 最后更新于 <span class=\"time-since poping up\" title=\"\" data-content=\"\" data-variation=\"inverted tiny\">" + getTime(recordMap["created_unix"],currentTime) +"</span>";
  153. html +=" </p>";
  154. html +=" </div>";
  155. html +=" </div>";
  156. html +="</div>";
  157. }
  158. document.getElementById("child_search_item").innerHTML=html;
  159. }
  160. }
  161. function displayOrgResult(page,jsonResult,onlyReturnNum,keyword){
  162. var data = jsonResult.Result;
  163. var total = jsonResult.Total;
  164. $('#org_total').text(total);
  165. if(!onlyReturnNum){
  166. setActivate("org_item");
  167. $('#keyword_desc').text(keyword);
  168. $('#obj_desc').text("组织");
  169. $('#child_total').text(total);
  170. var sortHtml = "";
  171. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(4,41);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">最近更新</a>";
  172. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(4,42);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_2\">名称自然序</a>";
  173. document.getElementById("sort_type").innerHTML=sortHtml;
  174. var html = "";
  175. var currentTime = new Date().getTime();
  176. for(var i = 0; i < data.length;i++){
  177. var recordMap = data[i];
  178. html += "<div class=\"item members\">";
  179. html += "<img class=\"ui avatar image\" src=\"" + recordMap["avatar"] + "\"></img>";
  180. html += " <div class=\"content\">";
  181. html += " <div class=\"ui header\">";
  182. html += " <a class=\"name\" href=\"/" + recordMap["repoUrl"] +"\">" + recordMap["name"] + "</a>";
  183. html +=" </div>";
  184. html += " <div class=\"description\">";
  185. html += " <p class=\"has-emoji\"> " + recordMap["description"] + "</p>";
  186. html +=" <p class=\"has-emoji\">";
  187. html +=" <i class=\"ri-map-pin-2-line\"></i> " + recordMap["location"];
  188. html +=" <span class=\"am-ml-10\"></span>";
  189. if(!isEmpty(recordMap["website"]) && recordMap["website"] != "null"){
  190. html +=" <i class=\"ri-links-line\"></i>" + "<a href=\""+ recordMap["website"] + "\" target=\"_blank\">" + recordMap["website"] + "</a>";
  191. }
  192. html +=" <i class=\"ri-time-line am-ml-10\"></i> 加入于 ";
  193. var userAddDate = new Date(recordMap["created_unix"]*1000);
  194. html += monthDisplay[userAddDate.getMonth()] + " " + userAddDate.getDay() + " " + userAddDate.getFullYear();
  195. html +=" </p>";
  196. html +=" </div>";
  197. html +=" </div>";
  198. html +="</div>";
  199. }
  200. document.getElementById("child_search_item").innerHTML=html;
  201. }
  202. }
  203. var monthDisplay=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Spt","Oct","Nov","Dec");
  204. function displayUserResult(page,jsonResult,onlyReturnNum,keyword){
  205. var data = jsonResult.Result;
  206. var total = jsonResult.Total;
  207. $('#user_total').text(total);
  208. if(!onlyReturnNum){
  209. setActivate("user_item");
  210. $('#keyword_desc').text(keyword);
  211. $('#obj_desc').text("用户");
  212. $('#child_total').text(total);
  213. var sortHtml = "";
  214. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(3,31);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">最近更新</a>";
  215. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(3,32);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_2\">名称自然序</a>";
  216. document.getElementById("sort_type").innerHTML=sortHtml;
  217. var html = "";
  218. var currentTime = new Date().getTime();
  219. for(var i = 0; i < data.length;i++){
  220. var recordMap = data[i];
  221. html += "<div class=\"item members\">";
  222. html += "<img class=\"ui avatar image\" src=\"" + recordMap["avatar"] + "\"></img>";
  223. html += " <div class=\"content\">";
  224. html += " <div class=\"ui header\">";
  225. html += " <a class=\"name\" href=\"/" + recordMap["real_name"] +"\">" + recordMap["name"] + "</a>";
  226. html +=" </div>";
  227. html += " <div class=\"description\">";
  228. html += " <p class=\"has-emoji\"> " + recordMap["description"] + "</p>";
  229. html +=" <p class=\"has-emoji\">";
  230. html +=" <i class=\"ri-mail-line\"></i>" + recordMap["email"];
  231. html +=" <i class=\"ri-time-line am-ml-10\"></i> 加入于 ";
  232. var userAddDate = new Date(recordMap["created_unix"]*1000);
  233. html += monthDisplay[userAddDate.getMonth()] + " " + userAddDate.getDay() + " " + userAddDate.getFullYear();
  234. html +=" </p>";
  235. html +=" </div>";
  236. html +=" </div>";
  237. html +="</div>";
  238. }
  239. document.getElementById("child_search_item").innerHTML=html;
  240. }
  241. }
  242. function setIssueOrPrInnerHtml(data){
  243. var sortHtml = "";
  244. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(2,21);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">最近更新</a>";
  245. document.getElementById("sort_type").innerHTML=sortHtml;
  246. var html = "";
  247. var currentTime = new Date().getTime();
  248. for(var i = 0; i < data.length;i++){
  249. var recordMap = data[i];
  250. html += "<div class=\"item\">";
  251. html += " <div class=\"content\">";
  252. html += " <div class=\"ui header\">";
  253. html += " <a class=\"name\" href=\"/" + recordMap["repoUrl"] +"\">" + recordMap["name"] + "</a>";
  254. html +=" </div>";
  255. html += " <div class=\"description\">";
  256. html += " <p class=\"has-emoji\"> " + recordMap["content"] + "</p>";
  257. html +=" <p class=\"time\">";
  258. html +=" <i class=\"ri-code-box-line\"></i>";
  259. html +=" <a class=\"am-text grey\" href=\"/" + recordMap["repoUrl"] +"/" + recordMap["id"] + "\"> " + recordMap["repoUrl"] +"#" + recordMap["id"] + "</a>";
  260. html +=" <i class=\"ri-information-line am-ml-10\"></i> ";
  261. if(isEmpty(recordMap["is_closed"]) || recordMap["is_closed"]=="f"){
  262. html += "关闭";
  263. }else{
  264. html += "开启中";
  265. }
  266. 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>";
  267. html +=" </p>";
  268. html +=" </div>";
  269. html +=" </div>";
  270. html +="</div>";
  271. }
  272. document.getElementById("child_search_item").innerHTML=html;
  273. }
  274. function displayIssueResult(page,jsonResult,onlyReturnNum,keyword){
  275. var data = jsonResult.Result;
  276. var total = jsonResult.Total;
  277. $('#issue_total').text(total);
  278. if(!onlyReturnNum){
  279. setActivate("issue_item");
  280. $('#keyword_desc').text(keyword);
  281. $('#obj_desc').text("任务");
  282. $('#child_total').text(total);
  283. setIssueOrPrInnerHtml(data);
  284. }
  285. }
  286. function setActivate(name){
  287. $('#repo_item').removeClass("active");
  288. $('#user_item').removeClass("active");
  289. $('#issue_item').removeClass("active");
  290. $('#dataset_item').removeClass("active");
  291. $('#org_item').removeClass("active");
  292. $('#pr_item').removeClass("active");
  293. var tmp = "#" + name;
  294. $(tmp).addClass("active");
  295. }
  296. function displayRepoResult(page,jsonResult,onlyReturnNum,keyword){
  297. var data = jsonResult.Result;
  298. var total = jsonResult.Total;
  299. $('#repo_total').text(total);
  300. if(!onlyReturnNum){
  301. setActivate("repo_item");
  302. $('#keyword_desc').text(keyword);
  303. $('#obj_desc').text("项目");
  304. $('#child_total').text(total);
  305. var sortHtml = "";
  306. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,11);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">最近更新</a>";
  307. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,12);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">关注数</a>";
  308. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,13);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">点赞数</a>";
  309. sortHtml +="<a class=\"item\" href=\"javascript:searchItem(1,14);\" tabindex=\"-1\" role=\"menuitem\" id=\"menuitem_1\">Fork数</a>";
  310. document.getElementById("sort_type").innerHTML=sortHtml;
  311. var html = "";
  312. var currentTime = new Date().getTime();
  313. for(var i = 0; i < data.length;i++){
  314. var recordMap = data[i];
  315. html += "<div class=\"item\">";
  316. if(!isEmpty(recordMap['avatar'])){
  317. html += "<img class=\"ui avatar image\" src=\"" + recordMap['avatar'] + "\">";
  318. }
  319. html += " <div class=\"content\">";
  320. html += " <div class=\"ui header\">";
  321. 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>";
  322. 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>";
  323. html +=" </div>";
  324. html += " <div class=\"description\">";
  325. html += " <p class=\"has-emoji\"> " + recordMap["description"] + "</p>";
  326. html += " <div class=\"ui tags\">";
  327. if(!isEmpty(recordMap["topics"]) && recordMap["topics"] !="null"){
  328. for(var j = 0; j < recordMap["topics"].length;j++){
  329. html +=" <a href=\"/explore/repos?q=" + recordMap["topics"][j] + "&amp;topic=\"><div class=\"ui small label topic\">"+ recordMap["topics"][j] + "</div></a>";
  330. }
  331. }
  332. html +=" </div>";
  333. html +=" <p class=\"time\">";
  334. html +=" <i class=\"ri-star-line\"></i>" + recordMap["num_stars"] + "<i class=\"ri-git-branch-line am-ml-10\"></i>" + recordMap["num_forks"];
  335. html +=" 最后更新于 <span class=\"time-since poping up\" title=\"\" data-content=\"\" data-variation=\"inverted tiny\">" + getTime(recordMap["updated_unix"],currentTime) +"</span>";
  336. if(!isEmpty(recordMap["lang"])){
  337. html +=" <span class=\"text grey am-ml-10\"><i class=\"color-icon\" style=\"background-color: #3572A5\"></i>" + recordMap["lang"] + "</span>";
  338. }
  339. html +=" </p>";
  340. html +=" </div>";
  341. html +=" </div>";
  342. html +="</div>";
  343. }
  344. document.getElementById("child_search_item").innerHTML=html;
  345. }
  346. }
  347. function getTime(UpdatedUnix,currentTime){
  348. UpdatedUnix = UpdatedUnix;
  349. currentTime = currentTime / 1000;
  350. var timeEscSecond = currentTime - UpdatedUnix;
  351. if( timeEscSecond < 0){
  352. timeEscSecond = 1;
  353. }
  354. console.log("currentTime=" + currentTime + " updateUnix=" + UpdatedUnix);
  355. var hours= Math.floor(timeEscSecond / 3600);
  356. //计算相差分钟数
  357. var leave2 = Math.floor(timeEscSecond % (3600)); //计算小时数后剩余的秒数
  358. var minutes= Math.floor(leave2 / 60);//计算相差分钟数
  359. var leave3=Math.floor(leave2 % 60); //计算分钟数后剩余的秒数
  360. var seconds= leave3;
  361. if(hours == 0 && minutes == 0){
  362. return seconds + getRepoOrOrg(6,isZh);
  363. }else{
  364. if(hours > 0){
  365. if(hours >= 24){
  366. return Math.ceil(hours/24) + getRepoOrOrg(7,isZh);
  367. }else{
  368. return hours + getRepoOrOrg(4,isZh);
  369. }
  370. }else{
  371. return minutes + getRepoOrOrg(5,isZh);
  372. }
  373. }
  374. }
  375. function getRepoOrOrg(key,isZhLang){
  376. if(isZhLang){
  377. return repoAndOrgZH[key];
  378. }else{
  379. return repoAndOrgEN[key];
  380. }
  381. }
  382. var repoAndOrgZH={
  383. "1":"项目",
  384. "2":"成员",
  385. "3":"团队",
  386. "4":"小时前",
  387. "5":"分钟前",
  388. "6":"秒前",
  389. "7":"天前"
  390. };
  391. var repoAndOrgEN={
  392. "1":"repository",
  393. "2":"Members ",
  394. "3":"Teams",
  395. "4":" hours ago",
  396. "5":" minutes ago",
  397. "6":" seconds ago",
  398. "7":" days ago"
  399. };