A semantic search engine for source code https://bitshift.benkurtovic.com/
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

170 líneas
4.4 KiB

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