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.

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. var searchResultsPage = 1;
  12. /*
  13. * Set all page callbacks.
  14. */
  15. (function setHomePageCallbabacks(){
  16. // Enable infinite scrolling down the results page.
  17. $(window).scroll(function(){
  18. if($(window).scrollTop() + $(window).height() == $(document).height() &&
  19. resultsDiv.querySelectorAll(".result").length > 0)
  20. loadMoreResults();
  21. });
  22. // Toggle the advanced-search form's visibility.
  23. advancedSearchButton.click(function(){
  24. var searchField = $("div#search-field");
  25. if(!advancedSearchDiv.hasClass("visible")){
  26. searchField.addClass("partly-visible");
  27. advancedSearchDiv.fadeIn(500).addClass("visible");
  28. advancedSearchButton.addClass("clicked");
  29. }
  30. else {
  31. advancedSearchDiv.fadeOut(300).removeClass("visible");
  32. advancedSearchButton.removeClass("clicked");
  33. if($("div#results .result").length == 0)
  34. searchField.removeClass("partly-visible");
  35. }
  36. });
  37. // Enable capturing the `enter` key.
  38. $("form#search-bar").submit(function(event){
  39. event.preventDefault();
  40. return false;
  41. });
  42. searchBar.onkeyup = typingTimer;
  43. }());
  44. //Obtained by parsing python file with pygments
  45. 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>'
  46. searchBar.onkeyup = typingTimer;
  47. var testCodelet = {
  48. 'url': 'https://github.com/earwig/bitshift/blob/develop/app.py',
  49. 'filename': 'app.py',
  50. 'language': 'python',
  51. 'date_created': 'May 10, 2014',
  52. 'date_modified': '2 days ago',
  53. 'origin': ['GitHub', 'https://github.com', ''],
  54. 'authors': ['sevko', 'earwig'],
  55. 'html_code': codeExample
  56. };
  57. // Enable infinite scrolling down the results page.
  58. $(window).scroll(function() {
  59. var searchField = $("div#search-field");
  60. if($(window).scrollTop() + $(window).height() == $(document).height() && searchField.hasClass('partly-visible')){
  61. loadMoreResults();
  62. }
  63. });
  64. /*
  65. * Clear the existing timer and set a new one the the user types text into the
  66. * search bar.
  67. */
  68. function typingTimer(event){
  69. clearTimeout(typingTimer);
  70. var enterKeyCode = 13;
  71. if(event.keyCode != enterKeyCode){
  72. if(lastValue != searchBar.value)
  73. typingTimer = setTimeout(finishedTyping, FINISH_TYPING_INTERVAL);
  74. }
  75. else {
  76. event.preventDefault();
  77. finishedTyping();
  78. return false;
  79. }
  80. };
  81. /*
  82. * Callback which queries the server whenver the user stops typing.
  83. *
  84. * Whenever the user doesn't type for a `FINISH_TYPING_INTERVAL` after having
  85. * entered new text in the search bar, send the current query request to the
  86. * server.
  87. */
  88. function finishedTyping(){
  89. lastValue = searchBar.value;
  90. var searchField = $("div#search-field");
  91. clearResults();
  92. if(searchBar.value){
  93. searchField.addClass("partly-visible");
  94. populateResults();
  95. }
  96. else {
  97. searchField.removeClass("partly-visible");
  98. $("div#advanced-search").fadeOut(50);
  99. advancedSearchButton.removeClass("clicked");
  100. }
  101. }
  102. /*
  103. * Removes any child elements of `div#results`.
  104. */
  105. function clearResults(){
  106. while(resultsDiv.firstChild)
  107. resultsDiv.removeChild(resultsDiv.firstChild);
  108. }
  109. /*
  110. * Query the server with the current search string, and populate `div#results`
  111. * with its response.
  112. */
  113. function populateResults(){
  114. searchResultsPage = 1;
  115. var results = queryServer();
  116. for(var result = 0; result < results.length; result++){
  117. var newDiv = results[result];
  118. resultsDiv.appendChild(newDiv);
  119. setTimeout(
  120. (function(divReference){
  121. return function(){
  122. divReference.classList.add("cascade");
  123. };
  124. }(newDiv)), result * 20);
  125. }
  126. }
  127. /*
  128. * Create a result element based upon a codelet instance.
  129. *
  130. * @return {Element} The result element.
  131. */
  132. function createResult(codelet) {
  133. //Level 1
  134. var newDiv = document.createElement("div"),
  135. table = document.createElement("table"),
  136. row = document.createElement("tr");
  137. //Level 2
  138. var displayInfo = document.createElement("div"),
  139. codeElt = document.createElement("td"),
  140. hiddenInfoContainer = document.createElement("td"),
  141. hiddenInfo = document.createElement("div"),
  142. cycle = document.createElement("div");
  143. //Level 3
  144. var title = document.createElement("span"),
  145. site = document.createElement("span"),
  146. nextMatch = document.createElement("a"),
  147. prevMatch = document.createElement("a"),
  148. dateModified = document.createElement("div"),
  149. language = document.createElement("div"),
  150. dateCreated = document.createElement("div"),
  151. authors = document.createElement("div");
  152. //Classes and ID's
  153. newDiv.classList.add('result');
  154. displayInfo.id = 'display-info';
  155. codeElt.id = 'code';
  156. hiddenInfo.id = 'hidden-info';
  157. cycle.id = 'cycle-matches'
  158. title.id = 'title';
  159. site.id = 'site';
  160. nextMatch.id = 'next-match';
  161. nextMatch.href = '#';
  162. prevMatch.id = 'prev-match';
  163. prevMatch.href = '#';
  164. dateModified.id = 'date-modified';
  165. language.id = 'language';
  166. dateCreated.id = 'date-created';
  167. authors.id = 'authors';
  168. //Add the bulk of the html
  169. title.innerHTML = ' &raquo; <a href="' + codelet.url + '">'
  170. + codelet.filename + '</a>';
  171. site.innerHTML = '<a href="' + codelet.origin[1] + '">' + codelet.origin[0] +'</a>';
  172. nextMatch.innerHTML = 'next match';
  173. prevMatch.innerHTML = 'prev match';
  174. language.innerHTML = 'Language: <span>' + codelet.language + '</span>';
  175. dateModified.innerHTML = 'Last modified: <span>' + codelet.date_modified + '</span>';
  176. // Needs to be changed from int to string on the server
  177. dateCreated.innerHTML = 'Created: <span>' + codelet.date_created + '</span>';
  178. var authorsHtml = 'Authors: <span>';
  179. codelet.authors.forEach(function(a, i) {
  180. if (i == codelet.authors.length - 1)
  181. authorsHtml += '<a href=#>' + a + ' </a>';
  182. else
  183. authorsHtml += '<a href=#>' + a + ' </a>, ';
  184. });
  185. authors.innerHTML = authorsHtml;
  186. // Needs to be processed on the server
  187. codeElt.innerHTML = '<div id=tablecontainer>' + codelet.html_code + '</div>';
  188. var matches = codeElt.querySelectorAll('.hll');
  189. $.each(matches, function(i, a) {
  190. a.id = 'match_' + i;
  191. });
  192. //Event binding
  193. $(codeElt).hover(function(e) {
  194. $(row).addClass('display-all');
  195. });
  196. $(newDiv).on('transitionend', function(e) {
  197. $(codeElt).one('mouseleave', function(e) {
  198. $(row).removeClass('display-all');
  199. });
  200. });
  201. var cur_match = -1;
  202. var newMatch = function(e) {
  203. var $code = $(newDiv).find('#tablecontainer'),
  204. $match = $code.find('#match_' + cur_match);
  205. $code.scrollTop($code.scrollTop() - $code.height() / 2 +
  206. $match.position().top + $match.height() / 2);
  207. $match.effect("highlight", {}, 750)
  208. }
  209. $(nextMatch).click(function(e) {
  210. e.stopPropagation()
  211. e.preventDefault()
  212. cur_match = cur_match >= matches.length - 1 ? 0 : cur_match + 1;
  213. newMatch();
  214. });
  215. $(prevMatch).click(function(e) {
  216. e.stopPropagation()
  217. cur_match = cur_match <= 0 ? matches.length - 1 : cur_match - 1;
  218. newMatch();
  219. });
  220. //Finish and append elements to parent elements
  221. hiddenInfo.appendChild(dateCreated);
  222. hiddenInfo.appendChild(dateModified);
  223. hiddenInfo.appendChild(language);
  224. hiddenInfo.appendChild(authors);
  225. hiddenInfoContainer.appendChild(hiddenInfo);
  226. row.appendChild(codeElt);
  227. row.appendChild(hiddenInfoContainer);
  228. table.appendChild(row);
  229. displayInfo.appendChild(site);
  230. displayInfo.appendChild(title);
  231. cycle.appendChild(prevMatch);
  232. cycle.appendChild(nextMatch);
  233. newDiv.appendChild(displayInfo);
  234. newDiv.appendChild(table);
  235. newDiv.appendChild(cycle);
  236. return newDiv;
  237. }
  238. /*
  239. * AJAX the current query string to the server, and return its response.
  240. *
  241. * @return {Array} The server's response in the form of `div.result` DOM
  242. * elements, to fill `div#results`.
  243. */
  244. function queryServer(){
  245. var queryUrl = document.URL + "search.json?" + $.param({
  246. "q" : searchBar.value,
  247. "p" : searchResultsPage++
  248. });
  249. console.log(queryUrl);
  250. var result = $.getJSON(queryUrl, function(result){
  251. $.each(result, function(key, value){
  252. if(key == "error")
  253. errorMessage(value);
  254. else
  255. console.log("Success.");
  256. });
  257. });
  258. // return [];
  259. var resultDivs = [];
  260. for(var result = 0; result < 20; result++){
  261. var newDiv = createResult(testCodelet);
  262. resultDivs.push(newDiv)
  263. }
  264. return resultDivs;
  265. }
  266. /*
  267. * Adds more results to `div#results`.
  268. */
  269. function loadMoreResults(){
  270. var results = queryServer();
  271. for(var result = 0; result < results.length; result++){
  272. var newDiv = results[result];
  273. resultsDiv.appendChild(newDiv);
  274. setTimeout(
  275. (function(divReference){
  276. return function(){
  277. divReference.classList.add("cascade");
  278. };
  279. }(newDiv)),
  280. result * 20);
  281. }
  282. }
  283. /*
  284. * Displays a warning message in the UI.
  285. *
  286. * @param msg (str) The message string.
  287. */
  288. function errorMessage(msg){
  289. alert(msg);
  290. }
  291. loadMoreResults();