No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

176 líneas
4.5 KiB

  1. import $ from 'jquery';
  2. require('jquery.easing');
  3. import {StudioPreviewUtil} from '../../_global/scripts/utils/StudioPreviewUtil';
  4. class IHKHeader {
  5. constructor(header) {
  6. this.header = header.addClass('initiated');
  7. this.window = $(window);
  8. this.body = $('body');
  9. this.overlayOpen = false;
  10. this.showOpenedNav = this.header.find('.primary').data("show-opened-menu");
  11. const t = this;
  12. this.initLogo();
  13. this.initTopLink();
  14. this.initScroll();
  15. this.initSearch();
  16. this.initToggling();
  17. this.initLanguage();
  18. if(this.showOpenedNav){
  19. t.header.find('.toggle-nav').click();
  20. }
  21. this.body.on('open-navigation', function (event, data) {
  22. if (data.openMenu) {
  23. t.header.find('.toggle-nav').click();
  24. }
  25. });
  26. }
  27. initLogo() {
  28. const t = this;
  29. const logo = t.header.find('.logo');
  30. const img = logo.find('img');
  31. const overlay = $('<span class="logo-overlay" />').appendTo(logo);
  32. const logoObserver = new ResizeObserver(() => {
  33. if (img.height() < 44) {
  34. overlay.css('left', img.height() * 2.1);
  35. } else {
  36. overlay.removeAttr('style');
  37. }
  38. })
  39. logoObserver.observe(img.get(0));
  40. };
  41. initTopLink() {
  42. const t = this;
  43. const toplink = $('<a href="#" class="toplink" tabindex="-1" />').text('Top').prependTo(t.header);
  44. toplink.on('click', function (e) {
  45. e.preventDefault();
  46. $('html, body').animate({'scrollTop': 0}, Math.round($(window).scrollTop() / 12 + 300), 'easeOutQuad');
  47. })
  48. }
  49. initSearch() {
  50. const t = this;
  51. const formNav = $('<div class="form-nav" />').appendTo(t.header.find('.search form'));
  52. t.header.find('nav .secondary').clone().appendTo(formNav);
  53. t.header.find('nav .meta').clone().appendTo(formNav);
  54. t.header.find('.open-search, .close-search').on('click', function (e) {
  55. t.header.toggleClass('search-open').removeClass('nav-open');
  56. if (t.header.hasClass('search-open')) {
  57. setTimeout(function () {
  58. t.header.find('.search-field').focus();
  59. }, 200)
  60. }
  61. t.toggleContentScroll();
  62. })
  63. };
  64. initScroll() {
  65. const t = this;
  66. window.addEventListener("scroll", function () {
  67. window.requestAnimationFrame(function () {
  68. t.checkScroll();
  69. })
  70. }, {passive: true});
  71. };
  72. checkScroll() {
  73. const t = this;
  74. const st = this.window.scrollTop();
  75. if (t.overlayOpen || $('.gallery-popup.open').length) {
  76. return false;
  77. }
  78. if (st > 59) {
  79. this.header.addClass('scrolled');
  80. this.body.addClass('header-scrolled');
  81. } else {
  82. this.header.removeClass('scrolled');
  83. this.body.removeClass('header-scrolled');
  84. }
  85. if (st > this.window.height() * 1.2) {
  86. this.header.addClass('show-toplink');
  87. }
  88. else {
  89. this.header.removeClass('show-toplink');
  90. }
  91. }
  92. initToggling() {
  93. this.header.find('.overlay-holder').on('click touch', this.toggleNavigation.bind(this));
  94. this.header.find('.toggle-nav').on('click touch', this.toggleNavigation.bind(this));
  95. };
  96. toggleNavigation() {
  97. const t = this;
  98. if (StudioPreviewUtil && StudioPreviewUtil.isStudioPreview()) {
  99. $("html").stop().animate({'scrollTop': 0}, 300, 'swing', function () {
  100. t.header.toggleClass('nav-open');
  101. t.toggleContentScroll();
  102. });
  103. } else {
  104. t.header.toggleClass('nav-open');
  105. t.toggleContentScroll();
  106. }
  107. };
  108. toggleContentScroll() {
  109. const t = this;
  110. if (t.header.hasClass('nav-open') || t.header.hasClass('search-open')) {
  111. t.overlayOpen = true;
  112. this.body.trigger('overlay-open');
  113. t.body.css('top', (t.window.scrollTop() * -1) + 'px').addClass('nav-open');
  114. if (t.header.hasClass('search-open')) {
  115. t.body.addClass('search-open');
  116. }
  117. } else {
  118. t.overlayOpen = false;
  119. this.body.trigger('overlay-close');
  120. const top = Math.abs(parseInt(t.body.css('top')));
  121. t.body.removeClass('nav-open').removeClass('search-open').removeAttr('style');
  122. t.window.scrollTop(top);
  123. }
  124. }
  125. initLanguage() {
  126. const lang = this.header.find('.language');
  127. lang.find('button').on('click', function (e) {
  128. lang.toggleClass('open');
  129. })
  130. lang.on('click', function (e) {
  131. e.stopPropagation();
  132. })
  133. this.body.on('click', function () {
  134. lang.removeClass('open');
  135. })
  136. lang.siblings().find('a').on('focus', function () {
  137. lang.removeClass('open');
  138. })
  139. }
  140. }
  141. export default IHKHeader;
  142. $('body').on('ihk-init', function () {
  143. $('.page-header:not(.initiated)').each((i, el) => {
  144. new IHKHeader($(el));
  145. });
  146. });