Skip to content

Commit 5d23e8e

Browse files
petersendiditscottgonzalez
authored andcommitted
Tabs: Fix issues with refresh method, add refresh method tests
1 parent dcb1720 commit 5d23e8e

2 files changed

Lines changed: 54 additions & 21 deletions

File tree

tests/unit/tabs/tabs_methods.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test( "disable( index )", function() {
8686
});
8787

8888
test('refresh', function() {
89-
expect(5);
89+
expect( 13 );
9090

9191
var el = $('<div id="tabs"><ul></ul></div>').tabs(),
9292
ul = el.find('ul');
@@ -95,22 +95,45 @@ test('refresh', function() {
9595

9696
ul.append('<li><a href="data/test.html">Test 1</a></li>');
9797
el.tabs('refresh');
98+
equals( el.tabs('option', 'active'), 0, 'First tab added should be auto active');
99+
ok( $( "li:eq(0)", el).is('.ui-tabs-active'), 'First tab should be auto active');
98100
equals( el.find('.ui-tabs-panel').length, 1, 'Panel created after refresh');
99101

100102
ul.find('li').remove();
101103
el.tabs('refresh');
102104
equals( el.find('.ui-tabs-panel').length, 0, 'Panel removed after refresh');
105+
equals( el.tabs('option', 'active'), false, 'No tabs are active');
103106

104-
ul.append('<li><a href="#test1">Test 1</a></li>');
105-
$('<div id="test1">Test Panel 1</div>').insertAfter( ul );
107+
// Hide second tab
108+
$('<li><a href="#test1">Test 1</a></li><li><a href="#test2">Test 2</a></li><li><a href="#test3">Test 3</a></li>')
109+
.appendTo( ul );
110+
$('<div id="test1">Test Panel 1</div><div id="test2">Test Panel 2</div><div id="test3">Test Panel 3</div>')
111+
.insertAfter( ul );
106112
el.tabs('refresh');
107-
el.tabs('option', 'active', 0);
108-
equals( el.tabs('option', 'active'), 0, 'First tab added should be auto active');
113+
equals( el.tabs('option', 'active'), 0, 'Second tab added should not be auto active');
114+
equals( $( "#test2", el ).css("display"), "none", 'Second panel is hidden');
109115

110-
ul.append('<li><a href="#test2">Test 2</a></li>');
111-
$('<div id="test2">Test Panel 2</div>').insertAfter( ul );
116+
// Make second tab active and then remove the first one
117+
el.tabs('option', 'active', 1);
118+
el.find('a[href="#test1"]').parent().remove();
112119
el.tabs('refresh');
113-
equals( el.tabs('option', 'active'), 0, 'Second tab added should not be auto active');
120+
equals( el.tabs('option', 'active'), 0, 'Active index correctly updated');
121+
ok( el.find('a[href="#test2"]').parent().is('.ui-tabs-active'), 'Tab is still active');
122+
123+
// Refresh with disabled tabs
124+
el.tabs('disable', 1);
125+
same( el.tabs('option', 'disabled'), [ 1 ], 'Second tab disabled');
126+
127+
el.find('a[href="#test3"]').remove();
128+
ul.append('<li><a href="#test4">Test 4</a></li>');
129+
$('<div id="test4">Test Panel 4</div>').insertAfter( ul );
130+
el.tabs('refresh');
131+
equals( el.tabs('option', 'disabled'), false, 'Not disabled');
132+
133+
ul.append('<li class="ui-state-disabled"><a href="#test3">Test 3</a></li>');
134+
$('<div id="test3">Test Panel 3</div>').insertAfter( ul );
135+
el.tabs('refresh');
136+
same( el.tabs('option', 'disabled'), [ 2 ], 'Second tab disabled');
114137
});
115138

116139
test('load', function() {

ui/jquery.ui.tabs.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ $.widget( "ui.tabs", {
8181
options.active = active;
8282

8383
// don't allow collapsible: false and active: false
84-
if ( !options.collapsible && options.active === false ) {
84+
if ( !options.collapsible && options.active === false && this.anchors.length ) {
8585
options.active = 0;
8686
}
8787

@@ -170,7 +170,14 @@ $.widget( "ui.tabs", {
170170
},
171171

172172
refresh: function() {
173-
var self = this;
173+
var self = this,
174+
options = this.options,
175+
lis = $( " > li:has(a[href])", this.list );
176+
177+
// Get disabled tabs from class attribute from HTML
178+
options.disabled = $.map( lis.filter( ".ui-state-disabled" ), function( n ) {
179+
return lis.index( n );
180+
});
174181

175182
this._processTabs();
176183

@@ -183,6 +190,20 @@ $.widget( "ui.tabs", {
183190
$( panel ).remove();
184191
}
185192
});
193+
194+
this.panels.not( this._getPanelForTab( this.active ) ).hide();
195+
196+
if ( !this.anchors.length ) {
197+
options.active = false;
198+
this.active = $();
199+
} else if ( !this.active || ( this.active && !$.contains( this.list[ 0 ], this.active[ 0 ] ) ) ) {
200+
// Activate previous tab
201+
var next = options.active - 1;
202+
this._activate( next >= 0 ? next : 0 );
203+
} else {
204+
// Make sure active index is correct
205+
options.active = this.anchors.index( this.active );
206+
}
186207
},
187208

188209
_refresh: function() {
@@ -841,17 +862,6 @@ if ( $.uiBackCompat !== false ) {
841862

842863
this.refresh();
843864

844-
if ( this.anchors.length == 1 ) {
845-
o.active = o.selected = 0;
846-
$li.addClass( "ui-tabs-active ui-state-active" );
847-
$panel.show();
848-
this.element.queue( "tabs", function() {
849-
self._trigger( "activate", null, self._ui( self.anchors[ 0 ], self.panels[ 0 ] ) );
850-
});
851-
852-
this.load( 0 );
853-
}
854-
855865
this._trigger( "add", null, this._ui( this.anchors[ index ], this.panels[ index ] ) );
856866
return this;
857867
};

0 commit comments

Comments
 (0)