nav-tabs.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. 'use strict';
  10. function init(i, tab) {
  11. var $tab = $(tab);
  12. var $target = $tab.find('[data-drupal-nav-tabs-target]');
  13. var 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. var $tabs = $tab.find('.tabs');
  20. var 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: function (context, settings) {
  38. var $tabs = $(context).find('[data-drupal-nav-tabs]');
  39. if ($tabs.length) {
  40. var notSmartPhone = window.matchMedia('(min-width: 300px)');
  41. if (notSmartPhone.matches) {
  42. $tabs.once('nav-tabs').each(init);
  43. }
  44. }
  45. }
  46. };
  47. })(jQuery, Drupal);