-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
49 lines (37 loc) · 1.25 KB
/
client.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
/* global window, location, document */
document.addEventListener('DOMContentLoaded', function () {
var cache = {}, last;
Array.prototype.forEach.call(document.querySelectorAll('[role="tablist"]'), function (tablist) {
Array.prototype.forEach.call(tablist.querySelectorAll('[href^="#"][role="tab"]'), function (tab, index, tabs) {
cache[tab.hash] = [tab, document.getElementById(tab.getAttribute('aria-controls'))];
if (tab.getAttribute('aria-selected') === 'true') {
last = cache[''] = cache[tab.hash];
} else {
tab.setAttribute('tabindex', -1);
}
tab.addEventListener('keydown', function (event) {
var next = event.keyCode === 37 ? tabs[index - 1] : event.keyCode === 39 ? tabs[index + 1] : null;
if (next) {
location.hash = next.hash;
next.focus();
}
});
});
});
window.addEventListener('hashchange', onhashchange);
onhashchange();
function onhashchange() {
var tab = cache[location.hash];
if (tab) {
if (last) {
last[0].removeAttribute('aria-selected');
last[0].setAttribute('tabindex', -1);
last[1].setAttribute('hidden', '');
}
tab[0].setAttribute('aria-selected', 'true');
tab[0].removeAttribute('tabindex');
tab[1].removeAttribute('hidden', '');
last = tab;
}
}
});