@@ -3,7 +3,7 @@ import * as params from '@params';
3
3
var fuse ; // holds our search engine
4
4
var resList = document . getElementById ( 'searchResults' ) ;
5
5
var sInput = document . getElementById ( 'searchInput' ) ;
6
- var first , last = null
6
+ var first , last , current_elem = null
7
7
var resultsAvailable = false ;
8
8
9
9
// load our search index
@@ -38,8 +38,18 @@ window.onload = function () {
38
38
xhr . send ( ) ;
39
39
}
40
40
41
- function activeToggle ( ) {
42
- document . activeElement . parentElement . classList . toggle ( "focus" )
41
+ function activeToggle ( ae ) {
42
+ document . querySelectorAll ( '.focus' ) . forEach ( function ( element ) {
43
+ // rm focus class
44
+ element . classList . remove ( "focus" )
45
+ } ) ;
46
+ if ( ae ) {
47
+ ae . focus ( )
48
+ document . activeElement = current_elem = ae ;
49
+ ae . parentElement . classList . add ( "focus" )
50
+ } else {
51
+ document . activeElement . parentElement . classList . add ( "focus" )
52
+ }
43
53
}
44
54
45
55
function reset ( ) {
@@ -82,44 +92,38 @@ sInput.addEventListener('search', function (e) {
82
92
// kb bindings
83
93
document . onkeydown = function ( e ) {
84
94
let key = e . key ;
85
- let ae = document . activeElement ;
95
+ var ae = document . activeElement ;
96
+
86
97
let inbox = document . getElementById ( "searchbox" ) . contains ( ae )
87
98
88
99
if ( ae === sInput ) {
89
100
var elements = document . getElementsByClassName ( 'focus' ) ;
90
101
while ( elements . length > 0 ) {
91
102
elements [ 0 ] . classList . remove ( 'focus' ) ;
92
103
}
93
- }
104
+ } else if ( current_elem ) ae = current_elem ;
94
105
95
106
if ( key === "ArrowDown" && resultsAvailable && inbox ) {
96
107
e . preventDefault ( ) ;
97
108
if ( ae == sInput ) {
98
109
// if the currently focused element is the search input, focus the <a> of first <li>
99
- activeToggle ( ) ; // rm focus class
100
- resList . firstChild . lastChild . focus ( ) ;
101
- activeToggle ( ) ; // add focus class
110
+ activeToggle ( resList . firstChild . lastChild ) ;
102
111
} else if ( ae . parentElement == last ) {
103
112
// if the currently focused element's parent is last, do nothing
104
113
} else {
105
114
// otherwise select the next search result
106
- activeToggle ( ) ; // rm focus class
107
- ae . parentElement . nextSibling . lastChild . focus ( ) ;
108
- activeToggle ( ) ; // add focus class
115
+ activeToggle ( ae . parentElement . nextSibling . lastChild ) ;
109
116
}
110
117
} else if ( key === "ArrowUp" && resultsAvailable && inbox ) {
111
118
e . preventDefault ( ) ;
112
119
if ( ae == sInput ) {
113
120
// if the currently focused element is input box, do nothing
114
121
} else if ( ae . parentElement == first ) {
115
122
// if the currently focused element is first item, go to input box
116
- activeToggle ( ) ; // rm focus class
117
- sInput . focus ( ) ;
123
+ activeToggle ( sInput ) ;
118
124
} else {
119
125
// otherwise select the previous search result
120
- activeToggle ( ) ; // rm focus class
121
- ae . parentElement . previousSibling . lastChild . focus ( ) ;
122
- activeToggle ( ) ; // add focus class
126
+ activeToggle ( ae . parentElement . previousSibling . lastChild ) ;
123
127
}
124
128
} else if ( key === "ArrowRight" && resultsAvailable && inbox ) {
125
129
ae . click ( ) ; // click on active link
0 commit comments