-
Notifications
You must be signed in to change notification settings - Fork 6
/
keyboard.js
47 lines (38 loc) · 1.31 KB
/
keyboard.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
var navclass = "kbnav";
function kbNav(key) {
// convert HTMLCollection to array for indexing
var navlinks = [].slice.call(document.getElementsByClassName(navclass));
var navindex = -1; // initialize counter before the first post
var currentfocus = document.activeElement; // get current index of focused post
// get current post index
if ( currentfocus.className.includes(navclass) ) {
navindex = navlinks.indexOf(currentfocus);
}
// increment post index
if ( key === 'j' && navindex < navlinks.length - 1 ) {
navindex++;
} else if (key === 'k' && navindex > 0 ) {
navindex--;
} else if (key === 'g') {
navindex = 0;
} else if (key === 'G') {
navindex = navlinks.length - 1;
}
// move focus
navlinks[navindex].focus();
}
function kbLaunch() {
var currentfocus = document.activeElement; // get current index of focused post
// open focused link
if ( currentfocus.className.includes(navclass) ) {
window.location.href = currentfocus;
}
}
Mousetrap.bind({
'j': function() { kbNav('j'); },
'k': function() { kbNav('k'); },
'g g': function() { kbNav('g'); },
'G': function() { kbNav('G'); },
'l': function() { kbLaunch(); },
'g h': function() { window.location.href = "index.html"; }
})