This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessible-menu.js
220 lines (199 loc) · 6.44 KB
/
accessible-menu.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
212
213
214
215
216
217
218
219
220
/*
* Accessible Menu
* Version: 1.0
* Author: Thomas Mester
* Repo: https://github.com/hotgeart/accessible-menu
*/
(function($){
$.fn.accessibleMenu = function (options) {
this.each(function() {
// Default variable
var el = "#" + this.id;
// Defaults settings
var defaults = {
animation: "none",
animationSpeed: 'fast'
};
var settings = $.extend({}, defaults, options);
// Hide all sub menus if the controller is expanded false
$(el + ' button[aria-expanded="false"]')
.next("ul")
.each(function () {
$(this).hide();
});
// Handle click
$("body").on("click", el + " button", function () {
accessibleMenuToggler($(this));
});
// Handle keyboard
$(this).keydown(function (e) {
e = e || window.event;
var focused = $(":focus");
if (e.keyCode == "27") {
// ESC
if (focused.is('button[aria-expanded="true"]')) {
accessibleMenuToggler(focused);
} else {
var button = focused
.parent()
.parent()
.parent()
.children("button");
button.focus();
accessibleMenuToggler(button);
}
} else if (e.keyCode == "37" || e.keyCode == "38") {
// ARROW LEFT & UP
e.preventDefault();
focused
.parent()
.prev("li")
.children("a, button")
.focus();
} else if (e.keyCode == "39" || e.keyCode == "40") {
// ARROW RIGHT & DOWN
e.preventDefault();
if(focused.is('button[aria-expanded="true"]')){
focused
.next("ul")
.children('li:first-child')
.children("a, button")
.focus();
} else {
focused
.parent()
.next("li")
.children("a, button")
.focus();
}
} else if (e.keyCode == "36") {
// HOME
e.preventDefault();
focused
.parent()
.parent()
.children('li:first-child')
.children('a, button')
.focus();
} else if (e.keyCode == "35") {
// END
e.preventDefault();
focused
.parent()
.parent()
.children("li:last-child")
.children('a, button')
.focus();
} else {
// Normal behavior
}
});
// Toggler
function accessibleMenuToggler(item) {
var depthString = " > ul > li ";
if (item == false) {
$(el + ' button[aria-expanded="true"]').each(function () {
$(this).attr("aria-expanded", false);
animate($(this), 'close');
});
} else {
$(el + depthString.repeat(getCurrentDepth(item)) + ' button[aria-expanded="true"]')
.not(item)
.each(function () {
$(this).attr("aria-expanded", false);
animate($(this), 'close');
});
var ariaExpanded = item.attr("aria-expanded") == "true" ? false : true;
item.attr("aria-expanded", ariaExpanded);
animate(item, 'toggle');
}
}
// To know which level you are in the menu
function getCurrentDepth(element) {
var depth = 0;
for (i = 0; i < element.parents().length; i++) {
if (element.parents()[i].tagName == "NAV") {
/*
We devide by 2 because otherwise it count </li> like a level of depth
Example:
ul > li > ul > li => 4 parents but a depth of 2
So depth = parents / 2
*/
depth = i / 2;
break;
}
}
return depth;
}
// Annimate function
function animate(element, action) {
switch (settings.animation) {
case "fade":
if(action == 'close') {
element.next("ul").fadeOut(settings.animationSpeed);
} else if(action == 'open'){
element.next("ul").fadeIn(settings.animationSpeed);
} else {
element.next("ul").fadeToggle(settings.animationSpeed);
}
break;
case "slide":
if(action == 'close') {
element.next("ul").slideUp(settings.animationSpeed);
} else if(action == 'open'){
element.next("ul").slideDown(settings.animationSpeed);
} else {
element.next("ul").slideToggle(settings.animationSpeed);
}
break;
default:
if(action == 'close') {
element.next("ul").hide();
} else if(action == 'open'){
element.next("ul").show();
} else {
element.next("ul").toggle();
}
}
}
$(el + ", " + el + " *").focusout(function (e) {
if (!$(e.relatedTarget).is(el + ", " + el + " *")) {
accessibleMenuToggler(false);
}
});
});
// polyfill
if (!String.prototype.repeat) {
String.prototype.repeat = function (count) {
"use strict";
if (this == null)
throw new TypeError("can't convert " + this + " to object");
var str = "" + this;
// To convert string to integer.
count = +count;
// Check NaN
if (count != count) count = 0;
if (count < 0) throw new RangeError("repeat count must be non-negative");
if (count == Infinity)
throw new RangeError("repeat count must be less than infinity");
count = Math.floor(count);
if (str.length == 0 || count == 0) return "";
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28)
throw new RangeError(
"repeat count must not overflow maximum string size"
);
var maxCount = str.length * count;
count = Math.floor(Math.log(count) / Math.log(2));
while (count) {
str += str;
count--;
}
str += str.substring(0, maxCount - str.length);
return str;
};
}
};
}(jQuery));