5f7345d8d4e80d1aa8f654049a7010e4f17d8797
[yaffs-website] / web / core / modules / toolbar / js / toolbar.menu.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, drupalSettings) {
9   var activeItem = Drupal.url(drupalSettings.path.currentPath);
10
11   $.fn.drupalToolbarMenu = function () {
12     var ui = {
13       handleOpen: Drupal.t('Extend'),
14       handleClose: Drupal.t('Collapse')
15     };
16
17     function toggleList($item, switcher) {
18       var $toggle = $item.children('.toolbar-box').children('.toolbar-handle');
19       switcher = typeof switcher !== 'undefined' ? switcher : !$item.hasClass('open');
20
21       $item.toggleClass('open', switcher);
22
23       $toggle.toggleClass('open', switcher);
24
25       $toggle.find('.action').text(switcher ? ui.handleClose : ui.handleOpen);
26     }
27
28     function toggleClickHandler(event) {
29       var $toggle = $(event.target);
30       var $item = $toggle.closest('li');
31
32       toggleList($item);
33
34       var $openItems = $item.siblings().filter('.open');
35       toggleList($openItems, false);
36     }
37
38     function linkClickHandler(event) {
39       if (!Drupal.toolbar.models.toolbarModel.get('isFixed')) {
40         Drupal.toolbar.models.toolbarModel.set('activeTab', null);
41       }
42
43       event.stopPropagation();
44     }
45
46     function initItems($menu) {
47       var options = {
48         class: 'toolbar-icon toolbar-handle',
49         action: ui.handleOpen,
50         text: ''
51       };
52
53       $menu.find('li > a').wrap('<div class="toolbar-box">');
54
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()
61           });
62           $item.children('.toolbar-box').append(Drupal.theme('toolbarMenuItemToggle', options));
63         }
64       });
65     }
66
67     function markListLevels($lists, level) {
68       level = !level ? 1 : level;
69       var $lis = $lists.children('li').addClass('level-' + level);
70       $lists = $lis.children('ul');
71       if ($lists.length) {
72         markListLevels($lists, level + 1);
73       }
74     }
75
76     function openActiveItem($menu) {
77       var pathItem = $menu.find('a[href="' + window.location.pathname + '"]');
78       if (pathItem.length && !activeItem) {
79         activeItem = window.location.pathname;
80       }
81       if (activeItem) {
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);
85       }
86     }
87
88     return this.each(function (selector) {
89       var $menu = $(this).once('toolbar-menu');
90       if ($menu.length) {
91         $menu.on('click.toolbar', '.toolbar-box', toggleClickHandler).on('click.toolbar', '.toolbar-box a', linkClickHandler);
92
93         $menu.addClass('root');
94         initItems($menu);
95         markListLevels($menu);
96
97         openActiveItem($menu);
98       }
99     });
100   };
101
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>';
104   };
105 })(jQuery, Drupal, drupalSettings);