-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropdown.js
211 lines (200 loc) · 5.89 KB
/
dropdown.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
const ENTER_KEY_CODE = 13;
const DOWN_ARROW_KEY_CODE = 40;
const UP_ARROW_KEY_CODE = 38;
const ESCAPE_KEY_CODE = 27;
const TAB_KEY_CODE = 9;
const SHIFT_KEY_CODE = 16;
const KEYCODE_MOVE_END = 35;
const KEYCODE_MOVE_HOME = 36;
$(".w-dropdown-toggle").each(function() {
/* Keyboards Event Part 1 of 2 */
/* Click events for the "dropdown toggle button" */
$(this).keydown(function(e) {
/* IF enter click (toogle menu) */
if (e.which == ENTER_KEY_CODE) {
console.log("1");
toggleAccessibilityDropdown(this);
toggledAriaExpanded();
}
/* IF click on TAB_KEY_CODE & menu not open ==> Open Menu */
if (e.which == TAB_KEY_CODE && !$("div.w-dropdown-toggle").hasClass("w--open")) {
console.log("2");
toggledAriaExpanded(this);
toggleAccessibilityDropdown(this);
$(this).next(".w--open").find(".w-dropdown-link").first().css("outline", "black solid 1px");
}
/* IF click on TAB_KEY_CODE + shiftKey & menu not open ==> Open Menu */
if (e.shiftKey && e.which == TAB_KEY_CODE && !$("div.w-dropdown-toggle").hasClass("w--open") ) {
console.log("3");
toggleAccessibilityDropdown(this);
toggledAriaExpanded();
}
/* DOWN_ARROW_KEY_CODE ==> Open Menu */
if (e.which == DOWN_ARROW_KEY_CODE) {
console.log("4");
$(this)
.closest(".w-dropdown")
.find("div.w-dropdown-toggle")
.addClass("w--open");
$(this)
.closest(".w-dropdown")
.find("nav.w-dropdown-list")
.addClass("w--open");
toggledAriaExpanded();
$(".w-dropdown nav a:first-child").focus();
}
//Esc KEY_CODE close menu
if (
e.which == ESCAPE_KEY_CODE &&
$("div.w-dropdown-toggle").hasClass("w--open")
) {
console.log("5");
toggleAccessibilityDropdown(this);
toggledAriaExpanded();
}
});
}); /*end for each */
/*########################################################################
Keyboards Event Part 2 of 2
########################################################################## */
/* Click events for the ".w-dropdown-link" child */
$(".w-dropdown-link").keydown(function(e) {
/* End Key --> If the listbox is displayed, moves focus to and selects the last option */
if (e.which == KEYCODE_MOVE_END) {
console.log("6");
$(this)
.closest("nav")
.children()
.last()
.focus();
}
/* Home Key --> If the listbox is displayed, moves focus to and selects the first option */
if (e.which == KEYCODE_MOVE_HOME) {
console.log("7");
$(this)
.closest("nav")
.children()
.first()
.focus();
}
/* IF enter click (toogle menu) */
if (e.which == ENTER_KEY_CODE) {
console.log("8");
toggleAccessibilityDropdown(this);
toggledAriaExpanded();
}
/* DOWN_ARROW_KEY_CODE + Not last child ==> Open Menu */
if (e.which == DOWN_ARROW_KEY_CODE && $(this).not(":last-child")) {
console.log("9");
$(this)
.next()
.focus();
}
/* LAST CHILD ############################## */
/* IF LAST CHILD click on TAB_KEY_CODE & menu not open ==> Open Menu */
if ($(this).is(":last-child")) {
if (e.which == TAB_KEY_CODE) {
console.log("10");
toggleAccessibilityDropdown(this);
toggledAriaExpanded();
}
if (e.shiftKey && e.which == TAB_KEY_CODE) {
//shift was down when tab was pressed
console.log("11");
$(this)
.prev()
.focus();
toggledAriaExpanded();
toggleAccessibilityDropdown(this);
}
/* DOWN_ARROW_KEY_CODE + last child ==> close Menu + Focus next element */
if (e.which == DOWN_ARROW_KEY_CODE) {
console.log("12");
$(this)
.closest(".w-dropdown")
.next()
.focus();
toggleAccessibilityDropdown(this);
}
}
/* If Not first nav element */
if($(this).not(":first-child")) {
console.log("13 - not first child");
if(e.which == UP_ARROW_KEY_CODE) {
$(this)
.prev()
.focus();
}
}
/* IF first nav element */
if ($(this).is(":first-child")) {
/* nested */
if (e.shiftKey && e.which == TAB_KEY_CODE) {
console.log("322222222222");
toggledAriaExpanded();
toggleAccessibilityDropdown(this);
}
/* First Child - UP_ARROW_KEY_CODE ==> Open Menu */
if (e.which == UP_ARROW_KEY_CODE) {
console.log("14");
$(this).closest(".w-dropdown-list").prev().focus();
removeClassAccessibilityDropdown(this);
toggledAriaExpanded();
}
}
//Esc KEY_CODE close menu
if (
e.which == ESCAPE_KEY_CODE &&
$("div.w-dropdown-toggle").hasClass("w--open")
) {
console.log("15");
toggleAccessibilityDropdown(this);
$(this)
.closest(".w-dropdown")
.find(".w-dropdown-toggle")
.focus();
toggledAriaExpanded();
}
});
/* Functions */
function toggleAccessibilityDropdown(obj) {
$(obj)
.closest(".w-dropdown")
.find("div.w-dropdown-toggle")
.toggleClass("w--open");
$(obj)
.closest(".w-dropdown")
.find("nav.w-dropdown-list")
.toggleClass("w--open");
}
function removeClassAccessibilityDropdown(obj) {
$(obj)
.closest("div.w-dropdown-toggle")
.removeClass("w--open");
$(obj)
.closest("nav.w-dropdown-list")
.removeClass("w--open");
}
/* ARIA Controls */
$(".w-dropdown").attr("aria-haspopup", "true");
var toggled = false;
function toggledAriaExpanded() {
toggled = !toggled;
$(".w-dropdown").attr("aria-expanded", toggled);
}
/* Add tab index */
$(".w-dropdown-toggle").each(function() {
$(this).attr("tabindex", "0");
});
$("a.w-dropdown-link").each(function() {
$(this).attr("tabindex", "0");
$(this).css("outline","initial");
});
/* fix bug (webflow add tabIndex=-1) - should be 0 */
var slides = document.getElementsByClassName("w-dropdown-link");
setTimeout(function(){
for(var i = 0; i < slides.length; i++)
{
slides.item(i).setAttribute("tabindex", "0");
}
}, 1000);