toolbar.menu.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. (function ($, Drupal, drupalSettings) {
  8. var activeItem = Drupal.url(drupalSettings.path.currentPath);
  9. $.fn.drupalToolbarMenu = function () {
  10. var ui = {
  11. handleOpen: Drupal.t('Extend'),
  12. handleClose: Drupal.t('Collapse')
  13. };
  14. function toggleClickHandler(event) {
  15. var $toggle = $(event.target);
  16. var $item = $toggle.closest('li');
  17. toggleList($item);
  18. var $openItems = $item.siblings().filter('.open');
  19. toggleList($openItems, false);
  20. }
  21. function linkClickHandler(event) {
  22. if (!Drupal.toolbar.models.toolbarModel.get('isFixed')) {
  23. Drupal.toolbar.models.toolbarModel.set('activeTab', null);
  24. }
  25. event.stopPropagation();
  26. }
  27. function toggleList($item, switcher) {
  28. var $toggle = $item.children('.toolbar-box').children('.toolbar-handle');
  29. switcher = typeof switcher !== 'undefined' ? switcher : !$item.hasClass('open');
  30. $item.toggleClass('open', switcher);
  31. $toggle.toggleClass('open', switcher);
  32. $toggle.find('.action').text(switcher ? ui.handleClose : ui.handleOpen);
  33. }
  34. function initItems($menu) {
  35. var options = {
  36. class: 'toolbar-icon toolbar-handle',
  37. action: ui.handleOpen,
  38. text: ''
  39. };
  40. $menu.find('li > a').wrap('<div class="toolbar-box">');
  41. $menu.find('li').each(function (index, element) {
  42. var $item = $(element);
  43. if ($item.children('ul.toolbar-menu').length) {
  44. var $box = $item.children('.toolbar-box');
  45. options.text = Drupal.t('@label', { '@label': $box.find('a').text() });
  46. $item.children('.toolbar-box').append(Drupal.theme('toolbarMenuItemToggle', options));
  47. }
  48. });
  49. }
  50. function markListLevels($lists, level) {
  51. level = !level ? 1 : level;
  52. var $lis = $lists.children('li').addClass('level-' + level);
  53. $lists = $lis.children('ul');
  54. if ($lists.length) {
  55. markListLevels($lists, level + 1);
  56. }
  57. }
  58. function openActiveItem($menu) {
  59. var pathItem = $menu.find('a[href="' + location.pathname + '"]');
  60. if (pathItem.length && !activeItem) {
  61. activeItem = location.pathname;
  62. }
  63. if (activeItem) {
  64. var $activeItem = $menu.find('a[href="' + activeItem + '"]').addClass('menu-item--active');
  65. var $activeTrail = $activeItem.parentsUntil('.root', 'li').addClass('menu-item--active-trail');
  66. toggleList($activeTrail, true);
  67. }
  68. }
  69. return this.each(function (selector) {
  70. var $menu = $(this).once('toolbar-menu');
  71. if ($menu.length) {
  72. $menu.on('click.toolbar', '.toolbar-box', toggleClickHandler).on('click.toolbar', '.toolbar-box a', linkClickHandler);
  73. $menu.addClass('root');
  74. initItems($menu);
  75. markListLevels($menu);
  76. openActiveItem($menu);
  77. }
  78. });
  79. };
  80. Drupal.theme.toolbarMenuItemToggle = function (options) {
  81. return '<button class="' + options.class + '"><span class="action">' + options.action + '</span><span class="label">' + options.text + '</span></button>';
  82. };
  83. })(jQuery, Drupal, drupalSettings);