-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-keybindings.js
229 lines (202 loc) · 6.73 KB
/
search-keybindings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Array of link elements in DOM that make up main search results
*/
var search_keybindings_resultLinks = []
var search_keybindings_currentIndex = -1
var search_keybindings_searchBar
console.log('Search_Keybindings initialized')
function selectResult (resultNum) {
search_keybindings_resultLinks[resultNum].focus()
}
/**
* Activate preceding search result. Does nothing if currently at top.
*/
function moveUp () {
if (search_keybindings_currentIndex <= 0) {
console.log('└ EOL')
return
}
console.log('│ current index is ' + search_keybindings_currentIndex)
search_keybindings_currentIndex--
console.log('└ targeting ' + search_keybindings_currentIndex)
selectResult(search_keybindings_currentIndex)
}
/**
* Activate following search result. Does nothing if currently at bottom.
*/
function moveDown () {
if (search_keybindings_currentIndex >= search_keybindings_resultLinks.length - 1) {
console.log('└ EOL')
return
}
console.log('│ current index is ' + search_keybindings_currentIndex)
search_keybindings_currentIndex++
console.log('└ targeting ' + search_keybindings_currentIndex)
selectResult(search_keybindings_currentIndex)
}
/**
* Activate top search result.
*/
function moveHigh () {
console.log('│ current index is ' + search_keybindings_currentIndex)
search_keybindings_currentIndex = 0
console.log('└ targeting ' + search_keybindings_currentIndex)
selectResult(search_keybindings_currentIndex)
}
/**
* Activate search result midway between bottom and top.
*/
function moveMiddle () {
console.log('│ current index is ' + search_keybindings_currentIndex)
var mid = Math.floor(search_keybindings_resultLinks.length / 2)
search_keybindings_currentIndex = mid
console.log('└ targeting ' + search_keybindings_currentIndex)
selectResult(search_keybindings_currentIndex)
}
/**
* Activate bottom search result.
*/
function moveLow () {
console.log('│ current index is ' + search_keybindings_currentIndex)
search_keybindings_currentIndex = search_keybindings_resultLinks.length - 1
console.log('└ targeting ' + search_keybindings_currentIndex)
selectResult(search_keybindings_currentIndex)
}
/**
* Activate search bar for text entry.
*/
function selectSearchBar () {
search_keybindings_searchBar.focus()
console.log('└ targeting search bar')
}
/*
function escapeSearchBar() {
selectResult(search_keybindings_currentIndex);
console.log("└ exiting search bar");
}
*/
window.addEventListener('keydown', checkKeyPressed, false)
/**
* Identifies keycode of pressed key and runs specified procedure
* @param {Event} e Keydown event
*/
function checkKeyPressed (e) {
console.log(e.key + ' key pressed.')
let ae = document.activeElement
console.log('│ Current active element is:')
console.log(ae)
let loc = search_keybindings_resultLinks.indexOf(ae)
if (loc >= 0) {
search_keybindings_currentIndex = loc
} else if (ae.isSameNode(search_keybindings_searchBar) || ae.nodeName == 'INPUT') {
console.log('└ Search bar active, exiting.')
return
}
/**
* Determines which key procedure to run
*/
switch (e.key.toLowerCase()) {
case search_keybindings_getKeyBinding('keyDown'):
console.log('│ Key matches down key.')
e.preventDefault()
moveDown()
break
case search_keybindings_getKeyBinding('keyUp'):
console.log('│ Key matches up key.')
e.preventDefault()
moveUp()
break
case search_keybindings_getKeyBinding('keySearchBar'):
console.log('│ Key matches search key.')
e.preventDefault()
selectSearchBar()
break
/*
case search_keybindings_getKeyBinding("keyEsc"):
console.log("│ Key matches escape key.");
cleanEvent(e);
escapeSearchBar();
break;
*/
case search_keybindings_getKeyBinding('keyHigh'):
console.log('│ Key matches high key.')
e.preventDefault()
moveHigh()
break
case search_keybindings_getKeyBinding('keyMiddle'):
console.log('│ Key matches middle key.')
e.preventDefault()
moveMiddle()
break
case search_keybindings_getKeyBinding('keyLow'):
console.log('│ Key matches low key.')
e.preventDefault()
moveLow()
break
}
}
/**
* Test if an element in the HTML DOM is visible
* @param {Node} obj A DOM element tested for visibility
*/
function isVisible(obj) {
if (obj == document) return true
if (!obj) return false
if (!obj.parentNode) return false
if (obj.style) {
if (obj.style.display == 'none') return false
if (obj.style.visibility == 'hidden') return false
}
//Try the computed style in a standard way
if (window.getComputedStyle) {
var style = window.getComputedStyle(obj, "")
if (style.display == 'none') return false
if (style.visibility == 'hidden') return false
}
//Or get the computed style using IE's silly proprietary way
var style = obj.currentStyle
if (style) {
if (style['display'] == 'none') return false
if (style['visibility'] == 'hidden') return false
}
return isVisible(obj.parentNode)
}
/*
document.addEventListener("DOMContentLoaded", indexResults()) {
console.log("DOM fully loaded and parsed");
});
*/
{
let resultListPane = document.getElementById(search_keybindings_siteMappingDictionary.get('sr-pane'))
let resultList = resultListPane.getElementsByTagName(search_keybindings_siteMappingDictionary.get('sr-hTag'))
for (var i = 0, len = resultList.length; i < len; i++) {
var res = resultList[i].getElementsByTagName(search_keybindings_siteMappingDictionary.get('sr-linkTag'))[0];
if (res && isVisible(res)) {
search_keybindings_resultLinks.push(res)
}
}
console.log(search_keybindings_resultLinks)
if (search_keybindings_siteMappingDictionary.get('search-bar-id')) {
//get node with certain id
search_keybindings_searchBar = document.getElementById(search_keybindings_siteMappingDictionary.get('search-bar-id'));
console.log('Located search bar:');
console.log(search_keybindings_searchBar);
}
else if (search_keybindings_siteMappingDictionary.get('search-bar-name')) {
//get NodeList of nodes with certain name
let sbname = search_keybindings_siteMappingDictionary.get('search-bar-name');
let poss = document.getElementsByName(sbname);
//find input
for (var i = 0, len = poss.length; i < len; i++) {
if (poss[i].nodeName == 'INPUT') {
search_keybindings_searchBar = poss[i]
console.log('Located search bar:');
console.log(search_keybindings_searchBar);
break;
}
}
}
else {
console.log('Search bar not located.')
}
}