nav-tabs.es6.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @file
  3. * Responsive navigation tabs.
  4. *
  5. * This also supports collapsible navigable is the 'is-collapsible' class is
  6. * added to the main element, and a target element is included.
  7. */
  8. (function ($, Drupal) {
  9. function init(i, tab) {
  10. const $tab = $(tab);
  11. const $target = $tab.find('[data-drupal-nav-tabs-target]');
  12. const isCollapsible = $tab.hasClass('is-collapsible');
  13. function openMenu(e) {
  14. $target.toggleClass('is-open');
  15. }
  16. function handleResize(e) {
  17. $tab.addClass('is-horizontal');
  18. const $tabs = $tab.find('.tabs');
  19. const isHorizontal = $tabs.outerHeight() <= $tabs.find('.tabs__tab').outerHeight();
  20. $tab.toggleClass('is-horizontal', isHorizontal);
  21. if (isCollapsible) {
  22. $tab.toggleClass('is-collapse-enabled', !isHorizontal);
  23. }
  24. if (isHorizontal) {
  25. $target.removeClass('is-open');
  26. }
  27. }
  28. $tab.addClass('position-container is-horizontal-enabled');
  29. $tab.on('click.tabs', '[data-drupal-nav-tabs-trigger]', openMenu);
  30. $(window).on('resize.tabs', Drupal.debounce(handleResize, 150)).trigger('resize.tabs');
  31. }
  32. /**
  33. * Initialise the tabs JS.
  34. */
  35. Drupal.behaviors.navTabs = {
  36. attach(context, settings) {
  37. const $tabs = $(context).find('[data-drupal-nav-tabs]');
  38. if ($tabs.length) {
  39. const notSmartPhone = window.matchMedia('(min-width: 300px)');
  40. if (notSmartPhone.matches) {
  41. $tabs.once('nav-tabs').each(init);
  42. }
  43. }
  44. },
  45. };
  46. }(jQuery, Drupal));