import $ from 'jquery'; require('jquery.easing'); import {StudioPreviewUtil} from '../../_global/scripts/utils/StudioPreviewUtil'; class IHKHeader { constructor(header) { this.header = header.addClass('initiated'); this.window = $(window); this.body = $('body'); this.overlayOpen = false; this.showOpenedNav = this.header.find('.primary').data("show-opened-menu"); const t = this; this.initLogo(); this.initTopLink(); this.initScroll(); this.initSearch(); this.initPrimaryNav(); this.initSecondaryNav(); this.initToggling(); this.initLanguage(); if (this.showOpenedNav) { t.header.find('.toggle-nav').click(); } this.body.on('open-navigation', function (event, data) { if (data.openMenu) { t.header.find('.toggle-nav').click(); } t.initPrimaryNav(data.rootUrl); }); } initLogo() { const t = this; const logo = t.header.find('.logo'); const img = logo.find('img'); const overlay = $('').appendTo(logo); const logoObserver = new ResizeObserver(() => { if (img.height() < 44) { overlay.css('left', img.height() * 2.1); } else { overlay.removeAttr('style'); } }) logoObserver.observe(img.get(0)); }; initTopLink() { const t = this; const toplink = $('').text('Top').prependTo(t.header); toplink.on('click', function (e) { e.preventDefault(); $('html, body').animate({'scrollTop': 0}, Math.round($(window).scrollTop() / 12 + 300), 'easeOutQuad'); }) } initSearch() { const t = this; const formNav = $('
').appendTo(t.header.find('.search form')); t.header.find('nav .secondary').clone().appendTo(formNav); t.header.find('nav .meta').clone().appendTo(formNav); t.header.find('.open-search, .close-search').on('click', function (e) { t.header.toggleClass('search-open').removeClass('nav-open'); if (t.header.hasClass('search-open')) { setTimeout(function () { t.header.find('.search-field').focus(); }, 200) } t.toggleContentScroll(); }) }; initScroll() { const t = this; // State Machine für Scroll-Verhalten t.scrollState = { current: 'top', // 'top', 'scrolling-down', 'scrolled', 'scrolling-up' lastPosition: 0, transitioning: false }; window.addEventListener("scroll", function () { if (t.scrollState.transitioning) return; window.requestAnimationFrame(function () { t.checkScrollWithStateMachine(); }); }, {passive: true}); } checkScrollWithStateMachine() { const t = this; const st = this.window.scrollTop(); const state = t.scrollState; if (t.overlayOpen || $('.gallery-popup.open').length) { return false; } const delta = st - state.lastPosition; const isScrollingDown = delta > 0; const isScrollingUp = delta < 0; switch (state.current) { case 'top': if (st > 100 && isScrollingDown) { t.transitionTo('scrolled'); } break; case 'scrolled': if (st < 30 && isScrollingUp) { t.transitionTo('top'); } break; } state.lastPosition = st; // Toplink logic if (st > this.window.height() * 1.2) { this.header.addClass('show-toplink'); } else { this.header.removeClass('show-toplink'); } } transitionTo(newState) { const t = this; t.scrollState.transitioning = true; if (newState === 'scrolled') { this.header.addClass('scrolled'); this.body.addClass('header-scrolled'); } else if (newState === 'top') { this.header.removeClass('scrolled'); this.body.removeClass('header-scrolled'); } t.scrollState.current = newState; // Transition Lock setTimeout(() => { t.scrollState.transitioning = false; }, 150); } initToggling() { this.header.find('.overlay-holder').on('click touch', this.toggleNavigation.bind(this)); this.header.find('.toggle-nav').on('click touch', this.toggleNavigation.bind(this)); }; toggleNavigation() { const t = this; if (StudioPreviewUtil && StudioPreviewUtil.isStudioPreview()) { $("html").stop().animate({'scrollTop': 0}, 300, 'swing', function () { t.header.toggleClass('nav-open'); t.toggleContentScroll(); }); } else { t.header.toggleClass('nav-open'); t.toggleContentScroll(); } }; initPrimaryNav(optionalRootUrl) { const t = this; const primary = t.header.find('.primary'); $(primary).empty(); t.baseUrl = primary.attr('data-base-url'); t.rootUrl = primary.attr('data-root-url'); if (optionalRootUrl) { t.rootUrl = optionalRootUrl; primary.off('click', 'a'); } primary.append($('