A semantic search engine for source code https://bitshift.benkurtovic.com/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 4.7 KiB

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