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