admin/tool/lp/amd/src/menubar.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Aria menubar functionality. Enhances a simple nested list structure into a full aria widget.
  17. * Based on the open ajax example: http://oaa-accessibility.org/example/26/
  18. *
  19. * @module tool_lp/menubar
  20. * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. define(['jquery'], function($) {
  24. /** @property {boolean} Flag to indicate if we have already registered a click event handler for the document. */
  25. var documentClickHandlerRegistered = false;
  26. /** @property {boolean} Flag to indicate whether there's an active, open menu. */
  27. var menuActive = false;
  28. /**
  29. * Close all open submenus anywhere in the page (there should only ever be one open at a time).
  30. *
  31. * @method closeAllSubMenus
  32. */
  33. var closeAllSubMenus = function() {
  34. $('.tool-lp-menu .tool-lp-sub-menu').attr('aria-hidden', 'true');
  35. // Every menu's closed at this point, so set the menu active flag to false.
  36. menuActive = false;
  37. };
  38. /**
  39. * Constructor
  40. *
  41. * @param {jQuery} menuRoot Jquery collection matching the root of the menu.
  42. * @param {Function[]} handlers called when a menu item is chosen.
  43. */
  44. var Menubar = function(menuRoot, handlers) {
  45. // Setup private class variables.
  46. this.menuRoot = menuRoot;
  47. this.handlers = handlers;
  48. this.rootMenus = this.menuRoot.children('li');
  49. this.subMenus = this.rootMenus.children('ul');
  50. this.subMenuItems = this.subMenus.children('li');
  51. this.allItems = this.rootMenus.add(this.subMenuItems);
  52. this.activeItem = null;
  53. this.isChildOpen = false;
  54. this.keys = {
  55. tab: 9,
  56. enter: 13,
  57. esc: 27,
  58. space: 32,
  59. left: 37,
  60. up: 38,
  61. right: 39,
  62. down: 40
  63. };
  64. this.addAriaAttributes();
  65. // Add the event listeners.
  66. this.addEventListeners();
  67. };
  68. /**
  69. * Open a submenu, first it closes all other sub-menus and sets the open direction.
  70. * @method openSubMenu
  71. * @param {Node} menu
  72. */
  73. Menubar.prototype.openSubMenu = function(menu) {
  74. this.setOpenDirection();
  75. closeAllSubMenus();
  76. menu.attr('aria-hidden', 'false');
  77. // Set menu active flag to true when a menu is opened.
  78. menuActive = true;
  79. };
  80. /**
  81. * Bind the event listeners to the DOM
  82. * @method addEventListeners
  83. */
  84. Menubar.prototype.addEventListeners = function() {
  85. var currentThis = this;
  86. // When clicking outside the menubar.
  87. if (documentClickHandlerRegistered === false) {
  88. $(document).click(function() {
  89. // Check if a menu is opened.
  90. if (menuActive) {
  91. // Close menu.
  92. closeAllSubMenus();
  93. }
  94. });
  95. // Set this flag to true so that we won't need to add a document click handler for the other Menubar instances.
  96. documentClickHandlerRegistered = true;
  97. }
  98. // Hovers.
  99. this.subMenuItems.mouseenter(function() {
  100. $(this).addClass('menu-hover');
  101. return true;
  102. });
  103. this.subMenuItems.mouseout(function() {
  104. $(this).removeClass('menu-hover');
  105. return true;
  106. });
  107. // Mouse listeners.
  108. this.allItems.click(function(e) {
  109. return currentThis.handleClick($(this), e);
  110. });
  111. // Key listeners.
  112. this.allItems.keydown(function(e) {
  113. return currentThis.handleKeyDown($(this), e);
  114. });
  115. this.allItems.focus(function() {
  116. return currentThis.handleFocus($(this));
  117. });
  118. this.allItems.blur(function() {
  119. return currentThis.handleBlur($(this));
  120. });
  121. };
  122. /**
  123. * Process click events for the top menus.
  124. *
  125. * @method handleClick
  126. * @param {Object} item is the jquery object of the item firing the event
  127. * @param {Event} e is the associated event object
  128. * @return {boolean} Returns false
  129. */
  130. Menubar.prototype.handleClick = function(item, e) {
  131. e.stopPropagation();
  132. var parentUL = item.parent();
  133. if (parentUL.is('.tool-lp-menu')) {
  134. // Toggle the child menu open/closed.
  135. if (item.children('ul').first().attr('aria-hidden') == 'true') {
  136. this.openSubMenu(item.children('ul').first());
  137. } else {
  138. item.children('ul').first().attr('aria-hidden', 'true');
  139. }
  140. } else {
  141. // Remove hover and focus styling.
  142. this.allItems.removeClass('menu-hover menu-focus');
  143. // Clear the active item.
  144. this.activeItem = null;
  145. // Close the menu.
  146. this.menuRoot.find('ul').not('.root-level').attr('aria-hidden', 'true');
  147. // Follow any link, or call the click handlers.
  148. var anchor = item.find('a').first();
  149. var clickEvent = new $.Event('click');
  150. clickEvent.target = anchor;
  151. var eventHandled = false;
  152. if (this.handlers) {
  153. $.each(this.handlers, function(selector, handler) {
  154. if (eventHandled) {
  155. return;
  156. }
  157. if (item.find(selector).length > 0) {
  158. var callable = $.proxy(handler, anchor);
  159. // False means stop propogatting events.
  160. eventHandled = (callable(clickEvent) === false) || clickEvent.isDefaultPrevented();
  161. }
  162. });
  163. }
  164. // If we didn't find a handler, and the HREF is # that probably means that
  165. // we are handling it from somewhere else. Let's just do nothing in that case.
  166. if (!eventHandled && anchor.attr('href') !== '#') {
  167. window.location.href = anchor.attr('href');
  168. }
  169. }
  170. return false;
  171. };
  172. /*
  173. * Process focus events for the menu.
  174. *
  175. * @method handleFocus
  176. * @param {Object} item is the jquery object of the item firing the event
  177. * @return boolean Returns false
  178. */
  179. Menubar.prototype.handleFocus = function(item) {
  180. // If activeItem is null, we are getting focus from outside the menu. Store
  181. // the item that triggered the event.
  182. if (this.activeItem === null) {
  183. this.activeItem = item;
  184. } else if (item[0] != this.activeItem[0]) {
  185. return true;
  186. }
  187. // Get the set of jquery objects for all the parent items of the active item.
  188. var parentItems = this.activeItem.parentsUntil('ul.tool-lp-menu').filter('li');
  189. // Remove focus styling from all other menu items.
  190. this.allItems.removeClass('menu-focus');
  191. // Add focus styling to the active item.
  192. this.activeItem.addClass('menu-focus');
  193. // Add focus styling to all parent items.
  194. parentItems.addClass('menu-focus');
  195. // If the bChildOpen flag has been set, open the active item's child menu (if applicable).
  196. if (this.isChildOpen === true) {
  197. var itemUL = item.parent();
  198. // If the itemUL is a root-level menu and item is a parent item,
  199. // show the child menu.
  200. if (itemUL.is('.tool-lp-menu') && (item.attr('aria-haspopup') == 'true')) {
  201. this.openSubMenu(item.children('ul').first());
  202. }
  203. }
  204. return true;
  205. };
  206. /*
  207. * Process blur events for the menu.
  208. *
  209. * @method handleBlur
  210. * @param {Object} item is the jquery object of the item firing the event
  211. * @return boolean Returns false
  212. */
  213. Menubar.prototype.handleBlur = function(item) {
  214. item.removeClass('menu-focus');
  215. return true;
  216. };
  217. /*
  218. * Determine if the menu should open to the left, or the right,
  219. * based on the screen size and menu position.
  220. * @method setOpenDirection
  221. */
  222. Menubar.prototype.setOpenDirection = function() {
  223. var pos = this.menuRoot.offset();
  224. var isRTL = $(document.body).hasClass('dir-rtl');
  225. var openLeft = true;
  226. var heightmenuRoot = this.rootMenus.outerHeight();
  227. var widthmenuRoot = this.rootMenus.outerWidth();
  228. // Sometimes the menuMinWidth is not enough to figure out if menu exceeds the window width.
  229. // So we have to calculate the real menu width.
  230. var subMenuContainer = this.rootMenus.find('ul.tool-lp-sub-menu');
  231. // Reset margins.
  232. subMenuContainer.css('margin-right', '');
  233. subMenuContainer.css('margin-left', '');
  234. subMenuContainer.css('margin-top', '');
  235. subMenuContainer.attr('aria-hidden', false);
  236. var menuRealWidth = subMenuContainer.outerWidth(),
  237. menuRealHeight = subMenuContainer.outerHeight();
  238. var margintop = null,
  239. marginright = null,
  240. marginleft = null;
  241. var top = pos.top - $(window).scrollTop();
  242. // Top is the same for RTL and LTR.
  243. if (top + menuRealHeight > $(window).height()) {
  244. margintop = menuRealHeight + heightmenuRoot;
  245. subMenuContainer.css('margin-top', '-' + margintop + 'px');
  246. }
  247. if (isRTL) {
  248. if (pos.left - menuRealWidth < 0) {
  249. marginright = menuRealWidth - widthmenuRoot;
  250. subMenuContainer.css('margin-right', '-' + marginright + 'px');
  251. }
  252. } else {
  253. if (pos.left + menuRealWidth > $(window).width()) {
  254. marginleft = menuRealWidth - widthmenuRoot;
  255. subMenuContainer.css('margin-left', '-' + marginleft + 'px');
  256. }
  257. }
  258. if (openLeft) {
  259. this.menuRoot.addClass('tool-lp-menu-open-left');
  260. } else {
  261. this.menuRoot.removeClass('tool-lp-menu-open-left');
  262. }
  263. };
  264. /*
  265. * Process keyDown events for the menu.
  266. *
  267. * @method handleKeyDown
  268. * @param {Object} item is the jquery object of the item firing the event
  269. * @param {Event} e is the associated event object
  270. * @return boolean Returns false if consuming the event
  271. */
  272. Menubar.prototype.handleKeyDown = function(item, e) {
  273. if (e.altKey || e.ctrlKey) {
  274. // Modifier key pressed: Do not process.
  275. return true;
  276. }
  277. switch (e.keyCode) {
  278. case this.keys.tab: {
  279. // Hide all menu items and update their aria attributes.
  280. this.menuRoot.find('ul').attr('aria-hidden', 'true');
  281. // Remove focus styling from all menu items.
  282. this.allItems.removeClass('menu-focus');
  283. this.activeItem = null;
  284. this.isChildOpen = false;
  285. break;
  286. }
  287. case this.keys.esc: {
  288. var itemUL = item.parent();
  289. if (itemUL.is('.tool-lp-menu')) {
  290. // Hide the child menu and update the aria attributes.
  291. item.children('ul').first().attr('aria-hidden', 'true');
  292. } else {
  293. // Move up one level.
  294. this.activeItem = itemUL.parent();
  295. // Reset the isChildOpen flag.
  296. this.isChildOpen = false;
  297. // Set focus on the new item.
  298. this.activeItem.focus();
  299. // Hide the active menu and update the aria attributes.
  300. itemUL.attr('aria-hidden', 'true');
  301. }
  302. e.stopPropagation();
  303. return false;
  304. }
  305. case this.keys.enter:
  306. case this.keys.space: {
  307. // Trigger click handler.
  308. return this.handleClick(item, e);
  309. }
  310. case this.keys.left: {
  311. this.activeItem = this.moveToPrevious(item);
  312. this.activeItem.focus();
  313. e.stopPropagation();
  314. return false;
  315. }
  316. case this.keys.right: {
  317. this.activeItem = this.moveToNext(item);
  318. this.activeItem.focus();
  319. e.stopPropagation();
  320. return false;
  321. }
  322. case this.keys.up: {
  323. this.activeItem = this.moveUp(item);
  324. this.activeItem.focus();
  325. e.stopPropagation();
  326. return false;
  327. }
  328. case this.keys.down: {
  329. this.activeItem = this.moveDown(item);
  330. this.activeItem.focus();
  331. e.stopPropagation();
  332. return false;
  333. }
  334. }
  335. return true;
  336. };
  337. /**
  338. * Move to the next menu level.
  339. * This will be either the next root-level menu or the child of a menu parent. If
  340. * at the root level and the active item is the last in the menu, this function will loop
  341. * to the first menu item.
  342. *
  343. * If the menu is a horizontal menu, the first child element of the newly selected menu will
  344. * be selected
  345. *
  346. * @method moveToNext
  347. * @param {Object} item is the active menu item
  348. * @return {Object} Returns the item to move to. Returns item is no move is possible
  349. */
  350. Menubar.prototype.moveToNext = function(item) {
  351. // Item's containing menu.
  352. var itemUL = item.parent();
  353. // The items in the currently active menu.
  354. var menuItems = itemUL.children('li');
  355. // The number of items in the active menu.
  356. var menuNum = menuItems.length;
  357. // The items index in its menu.
  358. var menuIndex = menuItems.index(item);
  359. var newItem = null;
  360. var childMenu = null;
  361. if (itemUL.is('.tool-lp-menu')) {
  362. // This is the root level move to next sibling. This will require closing
  363. // the current child menu and opening the new one.
  364. if (menuIndex < menuNum - 1) {
  365. // Not the last root menu.
  366. newItem = item.next();
  367. } else { // Wrap to first item.
  368. newItem = menuItems.first();
  369. }
  370. // Close the current child menu (if applicable).
  371. if (item.attr('aria-haspopup') == 'true') {
  372. childMenu = item.children('ul').first();
  373. if (childMenu.attr('aria-hidden') == 'false') {
  374. // Update the child menu's aria-hidden attribute.
  375. childMenu.attr('aria-hidden', 'true');
  376. this.isChildOpen = true;
  377. }
  378. }
  379. // Remove the focus styling from the current menu.
  380. item.removeClass('menu-focus');
  381. // Open the new child menu (if applicable).
  382. if ((newItem.attr('aria-haspopup') === 'true') && (this.isChildOpen === true)) {
  383. childMenu = newItem.children('ul').first();
  384. // Update the child's aria-hidden attribute.
  385. this.openSubMenu(childMenu);
  386. }
  387. } else {
  388. // This is not the root level. If there is a child menu to be moved into, do that;
  389. // otherwise, move to the next root-level menu if there is one.
  390. if (item.attr('aria-haspopup') == 'true') {
  391. childMenu = item.children('ul').first();
  392. newItem = childMenu.children('li').first();
  393. // Show the child menu and update its aria attributes.
  394. this.openSubMenu(childMenu);
  395. } else {
  396. // At deepest level, move to the next root-level menu.
  397. var parentMenus = null;
  398. var rootItem = null;
  399. // Get list of all parent menus for item, up to the root level.
  400. parentMenus = item.parentsUntil('ul.tool-lp-menu').filter('ul').not('.tool-lp-menu');
  401. // Hide the current menu and update its aria attributes accordingly.
  402. parentMenus.attr('aria-hidden', 'true');
  403. // Remove the focus styling from the active menu.
  404. parentMenus.find('li').removeClass('menu-focus');
  405. parentMenus.last().parent().removeClass('menu-focus');
  406. // The containing root for the menu.
  407. rootItem = parentMenus.last().parent();
  408. menuIndex = this.rootMenus.index(rootItem);
  409. // If this is not the last root menu item, move to the next one.
  410. if (menuIndex < this.rootMenus.length - 1) {
  411. newItem = rootItem.next();
  412. } else {
  413. // Loop.
  414. newItem = this.rootMenus.first();
  415. }
  416. // Add the focus styling to the new menu.
  417. newItem.addClass('menu-focus');
  418. if (newItem.attr('aria-haspopup') == 'true') {
  419. childMenu = newItem.children('ul').first();
  420. newItem = childMenu.children('li').first();
  421. // Show the child menu and update it's aria attributes.
  422. this.openSubMenu(childMenu);
  423. this.isChildOpen = true;
  424. }
  425. }
  426. }
  427. return newItem;
  428. };
  429. /**
  430. * Member function to move to the previous menu level.
  431. * This will be either the previous root-level menu or the child of a menu parent. If
  432. * at the root level and the active item is the first in the menu, this function will loop
  433. * to the last menu item.
  434. *
  435. * If the menu is a horizontal menu, the first child element of the newly selected menu will
  436. * be selected
  437. *
  438. * @method moveToPrevious
  439. * @param {Object} item is the active menu item
  440. * @return {Object} Returns the item to move to. Returns item is no move is possible
  441. */
  442. Menubar.prototype.moveToPrevious = function(item) {
  443. // Item's containing menu.
  444. var itemUL = item.parent();
  445. // The items in the currently active menu.
  446. var menuItems = itemUL.children('li');
  447. // The items index in its menu.
  448. var menuIndex = menuItems.index(item);
  449. var newItem = null;
  450. var childMenu = null;
  451. if (itemUL.is('.tool-lp-menu')) {
  452. // This is the root level move to previous sibling. This will require closing
  453. // the current child menu and opening the new one.
  454. if (menuIndex > 0) {
  455. // Not the first root menu.
  456. newItem = item.prev();
  457. } else {
  458. // Wrap to last item.
  459. newItem = menuItems.last();
  460. }
  461. // Close the current child menu (if applicable).
  462. if (item.attr('aria-haspopup') == 'true') {
  463. childMenu = item.children('ul').first();
  464. if (childMenu.attr('aria-hidden') == 'false') {
  465. // Update the child menu's aria-hidden attribute.
  466. childMenu.attr('aria-hidden', 'true');
  467. this.isChildOpen = true;
  468. }
  469. }
  470. // Remove the focus styling from the current menu.
  471. item.removeClass('menu-focus');
  472. // Open the new child menu (if applicable).
  473. if ((newItem.attr('aria-haspopup') === 'true') && (this.isChildOpen === true)) {
  474. childMenu = newItem.children('ul').first();
  475. // Update the child's aria-hidden attribute.
  476. this.openSubMenu(childMenu);
  477. }
  478. } else {
  479. // This is not the root level. If there is a parent menu that is not the
  480. // root menu, move up one level; otherwise, move to first item of the previous
  481. // root menu.
  482. var parentLI = itemUL.parent();
  483. var parentUL = parentLI.parent();
  484. // If this is a vertical menu or is not the first child menu
  485. // of the root-level menu, move up one level.
  486. if (!parentUL.is('.tool-lp-menu')) {
  487. newItem = itemUL.parent();
  488. // Hide the active menu and update aria-hidden.
  489. itemUL.attr('aria-hidden', 'true');
  490. // Remove the focus highlight from the item.
  491. item.removeClass('menu-focus');
  492. } else {
  493. // Move to previous root-level menu.
  494. // Hide the current menu and update the aria attributes accordingly.
  495. itemUL.attr('aria-hidden', 'true');
  496. // Remove the focus styling from the active menu.
  497. item.removeClass('menu-focus');
  498. parentLI.removeClass('menu-focus');
  499. menuIndex = this.rootMenus.index(parentLI);
  500. if (menuIndex > 0) {
  501. // Move to the previous root-level menu.
  502. newItem = parentLI.prev();
  503. } else {
  504. // Loop to last root-level menu.
  505. newItem = this.rootMenus.last();
  506. }
  507. // Add the focus styling to the new menu.
  508. newItem.addClass('menu-focus');
  509. if (newItem.attr('aria-haspopup') == 'true') {
  510. childMenu = newItem.children('ul').first();
  511. // Show the child menu and update it's aria attributes.
  512. this.openSubMenu(childMenu);
  513. this.isChildOpen = true;
  514. newItem = childMenu.children('li').first();
  515. }
  516. }
  517. }
  518. return newItem;
  519. };
  520. /**
  521. * Member function to select the next item in a menu.
  522. * If the active item is the last in the menu, this function will loop to the
  523. * first menu item.
  524. *
  525. * @method moveDown
  526. * @param {Object} item is the active menu item
  527. * @param {String} startChr is the character to attempt to match against the beginning of the
  528. * menu item titles. If found, focus moves to the next menu item beginning with that character.
  529. * @return {Object} Returns the item to move to. Returns item is no move is possible
  530. */
  531. Menubar.prototype.moveDown = function(item, startChr) {
  532. // Item's containing menu.
  533. var itemUL = item.parent();
  534. // The items in the currently active menu.
  535. var menuItems = itemUL.children('li').not('.separator');
  536. // The number of items in the active menu.
  537. var menuNum = menuItems.length;
  538. // The items index in its menu.
  539. var menuIndex = menuItems.index(item);
  540. var newItem = null;
  541. var newItemUL = null;
  542. if (itemUL.is('.tool-lp-menu')) {
  543. // This is the root level menu.
  544. if (item.attr('aria-haspopup') != 'true') {
  545. // No child menu to move to.
  546. return item;
  547. }
  548. // Move to the first item in the child menu.
  549. newItemUL = item.children('ul').first();
  550. newItem = newItemUL.children('li').first();
  551. // Make sure the child menu is visible.
  552. this.openSubMenu(newItemUL);
  553. return newItem;
  554. }
  555. // If $item is not the last item in its menu, move to the next item. If startChr is specified, move
  556. // to the next item with a title that begins with that character.
  557. if (startChr) {
  558. var match = false;
  559. var curNdx = menuIndex + 1;
  560. // Check if the active item was the last one on the list.
  561. if (curNdx == menuNum) {
  562. curNdx = 0;
  563. }
  564. // Iterate through the menu items (starting from the current item and wrapping) until a match is found
  565. // or the loop returns to the current menu item.
  566. while (curNdx != menuIndex) {
  567. var titleChr = menuItems.eq(curNdx).html().charAt(0);
  568. if (titleChr.toLowerCase() == startChr) {
  569. match = true;
  570. break;
  571. }
  572. curNdx = curNdx + 1;
  573. if (curNdx == menuNum) {
  574. // Reached the end of the list, start again at the beginning.
  575. curNdx = 0;
  576. }
  577. }
  578. if (match === true) {
  579. newItem = menuItems.eq(curNdx);
  580. // Remove the focus styling from the current item.
  581. item.removeClass('menu-focus');
  582. return newItem;
  583. } else {
  584. return item;
  585. }
  586. } else {
  587. if (menuIndex < menuNum - 1) {
  588. newItem = menuItems.eq(menuIndex + 1);
  589. } else {
  590. newItem = menuItems.first();
  591. }
  592. }
  593. // Remove the focus styling from the current item.
  594. item.removeClass('menu-focus');
  595. return newItem;
  596. };
  597. /**
  598. * Function moveUp() is a member function to select the previous item in a menu.
  599. * If the active item is the first in the menu, this function will loop to the
  600. * last menu item.
  601. *
  602. * @method moveUp
  603. * @param {Object} item is the active menu item
  604. * @return {Object} Returns the item to move to. Returns item is no move is possible
  605. */
  606. Menubar.prototype.moveUp = function(item) {
  607. // Item's containing menu.
  608. var itemUL = item.parent();
  609. // The items in the currently active menu.
  610. var menuItems = itemUL.children('li').not('.separator');
  611. // The items index in its menu.
  612. var menuIndex = menuItems.index(item);
  613. var newItem = null;
  614. if (itemUL.is('.tool-lp-menu')) {
  615. // This is the root level menu.
  616. // Nothing to do.
  617. return item;
  618. }
  619. // If item is not the first item in its menu, move to the previous item.
  620. if (menuIndex > 0) {
  621. newItem = menuItems.eq(menuIndex - 1);
  622. } else {
  623. // Loop to top of menu.
  624. newItem = menuItems.last();
  625. }
  626. // Remove the focus styling from the current item.
  627. item.removeClass('menu-focus');
  628. return newItem;
  629. };
  630. /**
  631. * Enhance the dom with aria attributes.
  632. * @method addAriaAttributes
  633. */
  634. Menubar.prototype.addAriaAttributes = function() {
  635. this.menuRoot.attr('role', 'menubar');
  636. this.rootMenus.attr('role', 'menuitem');
  637. this.rootMenus.attr('tabindex', '0');
  638. this.rootMenus.attr('aria-haspopup', 'true');
  639. this.subMenus.attr('role', 'menu');
  640. this.subMenus.attr('aria-hidden', 'true');
  641. this.subMenuItems.attr('role', 'menuitem');
  642. this.subMenuItems.attr('tabindex', '-1');
  643. // For CSS styling and effects.
  644. this.menuRoot.addClass('tool-lp-menu');
  645. this.allItems.addClass('tool-lp-menu-item');
  646. this.rootMenus.addClass('tool-lp-root-menu');
  647. this.subMenus.addClass('tool-lp-sub-menu');
  648. this.subMenuItems.addClass('dropdown-item');
  649. };
  650. return /** @alias module:tool_lp/menubar */ {
  651. /**
  652. * Create a menu bar object for every node matching the selector.
  653. *
  654. * The expected DOM structure is shown below.
  655. * <ul> <- This is the target of the selector parameter.
  656. * <li> <- This is repeated for each top level menu.
  657. * Text <- This is the text for the top level menu.
  658. * <ul> <- This is a list of the entries in this top level menu.
  659. * <li> <- This is repeated for each menu entry.
  660. * <a href="someurl">Choice 1</a> <- The anchor for the menu.
  661. * </li>
  662. * </ul>
  663. * </li>
  664. * </ul>
  665. *
  666. * @method enhance
  667. * @param {String} selector - The selector for the outer most menu node.
  668. * @param {Function} handler - Javascript handler for when a menu item was chosen. If the
  669. * handler returns true (or does not exist), the
  670. * menu will look for an anchor with a link to follow.
  671. * For example, if the menu entry has a "data-action" attribute
  672. * and we want to call a javascript function when that entry is chosen,
  673. * we could pass a list of handlers like this:
  674. * { "[data-action='add']" : callAddFunction }
  675. */
  676. enhance: function(selector, handler) {
  677. $(selector).each(function(index, element) {
  678. var menuRoot = $(element);
  679. // Don't enhance the same menu twice.
  680. if (menuRoot.data("menubarEnhanced") !== true) {
  681. (new Menubar(menuRoot, handler));
  682. menuRoot.data("menubarEnhanced", true);
  683. }
  684. });
  685. },
  686. /**
  687. * Handy function to close all open menus anywhere on the page.
  688. * @method closeAll
  689. */
  690. closeAll: closeAllSubMenus
  691. };
  692. });