nav-tabs.es6.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. /*eslint-disable */
  9. (function ($, Drupal) {
  10. function init(i, tab) {
  11. const $tab = $(tab);
  12. const $target = $tab.find('[data-drupal-nav-tabs-target]');
  13. const isCollapsible = $tab.hasClass('is-collapsible');
  14. function openMenu(e) {
  15. $target.toggleClass('is-open');
  16. }
  17. function handleResize(e) {
  18. $tab.addClass('is-horizontal');
  19. const $tabs = $tab.find('.tabs');
  20. const isHorizontal = $tabs.outerHeight() <= $tabs.find('.tabs__tab').outerHeight();
  21. $tab.toggleClass('is-horizontal', isHorizontal);
  22. if (isCollapsible) {
  23. $tab.toggleClass('is-collapse-enabled', !isHorizontal);
  24. }
  25. if (isHorizontal) {
  26. $target.removeClass('is-open');
  27. }
  28. }
  29. $tab.addClass('position-container is-horizontal-enabled');
  30. $tab.on('click.tabs', '[data-drupal-nav-tabs-trigger]', openMenu);
  31. $(window).on('resize.tabs', Drupal.debounce(handleResize, 150)).trigger('resize.tabs');
  32. }
  33. /**
  34. * Initialise the tabs JS.
  35. */
  36. Drupal.behaviors.navTabs = {
  37. attach(context, settings) {
  38. const $tabs = $(context).find('[data-drupal-nav-tabs]');
  39. if ($tabs.length) {
  40. const notSmartPhone = window.matchMedia('(min-width: 300px)');
  41. if (notSmartPhone.matches) {
  42. $tabs.once('nav-tabs').each(init);
  43. }
  44. }
  45. }
  46. };
  47. }(jQuery, Drupal));