-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoggle.js
70 lines (44 loc) · 1.64 KB
/
toggle.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
;(function(document, window, undefined) {
'use strict';
if (!document.addEventListener || !('classList' in document.createElement('span'))) { // classList = IE11
return;
}
function widget_toggle(e) {
var widget_ref = document.getElementById(this.getAttribute('aria-controls'));
if (widget_ref.getAttribute('aria-hidden') == 'true') {
this.setAttribute('aria-expanded', 'true');
this.textContent = this.getAttribute('data-toggle-hidden');
widget_ref.setAttribute('aria-hidden', 'false');
widget_ref.focus();
} else {
this.setAttribute('aria-expanded', 'false');
this.textContent = this.getAttribute('data-toggle-shown');
widget_ref.setAttribute('aria-hidden', 'true');
}
e.preventDefault();
}
function init() {
var links = document.querySelectorAll('a[data-toggle-hidden]'),
widget_ref_id,
widget_ref;
for (var k = (links.length - 1); k >= 0; k--) {
widget_ref_id = links[k].getAttribute('href').substr(1);
widget_ref = document.getElementById(widget_ref_id);
if (widget_ref) {
widget_ref.setAttribute('aria-hidden', 'true');
widget_ref.setAttribute('tabindex', '-1');
widget_ref.style.outline = 'none';
links[k].setAttribute('data-toggle-shown', links[k].textContent);
links[k].setAttribute('role', 'button');
links[k].setAttribute('aria-expanded', 'false');
links[k].setAttribute('aria-controls', widget_ref_id);
links[k].addEventListener('click', widget_toggle);
}
}
}
if (document.readyState !== 'loading') {
window.setTimeout(init); // Handle asynchronously
} else {
document.addEventListener('DOMContentLoaded', init);
}
})(document, window);