From 7b48a663503018dd47b81c94afd9c4df6d62467a Mon Sep 17 00:00:00 2001 From: Kirill Isakov Date: Sun, 24 Apr 2016 21:04:53 +0600 Subject: [PATCH] Add auto page scrolling to selected result --- searx/static/plugins/js/vim_hotkeys.js | 49 +++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/searx/static/plugins/js/vim_hotkeys.js b/searx/static/plugins/js/vim_hotkeys.js index 740b78ff..f7f5989c 100644 --- a/searx/static/plugins/js/vim_hotkeys.js +++ b/searx/static/plugins/js/vim_hotkeys.js @@ -140,8 +140,21 @@ $(document).ready(function() { next = which; } else { switch (which) { - // case 'visible': - // TODO + case 'visible': + var top = $(window).scrollTop(); + var bot = top + $(window).height(); + var results = $('.result'); + + for (var i = 0; i < results.length; i++) { + next = $(results[i]); + var etop = next.offset().top; + var ebot = etop + next.height(); + + if ((ebot <= bot) && (etop > top)) { + break; + } + } + break; case 'down': next = current.next('.result'); if (next.length === 0) { @@ -163,8 +176,11 @@ $(document).ready(function() { } } - current.removeAttr('data-vim-selected').removeClass('well well-sm'); - next.attr('data-vim-selected', 'true').addClass('well well-sm'); + if (next) { + current.removeAttr('data-vim-selected').removeClass('well well-sm'); + next.attr('data-vim-selected', 'true').addClass('well well-sm'); + scrollPageToSelected(); + } } } @@ -193,6 +209,31 @@ $(document).ready(function() { } } + function scrollPageToSelected() { + var sel = $('.result[data-vim-selected]'); + if (sel.length !== 1) { + return; + } + + var wnd = $(window); + + var wtop = wnd.scrollTop(); + var etop = sel.offset().top; + + var offset = 30; + + if (wtop > etop) { + wnd.scrollTop(etop - offset); + } else { + var ebot = etop + sel.height(); + var wbot = wtop + wnd.height(); + + if (wbot < ebot) { + wnd.scrollTop(ebot - wnd.height() + offset); + } + } + } + function scrollPage(amount) { return function() { window.scrollBy(0, amount);