toolbar.menu.es6.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @file
  3. * Builds a nested accordion widget.
  4. *
  5. * Invoke on an HTML list element with the jQuery plugin pattern.
  6. *
  7. * @example
  8. * $('.toolbar-menu').drupalToolbarMenu();
  9. */
  10. (function ($, Drupal, drupalSettings) {
  11. /**
  12. * Store the open menu tray.
  13. */
  14. let activeItem = Drupal.url(drupalSettings.path.currentPath);
  15. $.fn.drupalToolbarMenu = function () {
  16. const ui = {
  17. handleOpen: Drupal.t('Extend'),
  18. handleClose: Drupal.t('Collapse'),
  19. };
  20. /**
  21. * Handle clicks from the disclosure button on an item with sub-items.
  22. *
  23. * @param {Object} event
  24. * A jQuery Event object.
  25. */
  26. function toggleClickHandler(event) {
  27. const $toggle = $(event.target);
  28. const $item = $toggle.closest('li');
  29. // Toggle the list item.
  30. toggleList($item);
  31. // Close open sibling menus.
  32. const $openItems = $item.siblings().filter('.open');
  33. toggleList($openItems, false);
  34. }
  35. /**
  36. * Handle clicks from a menu item link.
  37. *
  38. * @param {Object} event
  39. * A jQuery Event object.
  40. */
  41. function linkClickHandler(event) {
  42. // If the toolbar is positioned fixed (and therefore hiding content
  43. // underneath), then users expect clicks in the administration menu tray
  44. // to take them to that destination but for the menu tray to be closed
  45. // after clicking: otherwise the toolbar itself is obstructing the view
  46. // of the destination they chose.
  47. if (!Drupal.toolbar.models.toolbarModel.get('isFixed')) {
  48. Drupal.toolbar.models.toolbarModel.set('activeTab', null);
  49. }
  50. // Stopping propagation to make sure that once a toolbar-box is clicked
  51. // (the whitespace part), the page is not redirected anymore.
  52. event.stopPropagation();
  53. }
  54. /**
  55. * Toggle the open/close state of a list is a menu.
  56. *
  57. * @param {jQuery} $item
  58. * The li item to be toggled.
  59. *
  60. * @param {Boolean} switcher
  61. * A flag that forces toggleClass to add or a remove a class, rather than
  62. * simply toggling its presence.
  63. */
  64. function toggleList($item, switcher) {
  65. const $toggle = $item.children('.toolbar-box').children('.toolbar-handle');
  66. switcher = (typeof switcher !== 'undefined') ? switcher : !$item.hasClass('open');
  67. // Toggle the item open state.
  68. $item.toggleClass('open', switcher);
  69. // Twist the toggle.
  70. $toggle.toggleClass('open', switcher);
  71. // Adjust the toggle text.
  72. $toggle
  73. .find('.action')
  74. // Expand Structure, Collapse Structure.
  75. .text((switcher) ? ui.handleClose : ui.handleOpen);
  76. }
  77. /**
  78. * Add markup to the menu elements.
  79. *
  80. * Items with sub-elements have a list toggle attached to them. Menu item
  81. * links and the corresponding list toggle are wrapped with in a div
  82. * classed with .toolbar-box. The .toolbar-box div provides a positioning
  83. * context for the item list toggle.
  84. *
  85. * @param {jQuery} $menu
  86. * The root of the menu to be initialized.
  87. */
  88. function initItems($menu) {
  89. const options = {
  90. class: 'toolbar-icon toolbar-handle',
  91. action: ui.handleOpen,
  92. text: '',
  93. };
  94. // Initialize items and their links.
  95. $menu.find('li > a').wrap('<div class="toolbar-box">');
  96. // Add a handle to each list item if it has a menu.
  97. $menu.find('li').each((index, element) => {
  98. const $item = $(element);
  99. if ($item.children('ul.toolbar-menu').length) {
  100. const $box = $item.children('.toolbar-box');
  101. options.text = Drupal.t('@label', { '@label': $box.find('a').text() });
  102. $item.children('.toolbar-box')
  103. .append(Drupal.theme('toolbarMenuItemToggle', options));
  104. }
  105. });
  106. }
  107. /**
  108. * Adds a level class to each list based on its depth in the menu.
  109. *
  110. * This function is called recursively on each sub level of lists elements
  111. * until the depth of the menu is exhausted.
  112. *
  113. * @param {jQuery} $lists
  114. * A jQuery object of ul elements.
  115. *
  116. * @param {number} level
  117. * The current level number to be assigned to the list elements.
  118. */
  119. function markListLevels($lists, level) {
  120. level = (!level) ? 1 : level;
  121. const $lis = $lists.children('li').addClass(`level-${level}`);
  122. $lists = $lis.children('ul');
  123. if ($lists.length) {
  124. markListLevels($lists, level + 1);
  125. }
  126. }
  127. /**
  128. * On page load, open the active menu item.
  129. *
  130. * Marks the trail of the active link in the menu back to the root of the
  131. * menu with .menu-item--active-trail.
  132. *
  133. * @param {jQuery} $menu
  134. * The root of the menu.
  135. */
  136. function openActiveItem($menu) {
  137. const pathItem = $menu.find(`a[href="${location.pathname}"]`);
  138. if (pathItem.length && !activeItem) {
  139. activeItem = location.pathname;
  140. }
  141. if (activeItem) {
  142. const $activeItem = $menu.find(`a[href="${activeItem}"]`).addClass('menu-item--active');
  143. const $activeTrail = $activeItem.parentsUntil('.root', 'li').addClass('menu-item--active-trail');
  144. toggleList($activeTrail, true);
  145. }
  146. }
  147. // Return the jQuery object.
  148. return this.each(function (selector) {
  149. const $menu = $(this).once('toolbar-menu');
  150. if ($menu.length) {
  151. // Bind event handlers.
  152. $menu
  153. .on('click.toolbar', '.toolbar-box', toggleClickHandler)
  154. .on('click.toolbar', '.toolbar-box a', linkClickHandler);
  155. $menu.addClass('root');
  156. initItems($menu);
  157. markListLevels($menu);
  158. // Restore previous and active states.
  159. openActiveItem($menu);
  160. }
  161. });
  162. };
  163. /**
  164. * A toggle is an interactive element often bound to a click handler.
  165. *
  166. * @param {object} options
  167. * Options for the button.
  168. * @param {string} options.class
  169. * Class to set on the button.
  170. * @param {string} options.action
  171. * Action for the button.
  172. * @param {string} options.text
  173. * Used as label for the button.
  174. *
  175. * @return {string}
  176. * A string representing a DOM fragment.
  177. */
  178. Drupal.theme.toolbarMenuItemToggle = function (options) {
  179. return `<button class="${options.class}"><span class="action">${options.action}</span><span class="label">${options.text}</span></button>`;
  180. };
  181. }(jQuery, Drupal, drupalSettings));