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