navigation-utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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) {
  8. Drupal.olivero = {};
  9. function isDesktopNav() {
  10. var navButtons = document.querySelector('[data-drupal-selector="mobile-buttons"]');
  11. return navButtons ? window.getComputedStyle(navButtons).getPropertyValue('display') === 'none' : false;
  12. }
  13. Drupal.olivero.isDesktopNav = isDesktopNav;
  14. var stickyHeaderToggleButton = document.querySelector('[data-drupal-selector="sticky-header-toggle"]');
  15. var siteHeaderFixable = document.querySelector('[data-drupal-selector="site-header-fixable"]');
  16. function stickyHeaderIsEnabled() {
  17. return stickyHeaderToggleButton.getAttribute('aria-checked') === 'true';
  18. }
  19. function setStickyHeaderStorage(expandedState) {
  20. var now = new Date();
  21. var item = {
  22. value: expandedState,
  23. expiry: now.getTime() + 20160000
  24. };
  25. localStorage.setItem('Drupal.olivero.stickyHeaderState', JSON.stringify(item));
  26. }
  27. function toggleStickyHeaderState(pinnedState) {
  28. if (isDesktopNav()) {
  29. if (pinnedState === true) {
  30. siteHeaderFixable.classList.add('is-expanded');
  31. } else {
  32. siteHeaderFixable.classList.remove('is-expanded');
  33. }
  34. stickyHeaderToggleButton.setAttribute('aria-checked', pinnedState);
  35. setStickyHeaderStorage(pinnedState);
  36. }
  37. }
  38. function getStickyHeaderStorage() {
  39. var stickyHeaderState = localStorage.getItem('Drupal.olivero.stickyHeaderState');
  40. if (!stickyHeaderState) return false;
  41. var item = JSON.parse(stickyHeaderState);
  42. var now = new Date();
  43. if (now.getTime() > item.expiry) {
  44. localStorage.removeItem('Drupal.olivero.stickyHeaderState');
  45. return false;
  46. }
  47. return item.value;
  48. }
  49. if ('IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
  50. var fixableElements = document.querySelectorAll('[data-drupal-selector="site-header-fixable"], [data-drupal-selector="social-bar-inner"]');
  51. function toggleDesktopNavVisibility(entries) {
  52. if (!isDesktopNav()) return;
  53. entries.forEach(function (entry) {
  54. if (entry.intersectionRatio < 1) {
  55. fixableElements.forEach(function (el) {
  56. return el.classList.add('is-fixed');
  57. });
  58. } else {
  59. fixableElements.forEach(function (el) {
  60. return el.classList.remove('is-fixed');
  61. });
  62. }
  63. });
  64. }
  65. function getRootMargin() {
  66. var rootMarginTop = 72;
  67. var _document = document,
  68. body = _document.body;
  69. if (body.classList.contains('toolbar-fixed')) {
  70. rootMarginTop -= 39;
  71. }
  72. if (body.classList.contains('toolbar-horizontal') && body.classList.contains('toolbar-tray-open')) {
  73. rootMarginTop -= 40;
  74. }
  75. return "".concat(rootMarginTop, "px 0px 0px 0px");
  76. }
  77. function monitorNavPosition() {
  78. var primaryNav = document.querySelector('[data-drupal-selector="site-header"]');
  79. var options = {
  80. rootMargin: getRootMargin(),
  81. threshold: [0.999, 1]
  82. };
  83. var observer = new IntersectionObserver(toggleDesktopNavVisibility, options);
  84. if (primaryNav) {
  85. observer.observe(primaryNav);
  86. }
  87. }
  88. if (stickyHeaderToggleButton) {
  89. stickyHeaderToggleButton.addEventListener('click', function () {
  90. toggleStickyHeaderState(!stickyHeaderIsEnabled());
  91. });
  92. }
  93. var siteHeaderInner = document.querySelector('[data-drupal-selector="site-header-inner"]');
  94. if (siteHeaderInner) {
  95. siteHeaderInner.addEventListener('focusin', function () {
  96. if (isDesktopNav() && !stickyHeaderIsEnabled()) {
  97. var header = document.querySelector('[data-drupal-selector="site-header"]');
  98. var headerNav = header.querySelector('[data-drupal-selector="header-nav"]');
  99. var headerMargin = header.clientHeight - headerNav.clientHeight;
  100. if (window.scrollY > headerMargin) {
  101. window.scrollTo(0, headerMargin);
  102. }
  103. }
  104. });
  105. }
  106. monitorNavPosition();
  107. setStickyHeaderStorage(getStickyHeaderStorage());
  108. toggleStickyHeaderState(getStickyHeaderStorage());
  109. }
  110. })(Drupal);