From 0f7628f57538c1a81c29c35cebe5ad59fb1d459c Mon Sep 17 00:00:00 2001 From: Benjamin Attal Date: Tue, 3 Jun 2014 22:11:34 -0400 Subject: [PATCH] Add h and j keymaps for result. --- static/js/index.js | 76 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/static/js/index.js b/static/js/index.js index 01b2572..2c0cbfc 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -82,7 +82,7 @@ var searchResultsPage = 1; var hotkeyActions = { "k" : previousResult, - "j" : nextResult + "j" : nextResult, "h" : previousSymbolMatch, "l" : nextSymbolMatch }; @@ -258,10 +258,6 @@ function createResult(codelet) { // Needs to be processed on the server codeElt.innerHTML = '
' + codelet.html_code + '
'; - var matches = codeElt.querySelectorAll('.hll'); - $.each(matches, function(i, a) { - a.id = 'match_' + i; - }); //Event binding $(codeElt).hover(function(e) { @@ -274,28 +270,14 @@ function createResult(codelet) { }); }); - var cur_match = -1; - var newMatch = function(e) { - var $code = $(newDiv).find('#tablecontainer'), - $match = $code.find('#match_' + cur_match); - - $code.scrollTop($code.scrollTop() - $code.height() / 2 + - $match.position().top + $match.height() / 2); - - $match.effect("highlight", {}, 750) - } - $(nextMatch).click(function(e) { - e.stopPropagation() - e.preventDefault() - cur_match = cur_match >= matches.length - 1 ? 0 : cur_match + 1; - newMatch(); + e.stopPropagation(); + e.preventDefault(); }); $(prevMatch).click(function(e) { - e.stopPropagation() - cur_match = cur_match <= 0 ? matches.length - 1 : cur_match - 1; - newMatch(); + e.stopPropagation(); + e.preventDefault(); }); //Finish and append elements to parent elements @@ -323,6 +305,54 @@ function createResult(codelet) { return newDiv; } +function previousSymbolMatch() { + var currResult = $(".display-all"), + currMatch = currResult.find(".hll.current"), + matches = currResult.find(".hll"), + scrollDiv = currResult.find('#tablecontainer'); + + if (currMatch.length == 0) { + currMatch = matches[0]; + $(currMatch).addClass('current'); + } + currMatch.removeClass('current'); + + var index = matches.index(currMatch.get(0)) - 1; + index = index <= 0 ? matches.length - 1 : index; + var newMatch = $(matches[index]); + + scrollDiv.scrollTop(scrollDiv.scrollTop() + - scrollDiv.height() / 2 + + newMatch.position().top + newMatch.height() / 2); + + newMatch.effect("highlight", {}, 750) + newMatch.addClass('current'); +}; + +function nextSymbolMatch() { + var currResult = $(".display-all"), + currMatch = currResult.find(".hll.current"), + matches = currResult.find(".hll"), + scrollDiv = currResult.find('#tablecontainer'); + + if (currMatch.length == 0) { + currMatch = $(matches[0]); + $(currMatch).addClass('current'); + } + currMatch.removeClass('current'); + + var index = matches.index(currMatch.get(0)) + 1; + index = index >= matches.length ? 0 : index; + var newMatch = $(matches[index]); + + scrollDiv.scrollTop(scrollDiv.scrollTop() + - scrollDiv.height() / 2 + + newMatch.position().top + newMatch.height() / 2); + + newMatch.effect("highlight", {}, 750); + newMatch.addClass('current'); +}; + /* * AJAX the current query string to the server, and return its response. *