| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- (function($, v, undefined) {
- "use strict";
- vamtam_greensock_wait( function() {
- var mainHeader = $('header.main-header');
- var main = $( '#main' );
- var body = $( 'body' );
- var header_contents = mainHeader.find( '.header-contents' );
- var menu_toggle = document.getElementById( 'vamtam-megamenu-main-menu-toggle' );
- var original_toggle = document.querySelector( '#main-menu > .mega-menu-wrap > .mega-menu-toggle' );
- // main menu custom toggle
- if ( menu_toggle ) {
- menu_toggle.addEventListener( 'click', function( e ) {
- e.preventDefault();
- requestAnimationFrame( function() {
- var is_open = original_toggle.classList.contains( 'mega-menu-open' );
- menu_toggle.classList.toggle( 'mega-menu-open', ! is_open );
- original_toggle.classList.toggle( 'mega-menu-open', ! is_open );
- } );
- } );
- }
- // overlay menu
- var overlay_menu = document.getElementById( 'vamtam-overlay-menu' );
- var overlay_menu_toggle = document.querySelector( '.vamtam-overlay-menu-toggle' );
- var overlay_open = false;
- var toggle_clone;
- var toggle_overlay_menu = function( e ) {
- e.preventDefault();
- requestAnimationFrame( function() {
- overlay_open = ! overlay_open;
- if ( overlay_open ) {
- toggle_clone = overlay_menu_toggle.cloneNode( true );
- // measure
- var offset = overlay_menu_toggle.getBoundingClientRect();
- // mutate
- document.body.appendChild( toggle_clone );
- Object.assign( toggle_clone.style, {
- position: 'fixed',
- top: offset.top + 'px',
- left: offset.left + 'px',
- } );
- requestAnimationFrame( function() {
- overlay_menu.classList.add( 'open' );
- toggle_clone.classList.add( 'is-active' );
- } );
- } else {
- toggle_clone.classList.remove( 'is-active' );
- overlay_menu.classList.remove( 'open' );
- setTimeout( function() {
- requestAnimationFrame( function() {
- toggle_clone.remove();
- } );
- }, 650 );
- }
- } );
- };
- document.body.addEventListener( 'click', function( e ) {
- var button = e.target.closest( 'button' );
- if ( button && button.classList.contains( 'vamtam-overlay-menu-toggle' ) ) {
- toggle_overlay_menu( e );
- }
- } );
- // add left/right classes to submenus depending on resolution
- var allSubMenus = $( '#main-menu .sub-menu, #top-nav-wrapper .sub-menu' );
- window.addEventListener( 'resize', window.VAMTAM.debounce( function() {
- var winWidth = window.innerWidth;
- allSubMenus.show().removeClass( 'invert-position' ).each( function() {
- if ( $( this ).offset().left + $( this ).width() > winWidth ) {
- $( this ).addClass( 'invert-position' );
- }
- } );
- allSubMenus.css( 'display', '' );
- }, 100 ), false );
- // scrolling below
- var smoothScrollTimer, smoothScrollCallback;
- var smoothScrollListener = function() {
- clearTimeout( smoothScrollTimer );
- smoothScrollTimer = setTimeout( scrollToElComplete, 200 );
- };
- var scrollToElComplete = function() {
- window.removeEventListener( 'scroll', smoothScrollListener, { passive: true } );
- v.blockStickyHeaderAnimation = false;
- setTimeout( function() {
- requestAnimationFrame( function() {
- document.body.classList.remove( 'no-sticky-header-animation-tmp' );
- } );
- }, 50 );
- if ( smoothScrollCallback ) {
- smoothScrollCallback();
- }
- };
- var scrollToEl = function( el, duration, callback ) {
- requestAnimationFrame( function() {
- var el_offset = el.offset().top;
- v.blockStickyHeaderAnimation = true;
- // measure header height
- var header_height = 0;
- if ( mainHeader.hasClass( 'layout-standard' ) || mainHeader.hasClass( 'logo-text-menu' ) ) {
- if ( el_offset >= main.offset().top ) {
- header_height = mainHeader.find( '.second-row-columns' ).height();
- } else {
- header_height = mainHeader.height();
- }
- } else {
- if ( body.hasClass( 'no-sticky-header-animation' ) ) {
- // single line header with a special page template
- header_height = mainHeader.height();
- } else {
- header_height = header_contents.height();
- if ( 'stickyHeader' in v ) {
- v.stickyHeader.singleRowStick();
- }
- // in this case stick the header,
- // we'd like the menu to be visible after scrolling
- document.body.classList.add( 'no-sticky-header-animation-tmp' );
- }
- }
- var scroll_position = el_offset - v.admin_bar_fix - header_height;
- smoothScrollCallback = callback;
- window.addEventListener( 'scroll', smoothScrollListener, { passive: true } );
- window.scroll( { left: 0, top: scroll_position, behavior: 'smooth' } );
- if ( el.attr( 'id' ) ) {
- if ( history.pushState ) {
- history.pushState( null, null, '#' + el.attr( 'id' ) );
- } else {
- window.location.hash = el.attr( 'id' );
- }
- }
- menu_toggle && menu_toggle.classList.remove( 'mega-menu-open' );
- original_toggle && original_toggle.classList.remove( 'mega-menu-open' );
- } );
- };
- window.FLBuilderLayout && Object.assign( window.FLBuilderLayout, {
- /**
- * Monkey patches the built-in animated scroll with a better implementation
- * which does not use jQuery
- */
- _scrollToElement: function( el, callback ) {
- var config = window.FLBuilderLayoutConfig.anchorLinkAnimations;
- if ( el.length ) {
- menu_toggle.classList.toggle( 'mega-menu-open', false );
- original_toggle.classList.toggle( 'mega-menu-open', false );
- scrollToEl( el, config.duration / 1000, callback );
- }
- },
- } );
- $( document.body ).on('click', '.vamtam-animated-page-scroll[href], .vamtam-animated-page-scroll [href], .vamtam-animated-page-scroll [data-href], .mega-vamtam-animated-page-scroll[href], .mega-vamtam-animated-page-scroll [href], .mega-vamtam-animated-page-scroll [data-href]', function(e) {
- var href = $( this ).prop( 'href' ) || $( this ).data( 'href' );
- var el = $( '#' + ( href ).split( "#" )[1] );
- var l = document.createElement('a');
- l.href = href;
- if(el.length && l.pathname === window.location.pathname) {
- menu_toggle.classList.toggle( 'mega-menu-open', false );
- original_toggle.classList.toggle( 'mega-menu-open', false );
- scrollToEl( el );
- e.preventDefault();
- }
- });
- if ( window.location.hash !== "" &&
- (
- $( '.vamtam-animated-page-scroll[href*="' + window.location.hash + '"]' ).length ||
- $( '.vamtam-animated-page-scroll [href*="' + window.location.hash + '"]').length ||
- $( '.vamtam-animated-page-scroll [data-href*="'+window.location.hash+'"]' ).length ||
- $( '.mega-vamtam-animated-page-scroll[href*="' + window.location.hash + '"]' ).length ||
- $( '.mega-vamtam-animated-page-scroll [href*="' + window.location.hash + '"]').length ||
- $( '.mega-vamtam-animated-page-scroll [data-href*="'+window.location.hash+'"]' ).length
- )
- ) {
- var el = $( window.location.hash );
- if ( el.length > 0 ) {
- $( window ).add( 'html, body, #page' ).scrollTop( 0 );
- }
- setTimeout( function() {
- scrollToEl( el );
- }, 400 );
- }
- // adds .current-menu-item classes
- var hashes = [
- // ['top', $('<div></div>'), $('#top')]
- ];
- $('#main-menu').find('.mega-menu, .menu').find('.maybe-current-menu-item, .mega-current-menu-item, .current-menu-item').each(function() {
- var link = $('> a', this);
- if(link.prop('href').indexOf('#') > -1) {
- var link_hash = link.prop('href').split('#')[1];
- if('#'+link_hash !== window.location.hash) {
- $(this).removeClass('mega-current-menu-item current-menu-item');
- }
- hashes.push([link_hash, $(this), $('#'+link_hash)]);
- }
- });
- if ( hashes.length ) {
- var winHeight = 0;
- var documentHeight = 0;
- var prev_upmost_data = null;
- v.addScrollHandler( {
- init: function() {},
- add_current_menu_item: function( hash ) {
- // there may be more than one links with the same hash,
- // so we need to loop over all of the hashes
- for ( var i = 0; i < hashes.length; i++ ) {
- if ( hashes[i][0] === hash ) {
- hashes[i][1][0].classList.add( 'mega-current-menu-item', 'current-menu-item' );
- }
- }
- },
- measure: function( cpos ) {
- winHeight = window.innerHeight;
- documentHeight = document.body.offsetHeight;
- this.upmost = Infinity;
- this.upmost_data = null;
- for ( var i = 0; i < hashes.length; i++ ) {
- var el = hashes[i][2];
- if ( el.length ) {
- var top = el.offset().top + 10;
- if (
- top > cpos &&
- top < this.upmost &&
- (
- top < cpos + winHeight / 2 ||
- ( top < cpos + winHeight && cpos + winHeight === documentHeight )
- )
- ) {
- this.upmost_data = hashes[i];
- this.upmost = top;
- }
- }
- }
- },
- mutate: function( cpos ) {
- for ( var i = 0; i < hashes.length; i++ ) {
- if ( hashes[i][2].length ) {
- hashes[i][1][0].classList.remove( 'mega-current-menu-item', 'current-menu-item' );
- hashes[i][1][0].childNodes[0].blur();
- }
- }
- if ( this.upmost_data ) {
- this.add_current_menu_item( this.upmost_data[0] );
- // attempt to push a state to the history if the current hash is different from the previous one
- if ( 'history' in window && ( prev_upmost_data !== null ? prev_upmost_data[0] : '' ) !== this.upmost_data[0] ) {
- window.history.pushState(
- this.upmost_data[0],
- $( '> a', this.upmost_data[1] ).text(),
- ( cpos !== 0 ? '#' + this.upmost_data[0] : location.href.replace( location.hash, '' ) )
- );
- prev_upmost_data = $.extend({}, this.upmost_data);
- }
- } else if ( this.upmost_data === null && prev_upmost_data !== null ) {
- this.add_current_menu_item( prev_upmost_data[0] );
- }
- }
- } );
- }
- });
- })( jQuery, window.VAMTAM );
|