comparison core/modules/toolbar/js/toolbar.menu.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 toggleClickHandler(event) {
18 var $toggle = $(event.target);
19 var $item = $toggle.closest('li');
20
21 toggleList($item);
22
23 var $openItems = $item.siblings().filter('.open');
24 toggleList($openItems, false);
25 }
26
27 function linkClickHandler(event) {
28 if (!Drupal.toolbar.models.toolbarModel.get('isFixed')) {
29 Drupal.toolbar.models.toolbarModel.set('activeTab', null);
30 }
31
32 event.stopPropagation();
33 }
34
35 function toggleList($item, switcher) {
36 var $toggle = $item.children('.toolbar-box').children('.toolbar-handle');
37 switcher = typeof switcher !== 'undefined' ? switcher : !$item.hasClass('open');
38
39 $item.toggleClass('open', switcher);
40
41 $toggle.toggleClass('open', switcher);
42
43 $toggle.find('.action').text(switcher ? ui.handleClose : ui.handleOpen);
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', { '@label': $box.find('a').text() });
60 $item.children('.toolbar-box').append(Drupal.theme('toolbarMenuItemToggle', options));
61 }
62 });
63 }
64
65 function markListLevels($lists, level) {
66 level = !level ? 1 : level;
67 var $lis = $lists.children('li').addClass('level-' + level);
68 $lists = $lis.children('ul');
69 if ($lists.length) {
70 markListLevels($lists, level + 1);
71 }
72 }
73
74 function openActiveItem($menu) {
75 var pathItem = $menu.find('a[href="' + location.pathname + '"]');
76 if (pathItem.length && !activeItem) {
77 activeItem = location.pathname;
78 }
79 if (activeItem) {
80 var $activeItem = $menu.find('a[href="' + activeItem + '"]').addClass('menu-item--active');
81 var $activeTrail = $activeItem.parentsUntil('.root', 'li').addClass('menu-item--active-trail');
82 toggleList($activeTrail, true);
83 }
84 }
85
86 return this.each(function (selector) {
87 var $menu = $(this).once('toolbar-menu');
88 if ($menu.length) {
89 $menu.on('click.toolbar', '.toolbar-box', toggleClickHandler).on('click.toolbar', '.toolbar-box a', linkClickHandler);
90
91 $menu.addClass('root');
92 initItems($menu);
93 markListLevels($menu);
94
95 openActiveItem($menu);
96 }
97 });
98 };
99
100 Drupal.theme.toolbarMenuItemToggle = function (options) {
101 return '<button class="' + options.class + '"><span class="action">' + options.action + '</span><span class="label">' + options.text + '</span></button>';
102 };
103 })(jQuery, Drupal, drupalSettings);