2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function ($, Drupal, drupalSettings) {
9 var activeItem = Drupal.url(drupalSettings.path.currentPath);
11 $.fn.drupalToolbarMenu = function () {
13 handleOpen: Drupal.t('Extend'),
14 handleClose: Drupal.t('Collapse')
17 function toggleList($item, switcher) {
18 var $toggle = $item.children('.toolbar-box').children('.toolbar-handle');
19 switcher = typeof switcher !== 'undefined' ? switcher : !$item.hasClass('open');
21 $item.toggleClass('open', switcher);
23 $toggle.toggleClass('open', switcher);
25 $toggle.find('.action').text(switcher ? ui.handleClose : ui.handleOpen);
28 function toggleClickHandler(event) {
29 var $toggle = $(event.target);
30 var $item = $toggle.closest('li');
34 var $openItems = $item.siblings().filter('.open');
35 toggleList($openItems, false);
38 function linkClickHandler(event) {
39 if (!Drupal.toolbar.models.toolbarModel.get('isFixed')) {
40 Drupal.toolbar.models.toolbarModel.set('activeTab', null);
43 event.stopPropagation();
46 function initItems($menu) {
48 class: 'toolbar-icon toolbar-handle',
49 action: ui.handleOpen,
53 $menu.find('li > a').wrap('<div class="toolbar-box">');
55 $menu.find('li').each(function (index, element) {
56 var $item = $(element);
57 if ($item.children('ul.toolbar-menu').length) {
58 var $box = $item.children('.toolbar-box');
59 options.text = Drupal.t('@label', {
60 '@label': $box.find('a').text()
62 $item.children('.toolbar-box').append(Drupal.theme('toolbarMenuItemToggle', options));
67 function markListLevels($lists, level) {
68 level = !level ? 1 : level;
69 var $lis = $lists.children('li').addClass('level-' + level);
70 $lists = $lis.children('ul');
72 markListLevels($lists, level + 1);
76 function openActiveItem($menu) {
77 var pathItem = $menu.find('a[href="' + window.location.pathname + '"]');
78 if (pathItem.length && !activeItem) {
79 activeItem = window.location.pathname;
82 var $activeItem = $menu.find('a[href="' + activeItem + '"]').addClass('menu-item--active');
83 var $activeTrail = $activeItem.parentsUntil('.root', 'li').addClass('menu-item--active-trail');
84 toggleList($activeTrail, true);
88 return this.each(function (selector) {
89 var $menu = $(this).once('toolbar-menu');
91 $menu.on('click.toolbar', '.toolbar-box', toggleClickHandler).on('click.toolbar', '.toolbar-box a', linkClickHandler);
93 $menu.addClass('root');
95 markListLevels($menu);
97 openActiveItem($menu);
102 Drupal.theme.toolbarMenuItemToggle = function (options) {
103 return '<button class="' + options.class + '"><span class="action">' + options.action + '</span> <span class="label">' + options.text + '</span></button>';
105 })(jQuery, Drupal, drupalSettings);