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.
 
 
 
 
 
 

156 line
4.0 KiB

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