Add search result navigation support

This commit is contained in:
Kirill Isakov 2016-04-24 18:01:02 +06:00
parent 1faf1b645b
commit 0d6625e070
2 changed files with 67 additions and 9 deletions

View File

@ -6,6 +6,7 @@
z-index: 9999999; z-index: 9999999;
overflow-y: auto; overflow-y: auto;
max-height: 80%; max-height: 80%;
box-shadow: 0 0 1em;
} }
.dflex { .dflex {

View File

@ -1,4 +1,10 @@
$(document).ready(function() { $(document).ready(function() {
highlightResult('top')();
$('.result').on('click', function() {
highlightResult($(this))();
});
var vimKeys = { var vimKeys = {
27: { 27: {
key: 'Escape', key: 'Escape',
@ -50,13 +56,13 @@ $(document).ready(function() {
}, },
75: { 75: {
key: 'k', key: 'k',
fun: previousResult, fun: highlightResult('up'),
des: 'select previous search result', des: 'select previous search result',
cat: 'Results' cat: 'Results'
}, },
74: { 74: {
key: 'j', key: 'j',
fun: nextResult, fun: highlightResult('down'),
des: 'select next search result', des: 'select next search result',
cat: 'Results' cat: 'Results'
}, },
@ -75,7 +81,7 @@ $(document).ready(function() {
79: { 79: {
key: 'o', key: 'o',
fun: openResult(false), fun: openResult(false),
des: 'open search result', des: 'open search result',
cat: 'Results' cat: 'Results'
}, },
84: { 84: {
@ -118,6 +124,50 @@ $(document).ready(function() {
} }
}); });
function highlightResult(which) {
return function() {
var current = $('.result[data-vim-selected]');
if (current.length === 0) {
current = $('.result:first');
if (current.length === 0) {
return;
}
}
var next;
if (typeof which !== 'string') {
next = which;
} else {
switch (which) {
// case 'visible':
// TODO
case 'down':
next = current.next('.result');
if (next.length === 0) {
next = $('.result:first');
}
break;
case 'up':
next = current.prev('.result');
if (next.length === 0) {
next = $('.result:last');
}
break;
case 'bottom':
next = $('.result:last');
break;
case 'top':
default:
next = $('.result:first');
}
}
current.removeAttr('data-vim-selected').removeClass('well well-sm');
next.attr('data-vim-selected', 'true').addClass('well well-sm');
}
}
function reloadPage() { function reloadPage() {
document.location.reload(false); document.location.reload(false);
} }
@ -146,12 +196,14 @@ $(document).ready(function() {
function scrollPage(amount) { function scrollPage(amount) {
return function() { return function() {
window.scrollBy(0, amount); window.scrollBy(0, amount);
highlightResult('visible')();
} }
} }
function scrollPageTo(position) { function scrollPageTo(position) {
return function() { return function() {
window.scrollTo(0, position); window.scrollTo(0, position);
highlightResult('visible')();
} }
} }
@ -159,13 +211,18 @@ $(document).ready(function() {
$('input#q').focus(); $('input#q').focus();
} }
function previousResult() {
}
function nextResult() {
}
function openResult(newTab) { function openResult(newTab) {
return function() {
var link = $('.result[data-vim-selected] .result_header a');
if (link.length) {
var url = link.attr('href');
if (newTab) {
window.open(url);
} else {
window.location.href = url;
}
}
};
} }
function toggleHelp() { function toggleHelp() {