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.
 
 
 
 
 
 

53 lines
1.6 KiB

  1. FINISH_TYPING_INTERVAL = 650;
  2. searchBar = document.querySelectorAll("form#search-bar input[type='text']")[0];
  3. var typingTimer, lastValue;
  4. searchBar.onkeyup = typingTimer;
  5. /*
  6. * Clear the existing timer and set a new one the the user types text into the
  7. * search bar.
  8. */
  9. function typingTimer(){
  10. clearTimeout(typingTimer);
  11. if(lastValue != searchBar.value && searchBar.value)
  12. typingTimer = setTimeout(finishedTyping, FINISH_TYPING_INTERVAL);
  13. };
  14. /*
  15. * Callback which queries the server whenver the user stops typing.
  16. *
  17. * Whenever the user doesn't type for a `FINISH_TYPING_INTERVAL` after having
  18. * entered new text in the search bar, send the current query request to the
  19. * server.
  20. */
  21. function finishedTyping(){
  22. queryServer();
  23. var searchField = document.querySelectorAll("div#search-field")[0]
  24. if(!searchField.classList.contains("partly-visible"))
  25. searchField.classList.add("partly-visible");
  26. }
  27. /*
  28. * AJAX the current query string to the server.
  29. *
  30. * Currently just fills `div#results` with random content.
  31. */
  32. function queryServer(){
  33. lastValue = searchBar.value;
  34. var resultsPage = document.querySelectorAll("div#results")[0]
  35. while(resultsPage.firstChild){
  36. resultsPage.removeChild(resultsPage.firstChild);
  37. }
  38. for(var result = 0; result < 200; result++){
  39. var newDiv = document.createElement("div");
  40. newDiv.innerHTML = Math.random();
  41. newDiv.style.textAlign = "center";
  42. newDiv.style.color = "#" + Math.floor(Math.random() *
  43. 16777215).toString(16);
  44. resultsPage.appendChild(newDiv)
  45. }
  46. }