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.
 
 
 
 
 
 

95 lines
2.6 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. var typingTimer, lastValue;
  8. searchBar.onkeyup = typingTimer;
  9. /*
  10. * Clear the existing timer and set a new one the the user types text into the
  11. * search bar.
  12. */
  13. function typingTimer(){
  14. clearTimeout(typingTimer);
  15. if(lastValue != searchBar.value)
  16. typingTimer = setTimeout(finishedTyping, FINISH_TYPING_INTERVAL);
  17. };
  18. /*
  19. * Callback which queries the server whenver the user stops typing.
  20. *
  21. * Whenever the user doesn't type for a `FINISH_TYPING_INTERVAL` after having
  22. * entered new text in the search bar, send the current query request to the
  23. * server.
  24. */
  25. function finishedTyping(){
  26. lastValue = searchBar.value;
  27. var searchField = document.querySelectorAll("div#search-field")[0]
  28. clearResults();
  29. if(searchBar.value){
  30. populateResults();
  31. if(!searchField.classList.contains("partly-visible"))
  32. searchField.classList.add("partly-visible");
  33. }
  34. else
  35. searchField.classList.remove("partly-visible");
  36. }
  37. /*
  38. * Removes any child elements of `div#results`.
  39. */
  40. function clearResults(){
  41. var resultsPage = document.querySelectorAll("div#results")[0];
  42. while(resultsPage.firstChild)
  43. resultsPage.removeChild(resultsPage.firstChild);
  44. }
  45. /*
  46. * Query the server with the current search string, and populate `div#results`
  47. * with its response.
  48. */
  49. function populateResults(){
  50. var resultsPage = document.querySelectorAll("div#results")[0];
  51. var results = queryServer();
  52. for(var result = 0; result < results.length; result++){
  53. var newDiv = results[result];
  54. resultsPage.appendChild(newDiv);
  55. setTimeout(
  56. (function(divReference){
  57. return function(){
  58. divReference.classList.add("cascade");
  59. };
  60. }(newDiv)), result * 20);
  61. }
  62. for(var result = 0; result < results.length; result++);
  63. }
  64. /*
  65. * AJAX the current query string to the server, and return its response.
  66. *
  67. * @return {Array} The server's response in the form of `div.result` DOM
  68. * elements, to fill `div#results`.
  69. */
  70. function queryServer(){
  71. var resultDivs = []
  72. for(var result = 0; result < 200; result++){
  73. var newDiv = document.createElement("div");
  74. newDiv.classList.add("result");
  75. newDiv.innerHTML = Math.random();
  76. newDiv.style.textAlign = "center";
  77. newDiv.style.color = "#" + Math.floor(Math.random() *
  78. 16777215).toString(16);
  79. resultDivs.push(newDiv)
  80. }
  81. return resultDivs;
  82. }