A semantic search engine for source code https://bitshift.benkurtovic.com/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

307 рядки
13 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. FINISH_TYPING_INTERVAL = 650;
  8. var searchBar = $("form#search-bar input[type='text']")[0];
  9. var resultsDiv = $("div#results")[0];
  10. var typingTimer, lastValue;
  11. /*
  12. * Set all page callbacks.
  13. */
  14. (function setHomePageCallbabacks(){
  15. // Enable infinite scrolling down the results page.
  16. $(window).scroll(function(){
  17. if($(window).scrollTop() + $(window).height() == $(document).height() &&
  18. resultsDiv.querySelectorAll(".result").length > 0)
  19. loadMoreResults();
  20. });
  21. // Toggle the advanced-search form's visibility.
  22. advancedSearchButton.click(function(){
  23. var searchField = $("div#search-field");
  24. if(!advancedSearchDiv.hasClass("visible")){
  25. searchField.addClass("partly-visible");
  26. advancedSearchDiv.fadeIn(500).addClass("visible");
  27. advancedSearchButton.addClass("clicked");
  28. }
  29. else {
  30. advancedSearchDiv.fadeOut(300).removeClass("visible");
  31. advancedSearchButton.removeClass("clicked");
  32. if($("div#results .result").length == 0)
  33. searchField.removeClass("partly-visible");
  34. }
  35. });
  36. // Enable capturing the `enter` key.
  37. $("form#search-bar").submit(function(event){
  38. event.preventDefault();
  39. return false;
  40. });
  41. searchBar.onkeyup = typingTimer;
  42. }());
  43. //Obtained by parsing python file with pygments
  44. var codeExample = '<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40</pre></div></td><td class="code"><div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>\n<span class="sd">Module to contain all the project&#39;s Flask server plumbing.</span>\n<span class="sd">&quot;&quot;&quot;</span>\n\n<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span>\n<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">render_template</span><span class="p">,</span> <span class="n">session</span>\n\n<span class="kn">from</span> <span class="nn">bitshift</span> <span class="kn">import</span> <span class="n">assets</span>\n<span class="c"># from bitshift.database import Database</span>\n<span class="c"># from bitshift.query import parse_query</span>\n\n<span class="hll"><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>\n</span><span class="hll"><span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">from_object</span><span class="p">(</span><span class="s">&quot;bitshift.config&quot;</span><span class="p">)</span>\n</span>\n<span class="hll"><span class="n">app_env</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">jinja_env</span>\n</span><span class="hll"><span class="n">app_env</span><span class="o">.</span><span class="n">line_statement_prefix</span> <span class="o">=</span> <span class="s">&quot;=&quot;</span>\n</span><span class="hll"><span class="n">app_env</span><span class="o">.</span><span class="n">globals</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">assets</span><span class="o">=</span><span class="n">assets</span><span class="p">)</span>\n</span>\n<span class="c"># database = Database()</span>\n\n<span class="hll"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span>\n</span><span class="k">def</span> <span class="nf">index</span><span class="p">():</span>\n <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">&quot;index.html&quot;</span><span class="p">)</span>\n\n<span class="hll"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/search/&lt;query&gt;&quot;</span><span class="p">)</span>\n</span><span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="n">query</span><span class="p">):</span>\n <span class="c"># tree = parse_query(query)</span>\n <span class="c"># database.search(tree)</span>\n <span class="k">pass</span>\n\n<span class="hll"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/about&quot;</span><span class="p">)</span>\n</span><span class="k">def</span> <span class="nf">about</span><span class="p">():</span>\n <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">&quot;about.html&quot;</span><span class="p">)</span>\n\n<span class="hll"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/developers&quot;</span><span class="p">)</span>\n</span><span class="k">def</span> <span class="nf">developers</span><span class="p">():</span>\n <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">&quot;developers.html&quot;</span><span class="p">)</span>\n\n<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>\n<span class="hll"> <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">debug</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>\n</span></pre></div>\n</td></tr></table>'
  45. searchBar.onkeyup = typingTimer;
  46. var testCodelet = {
  47. 'url': 'https://github.com/earwig/bitshift/blob/develop/app.py',
  48. 'filename': 'app.py',
  49. 'language': 'python',
  50. 'date_created': 'May 10, 2014',
  51. 'date_modified': '2 days ago',
  52. 'origin': ['GitHub', 'https://github.com', ''],
  53. 'authors': ['sevko', 'earwig'],
  54. 'html_code': codeExample
  55. };
  56. // Enable infinite scrolling down the results page.
  57. $(window).scroll(function() {
  58. var searchField = $("div#search-field");
  59. if($(window).scrollTop() + $(window).height() == $(document).height() && searchField.hasClass('partly-visible')){
  60. loadMoreResults();
  61. }
  62. });
  63. /*
  64. * Clear the existing timer and set a new one the the user types text into the
  65. * search bar.
  66. */
  67. function typingTimer(event){
  68. clearTimeout(typingTimer);
  69. var enterKeyCode = 13;
  70. if(event.keyCode != enterKeyCode){
  71. if(lastValue != searchBar.value)
  72. typingTimer = setTimeout(finishedTyping, FINISH_TYPING_INTERVAL);
  73. }
  74. else {
  75. event.preventDefault();
  76. finishedTyping();
  77. return false;
  78. }
  79. };
  80. /*
  81. * Callback which queries the server whenver the user stops typing.
  82. *
  83. * Whenever the user doesn't type for a `FINISH_TYPING_INTERVAL` after having
  84. * entered new text in the search bar, send the current query request to the
  85. * server.
  86. */
  87. function finishedTyping(){
  88. lastValue = searchBar.value;
  89. var searchField = $("div#search-field");
  90. clearResults();
  91. if(searchBar.value){
  92. searchField.addClass("partly-visible");
  93. populateResults();
  94. }
  95. else {
  96. searchField.removeClass("partly-visible");
  97. $("div#advanced-search").fadeOut(50);
  98. advancedSearchButton.removeClass("clicked");
  99. }
  100. }
  101. /*
  102. * Removes any child elements of `div#results`.
  103. */
  104. function clearResults(){
  105. while(resultsDiv.firstChild)
  106. resultsDiv.removeChild(resultsDiv.firstChild);
  107. }
  108. /*
  109. * Query the server with the current search string, and populate `div#results`
  110. * with its response.
  111. */
  112. function populateResults(){
  113. var results = queryServer();
  114. for(var result = 0; result < results.length; result++){
  115. var newDiv = results[result];
  116. resultsDiv.appendChild(newDiv);
  117. setTimeout(
  118. (function(divReference){
  119. return function(){
  120. divReference.classList.add("cascade");
  121. };
  122. }(newDiv)), result * 20);
  123. }
  124. }
  125. /*
  126. * Create a result element based upon a codelet instance.
  127. *
  128. * @return {Element} The result element.
  129. */
  130. function createResult(codelet) {
  131. //Level 1
  132. var newDiv = document.createElement("div"),
  133. table = document.createElement("table"),
  134. row = document.createElement("tr");
  135. //Level 2
  136. var displayInfo = document.createElement("div"),
  137. codeElt = document.createElement("td"),
  138. hiddenInfoContainer = document.createElement("td"),
  139. hiddenInfo = document.createElement("div"),
  140. cycle = document.createElement("div");
  141. //Level 3
  142. var title = document.createElement("span"),
  143. site = document.createElement("span"),
  144. nextMatch = document.createElement("a"),
  145. prevMatch = document.createElement("a"),
  146. dateModified = document.createElement("div"),
  147. language = document.createElement("div"),
  148. dateCreated = document.createElement("div"),
  149. authors = document.createElement("div");
  150. //Classes and ID's
  151. newDiv.classList.add('result');
  152. displayInfo.id = 'display-info';
  153. codeElt.id = 'code';
  154. hiddenInfo.id = 'hidden-info';
  155. cycle.id = 'cycle-matches'
  156. title.id = 'title';
  157. site.id = 'site';
  158. nextMatch.id = 'next-match';
  159. nextMatch.href = '#';
  160. prevMatch.id = 'prev-match';
  161. prevMatch.href = '#';
  162. dateModified.id = 'date-modified';
  163. language.id = 'language';
  164. dateCreated.id = 'date-created';
  165. authors.id = 'authors';
  166. //Add the bulk of the html
  167. title.innerHTML = 'File <a href="' + codelet.url + '">'
  168. + codelet.filename + '</a>';
  169. site.innerHTML = 'on <a href="' + codelet.origin[1] + '">' + codelet.origin[0] +'</a>';
  170. nextMatch.innerHTML = 'next match';
  171. prevMatch.innerHTML = 'prev match';
  172. language.innerHTML = 'Language: <span>' + codelet.language + '</span>';
  173. dateModified.innerHTML = 'Last modified: <span>' + codelet.date_modified + '</span>';
  174. // Needs to be changed from int to string on the server
  175. dateCreated.innerHTML = 'Created: <span>' + codelet.date_created + '</span>';
  176. var authorsHtml = 'Authors: <span>';
  177. codelet.authors.forEach(function(a, i) {
  178. if (i == codelet.authors.length - 1)
  179. authorsHtml += '<a href=#>' + a + ' </a>';
  180. else
  181. authorsHtml += '<a href=#>' + a + ' </a>, ';
  182. });
  183. authors.innerHTML = authorsHtml;
  184. // Needs to be processed on the server
  185. codeElt.innerHTML = '<div id=tablecontainer>' + codelet.html_code + '</div>';
  186. var matches = codeElt.querySelectorAll('.hll');
  187. $.each(matches, function(i, a) {
  188. a.id = 'match_' + i;
  189. });
  190. //Event binding
  191. $(codeElt).hover(function(e) {
  192. $(row).addClass('display-all');
  193. });
  194. $(newDiv).on('transitionend', function(e) {
  195. $(codeElt).one('mouseleave', function(e) {
  196. $(row).removeClass('display-all');
  197. });
  198. });
  199. var cur_match = -1;
  200. var newMatch = function(e) {
  201. var $code = $(newDiv).find('#tablecontainer'),
  202. $match = $code.find('#match_' + cur_match);
  203. $code.scrollTop($code.scrollTop() - $code.height() / 2 +
  204. $match.position().top + $match.height() / 2);
  205. $match.effect("highlight", {}, 750)
  206. }
  207. $(nextMatch).click(function(e) {
  208. e.stopPropagation()
  209. e.preventDefault()
  210. cur_match = cur_match >= matches.length - 1 ? 0 : cur_match + 1;
  211. newMatch();
  212. });
  213. $(prevMatch).click(function(e) {
  214. e.stopPropagation()
  215. cur_match = cur_match <= 0 ? matches.length - 1 : cur_match - 1;
  216. newMatch();
  217. });
  218. //Finish and append elements to parent elements
  219. hiddenInfo.appendChild(dateCreated);
  220. hiddenInfo.appendChild(dateModified);
  221. hiddenInfo.appendChild(language);
  222. hiddenInfo.appendChild(authors);
  223. hiddenInfoContainer.appendChild(hiddenInfo);
  224. row.appendChild(codeElt);
  225. row.appendChild(hiddenInfoContainer);
  226. table.appendChild(row);
  227. displayInfo.appendChild(title);
  228. displayInfo.appendChild(site);
  229. cycle.appendChild(prevMatch);
  230. cycle.appendChild(nextMatch);
  231. newDiv.appendChild(displayInfo);
  232. newDiv.appendChild(table);
  233. newDiv.appendChild(cycle);
  234. return newDiv;
  235. }
  236. /*
  237. * AJAX the current query string to the server, and return its response.
  238. *
  239. * @return {Array} The server's response in the form of `div.result` DOM
  240. * elements, to fill `div#results`.
  241. */
  242. function queryServer(){
  243. var resultDivs = []
  244. for(var result = 0; result < 20; result++){
  245. var newDiv = createResult(testCodelet);
  246. resultDivs.push(newDiv);
  247. }
  248. return resultDivs;
  249. }
  250. /*
  251. * Adds more results to `div#results`.
  252. */
  253. function loadMoreResults(){
  254. results = queryServer();
  255. for(var result = 0; result < results.length; result++){
  256. var newDiv = results[result];
  257. resultsDiv.appendChild(newDiv);
  258. setTimeout(
  259. (function(divReference){
  260. return function(){
  261. divReference.classList.add("cascade");
  262. };
  263. }(newDiv)),
  264. result * 20);
  265. }
  266. }
  267. loadMoreResults();