| 1 |
/** |
| 2 |
* Provides functionality for tabbed interfaces. |
| 3 |
* |
| 4 |
* @package WPZincDashboardWidget |
| 5 |
* @author WP Zinc |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Initializes tabbed interfaces |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
function wp_zinc_tabs_init() { |
| 14 |
|
| 15 |
( function ( $ ) { |
| 16 |
|
| 17 |
// Safely call this function by destroying any previously initialized instances. |
| 18 |
wp_zinc_tabs_destroy(); |
| 19 |
|
| 20 |
// Iterate through all JS tab instances, initializing each one. |
| 21 |
$( '.wpzinc-js-tabs' ).each( |
| 22 |
function () { |
| 23 |
|
| 24 |
var nav_tab_container = $( this ), |
| 25 |
nav_tab_panels_container = $( nav_tab_container ).data( 'panels-container' ), |
| 26 |
nav_tab_panel = $( nav_tab_container ).data( 'panel' ), |
| 27 |
nav_tab_active = $( nav_tab_container ).data( 'active' ), |
| 28 |
match_height = $( nav_tab_container ).data( 'match-height' ); |
| 29 |
|
| 30 |
// Call update. |
| 31 |
wp_zinc_tabs_update( nav_tab_container, nav_tab_panels_container, nav_tab_panel, nav_tab_active, $( 'a.' + nav_tab_active, $( nav_tab_container ) ).attr( 'href' ) ); |
| 32 |
|
| 33 |
// If fix height is set, define the height of the content areas to match the parent of the nav tab container. |
| 34 |
if ( typeof match_height !== 'undefined' ) { |
| 35 |
$( nav_tab_panels_container ).height( $( match_height ).innerHeight() ); |
| 36 |
} |
| 37 |
|
| 38 |
// Register a listener when a tab is clicked. |
| 39 |
$( nav_tab_container ).on( |
| 40 |
'click.wpzinc_tabs', |
| 41 |
'a', |
| 42 |
function ( e ) { |
| 43 |
|
| 44 |
// Don't do anything if this is an external URL. |
| 45 |
if ( location.hostname === this.hostname || ! this.hostname.length ) { |
| 46 |
// Local. |
| 47 |
e.preventDefault(); |
| 48 |
} else { |
| 49 |
// External. |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
// Update the UI to show the active tab and content associated with it. |
| 54 |
wp_zinc_tabs_update( nav_tab_container, nav_tab_panels_container, nav_tab_panel, nav_tab_active, $( this ).attr( 'href' ) ); |
| 55 |
|
| 56 |
} |
| 57 |
); |
| 58 |
|
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
// Register a listener when a form element has the data-tab attribute, to toggle a tab's enabled/disabled icon. |
| 63 |
$( '.wpzinc-tab-indicator' ).on( |
| 64 |
'change.wpzinc_tab_indicator', |
| 65 |
function ( e ) { |
| 66 |
|
| 67 |
var tab_href = $( this ).data( 'tab' ); |
| 68 |
|
| 69 |
if ( $( this ).is( 'input' ) ) { |
| 70 |
var enabled = $( this ).prop( 'checked' ); |
| 71 |
} else if ( $( this ).is( 'select' ) ) { |
| 72 |
var enabled = $( this ).val(); |
| 73 |
} |
| 74 |
|
| 75 |
// Enable or Disable Tab. |
| 76 |
if ( enabled == 1 ) { |
| 77 |
$( 'a[href=#' + tab_href + ']' ).addClass( 'enabled' ); |
| 78 |
} else { |
| 79 |
$( 'a[href=#' + tab_href + ']' ).removeClass( 'enabled' ); |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
} )( jQuery ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* For the given active tab: |
| 91 |
* - Hides all other content in the group |
| 92 |
* - Shows content associated with the active tab |
| 93 |
* |
| 94 |
* @since 1.0.0 |
| 95 |
* |
| 96 |
* @param object nav_tab_container <ul> Navigation Tab Container |
| 97 |
* @param string nav_tab_panels_container ID of element containing content panel, to display, which associate with the navigation tabs |
| 98 |
* @param string nav_tab_panel Class of elements containing content panels, to hide, which associate with the navigation tabs |
| 99 |
* @param string nav_tab_active Class to apply to the clicked active_tab element |
| 100 |
* @param string active_tab ID of <a> tab which has been selected / clicked |
| 101 |
*/ |
| 102 |
function wp_zinc_tabs_update( nav_tab_container, nav_tab_panels_container, nav_tab_panel, nav_tab_active, active_tab ) { |
| 103 |
|
| 104 |
( function ( $ ) { |
| 105 |
|
| 106 |
// If we don't have an active tab at this point, we don't have any tabs, so bail. |
| 107 |
if ( typeof active_tab == 'undefined' ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
if ( active_tab.length == 0 ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
// Fetch the <a> element that was clicked / selected. |
| 115 |
var link = $( 'a[href="' + active_tab + '"]', $( nav_tab_container ) ); |
| 116 |
|
| 117 |
// Deactivate all tabs in this container. |
| 118 |
$( 'a', $( nav_tab_container ) ).removeClass( nav_tab_active ); |
| 119 |
|
| 120 |
// Hide all panels in this container. |
| 121 |
$( nav_tab_panel, $( nav_tab_panels_container ) ).hide(); |
| 122 |
|
| 123 |
// Activate the clicked / selected tab in this container. |
| 124 |
$( 'a[href="' + active_tab + '"]', $( nav_tab_container ) ).addClass( nav_tab_active ); |
| 125 |
|
| 126 |
// Show the active tab's panels in this container. |
| 127 |
$( active_tab ).show(); |
| 128 |
|
| 129 |
// Update the Documentation tab, if it exists. |
| 130 |
if ( typeof $( link ).data( 'documentation' ) != 'undefined' ) { |
| 131 |
$( 'a.nav-tab.documentation' ).attr( 'href', $( link ).data( 'documentation' ) ); |
| 132 |
} |
| 133 |
|
| 134 |
// Fire a change event, with a slight delay. |
| 135 |
setTimeout( |
| 136 |
function () { |
| 137 |
$( nav_tab_container ).trigger( 'change', link ); |
| 138 |
}, |
| 139 |
500 |
| 140 |
); |
| 141 |
|
| 142 |
} )( jQuery ); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Destroys previously initialized tabbed interfaces, no longer |
| 148 |
* listening to events. |
| 149 |
* |
| 150 |
* @since 1.0.0 |
| 151 |
*/ |
| 152 |
function wp_zinc_tabs_destroy() { |
| 153 |
|
| 154 |
( function ( $ ) { |
| 155 |
|
| 156 |
// Iterate through all JS tab instances, destroying each one. |
| 157 |
$( '.wpzinc-js-tabs' ).each( |
| 158 |
function () { |
| 159 |
|
| 160 |
$( this ).off( 'click.wpzinc_tabs', 'a' ); |
| 161 |
$( this ).off( 'change.wpzinc_tab_indicator' ); |
| 162 |
|
| 163 |
} |
| 164 |
); |
| 165 |
|
| 166 |
} )( jQuery ); |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Tabbed UI |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
*/ |
| 175 |
jQuery( document ).ready( |
| 176 |
function ( $ ) { |
| 177 |
|
| 178 |
/** |
| 179 |
* Tabbed UI |
| 180 |
*/ |
| 181 |
if ( $( '.wpzinc-js-tabs' ).length > 0 ) { |
| 182 |
|
| 183 |
wp_zinc_tabs_init(); |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
); |
| 189 |
|