| 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 |
( function ( $ ) { |
| 18 |
|
| 19 |
// Safely call this function by destroying any previously initialized instances. |
| 20 |
convertKitTabsDestroy(); |
| 21 |
|
| 22 |
// Iterate through all JS tab instances, initializing each one. |
| 23 |
$( '.convertkit-js-tabs' ).each( |
| 24 |
function () { |
| 25 |
|
| 26 |
const nav_tab_container = $( this ), |
| 27 |
nav_tab_panels_container = $( nav_tab_container ).data( 'panels-container' ), |
| 28 |
nav_tab_panel = $( nav_tab_container ).data( 'panel' ), |
| 29 |
nav_tab_active = $( nav_tab_container ).data( 'active' ), |
| 30 |
match_height = $( nav_tab_container ).data( 'match-height' ); |
| 31 |
|
| 32 |
// Call update. |
| 33 |
convertKitTabsUpdate( nav_tab_container, nav_tab_panels_container, nav_tab_panel, nav_tab_active, $( 'a.' + nav_tab_active, $( nav_tab_container ) ).attr( 'href' ) ); |
| 34 |
|
| 35 |
// If fix height is set, define the height of the content areas to match the parent of the nav tab container. |
| 36 |
if ( typeof match_height !== 'undefined' ) { |
| 37 |
$( nav_tab_panels_container ).height( $( match_height ).innerHeight() ); |
| 38 |
} |
| 39 |
|
| 40 |
// Register a listener when a tab is clicked. |
| 41 |
$( nav_tab_container ).on( |
| 42 |
'click.convertkit_tabs', |
| 43 |
'a', |
| 44 |
function ( e ) { |
| 45 |
|
| 46 |
e.preventDefault(); |
| 47 |
|
| 48 |
// Update the UI to show the active tab and content associated with it. |
| 49 |
convertKitTabsUpdate( nav_tab_container, nav_tab_panels_container, nav_tab_panel, nav_tab_active, $( this ).attr( 'href' ) ); |
| 50 |
|
| 51 |
} |
| 52 |
); |
| 53 |
|
| 54 |
} |
| 55 |
); |
| 56 |
|
| 57 |
} )( jQuery ); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* For the given active tab: |
| 63 |
* - Hides all other content in the group |
| 64 |
* - Shows content associated with the active tab |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
* |
| 68 |
* @param object nav_tab_container <ul> Navigation Tab Container |
| 69 |
* @param string nav_tab_panels_container ID of element containing content panel, to display, which associate with the navigation tabs |
| 70 |
* @param string nav_tab_panel Class of elements containing content panels, to hide, which associate with the navigation tabs |
| 71 |
* @param string nav_tab_active Class to apply to the clicked active_tab element |
| 72 |
* @param string active_tab ID of <a> tab which has been selected / clicked |
| 73 |
*/ |
| 74 |
function convertKitTabsUpdate( nav_tab_container, nav_tab_panels_container, nav_tab_panel, nav_tab_active, active_tab ) { |
| 75 |
|
| 76 |
( function ( $ ) { |
| 77 |
|
| 78 |
// If we don't have an active tab at this point, we don't have any tabs, so bail. |
| 79 |
if ( typeof active_tab === 'undefined' ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
if ( active_tab.length === 0 ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// Deactivate all tabs in this container. |
| 87 |
$( 'a', $( nav_tab_container ) ).removeClass( nav_tab_active ); |
| 88 |
|
| 89 |
// Hide all panels in this container. |
| 90 |
$( nav_tab_panel, $( nav_tab_panels_container ) ).hide(); |
| 91 |
|
| 92 |
// Activate the clicked / selected tab in this container. |
| 93 |
$( 'a[href="' + active_tab + '"]', $( nav_tab_container ) ).addClass( nav_tab_active ); |
| 94 |
|
| 95 |
// Show the active tab's panels in this container. |
| 96 |
$( active_tab ).show(); |
| 97 |
|
| 98 |
} )( jQuery ); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Destroys previously initialized tabbed interfaces, no longer |
| 104 |
* listening to events. |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
*/ |
| 108 |
function convertKitTabsDestroy() { |
| 109 |
|
| 110 |
( function ( $ ) { |
| 111 |
|
| 112 |
// Iterate through all JS tab instances, destroying each one. |
| 113 |
$( '.convertkit-js-tabs' ).each( |
| 114 |
function () { |
| 115 |
|
| 116 |
$( this ).off( 'click.convertkit_tabs', 'a' ); |
| 117 |
|
| 118 |
} |
| 119 |
); |
| 120 |
|
| 121 |
} )( jQuery ); |
| 122 |
|
| 123 |
} |
| 124 |
|