toolbar.menu.js 3.2 KB

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