|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<title>YUI Library Examples: Focus Manager Node Plugin: Accessible Menu Button</title> |
|
|
6 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
7 |
<link rel="stylesheet" type="text/css" href="../../assets/yui.css" > |
|
|
8 |
|
|
|
9 |
<style> |
|
|
10 |
/*Supplemental CSS for the YUI distribution*/ |
|
|
11 |
#custom-doc { width: 95%; min-width: 950px; } |
|
|
12 |
#pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
|
13 |
/* #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}*/ |
|
|
14 |
</style> |
|
|
15 |
|
|
|
16 |
<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css"> |
|
|
17 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
18 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
19 |
<style type="text/css"> |
|
|
20 |
|
|
|
21 |
/* The following two styles are necessary to override style rules in the |
|
|
22 |
YUI CSS file. */ |
|
|
23 |
|
|
|
24 |
#example ul { |
|
|
25 |
margin: 0; |
|
|
26 |
} |
|
|
27 |
|
|
|
28 |
#example a:hover { |
|
|
29 |
text-decoration: none; |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
/* Hide the list while it is being transformed into a menu. */ |
|
|
34 |
|
|
|
35 |
.yui-loading #menu-1, |
|
|
36 |
.yui-loading #button-1 { |
|
|
37 |
display: none; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
</style> |
|
|
41 |
|
|
|
42 |
<script type="text/javascript"> |
|
|
43 |
|
|
|
44 |
// Add a class to the documentElement to prevent the user from seeing |
|
|
45 |
// the unstyled menu while the necessary CSS and JavaScript |
|
|
46 |
// dependancies are being fetched. |
|
|
47 |
|
|
|
48 |
document.documentElement.className = "yui-loading"; |
|
|
49 |
|
|
|
50 |
</script> |
|
|
51 |
</head> |
|
|
52 |
<body id="yahoo-com" class=" yui-skin-sam"> |
|
|
53 |
<div id="custom-doc" class="yui-t2"> |
|
|
54 |
<div id="hd"> |
|
|
55 |
<div id="ygunav"> |
|
|
56 |
<p> |
|
|
57 |
<em> |
|
|
58 |
<a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
|
59 |
</em> |
|
|
60 |
</p> |
|
|
61 |
<form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
|
62 |
<input name="vs" type="hidden" value="developer.yahoo.com"> |
|
|
63 |
<input name="vs" type="hidden" value="yuiblog.com"> |
|
|
64 |
<div id="sitesearch"> |
|
|
65 |
<label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
|
66 |
<input type="text" id="searchinput" name="p"> |
|
|
67 |
<input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
|
68 |
</div> |
|
|
69 |
</form> |
|
|
70 |
</div> |
|
|
71 |
<div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
|
72 |
<div id="pagetitle"><h1>YUI Library Examples: Focus Manager Node Plugin: Accessible Menu Button</h1></div> |
|
|
73 |
</div> |
|
|
74 |
<div id="bd"> |
|
|
75 |
|
|
|
76 |
<div id="bar-note"><p><strong>Note:</strong> This is YUI 3.x. Looking for <a href="http://developer.yahoo.com/yui/">YUI 2.x</a>?</p></div> |
|
|
77 |
|
|
|
78 |
<div id="yui-main"> |
|
|
79 |
<div class="yui-b"> |
|
|
80 |
<div class="yui-ge"> |
|
|
81 |
<div class="yui-u first example" id="main"> |
|
|
82 |
|
|
|
83 |
<h2>Focus Manager Node Plugin: Accessible Menu Button</h2> |
|
|
84 |
|
|
|
85 |
<div id="example" class="promo"> |
|
|
86 |
<p> |
|
|
87 |
<p> |
|
|
88 |
This example illustrates how to use the Focus Manager Node Plugin, |
|
|
89 |
Event's <a href="../../api/YUI.html#event_delegate">delegation support</a> and |
|
|
90 |
<a href="../../api/YUI.html#event_mouseenter">mouseenter</a> event, along with |
|
|
91 |
the <a href="../../overlay/">Overlay widget</a> and Node's support for the |
|
|
92 |
<a href="http://www.w3.org/TR/wai-aria/">WAI-ARIA Roles and States</a> to |
|
|
93 |
create an accessible menu button. |
|
|
94 |
</p> </p> |
|
|
95 |
|
|
|
96 |
<div class="module example-container "> |
|
|
97 |
<div class="hd exampleHd"> |
|
|
98 |
<p class="newWindowButton yui-skin-sam"> |
|
|
99 |
<a href="node-focusmanager-3_clean.html" target="_blank">View example in new window.</a> |
|
|
100 |
</p> |
|
|
101 |
</div> <div id="example-canvas" class="bd"> |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
105 |
|
|
|
106 |
<a id="button-1" href="#menu-1"><span><span>Move To</span></span></a> |
|
|
107 |
<div id="menu-1"> |
|
|
108 |
<ul> |
|
|
109 |
<li><input type="button" name="button-1" value="Inbox"></li> |
|
|
110 |
<li><input type="button" name="button-2" value="Archive"></li> |
|
|
111 |
<li><input type="button" name="button-3" value="Trash"></li> |
|
|
112 |
</ul> |
|
|
113 |
</div> |
|
|
114 |
|
|
|
115 |
<!-- YUI Seed --> |
|
|
116 |
<script type="text/javascript" src="../../build/yui/yui.js"></script> |
|
|
117 |
<script type="text/javascript"> |
|
|
118 |
|
|
|
119 |
YUI({ |
|
|
120 |
base:"../../build/", |
|
|
121 |
timeout: 10000, |
|
|
122 |
modules: { |
|
|
123 |
"menubutton-css": { |
|
|
124 |
type: "css", |
|
|
125 |
fullpath: "assets/menubutton.css" |
|
|
126 |
} |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
}).use("menubutton-css", "node-focusmanager", "node-event-simulate", "overlay", function(Y) { |
|
|
130 |
|
|
|
131 |
var menuButton = Y.Node.get("#button-1"), |
|
|
132 |
menu; |
|
|
133 |
|
|
|
134 |
|
|
|
135 |
var initMenu = function () { |
|
|
136 |
|
|
|
137 |
menu = new Y.Overlay({ |
|
|
138 |
contentBox: "#menu-1", |
|
|
139 |
visible: false, |
|
|
140 |
tabIndex: null |
|
|
141 |
}); |
|
|
142 |
|
|
|
143 |
menu.render(); |
|
|
144 |
|
|
|
145 |
|
|
|
146 |
Y.Node.get("#menu-1").setStyle("display", ""); |
|
|
147 |
|
|
|
148 |
var boundingBox = menu.get("boundingBox"), |
|
|
149 |
contentBox = menu.get("contentBox"); |
|
|
150 |
|
|
|
151 |
boundingBox.addClass("yui-buttonmenu"); |
|
|
152 |
contentBox.addClass("yui-buttonmenu-content"); |
|
|
153 |
|
|
|
154 |
|
|
|
155 |
// Append a decorator element to the bounding box to render the shadow. |
|
|
156 |
|
|
|
157 |
boundingBox.appendChild(Y.Node.create('<div class="yui-menu-shadow"></div>')); |
|
|
158 |
|
|
|
159 |
|
|
|
160 |
// Apply the ARIA roles, states and properties to the menu. |
|
|
161 |
|
|
|
162 |
boundingBox.setAttrs({ |
|
|
163 |
role: "menu", |
|
|
164 |
"aria-labelledby": menuButton.get("id") |
|
|
165 |
}); |
|
|
166 |
|
|
|
167 |
boundingBox.queryAll("input").set("role", "menuitem"); |
|
|
168 |
|
|
|
169 |
|
|
|
170 |
// For NVDA: Add the role of "presentation" to each LI |
|
|
171 |
// element to prevent NVDA from announcing the |
|
|
172 |
// "listitem" role. |
|
|
173 |
|
|
|
174 |
boundingBox.queryAll("div,ul,li").set("role", "presentation"); |
|
|
175 |
|
|
|
176 |
|
|
|
177 |
// Use the FocusManager Node Plugin to manage the focusability |
|
|
178 |
// of each menuitem in the menu. |
|
|
179 |
|
|
|
180 |
contentBox.plug(Y.Plugin.NodeFocusManager, { |
|
|
181 |
|
|
|
182 |
descendants: "input", |
|
|
183 |
keys: { next: "down:40", // Down arrow |
|
|
184 |
previous: "down:38" }, // Up arrow |
|
|
185 |
focusClass: { |
|
|
186 |
className: "yui-menuitem-active", |
|
|
187 |
fn: function (node) { |
|
|
188 |
return node.get("parentNode"); |
|
|
189 |
} |
|
|
190 |
}, |
|
|
191 |
circular: true |
|
|
192 |
|
|
|
193 |
}); |
|
|
194 |
|
|
|
195 |
|
|
|
196 |
// Subscribe to the change event for the "focused" attribute |
|
|
197 |
// to listen for when the menu initially gains focus, and |
|
|
198 |
// when the menu has lost focus completely. |
|
|
199 |
|
|
|
200 |
contentBox.focusManager.after("focusedChange", function (event) { |
|
|
201 |
|
|
|
202 |
if (!event.newVal) { // The menu has lost focus |
|
|
203 |
|
|
|
204 |
// Set the "activeDescendant" attribute to 0 when the |
|
|
205 |
// menu is hidden so that the user can tab from the |
|
|
206 |
// button to the first item in the menu the next time |
|
|
207 |
// the menu is made visible. |
|
|
208 |
|
|
|
209 |
this.set("activeDescendant", 0); |
|
|
210 |
|
|
|
211 |
} |
|
|
212 |
|
|
|
213 |
}); |
|
|
214 |
|
|
|
215 |
|
|
|
216 |
// Hide the button's menu if the user presses the escape key |
|
|
217 |
// while focused either on the button or its menu. |
|
|
218 |
|
|
|
219 |
Y.on("key", function () { |
|
|
220 |
|
|
|
221 |
menu.hide(); |
|
|
222 |
menuButton.focus(); |
|
|
223 |
|
|
|
224 |
}, [menuButton, boundingBox] ,"down:27"); |
|
|
225 |
|
|
|
226 |
|
|
|
227 |
if (Y.UA.ie === 6) { |
|
|
228 |
|
|
|
229 |
// Set the width and height of the menu's bounding box - |
|
|
230 |
// this is necessary for IE 6 so that the CSS for the |
|
|
231 |
// shadow element can simply set the shadow's width and |
|
|
232 |
// height to 100% to ensure that dimensions of the shadow |
|
|
233 |
// are always sync'd to the that of its parent menu. |
|
|
234 |
|
|
|
235 |
menu.on("visibleChange", function (event) { |
|
|
236 |
|
|
|
237 |
if (event.newVal) { |
|
|
238 |
|
|
|
239 |
boundingBox.setStyles({ height: "", width: "" }); |
|
|
240 |
|
|
|
241 |
boundingBox.setStyles({ |
|
|
242 |
height: (boundingBox.get("offsetHeight") + "px"), |
|
|
243 |
width: (boundingBox.get("offsetWidth") + "px") }); |
|
|
244 |
|
|
|
245 |
} |
|
|
246 |
|
|
|
247 |
}); |
|
|
248 |
|
|
|
249 |
} |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
menu.after("visibleChange", function (event) { |
|
|
253 |
|
|
|
254 |
var bVisible = event.newVal; |
|
|
255 |
|
|
|
256 |
// Focus the first item when the menu is made visible |
|
|
257 |
// to allow users to navigate the menu via the keyboard |
|
|
258 |
|
|
|
259 |
if (bVisible) { |
|
|
260 |
|
|
|
261 |
// Need to set focus via a timer for Webkit and Opera |
|
|
262 |
Y.Lang.later(0, contentBox.focusManager, contentBox.focusManager.focus); |
|
|
263 |
|
|
|
264 |
} |
|
|
265 |
|
|
|
266 |
boundingBox.set("aria-hidden", (!bVisible)); |
|
|
267 |
|
|
|
268 |
}); |
|
|
269 |
|
|
|
270 |
|
|
|
271 |
// Hide the menu when one of menu items is clicked. |
|
|
272 |
|
|
|
273 |
Y.on("delegate", function (event) { |
|
|
274 |
|
|
|
275 |
var oTarget = event.currentTarget; |
|
|
276 |
|
|
|
277 |
alert("You clicked " + oTarget.query("input").get("value")); |
|
|
278 |
|
|
|
279 |
contentBox.focusManager.blur(); |
|
|
280 |
menu.hide(); |
|
|
281 |
|
|
|
282 |
}, boundingBox, "click", "li"); |
|
|
283 |
|
|
|
284 |
|
|
|
285 |
// Focus each menuitem as the user moves the mouse over |
|
|
286 |
// the menu. |
|
|
287 |
|
|
|
288 |
Y.on("mouseenter", function (event) { |
|
|
289 |
|
|
|
290 |
var focusManager = contentBox.focusManager; |
|
|
291 |
|
|
|
292 |
if (focusManager.get("focused")) { |
|
|
293 |
focusManager.focus(event.currentTarget.query("input")); |
|
|
294 |
} |
|
|
295 |
|
|
|
296 |
}, boundingBox, "li"); |
|
|
297 |
|
|
|
298 |
|
|
|
299 |
// Hide the menu if the user clicks outside of it or if the |
|
|
300 |
// user doesn't click on the button |
|
|
301 |
|
|
|
302 |
boundingBox.get("ownerDocument").on("mousedown", function (event) { |
|
|
303 |
|
|
|
304 |
var oTarget = event.currentTarget; |
|
|
305 |
|
|
|
306 |
if (!oTarget.compareTo(menuButton) && |
|
|
307 |
!menuButton.contains(oTarget) && |
|
|
308 |
!oTarget.compareTo(boundingBox) && |
|
|
309 |
!boundingBox.contains(oTarget)) { |
|
|
310 |
|
|
|
311 |
menu.hide(); |
|
|
312 |
|
|
|
313 |
} |
|
|
314 |
|
|
|
315 |
}); |
|
|
316 |
|
|
|
317 |
}; |
|
|
318 |
|
|
|
319 |
|
|
|
320 |
menuButton.addClass("yui-menubutton"); |
|
|
321 |
|
|
|
322 |
|
|
|
323 |
// Hide the list until it is transformed into a menu |
|
|
324 |
|
|
|
325 |
Y.Node.get("#menu-1").setStyle("display", "none"); |
|
|
326 |
|
|
|
327 |
|
|
|
328 |
// Remove the "yui-loading" class from the documentElement |
|
|
329 |
// now that the necessary YUI dependencies are loaded. |
|
|
330 |
|
|
|
331 |
menuButton.get("ownerDocument").get("documentElement").removeClass("yui-loading"); |
|
|
332 |
|
|
|
333 |
|
|
|
334 |
// Apply the ARIA roles, states and properties to the anchor. |
|
|
335 |
|
|
|
336 |
menuButton.setAttrs({ |
|
|
337 |
role: "button", |
|
|
338 |
"aria-haspopup": true |
|
|
339 |
}); |
|
|
340 |
|
|
|
341 |
|
|
|
342 |
// Remove the "href" attribute from the anchor element to |
|
|
343 |
// prevent JAWS and NVDA from reading the value of the "href" |
|
|
344 |
// attribute when the anchor is focused. |
|
|
345 |
|
|
|
346 |
if ((Y.UA.gecko || Y.UA.ie) && navigator.userAgent.indexOf("Windows") > -1) { |
|
|
347 |
|
|
|
348 |
menuButton.removeAttribute("href"); |
|
|
349 |
|
|
|
350 |
// Since the anchor's "href" attribute has been removed, the |
|
|
351 |
// element will not fire the click event in Firefox when the |
|
|
352 |
// user presses the enter key. To fix this, dispatch the |
|
|
353 |
// "click" event to the anchor when the user presses the |
|
|
354 |
// enter key. |
|
|
355 |
|
|
|
356 |
Y.on("key", function (event) { |
|
|
357 |
|
|
|
358 |
event.currentTarget.simulate("click"); |
|
|
359 |
|
|
|
360 |
}, menuButton, "down:13"); |
|
|
361 |
|
|
|
362 |
} |
|
|
363 |
|
|
|
364 |
|
|
|
365 |
// Set the "tabIndex" attribute of the anchor element to 0 to |
|
|
366 |
// place it in the browser's default tab flow. This is |
|
|
367 |
// necessary since 1) anchor elements are not in the default |
|
|
368 |
// tab flow in Opera and 2) removing the "href" attribute |
|
|
369 |
// prevents the anchor from firing its "click" event |
|
|
370 |
// in Firefox. |
|
|
371 |
|
|
|
372 |
menuButton.set("tabIndex", 0); |
|
|
373 |
|
|
|
374 |
|
|
|
375 |
var showMenu = function (event) { |
|
|
376 |
|
|
|
377 |
// For performance: Defer the creation of the menu until |
|
|
378 |
// the first time the button is clicked. |
|
|
379 |
|
|
|
380 |
if (!menu) { |
|
|
381 |
initMenu(); |
|
|
382 |
} |
|
|
383 |
|
|
|
384 |
|
|
|
385 |
if (!menu.get("visible")) { |
|
|
386 |
|
|
|
387 |
menu.set("align", { |
|
|
388 |
node: menuButton, |
|
|
389 |
points: [Y.WidgetPositionExt.TL, Y.WidgetPositionExt.BL] |
|
|
390 |
}); |
|
|
391 |
|
|
|
392 |
menu.show(); |
|
|
393 |
|
|
|
394 |
} |
|
|
395 |
|
|
|
396 |
// Prevent the anchor element from being focused |
|
|
397 |
// when the users mouses down on it. |
|
|
398 |
event.preventDefault(); |
|
|
399 |
|
|
|
400 |
}; |
|
|
401 |
|
|
|
402 |
|
|
|
403 |
// Bind both a "mousedown" and "click" event listener to |
|
|
404 |
// ensure the button's menu can be invoked using both the |
|
|
405 |
// mouse and the keyboard. |
|
|
406 |
|
|
|
407 |
menuButton.on("mousedown", showMenu); |
|
|
408 |
menuButton.on("click", showMenu); |
|
|
409 |
|
|
|
410 |
}); |
|
|
411 |
|
|
|
412 |
</script> |
|
|
413 |
|
|
|
414 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
415 |
|
|
|
416 |
|
|
|
417 |
</div> |
|
|
418 |
</div> |
|
|
419 |
</div> |
|
|
420 |
|
|
|
421 |
<h3>Setting Up the HTML</h3> |
|
|
422 |
<p> |
|
|
423 |
For a menu button, start with an <code><a></code> element whose |
|
|
424 |
<code>href</code> attribute is set to the id of an <code><div></code> |
|
|
425 |
that wraps a list of <code><input></code> elements. |
|
|
426 |
Therefore, without JavaScript and CSS, the menu button is simply an in-page |
|
|
427 |
link to a set of additional buttons. |
|
|
428 |
</p> |
|
|
429 |
<textarea name="code" class="HTML" cols="60" rows="1"> |
|
|
430 |
<a id="button-1" href="#menu-1"><span><span>Move To</span></span></a> |
|
|
431 |
<div id="menu-1"> |
|
|
432 |
<ul> |
|
|
433 |
<li><input type="button" name="button-1" value="Inbox"></li> |
|
|
434 |
<li><input type="button" name="button-2" value="Archive"></li> |
|
|
435 |
<li><input type="button" name="button-3" value="Trash"></li> |
|
|
436 |
</ul> |
|
|
437 |
</div> |
|
|
438 |
</textarea> |
|
|
439 |
|
|
|
440 |
|
|
|
441 |
<h3>Progressive Enhancement</h3> |
|
|
442 |
<p> |
|
|
443 |
The markup above will be transformed into a menu button widget using both CSS |
|
|
444 |
and JavaScript. To account for the scenario where the user has CSS enabled in |
|
|
445 |
their browser but JavaScript is disabled, the CSS used to style the menu button |
|
|
446 |
will be loaded via JavaScript using the |
|
|
447 |
<a href="http://developer.yahoo.com/yui/3/yui/#loader">built-in loader of the YUI Global Object</a>. |
|
|
448 |
Additionally, JavaScript will be used to temporarily hide the menu button markup |
|
|
449 |
while the JavaScript and CSS are in the process of loading to prevent the user |
|
|
450 |
from seeing a flash unstyled content. |
|
|
451 |
</p> |
|
|
452 |
|
|
|
453 |
|
|
|
454 |
<h3>ARIA Support</h3> |
|
|
455 |
<p> |
|
|
456 |
Through the use of CSS and JavaScript the HTML for the menu button can be |
|
|
457 |
transformed into something that looks and behaves like a desktop menu button, |
|
|
458 |
but users of screen readers won't perceive it as an atomic widget, but rather |
|
|
459 |
simply as a set of HTML elements. However, through the application |
|
|
460 |
of the |
|
|
461 |
<a href="http://www.w3.org/TR/wai-aria/">WAI-ARIA Roles and States</a>, it is |
|
|
462 |
possible to improve the semantics of the markup such that users of screen |
|
|
463 |
readers perceive it as a menu button control. |
|
|
464 |
</p> |
|
|
465 |
|
|
|
466 |
|
|
|
467 |
<h3>Keyboard Functionality</h3> |
|
|
468 |
<p> |
|
|
469 |
The keyboard functionality for the button's menu will be provided by the Focus |
|
|
470 |
Manager Node Plugin. The Focus Manager's |
|
|
471 |
<a href="../../api/plugin.NodeFocusManager.html#config_descendants"><code>descendants</code></a> |
|
|
472 |
attribute is set to a value of "input", so that only one menuitem in the |
|
|
473 |
button's menu is in the browser's default tab flow. This allows users |
|
|
474 |
navigating via the keyboard to use the tab key to quickly move into and out of |
|
|
475 |
the menu. Once the menu has focus, the user can move focus among each menuitem |
|
|
476 |
using the up and down arrows keys, as defined by the value of the |
|
|
477 |
<a href="../../api/plugin.NodeFocusManager.html#config_keys"><code>keys</code></a> |
|
|
478 |
attribute. The |
|
|
479 |
<a href="../../api/plugin.NodeFocusManager.html#config_focusClass"><code>focusClass</code></a> |
|
|
480 |
attribute is used to apply a class of <code>yui-menuitem-active</code> to |
|
|
481 |
the parent <code><li></code> of each |
|
|
482 |
<code><input></code> when it is focused, making it easy to style the |
|
|
483 |
menuitem's focused state in each of the |
|
|
484 |
<a href="http://developer.yahoo.com/yui/articles/gbs/#gbschart">A-Grade browsers</a>. |
|
|
485 |
|
|
|
486 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
487 |
YUI({ |
|
|
488 |
base:"../../build/", |
|
|
489 |
timeout: 10000, |
|
|
490 |
modules: { |
|
|
491 |
"menubutton-css": { |
|
|
492 |
type: "css", |
|
|
493 |
fullpath: "assets/menubutton.css" |
|
|
494 |
} |
|
|
495 |
} |
|
|
496 |
|
|
|
497 |
}).use("menubutton-css", "node-focusmanager", "node-event-simulate", "overlay", function(Y) { |
|
|
498 |
|
|
|
499 |
var menuButton = Y.Node.get("#button-1"), |
|
|
500 |
menu; |
|
|
501 |
|
|
|
502 |
|
|
|
503 |
var initMenu = function () { |
|
|
504 |
|
|
|
505 |
menu = new Y.Overlay({ |
|
|
506 |
contentBox: "#menu-1", |
|
|
507 |
visible: false, |
|
|
508 |
tabIndex: null |
|
|
509 |
}); |
|
|
510 |
|
|
|
511 |
menu.render(); |
|
|
512 |
|
|
|
513 |
|
|
|
514 |
Y.Node.get("#menu-1").setStyle("display", ""); |
|
|
515 |
|
|
|
516 |
var boundingBox = menu.get("boundingBox"), |
|
|
517 |
contentBox = menu.get("contentBox"); |
|
|
518 |
|
|
|
519 |
boundingBox.addClass("yui-buttonmenu"); |
|
|
520 |
contentBox.addClass("yui-buttonmenu-content"); |
|
|
521 |
|
|
|
522 |
|
|
|
523 |
// Append a decorator element to the bounding box to render the shadow. |
|
|
524 |
|
|
|
525 |
boundingBox.appendChild(Y.Node.create('<div class="yui-menu-shadow"></div>')); |
|
|
526 |
|
|
|
527 |
|
|
|
528 |
// Apply the ARIA roles, states and properties to the menu. |
|
|
529 |
|
|
|
530 |
boundingBox.setAttrs({ |
|
|
531 |
role: "menu", |
|
|
532 |
"aria-labelledby": menuButton.get("id") |
|
|
533 |
}); |
|
|
534 |
|
|
|
535 |
boundingBox.queryAll("input").set("role", "menuitem"); |
|
|
536 |
|
|
|
537 |
|
|
|
538 |
// For NVDA: Add the role of "presentation" to each LI |
|
|
539 |
// element to prevent NVDA from announcing the |
|
|
540 |
// "listitem" role. |
|
|
541 |
|
|
|
542 |
boundingBox.queryAll("div,ul,li").set("role", "presentation"); |
|
|
543 |
|
|
|
544 |
|
|
|
545 |
// Use the FocusManager Node Plugin to manage the focusability |
|
|
546 |
// of each menuitem in the menu. |
|
|
547 |
|
|
|
548 |
contentBox.plug(Y.Plugin.NodeFocusManager, { |
|
|
549 |
|
|
|
550 |
descendants: "input", |
|
|
551 |
keys: { next: "down:40", // Down arrow |
|
|
552 |
previous: "down:38" }, // Up arrow |
|
|
553 |
focusClass: { |
|
|
554 |
className: "yui-menuitem-active", |
|
|
555 |
fn: function (node) { |
|
|
556 |
return node.get("parentNode"); |
|
|
557 |
} |
|
|
558 |
}, |
|
|
559 |
circular: true |
|
|
560 |
|
|
|
561 |
}); |
|
|
562 |
|
|
|
563 |
|
|
|
564 |
// Subscribe to the change event for the "focused" attribute |
|
|
565 |
// to listen for when the menu initially gains focus, and |
|
|
566 |
// when the menu has lost focus completely. |
|
|
567 |
|
|
|
568 |
contentBox.focusManager.after("focusedChange", function (event) { |
|
|
569 |
|
|
|
570 |
if (!event.newVal) { // The menu has lost focus |
|
|
571 |
|
|
|
572 |
// Set the "activeDescendant" attribute to 0 when the |
|
|
573 |
// menu is hidden so that the user can tab from the |
|
|
574 |
// button to the first item in the menu the next time |
|
|
575 |
// the menu is made visible. |
|
|
576 |
|
|
|
577 |
this.set("activeDescendant", 0); |
|
|
578 |
|
|
|
579 |
} |
|
|
580 |
|
|
|
581 |
}); |
|
|
582 |
|
|
|
583 |
|
|
|
584 |
// Hide the button's menu if the user presses the escape key |
|
|
585 |
// while focused either on the button or its menu. |
|
|
586 |
|
|
|
587 |
Y.on("key", function () { |
|
|
588 |
|
|
|
589 |
menu.hide(); |
|
|
590 |
menuButton.focus(); |
|
|
591 |
|
|
|
592 |
}, [menuButton, boundingBox] ,"down:27"); |
|
|
593 |
|
|
|
594 |
|
|
|
595 |
if (Y.UA.ie === 6) { |
|
|
596 |
|
|
|
597 |
// Set the width and height of the menu's bounding box - |
|
|
598 |
// this is necessary for IE 6 so that the CSS for the |
|
|
599 |
// shadow element can simply set the shadow's width and |
|
|
600 |
// height to 100% to ensure that dimensions of the shadow |
|
|
601 |
// are always sync'd to the that of its parent menu. |
|
|
602 |
|
|
|
603 |
menu.on("visibleChange", function (event) { |
|
|
604 |
|
|
|
605 |
if (event.newVal) { |
|
|
606 |
|
|
|
607 |
boundingBox.setStyles({ height: "", width: "" }); |
|
|
608 |
|
|
|
609 |
boundingBox.setStyles({ |
|
|
610 |
height: (boundingBox.get("offsetHeight") + "px"), |
|
|
611 |
width: (boundingBox.get("offsetWidth") + "px") }); |
|
|
612 |
|
|
|
613 |
} |
|
|
614 |
|
|
|
615 |
}); |
|
|
616 |
|
|
|
617 |
} |
|
|
618 |
|
|
|
619 |
|
|
|
620 |
menu.after("visibleChange", function (event) { |
|
|
621 |
|
|
|
622 |
var bVisible = event.newVal; |
|
|
623 |
|
|
|
624 |
// Focus the first item when the menu is made visible |
|
|
625 |
// to allow users to navigate the menu via the keyboard |
|
|
626 |
|
|
|
627 |
if (bVisible) { |
|
|
628 |
|
|
|
629 |
// Need to set focus via a timer for Webkit and Opera |
|
|
630 |
Y.Lang.later(0, contentBox.focusManager, contentBox.focusManager.focus); |
|
|
631 |
|
|
|
632 |
} |
|
|
633 |
|
|
|
634 |
boundingBox.set("aria-hidden", (!bVisible)); |
|
|
635 |
|
|
|
636 |
}); |
|
|
637 |
|
|
|
638 |
|
|
|
639 |
// Hide the menu when one of menu items is clicked. |
|
|
640 |
|
|
|
641 |
Y.on("delegate", function (event) { |
|
|
642 |
|
|
|
643 |
var oTarget = event.currentTarget; |
|
|
644 |
|
|
|
645 |
alert("You clicked " + oTarget.query("input").get("value")); |
|
|
646 |
|
|
|
647 |
contentBox.focusManager.blur(); |
|
|
648 |
menu.hide(); |
|
|
649 |
|
|
|
650 |
}, boundingBox, "click", "li"); |
|
|
651 |
|
|
|
652 |
|
|
|
653 |
// Focus each menuitem as the user moves the mouse over |
|
|
654 |
// the menu. |
|
|
655 |
|
|
|
656 |
Y.on("mouseenter", function (event) { |
|
|
657 |
|
|
|
658 |
var focusManager = contentBox.focusManager; |
|
|
659 |
|
|
|
660 |
if (focusManager.get("focused")) { |
|
|
661 |
focusManager.focus(event.currentTarget.query("input")); |
|
|
662 |
} |
|
|
663 |
|
|
|
664 |
}, boundingBox, "li"); |
|
|
665 |
|
|
|
666 |
|
|
|
667 |
// Hide the menu if the user clicks outside of it or if the |
|
|
668 |
// user doesn't click on the button |
|
|
669 |
|
|
|
670 |
boundingBox.get("ownerDocument").on("mousedown", function (event) { |
|
|
671 |
|
|
|
672 |
var oTarget = event.currentTarget; |
|
|
673 |
|
|
|
674 |
if (!oTarget.compareTo(menuButton) && |
|
|
675 |
!menuButton.contains(oTarget) && |
|
|
676 |
!oTarget.compareTo(boundingBox) && |
|
|
677 |
!boundingBox.contains(oTarget)) { |
|
|
678 |
|
|
|
679 |
menu.hide(); |
|
|
680 |
|
|
|
681 |
} |
|
|
682 |
|
|
|
683 |
}); |
|
|
684 |
|
|
|
685 |
}; |
|
|
686 |
|
|
|
687 |
|
|
|
688 |
menuButton.addClass("yui-menubutton"); |
|
|
689 |
|
|
|
690 |
|
|
|
691 |
// Hide the list until it is transformed into a menu |
|
|
692 |
|
|
|
693 |
Y.Node.get("#menu-1").setStyle("display", "none"); |
|
|
694 |
|
|
|
695 |
|
|
|
696 |
// Remove the "yui-loading" class from the documentElement |
|
|
697 |
// now that the necessary YUI dependencies are loaded. |
|
|
698 |
|
|
|
699 |
menuButton.get("ownerDocument").get("documentElement").removeClass("yui-loading"); |
|
|
700 |
|
|
|
701 |
|
|
|
702 |
// Apply the ARIA roles, states and properties to the anchor. |
|
|
703 |
|
|
|
704 |
menuButton.setAttrs({ |
|
|
705 |
role: "button", |
|
|
706 |
"aria-haspopup": true |
|
|
707 |
}); |
|
|
708 |
|
|
|
709 |
|
|
|
710 |
// Remove the "href" attribute from the anchor element to |
|
|
711 |
// prevent JAWS and NVDA from reading the value of the "href" |
|
|
712 |
// attribute when the anchor is focused. |
|
|
713 |
|
|
|
714 |
if ((Y.UA.gecko || Y.UA.ie) && navigator.userAgent.indexOf("Windows") > -1) { |
|
|
715 |
|
|
|
716 |
menuButton.removeAttribute("href"); |
|
|
717 |
|
|
|
718 |
// Since the anchor's "href" attribute has been removed, the |
|
|
719 |
// element will not fire the click event in Firefox when the |
|
|
720 |
// user presses the enter key. To fix this, dispatch the |
|
|
721 |
// "click" event to the anchor when the user presses the |
|
|
722 |
// enter key. |
|
|
723 |
|
|
|
724 |
Y.on("key", function (event) { |
|
|
725 |
|
|
|
726 |
event.currentTarget.simulate("click"); |
|
|
727 |
|
|
|
728 |
}, menuButton, "down:13"); |
|
|
729 |
|
|
|
730 |
} |
|
|
731 |
|
|
|
732 |
|
|
|
733 |
// Set the "tabIndex" attribute of the anchor element to 0 to |
|
|
734 |
// place it in the browser's default tab flow. This is |
|
|
735 |
// necessary since 1) anchor elements are not in the default |
|
|
736 |
// tab flow in Opera and 2) removing the "href" attribute |
|
|
737 |
// prevents the anchor from firing its "click" event |
|
|
738 |
// in Firefox. |
|
|
739 |
|
|
|
740 |
menuButton.set("tabIndex", 0); |
|
|
741 |
|
|
|
742 |
|
|
|
743 |
var showMenu = function (event) { |
|
|
744 |
|
|
|
745 |
// For performance: Defer the creation of the menu until |
|
|
746 |
// the first time the button is clicked. |
|
|
747 |
|
|
|
748 |
if (!menu) { |
|
|
749 |
initMenu(); |
|
|
750 |
} |
|
|
751 |
|
|
|
752 |
|
|
|
753 |
if (!menu.get("visible")) { |
|
|
754 |
|
|
|
755 |
menu.set("align", { |
|
|
756 |
node: menuButton, |
|
|
757 |
points: [Y.WidgetPositionExt.TL, Y.WidgetPositionExt.BL] |
|
|
758 |
}); |
|
|
759 |
|
|
|
760 |
menu.show(); |
|
|
761 |
|
|
|
762 |
} |
|
|
763 |
|
|
|
764 |
// Prevent the anchor element from being focused |
|
|
765 |
// when the users mouses down on it. |
|
|
766 |
event.preventDefault(); |
|
|
767 |
|
|
|
768 |
}; |
|
|
769 |
|
|
|
770 |
|
|
|
771 |
// Bind both a "mousedown" and "click" event listener to |
|
|
772 |
// ensure the button's menu can be invoked using both the |
|
|
773 |
// mouse and the keyboard. |
|
|
774 |
|
|
|
775 |
menuButton.on("mousedown", showMenu); |
|
|
776 |
menuButton.on("click", showMenu); |
|
|
777 |
|
|
|
778 |
}); |
|
|
779 |
</textarea> |
|
|
780 |
</div> |
|
|
781 |
<div class="yui-u sidebar"> |
|
|
782 |
|
|
|
783 |
|
|
|
784 |
<div id="examples" class="mod box4"> |
|
|
785 |
<div class="hd"> |
|
|
786 |
<h4> |
|
|
787 |
Focus Manager Node Plugin Examples:</h4> |
|
|
788 |
</div> |
|
|
789 |
<div class="bd"> |
|
|
790 |
<ul> |
|
|
791 |
<li><a href='../node-focusmanager/node-focusmanager-1.html'>Accessible Toolbar</a></li><li><a href='../node-focusmanager/node-focusmanager-2.html'>Accessible TabView</a></li><li class='selected'><a href='../node-focusmanager/node-focusmanager-3.html'>Accessible Menu Button</a></li> </ul> |
|
|
792 |
</div> |
|
|
793 |
</div> |
|
|
794 |
|
|
|
795 |
<div class="mod box4"> |
|
|
796 |
<div class="hd"> |
|
|
797 |
<h4>More Focus Manager Node Plugin Resources:</h4> |
|
|
798 |
</div> |
|
|
799 |
<div class="bd"> |
|
|
800 |
<ul> |
|
|
801 |
<!-- <li><a href="http://developer.yahoo.com/yui/node-focusmanager/">User's Guide</a> (external)</li> --> |
|
|
802 |
<li><a href="../../api/module_node-focusmanager.html">API Documentation</a></li> |
|
|
803 |
</ul> |
|
|
804 |
</div> |
|
|
805 |
</div> |
|
|
806 |
</div> |
|
|
807 |
</div> |
|
|
808 |
|
|
|
809 |
</div> |
|
|
810 |
</div> |
|
|
811 |
|
|
|
812 |
|
|
|
813 |
<div class="yui-b toc3" id="tocWrapper"> |
|
|
814 |
<!-- TABLE OF CONTENTS --> |
|
|
815 |
<div id="toc"> |
|
|
816 |
|
|
|
817 |
<ul> |
|
|
818 |
<li class="sect first">YUI 3.x Project</li><li class="item"><a title="The Yahoo! User Interface (YUI) Library, 3.x Branch, " href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site (external)</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="The YUI Library can be downloaded from SourceForge" href="http://sourceforge.net/projects/yui/">YUI 3 on Sourceforge (external)</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/3/license.html">YUI License (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI (Global Prerequisite) - Functional Examples" href="../../examples/yui/index.html">YUI (Global Prerequisite)</a></li><li class="item"><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="item"><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Plugin - Functional Examples" href="../../examples/plugin/index.html">Plugin</a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget</a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="Cache - Functional Examples" href="../../examples/cache/index.html">Cache</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="DataSchema - Functional Examples" href="../../examples/dataschema/index.html">DataSchema</a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource</a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType</a></li><li class="item"><a title="Drag & Drop - Functional Examples" href="../../examples/dd/index.html">Drag & Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="item"><a title="History - Functional Examples" href="../../examples/history/index.html">History</a></li><li class="item"><a title="ImageLoader - Functional Examples" href="../../examples/imageloader/index.html">ImageLoader</a></li><li class="item"><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Queue - Functional Examples" href="../../examples/queue/index.html">Queue</a></li><li class="item"><a title="Stylesheet - Functional Examples" href="../../examples/stylesheet/index.html">Stylesheet</a></li><li class="sect">YUI 3 Widgets - Examples</li><li class="item"><a title="Overlay - Functional Examples" href="../../examples/overlay/index.html">Overlay</a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider</a></li><li class="sect">YUI 3 Node Plugins - Examples</li><li class="item"><a title="FocusManager Node Plugin - Functional Examples" href="../../examples/node-focusmanager/index.html">FocusManager Node Plugin</a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin</a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">YUI 3 Developer Tools - Examples</li><li class="item"><a title="Console - Functional Examples" href="../../examples/console/index.html">Console</a></li><li class="item"><a title="Profiler - Functional Examples" href="../../examples/profiler/index.html">Profiler</a></li><li class="item"><a title="Test - Functional Examples" href="../../examples/test/index.html">Test</a></li><li class="sect">The YUI Community</li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="item"><a title="The Yahoo! Group YDN-JavaScript hosts the YUI community forum" href="http://tech.groups.yahoo.com/group/ydn-javascript/">YUI Forum (external)</a></li><li class="item"><a title="The Yahoo! Group yui3 is dedicated to the 3.x branch of the Yahoo! User Interface (YUI) Library." href="http://tech.groups.yahoo.com/group/yui3/">YUI 3 Forum (external)</a></li><li class="item"><a title="YUI is used by Yahoo! and by hundreds of other sites, including many you know and love." href="/yui/poweredby/">YUI Sightings (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li><li class="sect">YUI Articles on the YUI Website</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Reporting Bugs and Making Feature Requests for YUI Components" href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests (external)</a></li><li class="item"><a title="Serve YUI source files from Yahoo! -- free, fast, and simple" href="http://developer.yahoo.com/yui/3/articles/hosting/">Serving YUI Files from Yahoo! (external)</a></li></ul> |
|
|
819 |
</div> |
|
|
820 |
</div> |
|
|
821 |
</div><!--closes bd--> |
|
|
822 |
|
|
|
823 |
<div id="ft"> |
|
|
824 |
<p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
|
825 |
<p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
|
826 |
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
|
827 |
<a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
|
828 |
<a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
|
829 |
</div> |
|
|
830 |
</div> |
|
|
831 |
<script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
|
832 |
<script language="javascript"> |
|
|
833 |
dp.SyntaxHighlighter.HighlightAll('code'); |
|
|
834 |
</script> |
|
|
835 |
</body> |
|
|
836 |
</html> |