2
YUI 3.10.3 (build 2fb5187)
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),
712
menuNav._rootMenu = oRootMenu;
714
oRootMenu.all("ul:first-child").addClass(FIRST_OF_TYPE);
716
// Hide all visible submenus
718
oRootMenu.all(MENU_SELECTOR).addClass(CSS_MENU_HIDDEN);
721
// Wire up all event handlers
723
aHandlers.push(oRootMenu.on("mouseover", menuNav._onMouseOver, menuNav));
724
aHandlers.push(oRootMenu.on("mouseout", menuNav._onMouseOut, menuNav));
725
aHandlers.push(oRootMenu.on("mousemove", menuNav._onMouseMove, menuNav));
726
aHandlers.push(oRootMenu.on(MOUSEDOWN, menuNav._toggleSubmenuDisplay, menuNav));
727
aHandlers.push(Y.on("key", menuNav._toggleSubmenuDisplay, oRootMenu, "down:13", menuNav));
728
aHandlers.push(oRootMenu.on(CLICK, menuNav._toggleSubmenuDisplay, menuNav));
729
aHandlers.push(oRootMenu.on("keypress", menuNav._onKeyPress, menuNav));
730
aHandlers.push(oRootMenu.on(KEYDOWN, menuNav._onKeyDown, menuNav));
732
oDoc = oRootMenu.get("ownerDocument");
734
aHandlers.push(oDoc.on(MOUSEDOWN, menuNav._onDocMouseDown, menuNav));
735
aHandlers.push(oDoc.on("focus", menuNav._onDocFocus, menuNav));
737
this._eventHandlers = aHandlers;
739
menuNav._initFocusManager();
746
destructor: function () {
748
var aHandlers = this._eventHandlers;
752
Y.Array.each(aHandlers, function (handle) {
756
this._eventHandlers = null;
760
this.get(HOST).unplug("focusManager");
770
* @description Returns a boolean indicating if the specified menu is the
771
* root menu in the menu.
773
* @param {Node} menu Node instance representing a menu.
774
* @return {Boolean} Boolean indicating if the specified menu is the root
777
_isRoot: function (menu) {
779
return this._rootMenu.compareTo(menu);
785
* @method _getTopmostSubmenu
786
* @description Returns the topmost submenu of a submenu hierarchy.
788
* @param {Node} menu Node instance representing a menu.
789
* @return {Node} Node instance representing a menu.
791
_getTopmostSubmenu: function (menu) {
794
oMenu = getParentMenu(menu),
801
else if (menuNav._isRoot(oMenu)) {
805
returnVal = menuNav._getTopmostSubmenu(oMenu);
814
* @method _clearActiveItem
815
* @description Clears the menu's active descendent.
818
_clearActiveItem: function () {
821
oActiveItem = menuNav._activeItem;
824
oActiveItem.removeClass(getActiveClass(oActiveItem));
827
menuNav._activeItem = null;
833
* @method _setActiveItem
834
* @description Sets the specified menuitem or menu label as the menu's
837
* @param {Node} item Node instance representing a menuitem or menu label.
839
_setActiveItem: function (item) {
845
menuNav._clearActiveItem();
847
item.addClass(getActiveClass(item));
849
menuNav._activeItem = item;
858
* @description Focuses the specified menuitem or menu label.
860
* @param {Node} item Node instance representing a menuitem or menu label.
862
_focusItem: function (item) {
868
if (item && menuNav._hasFocus) {
870
oMenu = getParentMenu(item);
871
oItem = getItemAnchor(item);
873
if (oMenu && !oMenu.compareTo(menuNav._activeMenu)) {
874
menuNav._activeMenu = oMenu;
875
menuNav._initFocusManager();
878
menuNav._focusManager.focus(oItem);
887
* @description Shows the specified menu.
889
* @param {Node} menu Node instance representing a menu.
891
_showMenu: function (menu) {
893
var oParentMenu = getParentMenu(menu),
894
oLI = menu.get(PARENT_NODE),
898
if (this.get(USE_ARIA)) {
899
menu.set(ARIA_HIDDEN, false);
903
if (isHorizontalMenu(oParentMenu)) {
904
aXY[1] = aXY[1] + oLI.get(OFFSET_HEIGHT);
907
aXY[0] = aXY[0] + oLI.get(OFFSET_WIDTH);
914
if (UA.ie === 6 && !menu.hasIFrameShim) {
916
menu.appendChild(Y.Node.create(NodeMenuNav.SHIM_TEMPLATE));
917
menu.hasIFrameShim = true;
921
// Clear previous values for height and width
923
menu.setStyles({ height: EMPTY_STRING, width: EMPTY_STRING });
925
// Set the width and height of the menu's bounding box - this is
926
// necessary for IE 6 so that the CSS for the <iframe> shim can
927
// simply set the <iframe>'s width and height to 100% to ensure
928
// that dimensions of an <iframe> shim are always sync'd to the
929
// that of its parent menu. Specifying a width and height also
930
// helps when positioning decorator elements (for creating effects
931
// like rounded corners) inside a menu's bounding box in IE 7.
934
height: (menu.get(OFFSET_HEIGHT) + PX),
935
width: (menu.get(OFFSET_WIDTH) + PX) });
939
menu.previous().addClass(CSS_MENU_LABEL_MENUVISIBLE);
940
menu.removeClass(CSS_MENU_HIDDEN);
947
* @description Hides the specified menu.
949
* @param {Node} menu Node instance representing a menu.
950
* @param {Boolean} activateAndFocusLabel Boolean indicating if the label
952
* menu should be focused and set as active.
954
_hideMenu: function (menu, activateAndFocusLabel) {
957
oLabel = menu.previous(),
960
oLabel.removeClass(CSS_MENU_LABEL_MENUVISIBLE);
963
if (activateAndFocusLabel) {
964
menuNav._focusItem(oLabel);
965
menuNav._setActiveItem(oLabel);
968
oActiveItem = menu.one((PERIOD + CSS_MENUITEM_ACTIVE));
971
oActiveItem.removeClass(CSS_MENUITEM_ACTIVE);
974
// Clear the values for top and left that were set by the call to
975
// "setXY" when the menu was shown so that the hidden position
976
// specified in the core CSS file will take affect.
978
menu.setStyles({ left: EMPTY_STRING, top: EMPTY_STRING });
980
menu.addClass(CSS_MENU_HIDDEN);
982
if (menuNav.get(USE_ARIA)) {
983
menu.set(ARIA_HIDDEN, true);
990
* @method _hideAllSubmenus
991
* @description Hides all submenus of the specified menu.
993
* @param {Node} menu Node instance representing a menu.
995
_hideAllSubmenus: function (menu) {
999
menu.all(MENU_SELECTOR).each(Y.bind(function (submenuNode) {
1001
menuNav._hideMenu(submenuNode);
1009
* @method _cancelShowSubmenuTimer
1010
* @description Cancels the timer used to show a submenu.
1013
_cancelShowSubmenuTimer: function () {
1016
oShowSubmenuTimer = menuNav._showSubmenuTimer;
1018
if (oShowSubmenuTimer) {
1019
oShowSubmenuTimer.cancel();
1020
menuNav._showSubmenuTimer = null;
1027
* @method _cancelHideSubmenuTimer
1028
* @description Cancels the timer used to hide a submenu.
1031
_cancelHideSubmenuTimer: function () {
1034
oHideSubmenuTimer = menuNav._hideSubmenuTimer;
1037
if (oHideSubmenuTimer) {
1038
oHideSubmenuTimer.cancel();
1039
menuNav._hideSubmenuTimer = null;
1046
* @method _initFocusManager
1047
* @description Initializes and updates the Focus Manager so that is is
1048
* always managing descendants of the active menu.
1051
_initFocusManager: function () {
1054
oRootMenu = menuNav._rootMenu,
1055
oMenu = menuNav._activeMenu || oRootMenu,
1057
menuNav._isRoot(oMenu) ? EMPTY_STRING : ("#" + oMenu.get("id")),
1058
oFocusManager = menuNav._focusManager,
1060
sDescendantSelector,
1063
if (isHorizontalMenu(oMenu)) {
1065
sDescendantSelector = sSelectorBase + STANDARD_QUERY + "," +
1066
sSelectorBase + EXTENDED_QUERY;
1068
sKeysVal = { next: "down:39", previous: "down:37" };
1073
sDescendantSelector = sSelectorBase + STANDARD_QUERY;
1074
sKeysVal = { next: "down:40", previous: "down:38" };
1079
if (!oFocusManager) {
1081
oRootMenu.plug(Y.Plugin.NodeFocusManager, {
1082
descendants: sDescendantSelector,
1087
oFocusManager = oRootMenu.focusManager;
1089
sQuery = "#" + oRootMenu.get("id") + MENU_SELECTOR + " a," +
1090
MENU_TOGGLE_SELECTOR;
1092
oRootMenu.all(sQuery).set("tabIndex", -1);
1094
oFocusManager.on(ACTIVE_DESCENDANT_CHANGE,
1095
this._onActiveDescendantChange, oFocusManager, this);
1097
oFocusManager.after(ACTIVE_DESCENDANT_CHANGE,
1098
this._afterActiveDescendantChange, oFocusManager, this);
1100
menuNav._focusManager = oFocusManager;
1105
oFocusManager.set(ACTIVE_DESCENDANT, -1);
1106
oFocusManager.set(DESCENDANTS, sDescendantSelector);
1107
oFocusManager.set("keys", sKeysVal);
1114
// Event handlers for discrete pieces of pieces of the menu
1118
* @method _onActiveDescendantChange
1119
* @description "activeDescendantChange" event handler for menu's
1122
* @param {Object} event Object representing the Attribute change event.
1123
* @param {NodeMenuNav} menuNav Object representing the NodeMenuNav instance.
1125
_onActiveDescendantChange: function (event, menuNav) {
1127
if (event.src === UI && menuNav._activeMenu &&
1128
!menuNav._movingToSubmenu) {
1130
menuNav._hideAllSubmenus(menuNav._activeMenu);
1138
* @method _afterActiveDescendantChange
1139
* @description "activeDescendantChange" event handler for menu's
1142
* @param {Object} event Object representing the Attribute change event.
1143
* @param {NodeMenuNav} menuNav Object representing the NodeMenuNav instance.
1145
_afterActiveDescendantChange: function (event, menuNav) {
1149
if (event.src === UI) {
1150
oItem = getItem(this.get(DESCENDANTS).item(event.newVal), true);
1151
menuNav._setActiveItem(oItem);
1158
* @method _onDocFocus
1159
* @description "focus" event handler for the owner document of the MenuNav.
1161
* @param {Object} event Object representing the DOM event.
1163
_onDocFocus: function (event) {
1166
oActiveItem = menuNav._activeItem,
1167
oTarget = event.target,
1171
if (menuNav._rootMenu.contains(oTarget)) { // The menu has focus
1173
if (menuNav._hasFocus) {
1175
oMenu = getParentMenu(oTarget);
1177
// If the element that was focused is a descendant of the
1178
// root menu, but is in a submenu not currently being
1179
// managed by the Focus Manager, update the Focus Manager so
1180
// that it is now managing the submenu that is the parent of
1181
// the element that was focused.
1183
if (!menuNav._activeMenu.compareTo(oMenu)) {
1185
menuNav._activeMenu = oMenu;
1186
menuNav._initFocusManager();
1187
menuNav._focusManager.set(ACTIVE_DESCENDANT, oTarget);
1188
menuNav._setActiveItem(getItem(oTarget, true));
1193
else { // Initial focus
1195
// First time the menu has been focused, need to setup focused
1196
// state and established active active descendant
1198
menuNav._hasFocus = true;
1200
oActiveItem = getItem(oTarget, true);
1203
menuNav._setActiveItem(oActiveItem);
1209
else { // The menu has lost focus
1211
menuNav._clearActiveItem();
1213
menuNav._cancelShowSubmenuTimer();
1214
menuNav._hideAllSubmenus(menuNav._rootMenu);
1216
menuNav._activeMenu = menuNav._rootMenu;
1217
menuNav._initFocusManager();
1219
menuNav._focusManager.set(ACTIVE_DESCENDANT, 0);
1221
menuNav._hasFocus = false;
1229
* @method _onMenuMouseOver
1230
* @description "mouseover" event handler for a menu.
1232
* @param {Node} menu Node instance representing a menu.
1233
* @param {Object} event Object representing the DOM event.
1235
_onMenuMouseOver: function (menu, event) {
1238
oHideAllSubmenusTimer = menuNav._hideAllSubmenusTimer;
1240
if (oHideAllSubmenusTimer) {
1241
oHideAllSubmenusTimer.cancel();
1242
menuNav._hideAllSubmenusTimer = null;
1245
menuNav._cancelHideSubmenuTimer();
1247
// Need to update the FocusManager in advance of focus a new
1248
// Menu in order to avoid the FocusManager thinking that
1249
// it has lost focus
1251
if (menu && !menu.compareTo(menuNav._activeMenu)) {
1252
menuNav._activeMenu = menu;
1254
if (menuNav._hasFocus) {
1255
menuNav._initFocusManager();
1260
if (menuNav._movingToSubmenu && isHorizontalMenu(menu)) {
1261
menuNav._movingToSubmenu = false;
1268
* @method _hideAndFocusLabel
1269
* @description Hides all of the submenus of the root menu and focuses the
1270
* label of the topmost submenu
1273
_hideAndFocusLabel: function () {
1276
oActiveMenu = menuNav._activeMenu,
1279
menuNav._hideAllSubmenus(menuNav._rootMenu);
1283
// Focus the label element for the topmost submenu
1284
oSubmenu = menuNav._getTopmostSubmenu(oActiveMenu);
1285
menuNav._focusItem(oSubmenu.previous());
1293
* @method _onMenuMouseOut
1294
* @description "mouseout" event handler for a menu.
1296
* @param {Node} menu Node instance representing a menu.
1297
* @param {Object} event Object representing the DOM event.
1299
_onMenuMouseOut: function (menu, event) {
1302
oActiveMenu = menuNav._activeMenu,
1303
oRelatedTarget = event.relatedTarget,
1304
oActiveItem = menuNav._activeItem,
1309
if (oActiveMenu && !oActiveMenu.contains(oRelatedTarget)) {
1311
oParentMenu = getParentMenu(oActiveMenu);
1314
if (oParentMenu && !oParentMenu.contains(oRelatedTarget)) {
1316
if (menuNav.get(MOUSEOUT_HIDE_DELAY) > 0) {
1318
menuNav._cancelShowSubmenuTimer();
1320
menuNav._hideAllSubmenusTimer =
1322
later(menuNav.get(MOUSEOUT_HIDE_DELAY),
1323
menuNav, menuNav._hideAndFocusLabel);
1332
oMenu = getParentMenu(oActiveItem);
1334
if (!menuNav._isRoot(oMenu)) {
1335
menuNav._focusItem(oMenu.previous());
1348
* @method _onMenuLabelMouseOver
1349
* @description "mouseover" event handler for a menu label.
1351
* @param {Node} menuLabel Node instance representing a menu label.
1352
* @param {Object} event Object representing the DOM event.
1354
_onMenuLabelMouseOver: function (menuLabel, event) {
1357
oActiveMenu = menuNav._activeMenu,
1358
bIsRoot = menuNav._isRoot(oActiveMenu),
1359
bUseAutoSubmenuDisplay =
1360
(menuNav.get(AUTO_SUBMENU_DISPLAY) && bIsRoot || !bIsRoot),
1361
submenuShowDelay = menuNav.get("submenuShowDelay"),
1365
var showSubmenu = function (delay) {
1367
menuNav._cancelHideSubmenuTimer();
1368
menuNav._cancelShowSubmenuTimer();
1370
if (!hasVisibleSubmenu(menuLabel)) {
1372
oSubmenu = menuLabel.next();
1375
menuNav._hideAllSubmenus(oActiveMenu);
1376
menuNav._showSubmenuTimer = later(delay, menuNav, menuNav._showMenu, oSubmenu);
1384
menuNav._focusItem(menuLabel);
1385
menuNav._setActiveItem(menuLabel);
1388
if (bUseAutoSubmenuDisplay) {
1390
if (menuNav._movingToSubmenu) {
1392
// If the user is moving diagonally from a submenu to
1393
// another submenu and they then stop and pause on a
1394
// menu label for an amount of time equal to the amount of
1395
// time defined for the display of a submenu then show the
1396
// submenu immediately.
1397
// http://yuilibrary.com/projects/yui3/ticket/2528316
1399
//Y.message("Pause path");
1401
menuNav._hoverTimer = later(submenuShowDelay, menuNav, function () {
1407
showSubmenu(submenuShowDelay);
1416
* @method _onMenuLabelMouseOut
1417
* @description "mouseout" event handler for a menu label.
1419
* @param {Node} menuLabel Node instance representing a menu label.
1420
* @param {Object} event Object representing the DOM event.
1422
_onMenuLabelMouseOut: function (menuLabel, event) {
1425
bIsRoot = menuNav._isRoot(menuNav._activeMenu),
1426
bUseAutoSubmenuDisplay =
1427
(menuNav.get(AUTO_SUBMENU_DISPLAY) && bIsRoot || !bIsRoot),
1429
oRelatedTarget = event.relatedTarget,
1430
oSubmenu = menuLabel.next(),
1431
hoverTimer = menuNav._hoverTimer;
1434
hoverTimer.cancel();
1437
menuNav._clearActiveItem();
1439
if (bUseAutoSubmenuDisplay) {
1441
if (menuNav._movingToSubmenu &&
1442
!menuNav._showSubmenuTimer && oSubmenu) {
1444
// If the mouse is moving diagonally toward the submenu and
1445
// another submenu isn't in the process of being displayed
1446
// (via a timer), then hide the submenu via a timer to give
1447
// the user some time to reach the submenu.
1449
menuNav._hideSubmenuTimer =
1450
later(menuNav.get("submenuHideDelay"), menuNav,
1451
menuNav._hideMenu, oSubmenu);
1454
else if (!menuNav._movingToSubmenu && oSubmenu && (!oRelatedTarget ||
1456
!oSubmenu.contains(oRelatedTarget) &&
1457
!oRelatedTarget.compareTo(oSubmenu)))) {
1459
// If the mouse is not moving toward the submenu, cancel any
1460
// submenus that might be in the process of being displayed
1461
// (via a timer) and hide this submenu immediately.
1463
menuNav._cancelShowSubmenuTimer();
1465
menuNav._hideMenu(oSubmenu);
1475
* @method _onMenuItemMouseOver
1476
* @description "mouseover" event handler for a menuitem.
1478
* @param {Node} menuItem Node instance representing a menuitem.
1479
* @param {Object} event Object representing the DOM event.
1481
_onMenuItemMouseOver: function (menuItem, event) {
1484
oActiveMenu = menuNav._activeMenu,
1485
bIsRoot = menuNav._isRoot(oActiveMenu),
1486
bUseAutoSubmenuDisplay =
1487
(menuNav.get(AUTO_SUBMENU_DISPLAY) && bIsRoot || !bIsRoot);
1490
menuNav._focusItem(menuItem);
1491
menuNav._setActiveItem(menuItem);
1494
if (bUseAutoSubmenuDisplay && !menuNav._movingToSubmenu) {
1496
menuNav._hideAllSubmenus(oActiveMenu);
1504
* @method _onMenuItemMouseOut
1505
* @description "mouseout" event handler for a menuitem.
1507
* @param {Node} menuItem Node instance representing a menuitem.
1508
* @param {Object} event Object representing the DOM event.
1510
_onMenuItemMouseOut: function (menuItem, event) {
1512
this._clearActiveItem();
1518
* @method _onVerticalMenuKeyDown
1519
* @description "keydown" event handler for vertical menus.
1521
* @param {Object} event Object representing the DOM event.
1523
_onVerticalMenuKeyDown: function (event) {
1526
oActiveMenu = menuNav._activeMenu,
1527
oRootMenu = menuNav._rootMenu,
1528
oTarget = event.target,
1529
bPreventDefault = false,
1530
nKeyCode = event.keyCode,
1539
case 37: // left arrow
1541
oParentMenu = getParentMenu(oActiveMenu);
1543
if (oParentMenu && isHorizontalMenu(oParentMenu)) {
1545
menuNav._hideMenu(oActiveMenu);
1546
oLI = getPreviousSibling(oActiveMenu.get(PARENT_NODE));
1547
oItem = getItem(oLI);
1551
if (isMenuLabel(oItem)) { // Menu label
1553
oSubmenu = oItem.next();
1558
menuNav._showMenu(oSubmenu);
1559
menuNav._focusItem(getFirstItem(oSubmenu));
1560
menuNav._setActiveItem(getFirstItem(oSubmenu));
1565
menuNav._focusItem(oItem);
1566
menuNav._setActiveItem(oItem);
1573
menuNav._focusItem(oItem);
1574
menuNav._setActiveItem(oItem);
1581
else if (!menuNav._isRoot(oActiveMenu)) {
1582
menuNav._hideMenu(oActiveMenu, true);
1586
bPreventDefault = true;
1590
case 39: // right arrow
1592
if (isMenuLabel(oTarget)) {
1594
oSubmenu = oTarget.next();
1598
menuNav._showMenu(oSubmenu);
1599
menuNav._focusItem(getFirstItem(oSubmenu));
1600
menuNav._setActiveItem(getFirstItem(oSubmenu));
1605
else if (isHorizontalMenu(oRootMenu)) {
1607
oSubmenu = menuNav._getTopmostSubmenu(oActiveMenu);
1608
oLI = getNextSibling(oSubmenu.get(PARENT_NODE));
1609
oItem = getItem(oLI);
1611
menuNav._hideAllSubmenus(oRootMenu);
1615
if (isMenuLabel(oItem)) { // Menu label
1617
oSubmenu = oItem.next();
1621
menuNav._showMenu(oSubmenu);
1622
menuNav._focusItem(getFirstItem(oSubmenu));
1623
menuNav._setActiveItem(getFirstItem(oSubmenu));
1628
menuNav._focusItem(oItem);
1629
menuNav._setActiveItem(oItem);
1636
menuNav._focusItem(oItem);
1637
menuNav._setActiveItem(oItem);
1645
bPreventDefault = true;
1652
if (bPreventDefault) {
1654
// Prevent the browser from scrolling the window
1656
event.preventDefault();
1664
* @method _onHorizontalMenuKeyDown
1665
* @description "keydown" event handler for horizontal menus.
1667
* @param {Object} event Object representing the DOM event.
1669
_onHorizontalMenuKeyDown: function (event) {
1672
oActiveMenu = menuNav._activeMenu,
1673
oTarget = event.target,
1674
oFocusedItem = getItem(oTarget, true),
1675
bPreventDefault = false,
1676
nKeyCode = event.keyCode,
1680
if (nKeyCode === 40) {
1682
menuNav._hideAllSubmenus(oActiveMenu);
1684
if (isMenuLabel(oFocusedItem)) {
1686
oSubmenu = oFocusedItem.next();
1690
menuNav._showMenu(oSubmenu);
1691
menuNav._focusItem(getFirstItem(oSubmenu));
1692
menuNav._setActiveItem(getFirstItem(oSubmenu));
1696
bPreventDefault = true;
1703
if (bPreventDefault) {
1705
// Prevent the browser from scrolling the window
1707
event.preventDefault();
1714
// Generic DOM Event handlers
1718
* @method _onMouseMove
1719
* @description "mousemove" event handler for the menu.
1721
* @param {Object} event Object representing the DOM event.
1723
_onMouseMove: function (event) {
1727
// Using a timer to set the value of the "_currentMouseX" property
1728
// helps improve the reliability of the calculation used to set the
1729
// value of the "_movingToSubmenu" property - especially in Opera.
1731
later(10, menuNav, function () {
1733
menuNav._currentMouseX = event.pageX;
1741
* @method _onMouseOver
1742
* @description "mouseover" event handler for the menu.
1744
* @param {Object} event Object representing the DOM event.
1746
_onMouseOver: function (event) {
1756
if (menuNav._blockMouseEvent) {
1757
menuNav._blockMouseEvent = false;
1761
oTarget = event.target;
1762
oMenu = getMenu(oTarget, true);
1763
oMenuLabel = getMenuLabel(oTarget, true);
1764
oMenuItem = getMenuItem(oTarget, true);
1767
if (handleMouseOverForNode(oMenu, oTarget)) {
1769
menuNav._onMenuMouseOver(oMenu, event);
1771
oMenu[HANDLED_MOUSEOVER] = true;
1772
oMenu[HANDLED_MOUSEOUT] = false;
1774
oParentMenu = getParentMenu(oMenu);
1778
oParentMenu[HANDLED_MOUSEOUT] = true;
1779
oParentMenu[HANDLED_MOUSEOVER] = false;
1785
if (handleMouseOverForNode(oMenuLabel, oTarget)) {
1787
menuNav._onMenuLabelMouseOver(oMenuLabel, event);
1789
oMenuLabel[HANDLED_MOUSEOVER] = true;
1790
oMenuLabel[HANDLED_MOUSEOUT] = false;
1794
if (handleMouseOverForNode(oMenuItem, oTarget)) {
1796
menuNav._onMenuItemMouseOver(oMenuItem, event);
1798
oMenuItem[HANDLED_MOUSEOVER] = true;
1799
oMenuItem[HANDLED_MOUSEOUT] = false;
1809
* @method _onMouseOut
1810
* @description "mouseout" event handler for the menu.
1812
* @param {Object} event Object representing the DOM event.
1814
_onMouseOut: function (event) {
1817
oActiveMenu = menuNav._activeMenu,
1818
bMovingToSubmenu = false,
1827
menuNav._movingToSubmenu =
1828
(oActiveMenu && !isHorizontalMenu(oActiveMenu) &&
1829
((event.pageX - 5) > menuNav._currentMouseX));
1831
oTarget = event.target;
1832
oRelatedTarget = event.relatedTarget;
1833
oMenu = getMenu(oTarget, true);
1834
oMenuLabel = getMenuLabel(oTarget, true);
1835
oMenuItem = getMenuItem(oTarget, true);
1838
if (handleMouseOutForNode(oMenuLabel, oRelatedTarget)) {
1840
menuNav._onMenuLabelMouseOut(oMenuLabel, event);
1842
oMenuLabel[HANDLED_MOUSEOUT] = true;
1843
oMenuLabel[HANDLED_MOUSEOVER] = false;
1847
if (handleMouseOutForNode(oMenuItem, oRelatedTarget)) {
1849
menuNav._onMenuItemMouseOut(oMenuItem, event);
1851
oMenuItem[HANDLED_MOUSEOUT] = true;
1852
oMenuItem[HANDLED_MOUSEOVER] = false;
1859
oSubmenu = oMenuLabel.next();
1861
if (oSubmenu && oRelatedTarget &&
1862
(oRelatedTarget.compareTo(oSubmenu) ||
1863
oSubmenu.contains(oRelatedTarget))) {
1865
bMovingToSubmenu = true;
1872
if (handleMouseOutForNode(oMenu, oRelatedTarget) || bMovingToSubmenu) {
1874
menuNav._onMenuMouseOut(oMenu, event);
1876
oMenu[HANDLED_MOUSEOUT] = true;
1877
oMenu[HANDLED_MOUSEOVER] = false;
1885
* @method _toggleSubmenuDisplay
1886
* @description "mousedown," "keydown," and "click" event handler for the
1887
* menu used to toggle the display of a submenu.
1889
* @param {Object} event Object representing the DOM event.
1891
_toggleSubmenuDisplay: function (event) {
1894
oTarget = event.target,
1895
oMenuLabel = getMenuLabel(oTarget, true),
1907
oAnchor = isAnchor(oTarget) ? oTarget : oTarget.ancestor(isAnchor);
1912
// Need to pass "2" as a second argument to "getAttribute" for
1913
// IE otherwise IE will return a fully qualified URL for the
1914
// value of the "href" attribute.
1915
// http://msdn.microsoft.com/en-us/library/ms536429(VS.85).aspx
1917
sHref = oAnchor.getAttribute("href", 2);
1918
nHashPos = sHref.indexOf("#");
1919
nLen = sHref.length;
1921
if (nHashPos === 0 && nLen > 1) {
1923
sId = sHref.substr(1, nLen);
1924
oSubmenu = oMenuLabel.next();
1926
if (oSubmenu && (oSubmenu.get(ID) === sId)) {
1928
if (sType === MOUSEDOWN || sType === KEYDOWN) {
1930
if ((UA.opera || UA.gecko || UA.ie) && sType === KEYDOWN && !menuNav._preventClickHandle) {
1932
// Prevent the browser from following the URL of
1933
// the anchor element
1935
menuNav._preventClickHandle = menuNav._rootMenu.on("click", function (event) {
1937
event.preventDefault();
1939
menuNav._preventClickHandle.detach();
1940
menuNav._preventClickHandle = null;
1946
if (sType == MOUSEDOWN) {
1948
// Prevent the target from getting focused by
1949
// default, since the element to be focused will
1950
// be determined by weather or not the submenu
1952
event.preventDefault();
1954
// FocusManager will attempt to focus any
1955
// descendant that is the target of the mousedown
1956
// event. Since we want to explicitly control
1957
// where focus is going, we need to call
1958
// "stopImmediatePropagation" to stop the
1959
// FocusManager from doing its thing.
1960
event.stopImmediatePropagation();
1962
// The "_focusItem" method relies on the
1963
// "_hasFocus" property being set to true. The
1964
// "_hasFocus" property is normally set via a
1965
// "focus" event listener, but since we've
1966
// blocked focus from happening, we need to set
1967
// this property manually.
1968
menuNav._hasFocus = true;
1973
if (menuNav._isRoot(getParentMenu(oTarget))) { // Event target is a submenu label in the root menu
1975
// Menu label toggle functionality
1977
if (hasVisibleSubmenu(oMenuLabel)) {
1979
menuNav._hideMenu(oSubmenu);
1980
menuNav._focusItem(oMenuLabel);
1981
menuNav._setActiveItem(oMenuLabel);
1986
menuNav._hideAllSubmenus(menuNav._rootMenu);
1987
menuNav._showMenu(oSubmenu);
1989
menuNav._focusItem(getFirstItem(oSubmenu));
1990
menuNav._setActiveItem(getFirstItem(oSubmenu));
1995
else { // Event target is a submenu label within a submenu
1997
if (menuNav._activeItem == oMenuLabel) {
1999
menuNav._showMenu(oSubmenu);
2000
menuNav._focusItem(getFirstItem(oSubmenu));
2001
menuNav._setActiveItem(getFirstItem(oSubmenu));
2006
if (!oMenuLabel._clickHandle) {
2008
oMenuLabel._clickHandle = oMenuLabel.on("click", function () {
2010
menuNav._hideAllSubmenus(menuNav._rootMenu);
2012
menuNav._hasFocus = false;
2013
menuNav._clearActiveItem();
2016
oMenuLabel._clickHandle.detach();
2018
oMenuLabel._clickHandle = null;
2031
if (sType === CLICK) {
2033
// Prevent the browser from following the URL of
2034
// the anchor element
2036
event.preventDefault();
2053
* @method _onKeyPress
2054
* @description "keypress" event handler for the menu.
2056
* @param {Object} event Object representing the DOM event.
2058
_onKeyPress: function (event) {
2060
switch (event.keyCode) {
2062
case 37: // left arrow
2063
case 38: // up arrow
2064
case 39: // right arrow
2065
case 40: // down arrow
2067
// Prevent the browser from scrolling the window
2069
event.preventDefault();
2079
* @method _onKeyDown
2080
* @description "keydown" event handler for the menu.
2082
* @param {Object} event Object representing the DOM event.
2084
_onKeyDown: function (event) {
2087
oActiveItem = menuNav._activeItem,
2088
oTarget = event.target,
2089
oActiveMenu = getParentMenu(oTarget),
2094
menuNav._activeMenu = oActiveMenu;
2096
if (isHorizontalMenu(oActiveMenu)) {
2097
menuNav._onHorizontalMenuKeyDown(event);
2100
menuNav._onVerticalMenuKeyDown(event);
2104
if (event.keyCode === 27) {
2106
if (!menuNav._isRoot(oActiveMenu)) {
2109
later(0, menuNav, function () {
2110
menuNav._hideMenu(oActiveMenu, true);
2114
menuNav._hideMenu(oActiveMenu, true);
2117
event.stopPropagation();
2118
menuNav._blockMouseEvent = UA.gecko ? true : false;
2121
else if (oActiveItem) {
2123
if (isMenuLabel(oActiveItem) &&
2124
hasVisibleSubmenu(oActiveItem)) {
2126
oSubmenu = oActiveItem.next();
2129
menuNav._hideMenu(oSubmenu);
2135
menuNav._focusManager.blur();
2137
// This is necessary for Webkit since blurring the
2138
// active menuitem won't result in the document
2139
// gaining focus, meaning the that _onDocFocus
2140
// listener won't clear the active menuitem.
2142
menuNav._clearActiveItem();
2144
menuNav._hasFocus = false;
2157
* @method _onDocMouseDown
2158
* @description "mousedown" event handler for the owner document of
2161
* @param {Object} event Object representing the DOM event.
2163
_onDocMouseDown: function (event) {
2166
oRoot = menuNav._rootMenu,
2167
oTarget = event.target;
2170
if (!(oRoot.compareTo(oTarget) || oRoot.contains(oTarget))) {
2172
menuNav._hideAllSubmenus(oRoot);
2174
// Document doesn't receive focus in Webkit when the user mouses
2175
// down on it, so the "_hasFocus" property won't get set to the
2176
// correct value. The following line corrects the problem.
2179
menuNav._hasFocus = false;
2180
menuNav._clearActiveItem();
2190
Y.namespace('Plugin');
2192
Y.Plugin.NodeMenuNav = NodeMenuNav;
2195
}, '3.10.3', {"requires": ["node", "classnamemanager", "plugin", "node-focusmanager"], "skinnable": true});