Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix when select item from collaple and resize #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions bootstrap-tabcollapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// ======================

var TabCollapse = function (el, options) {
this.options = options;
this.$tabs = $(el);
this.options = options;
this.$tabs = $(el);

this._accordionVisible = false; //content is attached to tabs at first
this._initAccordion();
Expand All @@ -18,16 +18,16 @@
// shown-accordion.bs.tabcollapse event on page load.
// See https://github.com/flatlogic/bootstrap-tabcollapse/issues/23
var that = this;
setTimeout(function() {
that.checkState();
setTimeout(function () {
that.checkState();
}, 0);
};

TabCollapse.DEFAULTS = {
accordionClass: 'visible-xs',
tabsClass: 'hidden-xs',
accordionTemplate: function(heading, groupId, parentId, active) {
return '<div class="panel panel-default">' +
accordionTemplate: function (heading, groupId, parentId, active) {
return '<div class="panel panel-default">' +
' <div class="panel-heading">' +
' <h4 class="panel-title">' +
' </h4>' +
Expand All @@ -41,23 +41,23 @@
}
};

TabCollapse.prototype.checkState = function(){
if (this.$tabs.is(':visible') && this._accordionVisible){
TabCollapse.prototype.checkState = function () {
if (this.$tabs.is(':visible') && this._accordionVisible) {
this.showTabs();
this._accordionVisible = false;
} else if (this.$accordion.is(':visible') && !this._accordionVisible){
} else if (this.$accordion.is(':visible') && !this._accordionVisible) {
this.showAccordion();
this._accordionVisible = true;
}
};

TabCollapse.prototype.showTabs = function(){
TabCollapse.prototype.showTabs = function () {
var view = this;
this.$tabs.trigger($.Event('show-tabs.bs.tabcollapse'));

var $panelHeadings = this.$accordion.find('.js-tabcollapse-panel-heading').detach();

$panelHeadings.each(function() {
var selectedHref = '';
$panelHeadings.each(function () {
var $panelHeading = $(this),
$parentLi = $panelHeading.data('bs.tabcollapse.parentLi');

Expand All @@ -70,12 +70,13 @@

if (!$oldHeading.hasClass('collapsed')) {
$parentLi.addClass('active');
selectedHref = $panelHeading.attr('href').replace('#', '');
if ($parentLi.parent().hasClass('dropdown-menu')) {
$parentLi.parent().parent().addClass('active');
}
} else {
$oldHeading.removeClass('collapsed');
}
}

$parentLi.append($panelHeading);
});
Expand All @@ -85,16 +86,22 @@
}

var $panelBodies = this.$accordion.find('.js-tabcollapse-panel-body');
$panelBodies.each(function(){
$panelBodies.each(function () {
var $panelBody = $(this),
$tabPane = $panelBody.data('bs.tabcollapse.tabpane');

if ($tabPane.attr('id') === selectedHref) {
$tabPane.addClass('active').addClass('in');
} else {
$tabPane.removeClass('active').removeClass('in');
}
$tabPane.append($panelBody.contents().detach());
});
this.$accordion.html('');

if(this.options.updateLinks) {
if (this.options.updateLinks) {
var $tabContents = this.getTabContentElement();
$tabContents.find('[data-toggle-was="tab"], [data-toggle-was="pill"]').each(function() {
$tabContents.find('[data-toggle-was="tab"], [data-toggle-was="pill"]').each(function () {
var $el = $(this);
var href = $el.attr('href').replace(/-collapse$/g, '');
$el.attr({
Expand All @@ -109,30 +116,30 @@
this.$tabs.trigger($.Event('shown-tabs.bs.tabcollapse'));
};

TabCollapse.prototype.getTabContentElement = function(){
TabCollapse.prototype.getTabContentElement = function () {
var $tabContents = $(this.options.tabContentSelector);
if($tabContents.length === 0) {
if ($tabContents.length === 0) {
$tabContents = this.$tabs.siblings('.tab-content');
}
return $tabContents;
};

TabCollapse.prototype.showAccordion = function(){
TabCollapse.prototype.showAccordion = function () {
this.$tabs.trigger($.Event('show-accordion.bs.tabcollapse'));

var $headings = this.$tabs.find('li:not(.dropdown) [data-toggle="tab"], li:not(.dropdown) [data-toggle="pill"]'),
view = this;
$headings.each(function(){
$headings.each(function () {
var $heading = $(this),
$parentLi = $heading.parent();
$heading.data('bs.tabcollapse.parentLi', $parentLi);
view.$accordion.append(view._createAccordionGroup(view.$accordion.attr('id'), $heading.detach()));
});

if(this.options.updateLinks) {
if (this.options.updateLinks) {
var parentId = this.$accordion.attr('id');
var $selector = this.$accordion.find('.js-tabcollapse-panel-body');
$selector.find('[data-toggle="tab"], [data-toggle="pill"]').each(function() {
$selector.find('[data-toggle="tab"], [data-toggle="pill"]').each(function () {
var $el = $(this);
var href = $el.attr('href') + '-collapse';
$el.attr({
Expand All @@ -147,7 +154,7 @@
this.$tabs.trigger($.Event('shown-accordion.bs.tabcollapse'));
};

TabCollapse.prototype._panelHeadingToTabHeading = function($heading) {
TabCollapse.prototype._panelHeadingToTabHeading = function ($heading) {
var href = $heading.attr('href').replace(/-collapse$/g, '');
$heading.attr({
'data-toggle': 'tab',
Expand All @@ -157,7 +164,7 @@
return $heading;
};

TabCollapse.prototype._tabHeadingToPanelHeading = function($heading, groupId, parentId, active) {
TabCollapse.prototype._tabHeadingToPanelHeading = function ($heading, groupId, parentId, active) {
$heading.addClass('js-tabcollapse-panel-heading ' + (active ? '' : 'collapsed'));
$heading.attr({
'data-toggle': 'collapse',
Expand All @@ -167,22 +174,22 @@
return $heading;
};

TabCollapse.prototype._checkStateOnResize = function(){
TabCollapse.prototype._checkStateOnResize = function () {
var view = this;
$(window).resize(function(){
$(window).resize(function () {
clearTimeout(view._resizeTimeout);
view._resizeTimeout = setTimeout(function(){
view._resizeTimeout = setTimeout(function () {
view.checkState();
}, 100);
});
};


TabCollapse.prototype._initAccordion = function(){
var randomString = function() {
TabCollapse.prototype._initAccordion = function () {
var randomString = function () {
var result = "",
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ ) {
for (var i = 0; i < 5; i++) {
result += possible.charAt(Math.floor(Math.random() * possible.length));
}
return result;
Expand All @@ -191,13 +198,13 @@
var srcId = this.$tabs.attr('id'),
accordionId = (srcId ? srcId : randomString()) + '-accordion';

this.$accordion = $('<div class="panel-group ' + this.options.accordionClass + '" id="' + accordionId +'"></div>');
this.$accordion = $('<div class="panel-group ' + this.options.accordionClass + '" id="' + accordionId + '"></div>');
this.$tabs.after(this.$accordion);
this.$tabs.addClass(this.options.tabsClass);
this.getTabContentElement().addClass(this.options.tabsClass);
};

TabCollapse.prototype._createAccordionGroup = function(parentId, $heading){
TabCollapse.prototype._createAccordionGroup = function (parentId, $heading) {
var tabSelector = $heading.attr('data-target'),
active = $heading.data('bs.tabcollapse.parentLi').is('.active');

Expand All @@ -223,8 +230,8 @@

$.fn.tabCollapse = function (option) {
return this.each(function () {
var $this = $(this);
var data = $this.data('bs.tabcollapse');
var $this = $(this);
var data = $this.data('bs.tabcollapse');
var options = $.extend({}, TabCollapse.DEFAULTS, $this.data(), typeof option === 'object' && option);

if (!data) $this.data('bs.tabcollapse', new TabCollapse(this, options));
Expand Down