A semantic search engine for source code https://bitshift.benkurtovic.com/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * @file Manages all library initialization, jQuery callbacks, query entry
  3. * callbacks, server querying, and results diplay for `index.html`.
  4. */
  5. var advancedSearchDiv = $("div#advanced-search");
  6. var advancedSearchButton = $("button#advanced-search");
  7. advancedSearchButton.click(function(){
  8. var searchField = $("div#search-field");
  9. if(!advancedSearchDiv.hasClass("visible")){
  10. searchField.addClass("partly-visible");
  11. advancedSearchDiv.fadeIn(500).addClass("visible");
  12. advancedSearchButton.addClass("clicked");
  13. }
  14. else {
  15. advancedSearchDiv.fadeOut(300).removeClass("visible");
  16. advancedSearchButton.removeClass("clicked");
  17. if($("div#results .result").length == 0)
  18. searchField.removeClass("partly-visible");
  19. }
  20. });
  21. FINISH_TYPING_INTERVAL = 650;
  22. searchBar = $("form#search-bar input[type='text']")[0];
  23. resultsDiv = $("div#results")[0];
  24. var typingTimer, lastValue;
  25. searchBar.onkeyup = typingTimer;
  26. // Enable infinite scrolling down the results page.
  27. $(window).scroll(function() {
  28. if($(window).scrollTop() + $(window).height() == $(document).height()){
  29. loadMoreResults();
  30. }
  31. });
  32. // Enable capturing the `enter` key.
  33. $("form#search-bar").submit(function(event){
  34. event.preventDefault();
  35. return false;
  36. });
  37. /*
  38. * Clear the existing timer and set a new one the the user types text into the
  39. * search bar.
  40. */
  41. function typingTimer(event){
  42. clearTimeout(typingTimer);
  43. var enterKeyCode = 13;
  44. if(event.keyCode != enterKeyCode){
  45. if(lastValue != searchBar.value)
  46. typingTimer = setTimeout(finishedTyping, FINISH_TYPING_INTERVAL);
  47. }
  48. else {
  49. event.preventDefault();
  50. finishedTyping();
  51. return false;
  52. }
  53. };
  54. /*
  55. * Callback which queries the server whenver the user stops typing.
  56. *
  57. * Whenever the user doesn't type for a `FINISH_TYPING_INTERVAL` after having
  58. * entered new text in the search bar, send the current query request to the
  59. * server.
  60. */
  61. function finishedTyping(){
  62. lastValue = searchBar.value;
  63. var searchField = $("div#search-field");
  64. clearResults();
  65. if(searchBar.value){
  66. searchField.addClass("partly-visible");
  67. populateResults();
  68. }
  69. else {
  70. searchField.removeClass("partly-visible");
  71. $("div#advanced-search").fadeOut(50);
  72. advancedSearchButton.removeClass("clicked");
  73. }
  74. }
  75. /*
  76. * Removes any child elements of `div#results`.
  77. */
  78. function clearResults(){
  79. while(resultsDiv.firstChild)
  80. resultsDiv.removeChild(resultsDiv.firstChild);
  81. }
  82. /*
  83. * Query the server with the current search string, and populate `div#results`
  84. * with its response.
  85. */
  86. function populateResults(){
  87. var results = queryServer();
  88. for(var result = 0; result < results.length; result++){
  89. var newDiv = results[result];
  90. resultsDiv.appendChild(newDiv);
  91. setTimeout(
  92. (function(divReference){
  93. return function(){
  94. divReference.classList.add("cascade");
  95. };
  96. }(newDiv)), result * 20);
  97. }
  98. }
  99. /*
  100. * AJAX the current query string to the server, and return its response.
  101. *
  102. * @return {Array} The server's response in the form of `div.result` DOM
  103. * elements, to fill `div#results`.
  104. */
  105. function queryServer(){
  106. var resultDivs = []
  107. for(var result = 0; result < 20; result++){
  108. var newDiv = document.createElement("div");
  109. newDiv.classList.add("result");
  110. newDiv.innerHTML = Math.random();
  111. newDiv.style.textAlign = "center";
  112. newDiv.style.color = "#" + Math.floor(Math.random() *
  113. 16777215).toString(16);
  114. resultDivs.push(newDiv);
  115. }
  116. return resultDivs;
  117. }
  118. /*
  119. * Adds more results to `div#results`.
  120. */
  121. function loadMoreResults(){
  122. results = queryServer();
  123. for(var result = 0; result < results.length; result++){
  124. var newDiv = results[result];
  125. resultsDiv.appendChild(newDiv);
  126. setTimeout(
  127. (function(divReference){
  128. return function(){
  129. divReference.classList.add("cascade");
  130. };
  131. }(newDiv)),
  132. result * 20);
  133. }
  134. }