1 |
efrain |
1 |
YUI.add('node-focusmanager', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* <p>The Focus Manager Node Plugin makes it easy to manage focus among
|
|
|
5 |
* a Node's descendants. Primarily intended to help with widget development,
|
|
|
6 |
* the Focus Manager Node Plugin can be used to improve the keyboard
|
|
|
7 |
* accessibility of widgets.</p>
|
|
|
8 |
*
|
|
|
9 |
* <p>
|
|
|
10 |
* When designing widgets that manage a set of descendant controls (i.e. buttons
|
|
|
11 |
* in a toolbar, tabs in a tablist, menuitems in a menu, etc.) it is important to
|
|
|
12 |
* limit the number of descendants in the browser's default tab flow. The fewer
|
|
|
13 |
* number of descendants in the default tab flow, the easier it is for keyboard
|
|
|
14 |
* users to navigate between widgets by pressing the tab key. When a widget has
|
|
|
15 |
* focus it should provide a set of shortcut keys (typically the arrow keys)
|
|
|
16 |
* to move focus among its descendants.
|
|
|
17 |
* </p>
|
|
|
18 |
*
|
|
|
19 |
* <p>
|
|
|
20 |
* To this end, the Focus Manager Node Plugin makes it easy to define a Node's
|
|
|
21 |
* focusable descendants, define which descendant should be in the default tab
|
|
|
22 |
* flow, and define the keys that move focus among each descendant.
|
|
|
23 |
* Additionally, as the CSS
|
|
|
24 |
* <a href="http://www.w3.org/TR/CSS21/selector.html#x38"><code>:focus</code></a>
|
|
|
25 |
* pseudo class is not supported on all elements in all
|
|
|
26 |
* <a href="http://developer.yahoo.com/yui/articles/gbs/">A-Grade browsers</a>,
|
|
|
27 |
* the Focus Manager Node Plugin provides an easy, cross-browser means of
|
|
|
28 |
* styling focus.
|
|
|
29 |
* </p>
|
|
|
30 |
*
|
|
|
31 |
|
|
|
32 |
DEPRECATED: The FocusManager Node Plugin has been deprecated as of YUI 3.9.0. This module will be removed from the library in a future version. If you require functionality similar to the one provided by this module, consider taking a look at the various modules in the YUI Gallery <http://yuilibrary.com/gallery/>.
|
|
|
33 |
|
|
|
34 |
* @module node-focusmanager
|
|
|
35 |
* @deprecated 3.9.0
|
|
|
36 |
*/
|
|
|
37 |
|
|
|
38 |
// Frequently used strings
|
|
|
39 |
|
|
|
40 |
var ACTIVE_DESCENDANT = "activeDescendant",
|
|
|
41 |
ID = "id",
|
|
|
42 |
DISABLED = "disabled",
|
|
|
43 |
TAB_INDEX = "tabIndex",
|
|
|
44 |
FOCUSED = "focused",
|
|
|
45 |
FOCUS_CLASS = "focusClass",
|
|
|
46 |
CIRCULAR = "circular",
|
|
|
47 |
UI = "UI",
|
|
|
48 |
KEY = "key",
|
|
|
49 |
ACTIVE_DESCENDANT_CHANGE = ACTIVE_DESCENDANT + "Change",
|
|
|
50 |
HOST = "host",
|
|
|
51 |
|
|
|
52 |
// Collection of keys that, when pressed, cause the browser viewport
|
|
|
53 |
// to scroll.
|
|
|
54 |
scrollKeys = {
|
|
|
55 |
37: true,
|
|
|
56 |
38: true,
|
|
|
57 |
39: true,
|
|
|
58 |
40: true
|
|
|
59 |
},
|
|
|
60 |
|
|
|
61 |
clickableElements = {
|
|
|
62 |
"a": true,
|
|
|
63 |
"button": true,
|
|
|
64 |
"input": true,
|
|
|
65 |
"object": true
|
|
|
66 |
},
|
|
|
67 |
|
|
|
68 |
// Library shortcuts
|
|
|
69 |
|
|
|
70 |
Lang = Y.Lang,
|
|
|
71 |
UA = Y.UA,
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* The NodeFocusManager class is a plugin for a Node instance. The class is used
|
|
|
75 |
* via the <a href="Node.html#method_plug"><code>plug</code></a> method of Node
|
|
|
76 |
* and should not be instantiated directly.
|
|
|
77 |
* @namespace plugin
|
|
|
78 |
* @class NodeFocusManager
|
|
|
79 |
*/
|
|
|
80 |
NodeFocusManager = function () {
|
|
|
81 |
|
|
|
82 |
NodeFocusManager.superclass.constructor.apply(this, arguments);
|
|
|
83 |
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
NodeFocusManager.ATTRS = {
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Boolean indicating that one of the descendants is focused.
|
|
|
91 |
*
|
|
|
92 |
* @attribute focused
|
|
|
93 |
* @readOnly
|
|
|
94 |
* @default false
|
|
|
95 |
* @type boolean
|
|
|
96 |
*/
|
|
|
97 |
focused: {
|
|
|
98 |
|
|
|
99 |
value: false,
|
|
|
100 |
readOnly: true
|
|
|
101 |
|
|
|
102 |
},
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* String representing the CSS selector used to define the descendant Nodes
|
|
|
107 |
* whose focus should be managed.
|
|
|
108 |
*
|
|
|
109 |
* @attribute descendants
|
|
|
110 |
* @type Y.NodeList
|
|
|
111 |
*/
|
|
|
112 |
descendants: {
|
|
|
113 |
|
|
|
114 |
getter: function (value) {
|
|
|
115 |
|
|
|
116 |
return this.get(HOST).all(value);
|
|
|
117 |
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
},
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* <p>Node, or index of the Node, representing the descendant that is either
|
|
|
125 |
* focused or is focusable (<code>tabIndex</code> attribute is set to 0).
|
|
|
126 |
* The value cannot represent a disabled descendant Node. Use a value of -1
|
|
|
127 |
* to remove all descendant Nodes from the default tab flow.
|
|
|
128 |
* If no value is specified, the active descendant will be inferred using
|
|
|
129 |
* the following criteria:</p>
|
|
|
130 |
* <ol>
|
|
|
131 |
* <li>Examining the <code>tabIndex</code> attribute of each descendant and
|
|
|
132 |
* using the first descendant whose <code>tabIndex</code> attribute is set
|
|
|
133 |
* to 0</li>
|
|
|
134 |
* <li>If no default can be inferred then the value is set to either 0 or
|
|
|
135 |
* the index of the first enabled descendant.</li>
|
|
|
136 |
* </ol>
|
|
|
137 |
*
|
|
|
138 |
* @attribute activeDescendant
|
|
|
139 |
* @type Number
|
|
|
140 |
*/
|
|
|
141 |
activeDescendant: {
|
|
|
142 |
|
|
|
143 |
setter: function (value) {
|
|
|
144 |
|
|
|
145 |
var isNumber = Lang.isNumber,
|
|
|
146 |
INVALID_VALUE = Y.Attribute.INVALID_VALUE,
|
|
|
147 |
descendantsMap = this._descendantsMap,
|
|
|
148 |
descendants = this._descendants,
|
|
|
149 |
nodeIndex,
|
|
|
150 |
returnValue,
|
|
|
151 |
oNode;
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
if (isNumber(value)) {
|
|
|
155 |
nodeIndex = value;
|
|
|
156 |
returnValue = nodeIndex;
|
|
|
157 |
}
|
|
|
158 |
else if ((value instanceof Y.Node) && descendantsMap) {
|
|
|
159 |
|
|
|
160 |
nodeIndex = descendantsMap[value.get(ID)];
|
|
|
161 |
|
|
|
162 |
if (isNumber(nodeIndex)) {
|
|
|
163 |
returnValue = nodeIndex;
|
|
|
164 |
}
|
|
|
165 |
else {
|
|
|
166 |
|
|
|
167 |
// The user passed a reference to a Node that wasn't one
|
|
|
168 |
// of the descendants.
|
|
|
169 |
returnValue = INVALID_VALUE;
|
|
|
170 |
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
}
|
|
|
174 |
else {
|
|
|
175 |
returnValue = INVALID_VALUE;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
if (descendants) {
|
|
|
180 |
|
|
|
181 |
oNode = descendants.item(nodeIndex);
|
|
|
182 |
|
|
|
183 |
if (oNode && oNode.get("disabled")) {
|
|
|
184 |
|
|
|
185 |
// Setting the "activeDescendant" attribute to the index
|
|
|
186 |
// of a disabled descendant is invalid.
|
|
|
187 |
returnValue = INVALID_VALUE;
|
|
|
188 |
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
return returnValue;
|
|
|
195 |
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
},
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Object literal representing the keys to be used to navigate between the
|
|
|
203 |
* next/previous descendant. The format for the attribute's value is
|
|
|
204 |
* <code>{ next: "down:40", previous: "down:38" }</code>. The value for the
|
|
|
205 |
* "next" and "previous" properties are used to attach
|
|
|
206 |
* <a href="event/#keylistener"><code>key</code></a> event listeners. See
|
|
|
207 |
* the <a href="event/#keylistener">Using the key Event</a> section of
|
|
|
208 |
* the Event documentation for more information on "key" event listeners.
|
|
|
209 |
*
|
|
|
210 |
* @attribute keys
|
|
|
211 |
* @type Object
|
|
|
212 |
*/
|
|
|
213 |
keys: {
|
|
|
214 |
|
|
|
215 |
value: {
|
|
|
216 |
|
|
|
217 |
next: null,
|
|
|
218 |
previous: null
|
|
|
219 |
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
},
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* String representing the name of class applied to the focused active
|
|
|
228 |
* descendant Node. Can also be an object literal used to define both the
|
|
|
229 |
* class name, and the Node to which the class should be applied. If using
|
|
|
230 |
* an object literal, the format is:
|
|
|
231 |
* <code>{ className: "focus", fn: myFunction }</code>. The function
|
|
|
232 |
* referenced by the <code>fn</code> property in the object literal will be
|
|
|
233 |
* passed a reference to the currently focused active descendant Node.
|
|
|
234 |
*
|
|
|
235 |
* @attribute focusClass
|
|
|
236 |
* @type String|Object
|
|
|
237 |
*/
|
|
|
238 |
focusClass: { },
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Boolean indicating if focus should be set to the first/last descendant
|
|
|
243 |
* when the end or beginning of the descendants has been reached.
|
|
|
244 |
*
|
|
|
245 |
* @attribute circular
|
|
|
246 |
* @type Boolean
|
|
|
247 |
* @default true
|
|
|
248 |
*/
|
|
|
249 |
circular: {
|
|
|
250 |
value: true
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
};
|
|
|
254 |
|
|
|
255 |
Y.extend(NodeFocusManager, Y.Plugin.Base, {
|
|
|
256 |
|
|
|
257 |
// Protected properties
|
|
|
258 |
|
|
|
259 |
// Boolean indicating if the NodeFocusManager is active.
|
|
|
260 |
_stopped: true,
|
|
|
261 |
|
|
|
262 |
// NodeList representing the descendants selected via the
|
|
|
263 |
// "descendants" attribute.
|
|
|
264 |
_descendants: null,
|
|
|
265 |
|
|
|
266 |
// Object literal mapping the IDs of each descendant to its index in the
|
|
|
267 |
// "_descendants" NodeList.
|
|
|
268 |
_descendantsMap: null,
|
|
|
269 |
|
|
|
270 |
// Reference to the Node instance to which the focused class (defined
|
|
|
271 |
// by the "focusClass" attribute) is currently applied.
|
|
|
272 |
_focusedNode: null,
|
|
|
273 |
|
|
|
274 |
// Number representing the index of the last descendant Node.
|
|
|
275 |
_lastNodeIndex: 0,
|
|
|
276 |
|
|
|
277 |
// Array of handles for event handlers used for a NodeFocusManager instance.
|
|
|
278 |
_eventHandlers: null,
|
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
// Protected methods
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* @method _initDescendants
|
|
|
286 |
* @description Sets the <code>tabIndex</code> attribute of all of the
|
|
|
287 |
* descendants to -1, except the active descendant, whose
|
|
|
288 |
* <code>tabIndex</code> attribute is set to 0.
|
|
|
289 |
* @protected
|
|
|
290 |
*/
|
|
|
291 |
_initDescendants: function () {
|
|
|
292 |
|
|
|
293 |
var descendants = this.get("descendants"),
|
|
|
294 |
descendantsMap = {},
|
|
|
295 |
nFirstEnabled = -1,
|
|
|
296 |
nDescendants,
|
|
|
297 |
nActiveDescendant = this.get(ACTIVE_DESCENDANT),
|
|
|
298 |
oNode,
|
|
|
299 |
sID,
|
|
|
300 |
i = 0;
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
|
304 |
if (Lang.isUndefined(nActiveDescendant)) {
|
|
|
305 |
nActiveDescendant = -1;
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
if (descendants) {
|
|
|
310 |
|
|
|
311 |
nDescendants = descendants.size();
|
|
|
312 |
|
|
|
313 |
|
|
|
314 |
for (i = 0; i < nDescendants; i++) {
|
|
|
315 |
|
|
|
316 |
oNode = descendants.item(i);
|
|
|
317 |
|
|
|
318 |
if (nFirstEnabled === -1 && !oNode.get(DISABLED)) {
|
|
|
319 |
nFirstEnabled = i;
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
// If the user didn't specify a value for the
|
|
|
324 |
// "activeDescendant" attribute try to infer it from
|
|
|
325 |
// the markup.
|
|
|
326 |
|
|
|
327 |
// Need to pass "2" when using "getAttribute" for IE to get
|
|
|
328 |
// the attribute value as it is set in the markup.
|
|
|
329 |
// Need to use "parseInt" because IE always returns the
|
|
|
330 |
// value as a number, whereas all other browsers return
|
|
|
331 |
// the attribute as a string when accessed
|
|
|
332 |
// via "getAttribute".
|
|
|
333 |
|
|
|
334 |
if (nActiveDescendant < 0 &&
|
|
|
335 |
parseInt(oNode.getAttribute(TAB_INDEX, 2), 10) === 0) {
|
|
|
336 |
|
|
|
337 |
nActiveDescendant = i;
|
|
|
338 |
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
if (oNode) {
|
|
|
342 |
oNode.set(TAB_INDEX, -1);
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
sID = oNode.get(ID);
|
|
|
346 |
|
|
|
347 |
if (!sID) {
|
|
|
348 |
sID = Y.guid();
|
|
|
349 |
oNode.set(ID, sID);
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
descendantsMap[sID] = i;
|
|
|
353 |
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
// If the user didn't specify a value for the
|
|
|
358 |
// "activeDescendant" attribute and no default value could be
|
|
|
359 |
// determined from the markup, then default to 0.
|
|
|
360 |
|
|
|
361 |
if (nActiveDescendant < 0) {
|
|
|
362 |
nActiveDescendant = 0;
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
oNode = descendants.item(nActiveDescendant);
|
|
|
367 |
|
|
|
368 |
// Check to make sure the active descendant isn't disabled,
|
|
|
369 |
// and fall back to the first enabled descendant if it is.
|
|
|
370 |
|
|
|
371 |
if (!oNode || oNode.get(DISABLED)) {
|
|
|
372 |
oNode = descendants.item(nFirstEnabled);
|
|
|
373 |
nActiveDescendant = nFirstEnabled;
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
this._lastNodeIndex = nDescendants - 1;
|
|
|
377 |
this._descendants = descendants;
|
|
|
378 |
this._descendantsMap = descendantsMap;
|
|
|
379 |
|
|
|
380 |
this.set(ACTIVE_DESCENDANT, nActiveDescendant);
|
|
|
381 |
|
|
|
382 |
// Need to set the "tabIndex" attribute here, since the
|
|
|
383 |
// "activeDescendantChange" event handler used to manage
|
|
|
384 |
// the setting of the "tabIndex" attribute isn't wired up yet.
|
|
|
385 |
|
|
|
386 |
if (oNode) {
|
|
|
387 |
oNode.set(TAB_INDEX, 0);
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
},
|
|
|
393 |
|
|
|
394 |
|
|
|
395 |
/**
|
|
|
396 |
* @method _isDescendant
|
|
|
397 |
* @description Determines if the specified Node instance is a descendant
|
|
|
398 |
* managed by the Focus Manager.
|
|
|
399 |
* @param node {Node} Node instance to be checked.
|
|
|
400 |
* @return {Boolean} Boolean indicating if the specified Node instance is a
|
|
|
401 |
* descendant managed by the Focus Manager.
|
|
|
402 |
* @protected
|
|
|
403 |
*/
|
|
|
404 |
_isDescendant: function (node) {
|
|
|
405 |
|
|
|
406 |
return (node.get(ID) in this._descendantsMap);
|
|
|
407 |
|
|
|
408 |
},
|
|
|
409 |
|
|
|
410 |
|
|
|
411 |
/**
|
|
|
412 |
* @method _removeFocusClass
|
|
|
413 |
* @description Removes the class name representing focus (as specified by
|
|
|
414 |
* the "focusClass" attribute) from the Node instance to which it is
|
|
|
415 |
* currently applied.
|
|
|
416 |
* @protected
|
|
|
417 |
*/
|
|
|
418 |
_removeFocusClass: function () {
|
|
|
419 |
|
|
|
420 |
var oFocusedNode = this._focusedNode,
|
|
|
421 |
focusClass = this.get(FOCUS_CLASS),
|
|
|
422 |
sClassName;
|
|
|
423 |
|
|
|
424 |
if (focusClass) {
|
|
|
425 |
sClassName = Lang.isString(focusClass) ?
|
|
|
426 |
focusClass : focusClass.className;
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
if (oFocusedNode && sClassName) {
|
|
|
430 |
oFocusedNode.removeClass(sClassName);
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
},
|
|
|
434 |
|
|
|
435 |
|
|
|
436 |
/**
|
|
|
437 |
* @method _detachKeyHandler
|
|
|
438 |
* @description Detaches the "key" event handlers used to support the "keys"
|
|
|
439 |
* attribute.
|
|
|
440 |
* @protected
|
|
|
441 |
*/
|
|
|
442 |
_detachKeyHandler: function () {
|
|
|
443 |
|
|
|
444 |
var prevKeyHandler = this._prevKeyHandler,
|
|
|
445 |
nextKeyHandler = this._nextKeyHandler;
|
|
|
446 |
|
|
|
447 |
if (prevKeyHandler) {
|
|
|
448 |
prevKeyHandler.detach();
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
if (nextKeyHandler) {
|
|
|
452 |
nextKeyHandler.detach();
|
|
|
453 |
}
|
|
|
454 |
|
|
|
455 |
},
|
|
|
456 |
|
|
|
457 |
|
|
|
458 |
/**
|
|
|
459 |
* @method _preventScroll
|
|
|
460 |
* @description Prevents the viewport from scolling when the user presses
|
|
|
461 |
* the up, down, left, or right key.
|
|
|
462 |
* @protected
|
|
|
463 |
*/
|
|
|
464 |
_preventScroll: function (event) {
|
|
|
465 |
|
|
|
466 |
if (scrollKeys[event.keyCode] && this._isDescendant(event.target)) {
|
|
|
467 |
event.preventDefault();
|
|
|
468 |
}
|
|
|
469 |
|
|
|
470 |
},
|
|
|
471 |
|
|
|
472 |
|
|
|
473 |
/**
|
|
|
474 |
* @method _fireClick
|
|
|
475 |
* @description Fires the click event if the enter key is pressed while
|
|
|
476 |
* focused on an HTML element that is not natively clickable.
|
|
|
477 |
* @protected
|
|
|
478 |
*/
|
|
|
479 |
_fireClick: function (event) {
|
|
|
480 |
|
|
|
481 |
var oTarget = event.target,
|
|
|
482 |
sNodeName = oTarget.get("nodeName").toLowerCase();
|
|
|
483 |
|
|
|
484 |
if (event.keyCode === 13 && (!clickableElements[sNodeName] ||
|
|
|
485 |
(sNodeName === "a" && !oTarget.getAttribute("href")))) {
|
|
|
486 |
|
|
|
487 |
|
|
|
488 |
oTarget.simulate("click");
|
|
|
489 |
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
},
|
|
|
493 |
|
|
|
494 |
|
|
|
495 |
/**
|
|
|
496 |
* @method _attachKeyHandler
|
|
|
497 |
* @description Attaches the "key" event handlers used to support the "keys"
|
|
|
498 |
* attribute.
|
|
|
499 |
* @protected
|
|
|
500 |
*/
|
|
|
501 |
_attachKeyHandler: function () {
|
|
|
502 |
|
|
|
503 |
this._detachKeyHandler();
|
|
|
504 |
|
|
|
505 |
var sNextKey = this.get("keys.next"),
|
|
|
506 |
sPrevKey = this.get("keys.previous"),
|
|
|
507 |
oNode = this.get(HOST),
|
|
|
508 |
aHandlers = this._eventHandlers;
|
|
|
509 |
|
|
|
510 |
if (sPrevKey) {
|
|
|
511 |
this._prevKeyHandler =
|
|
|
512 |
Y.on(KEY, Y.bind(this._focusPrevious, this), oNode, sPrevKey);
|
|
|
513 |
}
|
|
|
514 |
|
|
|
515 |
if (sNextKey) {
|
|
|
516 |
this._nextKeyHandler =
|
|
|
517 |
Y.on(KEY, Y.bind(this._focusNext, this), oNode, sNextKey);
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
|
|
|
521 |
// In Opera it is necessary to call the "preventDefault" method in
|
|
|
522 |
// response to the user pressing the arrow keys in order to prevent
|
|
|
523 |
// the viewport from scrolling when the user is moving focus among
|
|
|
524 |
// the focusable descendants.
|
|
|
525 |
|
|
|
526 |
if (UA.opera) {
|
|
|
527 |
aHandlers.push(oNode.on("keypress", this._preventScroll, this));
|
|
|
528 |
}
|
|
|
529 |
|
|
|
530 |
|
|
|
531 |
// For all browsers except Opera: HTML elements that are not natively
|
|
|
532 |
// focusable but made focusable via the tabIndex attribute don't
|
|
|
533 |
// fire a click event when the user presses the enter key. It is
|
|
|
534 |
// possible to work around this problem by simplying dispatching a
|
|
|
535 |
// click event in response to the user pressing the enter key.
|
|
|
536 |
|
|
|
537 |
if (!UA.opera) {
|
|
|
538 |
aHandlers.push(oNode.on("keypress", this._fireClick, this));
|
|
|
539 |
}
|
|
|
540 |
|
|
|
541 |
},
|
|
|
542 |
|
|
|
543 |
|
|
|
544 |
/**
|
|
|
545 |
* @method _detachEventHandlers
|
|
|
546 |
* @description Detaches all event handlers used by the Focus Manager.
|
|
|
547 |
* @protected
|
|
|
548 |
*/
|
|
|
549 |
_detachEventHandlers: function () {
|
|
|
550 |
|
|
|
551 |
this._detachKeyHandler();
|
|
|
552 |
|
|
|
553 |
var aHandlers = this._eventHandlers;
|
|
|
554 |
|
|
|
555 |
if (aHandlers) {
|
|
|
556 |
|
|
|
557 |
Y.Array.each(aHandlers, function (handle) {
|
|
|
558 |
handle.detach();
|
|
|
559 |
});
|
|
|
560 |
|
|
|
561 |
this._eventHandlers = null;
|
|
|
562 |
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
},
|
|
|
566 |
|
|
|
567 |
|
|
|
568 |
/**
|
|
|
569 |
* @method _detachEventHandlers
|
|
|
570 |
* @description Attaches all event handlers used by the Focus Manager.
|
|
|
571 |
* @protected
|
|
|
572 |
*/
|
|
|
573 |
_attachEventHandlers: function () {
|
|
|
574 |
|
|
|
575 |
var descendants = this._descendants,
|
|
|
576 |
aHandlers,
|
|
|
577 |
oDocument,
|
|
|
578 |
handle;
|
|
|
579 |
|
|
|
580 |
if (descendants && descendants.size()) {
|
|
|
581 |
|
|
|
582 |
aHandlers = this._eventHandlers || [];
|
|
|
583 |
oDocument = this.get(HOST).get("ownerDocument");
|
|
|
584 |
|
|
|
585 |
|
|
|
586 |
if (aHandlers.length === 0) {
|
|
|
587 |
|
|
|
588 |
|
|
|
589 |
aHandlers.push(oDocument.on("focus", this._onDocFocus, this));
|
|
|
590 |
|
|
|
591 |
aHandlers.push(oDocument.on("mousedown",
|
|
|
592 |
this._onDocMouseDown, this));
|
|
|
593 |
|
|
|
594 |
aHandlers.push(
|
|
|
595 |
this.after("keysChange", this._attachKeyHandler));
|
|
|
596 |
|
|
|
597 |
aHandlers.push(
|
|
|
598 |
this.after("descendantsChange", this._initDescendants));
|
|
|
599 |
|
|
|
600 |
aHandlers.push(
|
|
|
601 |
this.after(ACTIVE_DESCENDANT_CHANGE,
|
|
|
602 |
this._afterActiveDescendantChange));
|
|
|
603 |
|
|
|
604 |
|
|
|
605 |
// For performance: defer attaching all key-related event
|
|
|
606 |
// handlers until the first time one of the specified
|
|
|
607 |
// descendants receives focus.
|
|
|
608 |
|
|
|
609 |
handle = this.after("focusedChange", Y.bind(function (event) {
|
|
|
610 |
|
|
|
611 |
if (event.newVal) {
|
|
|
612 |
|
|
|
613 |
|
|
|
614 |
this._attachKeyHandler();
|
|
|
615 |
|
|
|
616 |
// Detach this "focusedChange" handler so that the
|
|
|
617 |
// key-related handlers only get attached once.
|
|
|
618 |
|
|
|
619 |
handle.detach();
|
|
|
620 |
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
}, this));
|
|
|
624 |
|
|
|
625 |
aHandlers.push(handle);
|
|
|
626 |
|
|
|
627 |
}
|
|
|
628 |
|
|
|
629 |
|
|
|
630 |
this._eventHandlers = aHandlers;
|
|
|
631 |
|
|
|
632 |
}
|
|
|
633 |
|
|
|
634 |
},
|
|
|
635 |
|
|
|
636 |
|
|
|
637 |
// Protected event handlers
|
|
|
638 |
|
|
|
639 |
/**
|
|
|
640 |
* @method _onDocMouseDown
|
|
|
641 |
* @description "mousedown" event handler for the owner document of the
|
|
|
642 |
* Focus Manager's Node.
|
|
|
643 |
* @protected
|
|
|
644 |
* @param event {Object} Object representing the DOM event.
|
|
|
645 |
*/
|
|
|
646 |
_onDocMouseDown: function (event) {
|
|
|
647 |
|
|
|
648 |
var oHost = this.get(HOST),
|
|
|
649 |
oTarget = event.target,
|
|
|
650 |
bChildNode = oHost.contains(oTarget),
|
|
|
651 |
node,
|
|
|
652 |
|
|
|
653 |
getFocusable = function (node) {
|
|
|
654 |
|
|
|
655 |
var returnVal = false;
|
|
|
656 |
|
|
|
657 |
if (!node.compareTo(oHost)) {
|
|
|
658 |
|
|
|
659 |
returnVal = this._isDescendant(node) ? node :
|
|
|
660 |
getFocusable.call(this, node.get("parentNode"));
|
|
|
661 |
|
|
|
662 |
}
|
|
|
663 |
|
|
|
664 |
return returnVal;
|
|
|
665 |
|
|
|
666 |
};
|
|
|
667 |
|
|
|
668 |
|
|
|
669 |
if (bChildNode) {
|
|
|
670 |
|
|
|
671 |
// Check to make sure that the target isn't a child node of one
|
|
|
672 |
// of the focusable descendants.
|
|
|
673 |
|
|
|
674 |
node = getFocusable.call(this, oTarget);
|
|
|
675 |
|
|
|
676 |
if (node) {
|
|
|
677 |
oTarget = node;
|
|
|
678 |
}
|
|
|
679 |
else if (!node && this.get(FOCUSED)) {
|
|
|
680 |
|
|
|
681 |
// The target was a non-focusable descendant of the root
|
|
|
682 |
// node, so the "focused" attribute should be set to false.
|
|
|
683 |
|
|
|
684 |
this._set(FOCUSED, false);
|
|
|
685 |
this._onDocFocus(event);
|
|
|
686 |
|
|
|
687 |
}
|
|
|
688 |
|
|
|
689 |
}
|
|
|
690 |
|
|
|
691 |
|
|
|
692 |
if (bChildNode && this._isDescendant(oTarget)) {
|
|
|
693 |
|
|
|
694 |
// Fix general problem in Webkit: mousing down on a button or an
|
|
|
695 |
// anchor element doesn't focus it.
|
|
|
696 |
|
|
|
697 |
// For all browsers: makes sure that the descendant that
|
|
|
698 |
// was the target of the mousedown event is now considered the
|
|
|
699 |
// active descendant.
|
|
|
700 |
|
|
|
701 |
this.focus(oTarget);
|
|
|
702 |
}
|
|
|
703 |
else if (UA.webkit && this.get(FOCUSED) &&
|
|
|
704 |
(!bChildNode || (bChildNode && !this._isDescendant(oTarget)))) {
|
|
|
705 |
|
|
|
706 |
// Fix for Webkit:
|
|
|
707 |
|
|
|
708 |
// Document doesn't receive focus in Webkit when the user mouses
|
|
|
709 |
// down on it, so the "focused" attribute won't get set to the
|
|
|
710 |
// correct value.
|
|
|
711 |
|
|
|
712 |
// The goal is to force a blur if the user moused down on
|
|
|
713 |
// either: 1) A descendant node, but not one that managed by
|
|
|
714 |
// the FocusManager, or 2) an element outside of the
|
|
|
715 |
// FocusManager
|
|
|
716 |
|
|
|
717 |
this._set(FOCUSED, false);
|
|
|
718 |
this._onDocFocus(event);
|
|
|
719 |
|
|
|
720 |
}
|
|
|
721 |
|
|
|
722 |
},
|
|
|
723 |
|
|
|
724 |
|
|
|
725 |
/**
|
|
|
726 |
* @method _onDocFocus
|
|
|
727 |
* @description "focus" event handler for the owner document of the
|
|
|
728 |
* Focus Manager's Node.
|
|
|
729 |
* @protected
|
|
|
730 |
* @param event {Object} Object representing the DOM event.
|
|
|
731 |
*/
|
|
|
732 |
_onDocFocus: function (event) {
|
|
|
733 |
|
|
|
734 |
var oTarget = this._focusTarget || event.target,
|
|
|
735 |
bFocused = this.get(FOCUSED),
|
|
|
736 |
focusClass = this.get(FOCUS_CLASS),
|
|
|
737 |
oFocusedNode = this._focusedNode,
|
|
|
738 |
bInCollection;
|
|
|
739 |
|
|
|
740 |
if (this._focusTarget) {
|
|
|
741 |
this._focusTarget = null;
|
|
|
742 |
}
|
|
|
743 |
|
|
|
744 |
|
|
|
745 |
if (this.get(HOST).contains(oTarget)) {
|
|
|
746 |
|
|
|
747 |
// The target is a descendant of the root Node.
|
|
|
748 |
|
|
|
749 |
bInCollection = this._isDescendant(oTarget);
|
|
|
750 |
|
|
|
751 |
if (!bFocused && bInCollection) {
|
|
|
752 |
|
|
|
753 |
// The user has focused a focusable descendant.
|
|
|
754 |
|
|
|
755 |
bFocused = true;
|
|
|
756 |
|
|
|
757 |
}
|
|
|
758 |
else if (bFocused && !bInCollection) {
|
|
|
759 |
|
|
|
760 |
// The user has focused a child of the root Node that is
|
|
|
761 |
// not one of the descendants managed by this Focus Manager
|
|
|
762 |
// so clear the currently focused descendant.
|
|
|
763 |
|
|
|
764 |
bFocused = false;
|
|
|
765 |
|
|
|
766 |
}
|
|
|
767 |
|
|
|
768 |
}
|
|
|
769 |
else {
|
|
|
770 |
|
|
|
771 |
// The target is some other node in the document.
|
|
|
772 |
|
|
|
773 |
bFocused = false;
|
|
|
774 |
|
|
|
775 |
}
|
|
|
776 |
|
|
|
777 |
|
|
|
778 |
if (focusClass) {
|
|
|
779 |
|
|
|
780 |
if (oFocusedNode && (!oFocusedNode.compareTo(oTarget) || !bFocused)) {
|
|
|
781 |
this._removeFocusClass();
|
|
|
782 |
}
|
|
|
783 |
|
|
|
784 |
if (bInCollection && bFocused) {
|
|
|
785 |
|
|
|
786 |
if (focusClass.fn) {
|
|
|
787 |
oTarget = focusClass.fn(oTarget);
|
|
|
788 |
oTarget.addClass(focusClass.className);
|
|
|
789 |
}
|
|
|
790 |
else {
|
|
|
791 |
oTarget.addClass(focusClass);
|
|
|
792 |
}
|
|
|
793 |
|
|
|
794 |
this._focusedNode = oTarget;
|
|
|
795 |
|
|
|
796 |
}
|
|
|
797 |
|
|
|
798 |
}
|
|
|
799 |
|
|
|
800 |
|
|
|
801 |
this._set(FOCUSED, bFocused);
|
|
|
802 |
|
|
|
803 |
},
|
|
|
804 |
|
|
|
805 |
|
|
|
806 |
/**
|
|
|
807 |
* @method _focusNext
|
|
|
808 |
* @description Keydown event handler that moves focus to the next
|
|
|
809 |
* enabled descendant.
|
|
|
810 |
* @protected
|
|
|
811 |
* @param event {Object} Object representing the DOM event.
|
|
|
812 |
* @param activeDescendant {Number} Number representing the index of the
|
|
|
813 |
* next descendant to be focused
|
|
|
814 |
*/
|
|
|
815 |
_focusNext: function (event, activeDescendant) {
|
|
|
816 |
|
|
|
817 |
var nActiveDescendant = activeDescendant || this.get(ACTIVE_DESCENDANT),
|
|
|
818 |
oNode;
|
|
|
819 |
|
|
|
820 |
|
|
|
821 |
if (this._isDescendant(event.target) &&
|
|
|
822 |
(nActiveDescendant <= this._lastNodeIndex)) {
|
|
|
823 |
|
|
|
824 |
nActiveDescendant = nActiveDescendant + 1;
|
|
|
825 |
|
|
|
826 |
if (nActiveDescendant === (this._lastNodeIndex + 1) &&
|
|
|
827 |
this.get(CIRCULAR)) {
|
|
|
828 |
|
|
|
829 |
nActiveDescendant = 0;
|
|
|
830 |
|
|
|
831 |
}
|
|
|
832 |
|
|
|
833 |
oNode = this._descendants.item(nActiveDescendant);
|
|
|
834 |
|
|
|
835 |
if (oNode) {
|
|
|
836 |
|
|
|
837 |
if (oNode.get("disabled")) {
|
|
|
838 |
this._focusNext(event, nActiveDescendant);
|
|
|
839 |
}
|
|
|
840 |
else {
|
|
|
841 |
this.focus(nActiveDescendant);
|
|
|
842 |
}
|
|
|
843 |
|
|
|
844 |
}
|
|
|
845 |
|
|
|
846 |
}
|
|
|
847 |
|
|
|
848 |
this._preventScroll(event);
|
|
|
849 |
|
|
|
850 |
},
|
|
|
851 |
|
|
|
852 |
|
|
|
853 |
/**
|
|
|
854 |
* @method _focusPrevious
|
|
|
855 |
* @description Keydown event handler that moves focus to the previous
|
|
|
856 |
* enabled descendant.
|
|
|
857 |
* @protected
|
|
|
858 |
* @param event {Object} Object representing the DOM event.
|
|
|
859 |
* @param activeDescendant {Number} Number representing the index of the
|
|
|
860 |
* next descendant to be focused.
|
|
|
861 |
*/
|
|
|
862 |
_focusPrevious: function (event, activeDescendant) {
|
|
|
863 |
|
|
|
864 |
var nActiveDescendant = activeDescendant || this.get(ACTIVE_DESCENDANT),
|
|
|
865 |
oNode;
|
|
|
866 |
|
|
|
867 |
if (this._isDescendant(event.target) && nActiveDescendant >= 0) {
|
|
|
868 |
|
|
|
869 |
nActiveDescendant = nActiveDescendant - 1;
|
|
|
870 |
|
|
|
871 |
if (nActiveDescendant === -1 && this.get(CIRCULAR)) {
|
|
|
872 |
nActiveDescendant = this._lastNodeIndex;
|
|
|
873 |
}
|
|
|
874 |
|
|
|
875 |
oNode = this._descendants.item(nActiveDescendant);
|
|
|
876 |
|
|
|
877 |
if (oNode) {
|
|
|
878 |
|
|
|
879 |
if (oNode.get("disabled")) {
|
|
|
880 |
this._focusPrevious(event, nActiveDescendant);
|
|
|
881 |
}
|
|
|
882 |
else {
|
|
|
883 |
this.focus(nActiveDescendant);
|
|
|
884 |
}
|
|
|
885 |
|
|
|
886 |
}
|
|
|
887 |
|
|
|
888 |
}
|
|
|
889 |
|
|
|
890 |
this._preventScroll(event);
|
|
|
891 |
|
|
|
892 |
},
|
|
|
893 |
|
|
|
894 |
|
|
|
895 |
/**
|
|
|
896 |
* @method _afterActiveDescendantChange
|
|
|
897 |
* @description afterChange event handler for the
|
|
|
898 |
* "activeDescendant" attribute.
|
|
|
899 |
* @protected
|
|
|
900 |
* @param event {Object} Object representing the change event.
|
|
|
901 |
*/
|
|
|
902 |
_afterActiveDescendantChange: function (event) {
|
|
|
903 |
|
|
|
904 |
var oNode = this._descendants.item(event.prevVal);
|
|
|
905 |
|
|
|
906 |
if (oNode) {
|
|
|
907 |
oNode.set(TAB_INDEX, -1);
|
|
|
908 |
}
|
|
|
909 |
|
|
|
910 |
oNode = this._descendants.item(event.newVal);
|
|
|
911 |
|
|
|
912 |
if (oNode) {
|
|
|
913 |
oNode.set(TAB_INDEX, 0);
|
|
|
914 |
}
|
|
|
915 |
|
|
|
916 |
},
|
|
|
917 |
|
|
|
918 |
|
|
|
919 |
|
|
|
920 |
// Public methods
|
|
|
921 |
|
|
|
922 |
initializer: function (config) {
|
|
|
923 |
this.start();
|
|
|
924 |
|
|
|
925 |
},
|
|
|
926 |
|
|
|
927 |
destructor: function () {
|
|
|
928 |
|
|
|
929 |
this.stop();
|
|
|
930 |
this.get(HOST).focusManager = null;
|
|
|
931 |
|
|
|
932 |
},
|
|
|
933 |
|
|
|
934 |
|
|
|
935 |
/**
|
|
|
936 |
* @method focus
|
|
|
937 |
* @description Focuses the active descendant and sets the
|
|
|
938 |
* <code>focused</code> attribute to true.
|
|
|
939 |
* @param index {Number|Node} Optional. Number representing the index of the
|
|
|
940 |
* descendant to be set as the active descendant or Node instance
|
|
|
941 |
* representing the descendant to be set as the active descendant.
|
|
|
942 |
*/
|
|
|
943 |
focus: function (index) {
|
|
|
944 |
|
|
|
945 |
if (Lang.isUndefined(index)) {
|
|
|
946 |
index = this.get(ACTIVE_DESCENDANT);
|
|
|
947 |
}
|
|
|
948 |
|
|
|
949 |
this.set(ACTIVE_DESCENDANT, index, { src: UI });
|
|
|
950 |
|
|
|
951 |
var oNode = this._descendants.item(this.get(ACTIVE_DESCENDANT));
|
|
|
952 |
|
|
|
953 |
if (oNode) {
|
|
|
954 |
|
|
|
955 |
oNode.focus();
|
|
|
956 |
|
|
|
957 |
// In Opera focusing a <BUTTON> element programmatically
|
|
|
958 |
// will result in the document-level focus event handler
|
|
|
959 |
// "_onDocFocus" being called, resulting in the handler
|
|
|
960 |
// incorrectly setting the "focused" Attribute to false. To fix
|
|
|
961 |
// this, set a flag ("_focusTarget") that the "_onDocFocus" method
|
|
|
962 |
// can look for to properly handle this edge case.
|
|
|
963 |
|
|
|
964 |
if (UA.opera && oNode.get("nodeName").toLowerCase() === "button") {
|
|
|
965 |
this._focusTarget = oNode;
|
|
|
966 |
}
|
|
|
967 |
|
|
|
968 |
}
|
|
|
969 |
|
|
|
970 |
},
|
|
|
971 |
|
|
|
972 |
|
|
|
973 |
/**
|
|
|
974 |
* @method blur
|
|
|
975 |
* @description Blurs the current active descendant and sets the
|
|
|
976 |
* <code>focused</code> attribute to false.
|
|
|
977 |
*/
|
|
|
978 |
blur: function () {
|
|
|
979 |
|
|
|
980 |
var oNode;
|
|
|
981 |
|
|
|
982 |
if (this.get(FOCUSED)) {
|
|
|
983 |
|
|
|
984 |
oNode = this._descendants.item(this.get(ACTIVE_DESCENDANT));
|
|
|
985 |
|
|
|
986 |
if (oNode) {
|
|
|
987 |
|
|
|
988 |
oNode.blur();
|
|
|
989 |
|
|
|
990 |
// For Opera and Webkit: Blurring an element in either browser
|
|
|
991 |
// doesn't result in another element (such as the document)
|
|
|
992 |
// being focused. Therefore, the "_onDocFocus" method
|
|
|
993 |
// responsible for managing the application and removal of the
|
|
|
994 |
// focus indicator class name is never called.
|
|
|
995 |
|
|
|
996 |
this._removeFocusClass();
|
|
|
997 |
|
|
|
998 |
}
|
|
|
999 |
|
|
|
1000 |
this._set(FOCUSED, false, { src: UI });
|
|
|
1001 |
}
|
|
|
1002 |
|
|
|
1003 |
},
|
|
|
1004 |
|
|
|
1005 |
|
|
|
1006 |
/**
|
|
|
1007 |
* @method start
|
|
|
1008 |
* @description Enables the Focus Manager.
|
|
|
1009 |
*/
|
|
|
1010 |
start: function () {
|
|
|
1011 |
|
|
|
1012 |
if (this._stopped) {
|
|
|
1013 |
|
|
|
1014 |
this._initDescendants();
|
|
|
1015 |
this._attachEventHandlers();
|
|
|
1016 |
|
|
|
1017 |
this._stopped = false;
|
|
|
1018 |
|
|
|
1019 |
}
|
|
|
1020 |
|
|
|
1021 |
},
|
|
|
1022 |
|
|
|
1023 |
|
|
|
1024 |
/**
|
|
|
1025 |
* @method stop
|
|
|
1026 |
* @description Disables the Focus Manager by detaching all event handlers.
|
|
|
1027 |
*/
|
|
|
1028 |
stop: function () {
|
|
|
1029 |
|
|
|
1030 |
if (!this._stopped) {
|
|
|
1031 |
|
|
|
1032 |
this._detachEventHandlers();
|
|
|
1033 |
|
|
|
1034 |
this._descendants = null;
|
|
|
1035 |
this._focusedNode = null;
|
|
|
1036 |
this._lastNodeIndex = 0;
|
|
|
1037 |
this._stopped = true;
|
|
|
1038 |
|
|
|
1039 |
}
|
|
|
1040 |
|
|
|
1041 |
},
|
|
|
1042 |
|
|
|
1043 |
|
|
|
1044 |
/**
|
|
|
1045 |
* @method refresh
|
|
|
1046 |
* @description Refreshes the Focus Manager's descendants by re-executing the
|
|
|
1047 |
* CSS selector query specified by the <code>descendants</code> attribute.
|
|
|
1048 |
*/
|
|
|
1049 |
refresh: function () {
|
|
|
1050 |
|
|
|
1051 |
this._initDescendants();
|
|
|
1052 |
|
|
|
1053 |
if (!this._eventHandlers) {
|
|
|
1054 |
this._attachEventHandlers();
|
|
|
1055 |
}
|
|
|
1056 |
|
|
|
1057 |
}
|
|
|
1058 |
|
|
|
1059 |
});
|
|
|
1060 |
|
|
|
1061 |
|
|
|
1062 |
NodeFocusManager.NAME = "nodeFocusManager";
|
|
|
1063 |
NodeFocusManager.NS = "focusManager";
|
|
|
1064 |
|
|
|
1065 |
Y.namespace("Plugin");
|
|
|
1066 |
Y.Plugin.NodeFocusManager = NodeFocusManager;
|
|
|
1067 |
|
|
|
1068 |
|
|
|
1069 |
}, '3.18.1', {"requires": ["attribute", "node", "plugin", "node-event-simulate", "event-key", "event-focus"]});
|