A semantic search engine for source code https://bitshift.benkurtovic.com/
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

131 righe
3.4 KiB

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