123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- (function($, Drupal, drupalSettings) {
-
- let activeItem = Drupal.url(drupalSettings.path.currentPath);
- $.fn.drupalToolbarMenu = function() {
- const ui = {
- handleOpen: Drupal.t('Extend'),
- handleClose: Drupal.t('Collapse'),
- };
-
- function toggleList($item, switcher) {
- const $toggle = $item
- .children('.toolbar-box')
- .children('.toolbar-handle');
- switcher =
- typeof switcher !== 'undefined' ? switcher : !$item.hasClass('open');
-
- $item.toggleClass('open', switcher);
-
- $toggle.toggleClass('open', switcher);
-
- $toggle
- .find('.action')
-
- .text(switcher ? ui.handleClose : ui.handleOpen);
- }
-
- function toggleClickHandler(event) {
- const $toggle = $(event.target);
- const $item = $toggle.closest('li');
-
- toggleList($item);
-
- const $openItems = $item.siblings().filter('.open');
- toggleList($openItems, false);
- }
-
- function linkClickHandler(event) {
-
-
-
-
-
- if (!Drupal.toolbar.models.toolbarModel.get('isFixed')) {
- Drupal.toolbar.models.toolbarModel.set('activeTab', null);
- }
-
-
- event.stopPropagation();
- }
-
- function initItems($menu) {
- const options = {
- class: 'toolbar-icon toolbar-handle',
- action: ui.handleOpen,
- text: '',
- };
-
- $menu.find('li > a').wrap('<div class="toolbar-box">');
-
- $menu.find('li').each((index, element) => {
- const $item = $(element);
- if ($item.children('ul.toolbar-menu').length) {
- const $box = $item.children('.toolbar-box');
- options.text = Drupal.t('@label', {
- '@label': $box.find('a').text(),
- });
- $item
- .children('.toolbar-box')
- .append(Drupal.theme('toolbarMenuItemToggle', options));
- }
- });
- }
-
- function markListLevels($lists, level) {
- level = !level ? 1 : level;
- const $lis = $lists.children('li').addClass(`level-${level}`);
- $lists = $lis.children('ul');
- if ($lists.length) {
- markListLevels($lists, level + 1);
- }
- }
-
- function openActiveItem($menu) {
- const pathItem = $menu.find(`a[href="${window.location.pathname}"]`);
- if (pathItem.length && !activeItem) {
- activeItem = window.location.pathname;
- }
- if (activeItem) {
- const $activeItem = $menu
- .find(`a[href="${activeItem}"]`)
- .addClass('menu-item--active');
- const $activeTrail = $activeItem
- .parentsUntil('.root', 'li')
- .addClass('menu-item--active-trail');
- toggleList($activeTrail, true);
- }
- }
-
- return this.each(function(selector) {
- const $menu = $(this).once('toolbar-menu');
- if ($menu.length) {
-
- $menu
- .on('click.toolbar', '.toolbar-box', toggleClickHandler)
- .on('click.toolbar', '.toolbar-box a', linkClickHandler);
- $menu.addClass('root');
- initItems($menu);
- markListLevels($menu);
-
- openActiveItem($menu);
- }
- });
- };
-
- Drupal.theme.toolbarMenuItemToggle = function(options) {
- return `<button class="${options.class}"><span class="action">${options.action}</span> <span class="label">${options.text}</span></button>`;
- };
- })(jQuery, Drupal, drupalSettings);
|