const $ = require('jquery'); import {StudioPreviewUtil} from "../utils/StudioPreviewUtil"; import IHK from '../ihk'; import 'jquery.easing'; class Header { 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 browser = IHK.getBrowser(); t.resizeTimeout = 0; t.window.on('resize load', function () { if (img.height() < 44) { overlay.css('left', img.height() * 2.1); } }) if (browser.name === 'ie') { img.attr('src', img.attr('src').split('svg').join('png')); } }; 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; window.addEventListener("scroll", function () { window.requestAnimationFrame(function () { t.checkScroll(); }) }, {passive: true}); }; checkScroll() { const t = this; const st = t.window.scrollTop(); if (t.overlayOpen || $('.gallery-popup.open').length) { return false; } if (st > 59) { t.header.addClass('scrolled'); } else { t.header.removeClass('scrolled'); } if (st > t.window.height() * 1.2) { t.header.addClass('show-toplink'); } else { t.header.removeClass('show-toplink'); } } 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.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($('