jQuery(function ($) { // ====================================== // Set active heading in ez-toc-widget list // ====================================== var ezTOCWidgetStickyheadings = $('span.ez-toc-section').toArray(); var ezTOCWidgetStickyheadingToListElementLinkMap = ezTOCWidgetStickygetHeadingToListElementLinkMap(ezTOCWidgetStickyheadings); var ezTOCWidgetStickylistElementLinks = $.map(ezTOCWidgetStickyheadingToListElementLinkMap, function (value, key) { return value }); var ezTOCWidgetStickyscrollOffset = ezTOCWidgetStickygetScrollOffset(); ezTOCWidgetStickyactivateSetActiveEzTocListElement(); function ezTOCWidgetStickysetActiveEzTocListElement() { var ezTOCWidgetStickyactiveHeading = ezTOCWidgetStickygetActiveHeading(ezTOCWidgetStickyscrollOffset, ezTOCWidgetStickyheadings); if (ezTOCWidgetStickyactiveHeading) { var ezTOCWidgetStickyactiveListElementLink = ezTOCWidgetStickyheadingToListElementLinkMap[ ezTOCWidgetStickyactiveHeading.id ]; ezTOCWidgetStickyremoveStyleFromNonActiveListElement(ezTOCWidgetStickyactiveListElementLink, ezTOCWidgetStickylistElementLinks); ezTOCWidgetStickysetStyleForActiveListElementElement(ezTOCWidgetStickyactiveListElementLink); } } function ezTOCWidgetStickyactivateSetActiveEzTocListElement() { if (ezTOCWidgetStickyheadings.length > 0 && $('.ez-toc-widget-sticky-container').length) { $(window).on('load resize scroll', ezTOCWidgetStickysetActiveEzTocListElement); } } function ezTOCWidgetStickydeactivateSetActiveEzTocListElement() { $(window).off('load resize scroll', ezTOCWidgetStickysetActiveEzTocListElement); } function ezTOCWidgetStickygetEzTocListElementLinkByHeading(ezTOCWidgetStickyheading) { return $('.ez-toc-widget-sticky-container .ez-toc-widget-sticky-list a[href="#' + $(ezTOCWidgetStickyheading).attr('id') + '"]'); } function ezTOCWidgetStickygetHeadingToListElementLinkMap(ezTOCWidgetStickyheadings) { return ezTOCWidgetStickyheadings.reduce(function (ezTOCWidgetStickymap, ezTOCWidgetStickyheading) { ezTOCWidgetStickymap[ ezTOCWidgetStickyheading.id ] = ezTOCWidgetStickygetEzTocListElementLinkByHeading(ezTOCWidgetStickyheading); return ezTOCWidgetStickymap; }, {}); } function ezTOCWidgetStickygetScrollOffset() { var ezTOCWidgetStickyscrollOffset = 30; // so if smooth offset is off, the correct title is set as active if (typeof ezTOC != 'undefined' && typeof ezTOC.smooth_scroll != 'undefined' && parseInt(ezTOC.smooth_scroll) === 1) { ezTOCWidgetStickyscrollOffset = (typeof ezTOC.scroll_offset != 'undefined') ? parseInt(ezTOC.scroll_offset) : 30; } var adminbar = $('#wpadminbar'); if (adminbar.length) { ezTOCWidgetStickyscrollOffset += adminbar.height(); } return ezTOCWidgetStickyscrollOffset; } function ezTOCWidgetStickygetActiveHeading(ezTOCWidgetStickytopOffset, ezTOCWidgetStickyheadings) { var ezTOCWidgetStickyscrollTop = $(window).scrollTop(); var ezTOCWidgetStickyrelevantOffset = ezTOCWidgetStickyscrollTop + ezTOCWidgetStickytopOffset + 1; var ezTOCWidgetStickyactiveHeading = ezTOCWidgetStickyheadings[ 0 ]; var ezTOCWidgetStickyclosestHeadingAboveOffset = ezTOCWidgetStickyrelevantOffset - $(ezTOCWidgetStickyactiveHeading).offset().top; ezTOCWidgetStickyheadings.forEach(function (ezTOCWidgetStickysection) { var ezTOCWidgetStickytopOffset = ezTOCWidgetStickyrelevantOffset - $(ezTOCWidgetStickysection).offset().top; if (ezTOCWidgetStickytopOffset > 0 && ezTOCWidgetStickytopOffset < ezTOCWidgetStickyclosestHeadingAboveOffset) { ezTOCWidgetStickyclosestHeadingAboveOffset = ezTOCWidgetStickytopOffset; ezTOCWidgetStickyactiveHeading = ezTOCWidgetStickysection; } }); return ezTOCWidgetStickyactiveHeading; } function ezTOCWidgetStickyremoveStyleFromNonActiveListElement(ezTOCWidgetStickyactiveListElementLink, ezTOCWidgetStickylistElementLinks) { ezTOCWidgetStickylistElementLinks.forEach(function (ezTOCWidgetStickylistElementLink) { if (ezTOCWidgetStickyactiveListElementLink !== ezTOCWidgetStickylistElementLink && ezTOCWidgetStickylistElementLink.parent().hasClass('active')) { ezTOCWidgetStickylistElementLink.parent().removeClass('active'); } }); } function ezTOCWidgetStickycorrectActiveListElementBackgroundColorHeight(ezTOCWidgetStickyactiveListElement) { var ezTOCWidgetStickylistElementHeight = ezTOCWidgetStickygetListElementHeightWithoutUlChildren(ezTOCWidgetStickyactiveListElement); ezTOCWidgetStickyaddListElementBackgroundColorHeightStyleToHead(ezTOCWidgetStickylistElementHeight); } function ezTOCWidgetStickygetListElementHeightWithoutUlChildren(ezTOCWidgetStickylistElement) { var $ezTOCWidgetStickylistElement = $(ezTOCWidgetStickylistElement); var ezTOCWidgetStickycontent = $ezTOCWidgetStickylistElement.html(); // Adding list item with class '.active' to get the real height. // When adding a class to an existing element and using jQuery(..).height() directly afterwards, // the height is the 'old' height. The height might change due to text-wraps when setting the text-weight bold for example // When adding a new item, the height is calculated correctly. // But only when it might be visible (so display:none; is not possible...) // But because it get's directly removed afterwards it never will be rendered by the browser // (at least in my tests in FF, Chrome, IE11 and Edge) $ezTOCWidgetStickylistElement.parent().append('