A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

117 lines
3.2 KiB

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