| 1 |
/** |
| 2 |
* Plugin: Tabs |
| 3 |
*/ |
| 4 |
( function( $ ) { |
| 5 |
const adminTabs = function( $el, options ) { |
| 6 |
let $tabs = $el.find( '.tabs-nav' ).find( 'li' ), |
| 7 |
$tabsWrap = $tabs.parent(), |
| 8 |
$contents = $el.find( '.tabs-content-container > li' ), |
| 9 |
$currentTab = null, |
| 10 |
$currentContent = null; |
| 11 |
|
| 12 |
function selectTab( $tab ) { |
| 13 |
const index = $tabs.index( $tab ), |
| 14 |
url = $tab.find( 'a' ).attr( 'href' ); |
| 15 |
|
| 16 |
$currentContent = $contents.eq( index ); |
| 17 |
|
| 18 |
$tab.addClass( 'active' ).siblings( 'li.active' ).removeClass( 'active' ); |
| 19 |
$currentContent.show().css( { visibility: 'hidden' } ); |
| 20 |
calculateHeight( $currentContent ); |
| 21 |
$currentContent.hide(); |
| 22 |
$currentContent.show(); |
| 23 |
$currentContent.siblings( 'li.active' ).fadeOut( 0, function() { |
| 24 |
$currentContent.addClass( 'active' ).siblings( 'li.active' ).removeClass( 'active' ); |
| 25 |
} ); |
| 26 |
|
| 27 |
LP.setUrl( url ); |
| 28 |
} |
| 29 |
|
| 30 |
function calculateHeight( $currentContent ) { |
| 31 |
let contentHeight = $currentContent.height(), |
| 32 |
tabsHeight = $tabsWrap.outerHeight(); |
| 33 |
|
| 34 |
if ( contentHeight < tabsHeight ) { |
| 35 |
contentHeight = tabsHeight + parseInt( $tabsWrap.css( 'margin' ) ) * 2; |
| 36 |
} else { |
| 37 |
contentHeight = undefined; |
| 38 |
} |
| 39 |
$currentContent.css( 'visibility', '' ).css( { height: contentHeight } ); |
| 40 |
} |
| 41 |
|
| 42 |
function selectDefaultTab() { |
| 43 |
$currentTab = $tabs.filter( '.active' ); |
| 44 |
if ( ! $currentTab.length ) { |
| 45 |
$currentTab = $tabs.first(); |
| 46 |
} |
| 47 |
$currentTab.find( 'a' ).trigger( 'click' ); |
| 48 |
} |
| 49 |
|
| 50 |
function initEvents() { |
| 51 |
$el.on( 'click', '.tabs-nav a', function( event ) { |
| 52 |
event.preventDefault(); |
| 53 |
$currentTab = $( this ).parent(); |
| 54 |
selectTab( $currentTab ); |
| 55 |
} ); |
| 56 |
} |
| 57 |
|
| 58 |
function init() { |
| 59 |
initEvents(); |
| 60 |
selectDefaultTab(); |
| 61 |
$( window ).on( 'resize.calculate-tab', function() { |
| 62 |
const $currentContent = $el.find( '.tabs-content-container .active' ).css( 'height', '' ); |
| 63 |
calculateHeight( $currentContent ); |
| 64 |
} ); |
| 65 |
} |
| 66 |
|
| 67 |
init(); |
| 68 |
}; |
| 69 |
$.fn.LP( 'AdminTab', function( options ) { |
| 70 |
options = $.extend( {}, options || {} ); |
| 71 |
return $.each( this, function() { |
| 72 |
let $el = $( this ), |
| 73 |
tabs = $el.data( 'learn-press/tabs' ); |
| 74 |
|
| 75 |
if ( ! tabs ) { |
| 76 |
tabs = new adminTabs( $el, options ); |
| 77 |
$el.data( 'learn-press/tabs', tabs ); |
| 78 |
} |
| 79 |
return $el; |
| 80 |
} ); |
| 81 |
} ); |
| 82 |
}( jQuery ) ); |
| 83 |
|