2
YUI 3.13.0 (build 508226d)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('node-menunav', function (Y, NAME) {
11
* <p>The MenuNav Node Plugin makes it easy to transform existing list-based
12
* markup into traditional, drop down navigational menus that are both accessible
13
* and easy to customize, and only require a small set of dependencies.</p>
16
* <p>To use the MenuNav Node Plugin, simply pass a reference to the plugin to a
17
* Node instance's <code>plug</code> method.</p>
21
* <script type="text/javascript"> <br>
23
* // Call the "use" method, passing in "node-menunav". This will <br>
24
* // load the script and CSS for the MenuNav Node Plugin and all of <br>
25
* // the required dependencies. <br>
27
* YUI().use("node-menunav", function(Y) { <br>
29
* // Use the "contentready" event to initialize the menu when <br>
30
* // the subtree of element representing the root menu <br>
31
* // (<div id="menu-1">) is ready to be scripted. <br>
33
* Y.on("contentready", function () { <br>
35
* // The scope of the callback will be a Node instance <br>
36
* // representing the root menu (<div id="menu-1">). <br>
37
* // Therefore, since "this" represents a Node instance, it <br>
38
* // is possible to just call "this.plug" passing in a <br>
39
* // reference to the MenuNav Node Plugin. <br>
41
* this.plug(Y.Plugin.NodeMenuNav); <br>
47
* </script> <br>
51
* <p>The MenuNav Node Plugin has several configuration properties that can be
52
* set via an object literal that is passed as a second argument to a Node
53
* instance's <code>plug</code> method.
58
* <script type="text/javascript"> <br>
60
* // Call the "use" method, passing in "node-menunav". This will <br>
61
* // load the script and CSS for the MenuNav Node Plugin and all of <br>
62
* // the required dependencies. <br>
64
* YUI().use("node-menunav", function(Y) { <br>
66
* // Use the "contentready" event to initialize the menu when <br>
67
* // the subtree of element representing the root menu <br>
68
* // (<div id="menu-1">) is ready to be scripted. <br>
70
* Y.on("contentready", function () { <br>
72
* // The scope of the callback will be a Node instance <br>
73
* // representing the root menu (<div id="menu-1">). <br>
74
* // Therefore, since "this" represents a Node instance, it <br>
75
* // is possible to just call "this.plug" passing in a <br>
76
* // reference to the MenuNav Node Plugin. <br>
78
* this.plug(Y.Plugin.NodeMenuNav, { mouseOutHideDelay: 1000 });
84
* </script> <br>
88
DEPRECATED. The MenuNav 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/>.
99
getClassName = Y.ClassNameManager.getClassName,
103
// Frequently used strings
106
MENUITEM = "menuitem",
108
PARENT_NODE = "parentNode",
109
CHILDREN = "children",
110
OFFSET_HEIGHT = "offsetHeight",
111
OFFSET_WIDTH = "offsetWidth",
115
HANDLED_MOUSEOUT = "handledMouseOut",
116
HANDLED_MOUSEOVER = "handledMouseOver",
120
MOUSEDOWN = "mousedown",
124
FIRST_OF_TYPE = "first-of-type",
126
PRESENTATION = "presentation",
127
DESCENDANTS = "descendants",
129
ACTIVE_DESCENDANT = "activeDescendant",
130
USE_ARIA = "useARIA",
131
ARIA_HIDDEN = "aria-hidden",
134
ACTIVE_DESCENDANT_CHANGE = ACTIVE_DESCENDANT + "Change",
139
AUTO_SUBMENU_DISPLAY = "autoSubmenuDisplay",
140
MOUSEOUT_HIDE_DELAY = "mouseOutHideDelay",
145
CSS_MENU = getClassName(MENU),
146
CSS_MENU_HIDDEN = getClassName(MENU, HIDDEN),
147
CSS_MENU_HORIZONTAL = getClassName(MENU, "horizontal"),
148
CSS_MENU_LABEL = getClassName(MENU, LABEL),
149
CSS_MENU_LABEL_ACTIVE = getClassName(MENU, LABEL, ACTIVE),
150
CSS_MENU_LABEL_MENUVISIBLE = getClassName(MENU, LABEL, (MENU + "visible")),
151
CSS_MENUITEM = getClassName(MENUITEM),
152
CSS_MENUITEM_ACTIVE = getClassName(MENUITEM, ACTIVE),
157
MENU_SELECTOR = PERIOD + CSS_MENU,
158
MENU_TOGGLE_SELECTOR = (PERIOD + getClassName(MENU, "toggle")),
159
MENU_CONTENT_SELECTOR = PERIOD + getClassName(MENU, CONTENT),
160
MENU_LABEL_SELECTOR = PERIOD + CSS_MENU_LABEL,
162
STANDARD_QUERY = ">" + MENU_CONTENT_SELECTOR + ">ul>li>a",
163
EXTENDED_QUERY = ">" + MENU_CONTENT_SELECTOR + ">ul>li>" + MENU_LABEL_SELECTOR + ">a:first-child";
168
var getPreviousSibling = function (node) {
170
var oPrevious = node.previous(),
174
oChildren = node.get(PARENT_NODE).get(CHILDREN);
175
oPrevious = oChildren.item(oChildren.size() - 1);
184
var getNextSibling = function (node) {
186
var oNext = node.next();
189
oNext = node.get(PARENT_NODE).get(CHILDREN).item(0);
197
var isAnchor = function (node) {
199
var bReturnVal = false;
202
bReturnVal = node.get("nodeName").toLowerCase() === LOWERCASE_A;
210
var isMenuItem = function (node) {
212
return node.hasClass(CSS_MENUITEM);
217
var isMenuLabel = function (node) {
219
return node.hasClass(CSS_MENU_LABEL);
224
var isHorizontalMenu = function (menu) {
226
return menu.hasClass(CSS_MENU_HORIZONTAL);
231
var hasVisibleSubmenu = function (menuLabel) {
233
return menuLabel.hasClass(CSS_MENU_LABEL_MENUVISIBLE);
238
var getItemAnchor = function (node) {
240
return isAnchor(node) ? node : node.one(LOWERCASE_A);
245
var getNodeWithClass = function (node, className, searchAncestors) {
251
if (node.hasClass(className)) {
255
if (!oItem && searchAncestors) {
256
oItem = node.ancestor((PERIOD + className));
266
var getParentMenu = function (node) {
268
return node.ancestor(MENU_SELECTOR);
273
var getMenu = function (node, searchAncestors) {
275
return getNodeWithClass(node, CSS_MENU, searchAncestors);
280
var getMenuItem = function (node, searchAncestors) {
285
oItem = getNodeWithClass(node, CSS_MENUITEM, searchAncestors);
293
var getMenuLabel = function (node, searchAncestors) {
299
if (searchAncestors) {
300
oItem = getNodeWithClass(node, CSS_MENU_LABEL, searchAncestors);
303
oItem = getNodeWithClass(node, CSS_MENU_LABEL) ||
304
node.one((PERIOD + CSS_MENU_LABEL));
314
var getItem = function (node, searchAncestors) {
319
oItem = getMenuItem(node, searchAncestors) ||
320
getMenuLabel(node, searchAncestors);
328
var getFirstItem = function (menu) {
330
return getItem(menu.one("li"));
335
var getActiveClass = function (node) {
337
return isMenuItem(node) ? CSS_MENUITEM_ACTIVE : CSS_MENU_LABEL_ACTIVE;
342
var handleMouseOverForNode = function (node, target) {
344
return node && !node[HANDLED_MOUSEOVER] &&
345
(node.compareTo(target) || node.contains(target));
350
var handleMouseOutForNode = function (node, relatedTarget) {
352
return node && !node[HANDLED_MOUSEOUT] &&
353
(!node.compareTo(relatedTarget) && !node.contains(relatedTarget));
358
* The NodeMenuNav class is a plugin for a Node instance. The class is used via
359
* the <a href="Node.html#method_plug"><code>plug</code></a> method of Node and
360
* should not be instantiated directly.
364
var NodeMenuNav = function () {
366
NodeMenuNav.superclass.constructor.apply(this, arguments);
370
NodeMenuNav.NAME = "nodeMenuNav";
371
NodeMenuNav.NS = "menuNav";
375
* @property SHIM_TEMPLATE_TITLE
376
* @description String representing the value for the <code>title</code>
377
* attribute for the shim used to prevent <code><select></code> elements
378
* from poking through menus in IE 6.
379
* @default "Menu Stacking Shim"
382
NodeMenuNav.SHIM_TEMPLATE_TITLE = "Menu Stacking Shim";
386
* @property SHIM_TEMPLATE
387
* @description String representing the HTML used to create the
388
* <code><iframe></code> shim used to prevent
389
* <code><select></code> elements from poking through menus in IE 6.
390
* @default "<iframe frameborder="0" tabindex="-1"
391
* class="yui-shim" title="Menu Stacking Shim"
392
* src="javascript:false;"></iframe>"
396
// <iframe> shim notes:
398
// 1) Need to set the "frameBorder" property to 0 to suppress the default
399
// <iframe> border in IE. (Setting the CSS "border" property alone doesn't
402
// 2) The "src" attribute of the <iframe> is set to "javascript:false;" so
403
// that it won't load a page inside it, preventing the secure/nonsecure
404
// warning in IE when using HTTPS.
406
// 3) Since the role of the <iframe> shim is completely presentational, its
407
// "tabindex" attribute is set to "-1" and its title attribute is set to
408
// "Menu Stacking Shim". Both strategies help users of screen readers to
409
// avoid mistakenly interacting with the <iframe> shim.
411
NodeMenuNav.SHIM_TEMPLATE = '<iframe frameborder="0" tabindex="-1" class="' +
412
getClassName("shim") +
413
'" title="' + NodeMenuNav.SHIM_TEMPLATE_TITLE +
414
'" src="javascript:false;"></iframe>';
417
NodeMenuNav.ATTRS = {
420
* Boolean indicating if use of the WAI-ARIA Roles and States should be
421
* enabled for the menu.
434
setter: function (value) {
436
var oMenu = this.get(HOST),
444
oMenu.set(ROLE, MENU);
446
oMenu.all("ul,li," + MENU_CONTENT_SELECTOR).set(ROLE, PRESENTATION);
448
oMenu.all((PERIOD + getClassName(MENUITEM, CONTENT))).set(ROLE, MENUITEM);
450
oMenu.all((PERIOD + CSS_MENU_LABEL)).each(function (node) {
453
oMenuToggle = node.one(MENU_TOGGLE_SELECTOR);
456
oMenuToggle.set(ROLE, PRESENTATION);
457
oMenuLabel = oMenuToggle.previous();
460
oMenuLabel.set(ROLE, MENUITEM);
461
oMenuLabel.set("aria-haspopup", true);
463
oSubmenu = node.next();
467
oSubmenu.set(ROLE, MENU);
469
oMenuLabel = oSubmenu.previous();
470
oMenuToggle = oMenuLabel.one(MENU_TOGGLE_SELECTOR);
473
oMenuLabel = oMenuToggle;
476
sID = Y.stamp(oMenuLabel);
478
if (!oMenuLabel.get(ID)) {
479
oMenuLabel.set(ID, sID);
482
oSubmenu.set("aria-labelledby", sID);
483
oSubmenu.set(ARIA_HIDDEN, true);
497
* Boolean indicating if submenus are automatically made visible when the
498
* user mouses over the menu's items.
500
* @attribute autoSubmenuDisplay
506
autoSubmenuDisplay: {
515
* Number indicating the time (in milliseconds) that should expire before a
516
* submenu is made visible when the user mouses over the menu's label.
518
* @attribute submenuShowDelay
533
* Number indicating the time (in milliseconds) that should expire before a
534
* submenu is hidden when the user mouses out of a menu label heading in the
535
* direction of a submenu.
537
* @attribute submenuHideDelay
552
* Number indicating the time (in milliseconds) that should expire before a
553
* submenu is hidden when the user mouses out of it.
555
* @attribute mouseOutHideDelay
571
Y.extend(NodeMenuNav, Y.Plugin.Base, {
573
// Protected properties
576
* @property _rootMenu
577
* @description Node instance representing the root menu in the menu.
586
* @property _activeItem
587
* @description Node instance representing the menu's active descendent:
588
* the menuitem or menu label the user is currently interacting with.
597
* @property _activeMenu
598
* @description Node instance representing the menu that is the parent of
599
* the menu's active descendent.
608
* @property _hasFocus
609
* @description Boolean indicating if the menu has focus.
617
// In gecko-based browsers a mouseover and mouseout event will fire even
618
// if a DOM element moves out from under the mouse without the user
619
// actually moving the mouse. This bug affects NodeMenuNav because the
620
// user can hit the Esc key to hide a menu, and if the mouse is over the
621
// menu when the user presses Esc, the _onMenuMouseOut handler will be
622
// called. To fix this bug the following flag (_blockMouseEvent) is used
623
// to block the code in the _onMenuMouseOut handler from executing.
626
* @property _blockMouseEvent
627
* @description Boolean indicating whether or not to handle the
633
_blockMouseEvent: false,
637
* @property _currentMouseX
638
* @description Number representing the current x coordinate of the mouse
648
* @property _movingToSubmenu
649
* @description Boolean indicating if the mouse is moving from a menu
650
* label to its corresponding submenu.
655
_movingToSubmenu: false,
659
* @property _showSubmenuTimer
660
* @description Timer used to show a submenu.
665
_showSubmenuTimer: null,
669
* @property _hideSubmenuTimer
670
* @description Timer used to hide a submenu.
675
_hideSubmenuTimer: null,
679
* @property _hideAllSubmenusTimer
680
* @description Timer used to hide a all submenus.
685
_hideAllSubmenusTimer: null,
689
* @property _firstItem
690
* @description Node instance representing the first item (menuitem or menu
691
* label) in the root menu of a menu.
702
initializer: function (config) {
705
oRootMenu = this.get(HOST),
709
Y.log("WARNING: Node-MenuNav is a deprecated module as of YUI 3.9.0. This module will be removed from a later version of the library.", "warn");
713
menuNav._rootMenu = oRootMenu;
715
oRootMenu.all("ul:first-child").addClass(FIRST_OF_TYPE);
717
// Hide all visible submenus
719
oRootMenu.all(MENU_SELECTOR).addClass(CSS_MENU_HIDDEN);
722
// Wire up all event handlers
724
aHandlers.push(oRootMenu.on("mouseover", menuNav._onMouseOver, menuNav));
725
aHandlers.push(oRootMenu.on("mouseout", menuNav._onMouseOut, menuNav));
726
aHandlers.push(oRootMenu.on("mousemove", menuNav._onMouseMove, menuNav));
727
aHandlers.push(oRootMenu.on(MOUSEDOWN, menuNav._toggleSubmenuDisplay, menuNav));
728
aHandlers.push(Y.on("key", menuNav._toggleSubmenuDisplay, oRootMenu, "down:13", menuNav));
729
aHandlers.push(oRootMenu.on(CLICK, menuNav._toggleSubmenuDisplay, menuNav));
730
aHandlers.push(oRootMenu.on("keypress", menuNav._onKeyPress, menuNav));
731
aHandlers.push(oRootMenu.on(KEYDOWN, menuNav._onKeyDown, menuNav));
733
oDoc = oRootMenu.get("ownerDocument");
735
aHandlers.push(oDoc.on(MOUSEDOWN, menuNav._onDocMouseDown, menuNav));
736
aHandlers.push(oDoc.on("focus", menuNav._onDocFocus, menuNav));
738
this._eventHandlers = aHandlers;
740
menuNav._initFocusManager();
747
destructor: function () {
749
var aHandlers = this._eventHandlers;
753
Y.Array.each(aHandlers, function (handle) {
757
this._eventHandlers = null;
761
this.get(HOST).unplug("focusManager");
771
* @description Returns a boolean indicating if the specified menu is the
772
* root menu in the menu.
774
* @param {Node} menu Node instance representing a menu.
775
* @return {Boolean} Boolean indicating if the specified menu is the root
778
_isRoot: function (menu) {
780
return this._rootMenu.compareTo(menu);
786
* @method _getTopmostSubmenu
787
* @description Returns the topmost submenu of a submenu hierarchy.
789
* @param {Node} menu Node instance representing a menu.
790
* @return {Node} Node instance representing a menu.
792
_getTopmostSubmenu: function (menu) {
795
oMenu = getParentMenu(menu),
802
else if (menuNav._isRoot(oMenu)) {
806
returnVal = menuNav._getTopmostSubmenu(oMenu);
815
* @method _clearActiveItem
816
* @description Clears the menu's active descendent.
819
_clearActiveItem: function () {
822
oActiveItem = menuNav._activeItem;
825
oActiveItem.removeClass(getActiveClass(oActiveItem));
828
menuNav._activeItem = null;
834
* @method _setActiveItem
835
* @description Sets the specified menuitem or menu label as the menu's
838
* @param {Node} item Node instance representing a menuitem or menu label.
840
_setActiveItem: function (item) {
846
menuNav._clearActiveItem();
848
item.addClass(getActiveClass(item));
850
menuNav._activeItem = item;
859
* @description Focuses the specified menuitem or menu label.
861
* @param {Node} item Node instance representing a menuitem or menu label.
863
_focusItem: function (item) {
869
if (item && menuNav._hasFocus) {
871
oMenu = getParentMenu(item);
872
oItem = getItemAnchor(item);
874
if (oMenu && !oMenu.compareTo(menuNav._activeMenu)) {
875
menuNav._activeMenu = oMenu;
876
menuNav._initFocusManager();
879
menuNav._focusManager.focus(oItem);
888
* @description Shows the specified menu.
890
* @param {Node} menu Node instance representing a menu.
892
_showMenu: function (menu) {
894
var oParentMenu = getParentMenu(menu),
895
oLI = menu.get(PARENT_NODE),
899
if (this.get(USE_ARIA)) {
900
menu.set(ARIA_HIDDEN, false);
904
if (isHorizontalMenu(oParentMenu)) {
905
aXY[1] = aXY[1] + oLI.get(OFFSET_HEIGHT);
908
aXY[0] = aXY[0] + oLI.get(OFFSET_WIDTH);
915
if (UA.ie === 6 && !menu.hasIFrameShim) {
917
menu.appendChild(Y.Node.create(NodeMenuNav.SHIM_TEMPLATE));
918
menu.hasIFrameShim = true;
922
// Clear previous values for height and width
924
menu.setStyles({ height: EMPTY_STRING, width: EMPTY_STRING });
926
// Set the width and height of the menu's bounding box - this is
927
// necessary for IE 6 so that the CSS for the <iframe> shim can
928
// simply set the <iframe>'s width and height to 100% to ensure
929
// that dimensions of an <iframe> shim are always sync'd to the
930
// that of its parent menu. Specifying a width and height also
931
// helps when positioning decorator elements (for creating effects
932
// like rounded corners) inside a menu's bounding box in IE 7.
935
height: (menu.get(OFFSET_HEIGHT) + PX),
936
width: (menu.get(OFFSET_WIDTH) + PX) });
940
menu.previous().addClass(CSS_MENU_LABEL_MENUVISIBLE);
941
menu.removeClass(CSS_MENU_HIDDEN);
948
* @description Hides the specified menu.
950
* @param {Node} menu Node instance representing a menu.
951
* @param {Boolean} activateAndFocusLabel Boolean indicating if the label
953
* menu should be focused and set as active.
955
_hideMenu: function (menu, activateAndFocusLabel) {
958
oLabel = menu.previous(),
961
oLabel.removeClass(CSS_MENU_LABEL_MENUVISIBLE);
964
if (activateAndFocusLabel) {
965
menuNav._focusItem(oLabel);
966
menuNav._setActiveItem(oLabel);
969
oActiveItem = menu.one((PERIOD + CSS_MENUITEM_ACTIVE));
972
oActiveItem.removeClass(CSS_MENUITEM_ACTIVE);
975
// Clear the values for top and left that were set by the call to
976
// "setXY" when the menu was shown so that the hidden position
977
// specified in the core CSS file will take affect.
979
menu.setStyles({ left: EMPTY_STRING, top: EMPTY_STRING });
981
menu.addClass(CSS_MENU_HIDDEN);
983
if (menuNav.get(USE_ARIA)) {
984
menu.set(ARIA_HIDDEN, true);
991
* @method _hideAllSubmenus
992
* @description Hides all submenus of the specified menu.
994
* @param {Node} menu Node instance representing a menu.
996
_hideAllSubmenus: function (menu) {
1000
menu.all(MENU_SELECTOR).each(Y.bind(function (submenuNode) {
1002
menuNav._hideMenu(submenuNode);
1010
* @method _cancelShowSubmenuTimer
1011
* @description Cancels the timer used to show a submenu.
1014
_cancelShowSubmenuTimer: function () {
1017
oShowSubmenuTimer = menuNav._showSubmenuTimer;
1019
if (oShowSubmenuTimer) {
1020
oShowSubmenuTimer.cancel();
1021
menuNav._showSubmenuTimer = null;
1028
* @method _cancelHideSubmenuTimer
1029
* @description Cancels the timer used to hide a submenu.
1032
_cancelHideSubmenuTimer: function () {
1035
oHideSubmenuTimer = menuNav._hideSubmenuTimer;
1038
if (oHideSubmenuTimer) {
1039
oHideSubmenuTimer.cancel();
1040
menuNav._hideSubmenuTimer = null;
1047
* @method _initFocusManager
1048
* @description Initializes and updates the Focus Manager so that is is
1049
* always managing descendants of the active menu.
1052
_initFocusManager: function () {
1055
oRootMenu = menuNav._rootMenu,
1056
oMenu = menuNav._activeMenu || oRootMenu,
1058
menuNav._isRoot(oMenu) ? EMPTY_STRING : ("#" + oMenu.get("id")),
1059
oFocusManager = menuNav._focusManager,
1061
sDescendantSelector,
1064
if (isHorizontalMenu(oMenu)) {
1066
sDescendantSelector = sSelectorBase + STANDARD_QUERY + "," +
1067
sSelectorBase + EXTENDED_QUERY;
1069
sKeysVal = { next: "down:39", previous: "down:37" };
1074
sDescendantSelector = sSelectorBase + STANDARD_QUERY;
1075
sKeysVal = { next: "down:40", previous: "down:38" };
1080
if (!oFocusManager) {
1082
oRootMenu.plug(Y.Plugin.NodeFocusManager, {
1083
descendants: sDescendantSelector,
1088
oFocusManager = oRootMenu.focusManager;
1090
sQuery = "#" + oRootMenu.get("id") + MENU_SELECTOR + " a," +
1091
MENU_TOGGLE_SELECTOR;
1093
oRootMenu.all(sQuery).set("tabIndex", -1);
1095
oFocusManager.on(ACTIVE_DESCENDANT_CHANGE,
1096
this._onActiveDescendantChange, oFocusManager, this);
1098
oFocusManager.after(ACTIVE_DESCENDANT_CHANGE,
1099
this._afterActiveDescendantChange, oFocusManager, this);
1101
menuNav._focusManager = oFocusManager;
1106
oFocusManager.set(ACTIVE_DESCENDANT, -1);
1107
oFocusManager.set(DESCENDANTS, sDescendantSelector);
1108
oFocusManager.set("keys", sKeysVal);
1115
// Event handlers for discrete pieces of pieces of the menu
1119
* @method _onActiveDescendantChange
1120
* @description "activeDescendantChange" event handler for menu's
1123
* @param {Object} event Object representing the Attribute change event.
1124
* @param {NodeMenuNav} menuNav Object representing the NodeMenuNav instance.
1126
_onActiveDescendantChange: function (event, menuNav) {
1128
if (event.src === UI && menuNav._activeMenu &&
1129
!menuNav._movingToSubmenu) {
1131
menuNav._hideAllSubmenus(menuNav._activeMenu);
1139
* @method _afterActiveDescendantChange
1140
* @description "activeDescendantChange" event handler for menu's
1143
* @param {Object} event Object representing the Attribute change event.
1144
* @param {NodeMenuNav} menuNav Object representing the NodeMenuNav instance.
1146
_afterActiveDescendantChange: function (event, menuNav) {
1150
if (event.src === UI) {
1151
oItem = getItem(this.get(DESCENDANTS).item(event.newVal), true);
1152
menuNav._setActiveItem(oItem);
1159
* @method _onDocFocus
1160
* @description "focus" event handler for the owner document of the MenuNav.
1162
* @param {Object} event Object representing the DOM event.
1164
_onDocFocus: function (event) {
1167
oActiveItem = menuNav._activeItem,
1168
oTarget = event.target,
1172
if (menuNav._rootMenu.contains(oTarget)) { // The menu has focus
1174
if (menuNav._hasFocus) {
1176
oMenu = getParentMenu(oTarget);
1178
// If the element that was focused is a descendant of the
1179
// root menu, but is in a submenu not currently being
1180
// managed by the Focus Manager, update the Focus Manager so
1181
// that it is now managing the submenu that is the parent of
1182
// the element that was focused.
1184
if (!menuNav._activeMenu.compareTo(oMenu)) {
1186
menuNav._activeMenu = oMenu;
1187
menuNav._initFocusManager();
1188
menuNav._focusManager.set(ACTIVE_DESCENDANT, oTarget);
1189
menuNav._setActiveItem(getItem(oTarget, true));
1194
else { // Initial focus
1196
// First time the menu has been focused, need to setup focused
1197
// state and established active active descendant
1199
menuNav._hasFocus = true;
1201
oActiveItem = getItem(oTarget, true);
1204
menuNav._setActiveItem(oActiveItem);
1210
else { // The menu has lost focus
1212
menuNav._clearActiveItem();
1214
menuNav._cancelShowSubmenuTimer();
1215
menuNav._hideAllSubmenus(menuNav._rootMenu);
1217
menuNav._activeMenu = menuNav._rootMenu;
1218
menuNav._initFocusManager();
1220
menuNav._focusManager.set(ACTIVE_DESCENDANT, 0);
1222
menuNav._hasFocus = false;
1230
* @method _onMenuMouseOver
1231
* @description "mouseover" event handler for a menu.
1233
* @param {Node} menu Node instance representing a menu.
1234
* @param {Object} event Object representing the DOM event.
1236
_onMenuMouseOver: function (menu, event) {
1239
oHideAllSubmenusTimer = menuNav._hideAllSubmenusTimer;
1241
if (oHideAllSubmenusTimer) {
1242
oHideAllSubmenusTimer.cancel();
1243
menuNav._hideAllSubmenusTimer = null;
1246
menuNav._cancelHideSubmenuTimer();
1248
// Need to update the FocusManager in advance of focus a new
1249
// Menu in order to avoid the FocusManager thinking that
1250
// it has lost focus
1252
if (menu && !menu.compareTo(menuNav._activeMenu)) {
1253
menuNav._activeMenu = menu;
1255
if (menuNav._hasFocus) {
1256
menuNav._initFocusManager();
1261
if (menuNav._movingToSubmenu && isHorizontalMenu(menu)) {
1262
menuNav._movingToSubmenu = false;
1269
* @method _hideAndFocusLabel
1270
* @description Hides all of the submenus of the root menu and focuses the
1271
* label of the topmost submenu
1274
_hideAndFocusLabel: function () {
1277
oActiveMenu = menuNav._activeMenu,
1280
menuNav._hideAllSubmenus(menuNav._rootMenu);
1284
// Focus the label element for the topmost submenu
1285
oSubmenu = menuNav._getTopmostSubmenu(oActiveMenu);
1286
menuNav._focusItem(oSubmenu.previous());
1294
* @method _onMenuMouseOut
1295
* @description "mouseout" event handler for a menu.
1297
* @param {Node} menu Node instance representing a menu.
1298
* @param {Object} event Object representing the DOM event.
1300
_onMenuMouseOut: function (menu, event) {
1303
oActiveMenu = menuNav._activeMenu,
1304
oRelatedTarget = event.relatedTarget,
1305
oActiveItem = menuNav._activeItem,
1310
if (oActiveMenu && !oActiveMenu.contains(oRelatedTarget)) {
1312
oParentMenu = getParentMenu(oActiveMenu);
1315
if (oParentMenu && !oParentMenu.contains(oRelatedTarget)) {
1317
if (menuNav.get(MOUSEOUT_HIDE_DELAY) > 0) {
1319
menuNav._cancelShowSubmenuTimer();
1321
menuNav._hideAllSubmenusTimer =
1323
later(menuNav.get(MOUSEOUT_HIDE_DELAY),
1324
menuNav, menuNav._hideAndFocusLabel);
1333
oMenu = getParentMenu(oActiveItem);
1335
if (!menuNav._isRoot(oMenu)) {
1336
menuNav._focusItem(oMenu.previous());
1349
* @method _onMenuLabelMouseOver
1350
* @description "mouseover" event handler for a menu label.
1352
* @param {Node} menuLabel Node instance representing a menu label.
1353
* @param {Object} event Object representing the DOM event.
1355
_onMenuLabelMouseOver: function (menuLabel, event) {
1358
oActiveMenu = menuNav._activeMenu,
1359
bIsRoot = menuNav._isRoot(oActiveMenu),
1360
bUseAutoSubmenuDisplay =
1361
(menuNav.get(AUTO_SUBMENU_DISPLAY) && bIsRoot || !bIsRoot),
1362
submenuShowDelay = menuNav.get("submenuShowDelay"),
1366
var showSubmenu = function (delay) {
1368
menuNav._cancelHideSubmenuTimer();
1369
menuNav._cancelShowSubmenuTimer();
1371
if (!hasVisibleSubmenu(menuLabel)) {
1373
oSubmenu = menuLabel.next();
1376
menuNav._hideAllSubmenus(oActiveMenu);
1377
menuNav._showSubmenuTimer = later(delay, menuNav, menuNav._showMenu, oSubmenu);
1385
menuNav._focusItem(menuLabel);
1386
menuNav._setActiveItem(menuLabel);
1389
if (bUseAutoSubmenuDisplay) {
1391
if (menuNav._movingToSubmenu) {
1393
// If the user is moving diagonally from a submenu to
1394
// another submenu and they then stop and pause on a
1395
// menu label for an amount of time equal to the amount of
1396
// time defined for the display of a submenu then show the
1397
// submenu immediately.
1398
// http://yuilibrary.com/projects/yui3/ticket/2528316
1400
//Y.message("Pause path");
1402
menuNav._hoverTimer = later(submenuShowDelay, menuNav, function () {
1408
showSubmenu(submenuShowDelay);
1417
* @method _onMenuLabelMouseOut
1418
* @description "mouseout" event handler for a menu label.
1420
* @param {Node} menuLabel Node instance representing a menu label.
1421
* @param {Object} event Object representing the DOM event.
1423
_onMenuLabelMouseOut: function (menuLabel, event) {
1426
bIsRoot = menuNav._isRoot(menuNav._activeMenu),
1427
bUseAutoSubmenuDisplay =
1428
(menuNav.get(AUTO_SUBMENU_DISPLAY) && bIsRoot || !bIsRoot),
1430
oRelatedTarget = event.relatedTarget,
1431
oSubmenu = menuLabel.next(),
1432
hoverTimer = menuNav._hoverTimer;
1435
hoverTimer.cancel();
1438
menuNav._clearActiveItem();
1440
if (bUseAutoSubmenuDisplay) {
1442
if (menuNav._movingToSubmenu &&
1443
!menuNav._showSubmenuTimer && oSubmenu) {
1445
// If the mouse is moving diagonally toward the submenu and
1446
// another submenu isn't in the process of being displayed
1447
// (via a timer), then hide the submenu via a timer to give
1448
// the user some time to reach the submenu.
1450
menuNav._hideSubmenuTimer =
1451
later(menuNav.get("submenuHideDelay"), menuNav,
1452
menuNav._hideMenu, oSubmenu);
1455
else if (!menuNav._movingToSubmenu && oSubmenu && (!oRelatedTarget ||
1457
!oSubmenu.contains(oRelatedTarget) &&
1458
!oRelatedTarget.compareTo(oSubmenu)))) {
1460
// If the mouse is not moving toward the submenu, cancel any
1461
// submenus that might be in the process of being displayed
1462
// (via a timer) and hide this submenu immediately.
1464
menuNav._cancelShowSubmenuTimer();
1466
menuNav._hideMenu(oSubmenu);
1476
* @method _onMenuItemMouseOver
1477
* @description "mouseover" event handler for a menuitem.
1479
* @param {Node} menuItem Node instance representing a menuitem.
1480
* @param {Object} event Object representing the DOM event.
1482
_onMenuItemMouseOver: function (menuItem, event) {
1485
oActiveMenu = menuNav._activeMenu,
1486
bIsRoot = menuNav._isRoot(oActiveMenu),
1487
bUseAutoSubmenuDisplay =
1488
(menuNav.get(AUTO_SUBMENU_DISPLAY) && bIsRoot || !bIsRoot);
1491
menuNav._focusItem(menuItem);
1492
menuNav._setActiveItem(menuItem);
1495
if (bUseAutoSubmenuDisplay && !menuNav._movingToSubmenu) {
1497
menuNav._hideAllSubmenus(oActiveMenu);
1505
* @method _onMenuItemMouseOut
1506
* @description "mouseout" event handler for a menuitem.
1508
* @param {Node} menuItem Node instance representing a menuitem.
1509
* @param {Object} event Object representing the DOM event.
1511
_onMenuItemMouseOut: function (menuItem, event) {
1513
this._clearActiveItem();
1519
* @method _onVerticalMenuKeyDown
1520
* @description "keydown" event handler for vertical menus.
1522
* @param {Object} event Object representing the DOM event.
1524
_onVerticalMenuKeyDown: function (event) {
1527
oActiveMenu = menuNav._activeMenu,
1528
oRootMenu = menuNav._rootMenu,
1529
oTarget = event.target,
1530
bPreventDefault = false,
1531
nKeyCode = event.keyCode,
1540
case 37: // left arrow
1542
oParentMenu = getParentMenu(oActiveMenu);
1544
if (oParentMenu && isHorizontalMenu(oParentMenu)) {
1546
menuNav._hideMenu(oActiveMenu);
1547
oLI = getPreviousSibling(oActiveMenu.get(PARENT_NODE));
1548
oItem = getItem(oLI);
1552
if (isMenuLabel(oItem)) { // Menu label
1554
oSubmenu = oItem.next();
1559
menuNav._showMenu(oSubmenu);
1560
menuNav._focusItem(getFirstItem(oSubmenu));
1561
menuNav._setActiveItem(getFirstItem(oSubmenu));
1566
menuNav._focusItem(oItem);
1567
menuNav._setActiveItem(oItem);
1574
menuNav._focusItem(oItem);
1575
menuNav._setActiveItem(oItem);
1582
else if (!menuNav._isRoot(oActiveMenu)) {
1583
menuNav._hideMenu(oActiveMenu, true);
1587
bPreventDefault = true;
1591
case 39: // right arrow
1593
if (isMenuLabel(oTarget)) {
1595
oSubmenu = oTarget.next();
1599
menuNav._showMenu(oSubmenu);
1600
menuNav._focusItem(getFirstItem(oSubmenu));
1601
menuNav._setActiveItem(getFirstItem(oSubmenu));
1606
else if (isHorizontalMenu(oRootMenu)) {
1608
oSubmenu = menuNav._getTopmostSubmenu(oActiveMenu);
1609
oLI = getNextSibling(oSubmenu.get(PARENT_NODE));
1610
oItem = getItem(oLI);
1612
menuNav._hideAllSubmenus(oRootMenu);
1616
if (isMenuLabel(oItem)) { // Menu label
1618
oSubmenu = oItem.next();
1622
menuNav._showMenu(oSubmenu);
1623
menuNav._focusItem(getFirstItem(oSubmenu));
1624
menuNav._setActiveItem(getFirstItem(oSubmenu));
1629
menuNav._focusItem(oItem);
1630
menuNav._setActiveItem(oItem);
1637
menuNav._focusItem(oItem);
1638
menuNav._setActiveItem(oItem);
1646
bPreventDefault = true;
1653
if (bPreventDefault) {
1655
// Prevent the browser from scrolling the window
1657
event.preventDefault();
1665
* @method _onHorizontalMenuKeyDown
1666
* @description "keydown" event handler for horizontal menus.
1668
* @param {Object} event Object representing the DOM event.
1670
_onHorizontalMenuKeyDown: function (event) {
1673
oActiveMenu = menuNav._activeMenu,
1674
oTarget = event.target,
1675
oFocusedItem = getItem(oTarget, true),
1676
bPreventDefault = false,
1677
nKeyCode = event.keyCode,
1681
if (nKeyCode === 40) {
1683
menuNav._hideAllSubmenus(oActiveMenu);
1685
if (isMenuLabel(oFocusedItem)) {
1687
oSubmenu = oFocusedItem.next();
1691
menuNav._showMenu(oSubmenu);
1692
menuNav._focusItem(getFirstItem(oSubmenu));
1693
menuNav._setActiveItem(getFirstItem(oSubmenu));
1697
bPreventDefault = true;
1704
if (bPreventDefault) {
1706
// Prevent the browser from scrolling the window
1708
event.preventDefault();
1715
// Generic DOM Event handlers
1719
* @method _onMouseMove
1720
* @description "mousemove" event handler for the menu.
1722
* @param {Object} event Object representing the DOM event.
1724
_onMouseMove: function (event) {
1728
// Using a timer to set the value of the "_currentMouseX" property
1729
// helps improve the reliability of the calculation used to set the
1730
// value of the "_movingToSubmenu" property - especially in Opera.
1732
later(10, menuNav, function () {
1734
menuNav._currentMouseX = event.pageX;
1742
* @method _onMouseOver
1743
* @description "mouseover" event handler for the menu.
1745
* @param {Object} event Object representing the DOM event.
1747
_onMouseOver: function (event) {
1757
if (menuNav._blockMouseEvent) {
1758
menuNav._blockMouseEvent = false;
1762
oTarget = event.target;
1763
oMenu = getMenu(oTarget, true);
1764
oMenuLabel = getMenuLabel(oTarget, true);
1765
oMenuItem = getMenuItem(oTarget, true);
1768
if (handleMouseOverForNode(oMenu, oTarget)) {
1770
menuNav._onMenuMouseOver(oMenu, event);
1772
oMenu[HANDLED_MOUSEOVER] = true;
1773
oMenu[HANDLED_MOUSEOUT] = false;
1775
oParentMenu = getParentMenu(oMenu);
1779
oParentMenu[HANDLED_MOUSEOUT] = true;
1780
oParentMenu[HANDLED_MOUSEOVER] = false;
1786
if (handleMouseOverForNode(oMenuLabel, oTarget)) {
1788
menuNav._onMenuLabelMouseOver(oMenuLabel, event);
1790
oMenuLabel[HANDLED_MOUSEOVER] = true;
1791
oMenuLabel[HANDLED_MOUSEOUT] = false;
1795
if (handleMouseOverForNode(oMenuItem, oTarget)) {
1797
menuNav._onMenuItemMouseOver(oMenuItem, event);
1799
oMenuItem[HANDLED_MOUSEOVER] = true;
1800
oMenuItem[HANDLED_MOUSEOUT] = false;
1810
* @method _onMouseOut
1811
* @description "mouseout" event handler for the menu.
1813
* @param {Object} event Object representing the DOM event.
1815
_onMouseOut: function (event) {
1818
oActiveMenu = menuNav._activeMenu,
1819
bMovingToSubmenu = false,
1828
menuNav._movingToSubmenu =
1829
(oActiveMenu && !isHorizontalMenu(oActiveMenu) &&
1830
((event.pageX - 5) > menuNav._currentMouseX));
1832
oTarget = event.target;
1833
oRelatedTarget = event.relatedTarget;
1834
oMenu = getMenu(oTarget, true);
1835
oMenuLabel = getMenuLabel(oTarget, true);
1836
oMenuItem = getMenuItem(oTarget, true);
1839
if (handleMouseOutForNode(oMenuLabel, oRelatedTarget)) {
1841
menuNav._onMenuLabelMouseOut(oMenuLabel, event);
1843
oMenuLabel[HANDLED_MOUSEOUT] = true;
1844
oMenuLabel[HANDLED_MOUSEOVER] = false;
1848
if (handleMouseOutForNode(oMenuItem, oRelatedTarget)) {
1850
menuNav._onMenuItemMouseOut(oMenuItem, event);
1852
oMenuItem[HANDLED_MOUSEOUT] = true;
1853
oMenuItem[HANDLED_MOUSEOVER] = false;
1860
oSubmenu = oMenuLabel.next();
1862
if (oSubmenu && oRelatedTarget &&
1863
(oRelatedTarget.compareTo(oSubmenu) ||
1864
oSubmenu.contains(oRelatedTarget))) {
1866
bMovingToSubmenu = true;
1873
if (handleMouseOutForNode(oMenu, oRelatedTarget) || bMovingToSubmenu) {
1875
menuNav._onMenuMouseOut(oMenu, event);
1877
oMenu[HANDLED_MOUSEOUT] = true;
1878
oMenu[HANDLED_MOUSEOVER] = false;
1886
* @method _toggleSubmenuDisplay
1887
* @description "mousedown," "keydown," and "click" event handler for the
1888
* menu used to toggle the display of a submenu.
1890
* @param {Object} event Object representing the DOM event.
1892
_toggleSubmenuDisplay: function (event) {
1895
oTarget = event.target,
1896
oMenuLabel = getMenuLabel(oTarget, true),
1908
oAnchor = isAnchor(oTarget) ? oTarget : oTarget.ancestor(isAnchor);
1913
// Need to pass "2" as a second argument to "getAttribute" for
1914
// IE otherwise IE will return a fully qualified URL for the
1915
// value of the "href" attribute.
1916
// http://msdn.microsoft.com/en-us/library/ms536429(VS.85).aspx
1918
sHref = oAnchor.getAttribute("href", 2);
1919
nHashPos = sHref.indexOf("#");
1920
nLen = sHref.length;
1922
if (nHashPos === 0 && nLen > 1) {
1924
sId = sHref.substr(1, nLen);
1925
oSubmenu = oMenuLabel.next();
1927
if (oSubmenu && (oSubmenu.get(ID) === sId)) {
1929
if (sType === MOUSEDOWN || sType === KEYDOWN) {
1931
if ((UA.opera || UA.gecko || UA.ie) && sType === KEYDOWN && !menuNav._preventClickHandle) {
1933
// Prevent the browser from following the URL of
1934
// the anchor element
1936
menuNav._preventClickHandle = menuNav._rootMenu.on("click", function (event) {
1938
event.preventDefault();
1940
menuNav._preventClickHandle.detach();
1941
menuNav._preventClickHandle = null;
1947
if (sType == MOUSEDOWN) {
1949
// Prevent the target from getting focused by
1950
// default, since the element to be focused will
1951
// be determined by weather or not the submenu
1953
event.preventDefault();
1955
// FocusManager will attempt to focus any
1956
// descendant that is the target of the mousedown
1957
// event. Since we want to explicitly control
1958
// where focus is going, we need to call
1959
// "stopImmediatePropagation" to stop the
1960
// FocusManager from doing its thing.
1961
event.stopImmediatePropagation();
1963
// The "_focusItem" method relies on the
1964
// "_hasFocus" property being set to true. The
1965
// "_hasFocus" property is normally set via a
1966
// "focus" event listener, but since we've
1967
// blocked focus from happening, we need to set
1968
// this property manually.
1969
menuNav._hasFocus = true;
1974
if (menuNav._isRoot(getParentMenu(oTarget))) { // Event target is a submenu label in the root menu
1976
// Menu label toggle functionality
1978
if (hasVisibleSubmenu(oMenuLabel)) {
1980
menuNav._hideMenu(oSubmenu);
1981
menuNav._focusItem(oMenuLabel);
1982
menuNav._setActiveItem(oMenuLabel);
1987
menuNav._hideAllSubmenus(menuNav._rootMenu);
1988
menuNav._showMenu(oSubmenu);
1990
menuNav._focusItem(getFirstItem(oSubmenu));
1991
menuNav._setActiveItem(getFirstItem(oSubmenu));
1996
else { // Event target is a submenu label within a submenu
1998
if (menuNav._activeItem == oMenuLabel) {
2000
menuNav._showMenu(oSubmenu);
2001
menuNav._focusItem(getFirstItem(oSubmenu));
2002
menuNav._setActiveItem(getFirstItem(oSubmenu));
2007
if (!oMenuLabel._clickHandle) {
2009
oMenuLabel._clickHandle = oMenuLabel.on("click", function () {
2011
menuNav._hideAllSubmenus(menuNav._rootMenu);
2013
menuNav._hasFocus = false;
2014
menuNav._clearActiveItem();
2017
oMenuLabel._clickHandle.detach();
2019
oMenuLabel._clickHandle = null;
2032
if (sType === CLICK) {
2034
// Prevent the browser from following the URL of
2035
// the anchor element
2037
event.preventDefault();
2054
* @method _onKeyPress
2055
* @description "keypress" event handler for the menu.
2057
* @param {Object} event Object representing the DOM event.
2059
_onKeyPress: function (event) {
2061
switch (event.keyCode) {
2063
case 37: // left arrow
2064
case 38: // up arrow
2065
case 39: // right arrow
2066
case 40: // down arrow
2068
// Prevent the browser from scrolling the window
2070
event.preventDefault();
2080
* @method _onKeyDown
2081
* @description "keydown" event handler for the menu.
2083
* @param {Object} event Object representing the DOM event.
2085
_onKeyDown: function (event) {
2088
oActiveItem = menuNav._activeItem,
2089
oTarget = event.target,
2090
oActiveMenu = getParentMenu(oTarget),
2095
menuNav._activeMenu = oActiveMenu;
2097
if (isHorizontalMenu(oActiveMenu)) {
2098
menuNav._onHorizontalMenuKeyDown(event);
2101
menuNav._onVerticalMenuKeyDown(event);
2105
if (event.keyCode === 27) {
2107
if (!menuNav._isRoot(oActiveMenu)) {
2110
later(0, menuNav, function () {
2111
menuNav._hideMenu(oActiveMenu, true);
2115
menuNav._hideMenu(oActiveMenu, true);
2118
event.stopPropagation();
2119
menuNav._blockMouseEvent = UA.gecko ? true : false;
2122
else if (oActiveItem) {
2124
if (isMenuLabel(oActiveItem) &&
2125
hasVisibleSubmenu(oActiveItem)) {
2127
oSubmenu = oActiveItem.next();
2130
menuNav._hideMenu(oSubmenu);
2136
menuNav._focusManager.blur();
2138
// This is necessary for Webkit since blurring the
2139
// active menuitem won't result in the document
2140
// gaining focus, meaning the that _onDocFocus
2141
// listener won't clear the active menuitem.
2143
menuNav._clearActiveItem();
2145
menuNav._hasFocus = false;
2158
* @method _onDocMouseDown
2159
* @description "mousedown" event handler for the owner document of
2162
* @param {Object} event Object representing the DOM event.
2164
_onDocMouseDown: function (event) {
2167
oRoot = menuNav._rootMenu,
2168
oTarget = event.target;
2171
if (!(oRoot.compareTo(oTarget) || oRoot.contains(oTarget))) {
2173
menuNav._hideAllSubmenus(oRoot);
2175
// Document doesn't receive focus in Webkit when the user mouses
2176
// down on it, so the "_hasFocus" property won't get set to the
2177
// correct value. The following line corrects the problem.
2180
menuNav._hasFocus = false;
2181
menuNav._clearActiveItem();
2191
Y.namespace('Plugin');
2193
Y.Plugin.NodeMenuNav = NodeMenuNav;
2196
}, '3.13.0', {"requires": ["node", "classnamemanager", "plugin", "node-focusmanager"], "skinnable": true});