-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
177 lines (153 loc) · 6.26 KB
/
index.js
File metadata and controls
177 lines (153 loc) · 6.26 KB
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
$(document).ready(function() {
console.log("accessible-bootstrap3 loaded.");
var getKeyCode = function(event) {
// Some browsers use keyCode and some use which.
// Return whichever one doesn't blow up.
return event.keyCode || event.which;
}
// Make sure all submenu links are focusable
$(".trigger, .dropdown-toggle").each(function() {
$(this).attr("tabindex", 0);
$(this).attr("aria-haspopup", true)
});
// properly label menu's
$(".dropdown-menu, .dropdown-submenu").each(function() {
$(this).attr("role", "menu");
});
//////////////////////////////////////////////////////////////////
// This code removes the open class from a .dropdown menu,
// or adds {"display": "none"} css to .sub-menu menu
// after it && its children elements loose focus. Which in turn
// causes the dropdown menu to close or hide.
$('body').focusin(function(){
// remove open class from all .dropdown elements
$('.dropdown').removeClass('open');
// Set aria-expanded="false" after we close the dropdown.
$('.dropdown .dropdown-toggle').attr("aria-expanded", false);
});
$('.dropdown').focusin(function(){
var children = $(this).children();
children.focusin(function(event) {
// stop the bubble up event from triggering
// the $('body').focusin function (line 16)
// This prevents the dropdown from collapsing while focus is
// on any of the children elements.
event.stopPropagation();
// Close sub-menu dropdown by adding display: none css.
$('.dropdown-submenu > .sub-menu').css({"display": "none"})
});
});
$('.dropdown-submenu').focusin(function(){
var children = $(this).children();
children.focusin(function(event) {
// stop the bubble up event from triggering
// the $('.dropdown').focusin function (line 21)
// This prevents the dropdown from collapsing while focus is
// on any of the children elements.
event.stopPropagation();
});
});
//////////////////////////////////////////////////////////////////
/// Toggle aria-expanded attribute for dropdown and submenus //////////
$(".dropdown-toggle").bind("click", function() {
var self = $(this);
var parent = self.parent('.dropdown');
expanded = !parent.hasClass("open");
self.attr("aria-expanded", expanded);
});
$("a.trigger").bind("click", function() {
var self = $(this);
var submenu = self.next(".sub-menu");
expanded = !(submenu.css("display") === "none");
self.attr("aria-expanded", expanded);
});
/////////////////////////////////////////////////////////////////////////
//// Modify keyboard defaults for dropdown menu and submenus.//////
$('.dropdown-toggle').keydown(function(event) {
var code = getKeyCode(event);
var thisDropdown = $(this).parent('.dropdown');
if ( !thisDropdown.hasClass('open') && (code == 27) ) {
// stop escape key from opening dropdown
event.stopPropagation();
// If space bar hit open or close dropdown
} else if (code === 32 ) {
event.preventDefault();
$(this).click()
}
});
$('.dropdown-submenu a.trigger').on('keydown click', function(event) {
var code = getKeyCode(event);
var subMenu = $(this).next("ul.sub-menu");
if (code === 32 || code === 1 || code == 13) {
// Stop page scroll
event.stopPropagation();
event.preventDefault();
// Open and close submenu when space bar is hit.
subMenu.css('display') == "none" ? subMenu.css('display', 'block' ) : subMenu.css('display', 'none' )
}
});
$(".dropdown-menu").keydown(function(event) {
closeIfEscapeKeyDown(event, $(this));
}).children().bind("click",function(event){
closeIfEscapeKeyDown(event, $(this));
});
var closeIfEscapeKeyDown = function(event, element) {
if ((event.keyCode || event.which) == 27) {
element.prev("a").focus().click();
}
}
///////////////////////////////////////////////////////
//// Skip Navigation ////////////////////////////////////////////////////
// bind a click event to the 'skip' link
$("#skip-nav").click(function(event){
// strip the leading hash and declare
// the content we're skipping to
var skipTo="#"+this.href.split('#')[1];
// Setting 'tabindex' to -1 takes an element out of normal
// tab flow but allows it to be focused via javascript
$(skipTo).attr('tabindex', -1).on('blur focusout', function () {
// when focus leaves this element,
// remove the tabindex attribute
$(this).removeAttr('tabindex');
}).focus(); // focus on the content container
});
/////////////////////////////////////////////////////////////////////////
/// Prevent enter from submitting form //////////////////////////////////
$(document).on("keypress", "form", function (event) {
var code = getKeyCode(event);
// If target is <textarea> or submit return true and do NOT modify normal behavior.
if ($(event.target).is("textarea") || $(event.target).is("input:submit")) {return true}
if (code == 13 ) {
if ($(event.target).is("input:checkbox") || $(event.target).is("input:radio") ) {
event.preventDefault();
event.target.click();
return false
}
if ($(event.target).is("select")) {
event.preventDefault();
mouseDown(event.target);
return false
}
if ($(event.target).is(".redactor-editor")) {return true}
// If true prevent the form submit.
if (tabIndexForward(event)) event.preventDefault();
}
});
//click element with virtual mouse.
var mouseDown = function (element) {
var event = document.createEvent('MouseEvents');
event.initMouseEvent('mousedown', true, true, window);
element.dispatchEvent(event);
};
// Move tab index forward one input inside the form an event originates.
var tabIndexForward = function(event) {
var inputs = $(event.target).parents("form").eq(0).find(":input:visible");
var inputIndex = inputs.index(event.target);
// If at end of focusable elements return false, if not move forward one.
if (inputIndex == inputs.length - 1) {
// return false on last form input so we know to let the form submit.
return false
} else {inputs[inputIndex + 1].focus();}
}
/////////////////////////////////////////////////////////////////////////
});