-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tabbed-codeblock): Resize active codeblock when displayed
- fixes #572
- Loading branch information
1 parent
f817058
commit 82b4489
Showing
1 changed file
with
25 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,35 @@ | ||
(function($) { | ||
'use strict'; | ||
|
||
// Animate tabs of tabbed code blocks | ||
|
||
/** | ||
* TabbedCodeBlock | ||
* @param {String} elems | ||
* @constructor | ||
* Animate tabs and tab contents of tabbed codeblocks | ||
* @param {Object} $tabbedCodeblocks | ||
* @return {undefined} | ||
*/ | ||
var TabbedCodeBlock = function(elems) { | ||
this.$tabbedCodeBlocs = $(elems); | ||
}; | ||
function animateTabbedCodeBlocks($tabbedCodeblocks) { | ||
$tabbedCodeblocks.find('.tab').click(function() { | ||
var $currentTabButton = $(this); | ||
var $currentTabbedCodeblock = $currentTabButton.parent().parent().parent(); | ||
var $codeblocks = $currentTabbedCodeblock.find('.tabs-content').children('pre, .highlight'); | ||
var $activeCodeblock = $codeblocks.eq($currentTabButton.index()); | ||
var $tabButtons = $currentTabButton.siblings(); | ||
|
||
$tabButtons.removeClass('active'); | ||
$currentTabButton.addClass('active'); | ||
$codeblocks.hide(); | ||
$activeCodeblock.show(); | ||
|
||
TabbedCodeBlock.prototype = { | ||
/** | ||
* Run TabbedCodeBlock feature | ||
* @return {void} | ||
*/ | ||
run: function() { | ||
var self = this; | ||
self.$tabbedCodeBlocs.find('.tab').click(function() { | ||
var $codeblock = $(this).parent().parent().parent(); | ||
var $tabsContent = $codeblock.find('.tabs-content').children('pre, .highlight'); | ||
// remove `active` css class on all tabs | ||
$(this).siblings().removeClass('active'); | ||
// add `active` css class on the clicked tab | ||
$(this).addClass('active'); | ||
// hide all tab contents | ||
$tabsContent.hide(); | ||
// show only the right one | ||
$tabsContent.eq($(this).index()).show(); | ||
}); | ||
} | ||
}; | ||
// Resize the active codeblock according to the width of the window. | ||
var $gutter = $activeCodeblock.find('.gutter'); | ||
var $code = $activeCodeblock.find('.code'); | ||
var codePaddings = $code.width() - $code.innerWidth(); | ||
var width = $activeCodeblock.outerWidth() - $gutter.outerWidth() + codePaddings; | ||
$code.css('width', width); | ||
$code.children('pre').css('width', width); | ||
}); | ||
} | ||
|
||
$(document).ready(function() { | ||
var tabbedCodeBlocks = new TabbedCodeBlock('.codeblock--tabbed'); | ||
tabbedCodeBlocks.run(); | ||
animateTabbedCodeBlocks($('.codeblock--tabbed')); | ||
}); | ||
})(jQuery); |