Skip to content

Commit 26e16a8

Browse files
committed
Search: Result navigation with up/down
1 parent 4dcba19 commit 26e16a8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/theme/book.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ ul#searchresults {
220220
}
221221
ul#searchresults li {
222222
margin: 10px 0px;
223+
padding: 2px;
224+
border-radius: 2px;
223225
}
224226
ul#searchresults .breadcrumbs {
225227
float: right;
@@ -445,6 +447,9 @@ ul#searchresults span.teaser em {
445447
.light .searchresults-outer {
446448
border-bottom-color: #888;
447449
}
450+
.light ul#searchresults li.focus {
451+
background-color: #e4f2fe;
452+
}
448453
.light .breadcrumbs {
449454
color: #CCC;
450455
}
@@ -583,6 +588,9 @@ ul#searchresults span.teaser em {
583588
.coal .searchresults-outer {
584589
border-bottom-color: #98a3ad;
585590
}
591+
.coal ul#searchresults li.focus {
592+
background-color: #2b2b2f;
593+
}
586594
.coal .breadcrumbs {
587595
color: #686868;
588596
}
@@ -721,6 +729,9 @@ ul#searchresults span.teaser em {
721729
.navy .searchresults-outer {
722730
border-bottom-color: #5c5c68;
723731
}
732+
.navy ul#searchresults li.focus {
733+
background-color: #242430;
734+
}
724735
.navy .breadcrumbs {
725736
color: #5c5c68;
726737
}
@@ -859,6 +870,9 @@ ul#searchresults span.teaser em {
859870
.rust .searchresults-outer {
860871
border-bottom-color: #888;
861872
}
873+
.rust ul#searchresults li.focus {
874+
background-color: #dec2a2;
875+
}
862876
.rust .breadcrumbs {
863877
color: #757575;
864878
}
@@ -1001,6 +1015,9 @@ ul#searchresults span.teaser em {
10011015
.ayu .searchresults-outer {
10021016
border-bottom-color: #888;
10031017
}
1018+
.ayu ul#searchresults li.focus {
1019+
background-color: #252932;
1020+
}
10041021
.ayu .breadcrumbs {
10051022
color: #5f5f5f;
10061023
}

src/theme/book.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ $( document ).ready(function() {
3232

3333
SEARCH_HOTKEY_KEYCODE: 83,
3434
ESCAPE_KEYCODE: 27,
35+
DOWN_KEYCODE: 40,
36+
UP_KEYCODE: 38,
37+
SELECT_KEYCODE: 13,
3538

3639
formatSearchMetric : function(count, searchterm) {
3740
if (count == 1) {
@@ -371,6 +374,7 @@ $( document ).ready(function() {
371374
,
372375
globalKeyHandler : function (e) {
373376
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
377+
374378
if (e.keyCode == this.ESCAPE_KEYCODE) {
375379
e.preventDefault();
376380
this.searchbar.removeClass("active");
@@ -386,6 +390,38 @@ $( document ).ready(function() {
386390
e.preventDefault();
387391
this.searchbar_outer.slideDown()
388392
this.searchbar.focus();
393+
return;
394+
}
395+
if (this.hasFocus() && e.keyCode == this.DOWN_KEYCODE) {
396+
e.preventDefault();
397+
this.unfocusSearchbar();
398+
this.searchresults.children('li').first().addClass("focus");
399+
return;
400+
}
401+
if (!this.hasFocus() && (e.keyCode == this.DOWN_KEYCODE
402+
|| e.keyCode == this.UP_KEYCODE
403+
|| e.keyCode == this.SELECT_KEYCODE)) {
404+
// not `:focus` because browser does annoying scrolling
405+
var current_focus = search.searchresults.find("li.focus");
406+
if (current_focus.length == 0) return;
407+
e.preventDefault();
408+
if (e.keyCode == this.DOWN_KEYCODE) {
409+
var next = current_focus.next()
410+
if (next.length > 0) {
411+
current_focus.removeClass("focus");
412+
next.addClass("focus");
413+
}
414+
} else if (e.keyCode == this.UP_KEYCODE) {
415+
current_focus.removeClass("focus");
416+
var prev = current_focus.prev();
417+
if (prev.length == 0) {
418+
this.searchbar.focus();
419+
} else {
420+
prev.addClass("focus");
421+
}
422+
} else {
423+
window.location = current_focus.children('a').attr('href');
424+
}
389425
}
390426
}
391427
,

0 commit comments

Comments
 (0)