| 1 |
/** |
| 2 |
* Provides functionality for tabbed interfaces. |
| 3 |
* |
| 4 |
* @since 2.2.5 |
| 5 |
* |
| 6 |
* @package ConvertKit |
| 7 |
* @author ConvertKit |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Initializes tabbed interfaces. |
| 12 |
* |
| 13 |
* @since 2.2.5 |
| 14 |
*/ |
| 15 |
function convertKitTabsInit() { |
| 16 |
|
| 17 |
// Safely call this function by destroying any previously initialized instances. |
| 18 |
convertKitTabsDestroy(); |
| 19 |
|
| 20 |
// Iterate through all JS tab instances, initializing each one. |
| 21 |
document.querySelectorAll( '.convertkit-js-tabs' ).forEach( |
| 22 |
function ( navTabContainer ) { |
| 23 |
const navTabPanelsContainer = navTabContainer.dataset.panelsContainer; |
| 24 |
const navTabPanel = navTabContainer.dataset.panel; |
| 25 |
const navTabActive = navTabContainer.dataset.active; |
| 26 |
|
| 27 |
// Call update. |
| 28 |
const activeTabElement = navTabContainer.querySelector( 'a.' + navTabActive ); |
| 29 |
convertKitTabsUpdate( navTabContainer, navTabPanelsContainer, navTabPanel, navTabActive, activeTabElement ? activeTabElement.getAttribute( 'href' ) : null ); |
| 30 |
|
| 31 |
// Register a listener when a tab is clicked. |
| 32 |
navTabContainer.addEventListener( |
| 33 |
'click', |
| 34 |
function ( e ) { |
| 35 |
if ( e.target.tagName === 'A' ) { |
| 36 |
e.preventDefault(); |
| 37 |
|
| 38 |
// Update the UI to show the active tab and content associated with it. |
| 39 |
convertKitTabsUpdate( navTabContainer, navTabPanelsContainer, navTabPanel, navTabActive, e.target.getAttribute( 'href' ) ); |
| 40 |
} |
| 41 |
} |
| 42 |
); |
| 43 |
} |
| 44 |
); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* For the given active tab: |
| 50 |
* - Hides all other content in the group |
| 51 |
* - Shows content associated with the active tab |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @param {Object} navTabContainer <ul> Navigation Tab Container. |
| 56 |
* @param {string} navTabPanelsContainer ID of element containing content panel, to display, which associate with the navigation tabs. |
| 57 |
* @param {string} navTabPanel Class of elements containing content panels, to hide, which associate with the navigation tabs. |
| 58 |
* @param {string} navTabActive Class to apply to the clicked activeTab element. |
| 59 |
* @param {string} activeTab ID of <a> tab which has been selected / clicked. |
| 60 |
*/ |
| 61 |
function convertKitTabsUpdate( navTabContainer, navTabPanelsContainer, navTabPanel, navTabActive, activeTab ) { |
| 62 |
|
| 63 |
// If we don't have an active tab at this point, we don't have any tabs, so bail. |
| 64 |
if ( typeof activeTab === 'undefined' || activeTab === null || activeTab.length === 0 ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
// Deactivate all tabs in this container. |
| 69 |
navTabContainer.querySelectorAll( 'a' ).forEach( tab => tab.classList.remove( navTabActive ) ); |
| 70 |
|
| 71 |
// Hide all panels in this container. |
| 72 |
document.querySelectorAll( navTabPanelsContainer + ' ' + navTabPanel ).forEach( panel => panel.style.display = 'none' ); |
| 73 |
|
| 74 |
// Activate the clicked / selected tab in this container. |
| 75 |
navTabContainer.querySelector( 'a[href="' + activeTab + '"]' ).classList.add( navTabActive ); |
| 76 |
|
| 77 |
// Show the active tab's panels in this container. |
| 78 |
document.querySelector( activeTab ).style.display = 'block'; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Destroys previously initialized tabbed interfaces, no longer |
| 84 |
* listening to events. |
| 85 |
* |
| 86 |
* @since 1.0.0 |
| 87 |
*/ |
| 88 |
function convertKitTabsDestroy() { |
| 89 |
// Iterate through all JS tab instances, destroying each one. |
| 90 |
document.querySelectorAll( '.convertkit-js-tabs' ).forEach( |
| 91 |
function ( tabInstance ) { |
| 92 |
// Remove the click event listener. |
| 93 |
tabInstance.removeEventListener( 'click', tabInstance.clickHandler ); |
| 94 |
// Remove the clickHandler property. |
| 95 |
delete tabInstance.clickHandler; |
| 96 |
} |
| 97 |
); |
| 98 |
} |
| 99 |
|