This commit is contained in:
Scott González 2011-03-21 11:30:11 -04:00
commit 923b5b67b9
4 changed files with 85 additions and 32 deletions

View File

@ -9,7 +9,7 @@ var tabs_defaults = {
collapsible: false, collapsible: false,
cookie: null, cookie: null,
disable: null, disable: null,
disabled: [], disabled: false,
enable: null, enable: null,
event: "click", event: "click",
fx: null, fx: null,

View File

@ -73,16 +73,32 @@ test('destroy', function() {
}); });
test('enable', function() { test('enable', function() {
expect(2); expect(8);
el = $('#tabs1').tabs({ disabled: [ 0, 1 ] }); el = $('#tabs1').tabs({ disabled: [ 0, 1 ] });
el.tabs("enable", 1); el.tabs("enable", 1);
ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li'); ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
same(el.tabs('option', 'disabled'), [ ], 'update property'); same(el.tabs('option', 'disabled'), [ 0 ], 'update property');
// enable all tabs
el.tabs({ disabled: [ 0, 1 ] });
el.tabs("enable");
ok( !$('li.ui-state-disabled', el).length, 'enable all');
same(el.tabs('option', 'disabled'), false, 'update property');
el.tabs('destroy');
// enable all tabs one by one
el.tabs({ disabled: [ 1, 2 ] });
el.tabs("enable", 1);
ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
same(el.tabs('option', 'disabled'), [ 2 ], 'update property');
el.tabs("enable", 2);
ok( $('li:eq(2)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
same( el.tabs('option', 'disabled'), false, 'set to false');
}); });
test('disable', function() { test('disable', function() {
expect(4); expect(12);
// normal // normal
el = $('#tabs1').tabs(); el = $('#tabs1').tabs();
@ -90,10 +106,28 @@ test('disable', function() {
ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li'); ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li');
same(el.tabs('option', 'disabled'), [ 1 ], 'update disabled property'); same(el.tabs('option', 'disabled'), [ 1 ], 'update disabled property');
// attempt to disable selected has no effect // disable selected
el.tabs('disable', 0); el.tabs('disable', 0);
ok( $('li:eq(0)', el).is(':not(.ui-state-disabled)'), 'not add class to li'); ok( $('li:eq(0)', el).is('.ui-state-disabled'), 'add class to selected li');
same(el.tabs('option', 'disabled'), [ 1 ], 'not update property'); same(el.tabs('option', 'disabled'), [ 0, 1 ], 'update disabled property');
// disable all tabs
el.tabs('disable');
same( $('li.ui-state-disabled', el).length, 3, 'disable all');
same(el.tabs('option', 'disabled'), true, 'set to true');
el.tabs("destroy");
// disable all tabs one by one
el.tabs();
el.tabs('disable', 0);
ok( $('li:eq(0)', el).is('.ui-state-disabled'), 'add class to li');
same(el.tabs('option', 'disabled'), [ 0 ], 'update disabled property');
el.tabs('disable', 1);
ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li');
same(el.tabs('option', 'disabled'), [ 0, 1 ], 'update disabled property');
el.tabs('disable', 2);
ok( $('li:eq(2)', el).is('.ui-state-disabled'), 'add class to li');
same(el.tabs('option', 'disabled'), true, 'set to true');
}); });
test('add', function() { test('add', function() {

View File

@ -67,17 +67,16 @@ test('disabled', function() {
expect(4); expect(4);
el = $('#tabs1').tabs(); el = $('#tabs1').tabs();
same(el.tabs('option', 'disabled'), [ ], "should not disable any tab by default"); same(el.tabs('option', 'disabled'), false, "should not disable any tab by default");
el.tabs('option', 'disabled', [ 1 ]); el.tabs('option', 'disabled', [ 1 ]);
same(el.tabs('option', 'disabled'), [ 1 ], "should set property"); // everything else is being tested in methods module... same(el.tabs('option', 'disabled'), [ 1 ], "should set property"); // everything else is being tested in methods module...
// FIXME bug... property needs to be [ 1 ], since selected tab cannot be disabled!
el.tabs('option', 'disabled', [ 0, 1 ]); el.tabs('option', 'disabled', [ 0, 1 ]);
same(el.tabs('option', 'disabled'), [ 1 ], "should disable given tabs but not selected one"); // ... same(el.tabs('option', 'disabled'), [ 0, 1 ], "should disable given tabs, even selected one"); // ...
el.tabs('option', 'disabled', [ ]); el.tabs('option', 'disabled', [ ]);
same(el.tabs('option', 'disabled'), [ ], "should not disable any tab"); // ... same(el.tabs('option', 'disabled'), false, "should not disable any tab"); // ...
}); });
test('event', function() { test('event', function() {

62
ui/jquery.ui.tabs.js vendored
View File

@ -32,7 +32,7 @@ $.widget( "ui.tabs", {
cookie: null, // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true } cookie: null, // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true }
collapsible: false, collapsible: false,
disable: null, disable: null,
disabled: [], disabled: false,
enable: null, enable: null,
event: "click", event: "click",
fx: null, // e.g. { height: 'toggle', opacity: 'toggle', duration: 200 } fx: null, // e.g. { height: 'toggle', opacity: 'toggle', duration: 200 }
@ -194,15 +194,12 @@ $.widget( "ui.tabs", {
// Take disabling tabs via class attribute from HTML // Take disabling tabs via class attribute from HTML
// into account and update option properly. // into account and update option properly.
// A selected tab cannot become disabled. if ( $.isArray( o.disabled ) ) {
o.disabled = $.unique( o.disabled.concat( o.disabled = $.unique( o.disabled.concat(
$.map( this.lis.filter( ".ui-state-disabled" ), function( n, i ) { $.map( this.lis.filter( ".ui-state-disabled" ), function( n, i ) {
return self.lis.index( n ); return self.lis.index( n );
}) })
) ).sort(); ) ).sort();
if ( $.inArray( o.selected, o.disabled ) != -1 ) {
o.disabled.splice( $.inArray( o.selected, o.disabled ), 1 );
} }
// highlight selected tab // highlight selected tab
@ -233,6 +230,10 @@ $.widget( "ui.tabs", {
o.selected = this.lis.index( this.lis.filter( ".ui-tabs-selected" ) ); o.selected = this.lis.index( this.lis.filter( ".ui-tabs-selected" ) );
} }
if ( !o.disabled.length ) {
o.disabled = false;
}
this.element.toggleClass( "ui-tabs-collapsible", o.collapsible ); this.element.toggleClass( "ui-tabs-collapsible", o.collapsible );
// set or update cookie after init and add/remove respectively // set or update cookie after init and add/remove respectively
@ -242,8 +243,7 @@ $.widget( "ui.tabs", {
// disable tabs // disable tabs
for ( var i = 0, li; ( li = this.lis[ i ] ); i++ ) { for ( var i = 0, li; ( li = this.lis[ i ] ); i++ ) {
$( li ).toggleClass( "ui-state-disabled", $( li ).toggleClass( "ui-state-disabled", $.inArray( i, o.disabled ) != -1 );
$.inArray( i, o.disabled ) != -1 && !$( li ).hasClass( "ui-tabs-selected" ) );
} }
// reset cache if switching from cached to not cached // reset cache if switching from cached to not cached
@ -533,30 +533,50 @@ $.widget( "ui.tabs", {
}, },
enable: function( index ) { enable: function( index ) {
if ( index === undefined ) {
for ( var i = 0, len = this.lis.length; i < len; i++ ) {
this.enable( i );
}
return this;
}
index = this._getIndex( index ); index = this._getIndex( index );
var o = this.options; var o = this.options;
if ( $.inArray( index, o.disabled ) == -1 ) { if ( !o.disabled || ($.isArray( o.disabled ) && $.inArray( index, o.disabled ) == -1 ) ) {
return; return;
} }
this.lis.eq( index ).removeClass( "ui-state-disabled" ); this.lis.eq( index ).removeClass( "ui-state-disabled" );
o.disabled = $.grep( o.disabled, function( n, i ) { o.disabled = this.lis.map( function( i ) {
return n != index; return $(this).is( ".ui-state-disabled" ) ? i : null;
}); }).get();
if ( !o.disabled.length ) {
o.disabled = false;
}
this._trigger( "enable", null, this._ui( this.anchors[ index ], this.panels[ index ] ) ); this._trigger( "enable", null, this._ui( this.anchors[ index ], this.panels[ index ] ) );
return this; return this;
}, },
disable: function( index ) { disable: function( index ) {
if ( index === undefined ) {
for ( var i = 0, len = this.lis.length; i < len; i++ ) {
this.disable( i );
}
return this;
}
index = this._getIndex( index ); index = this._getIndex( index );
var self = this, o = this.options; var o = this.options;
// cannot disable already selected tab if ( !o.disabled || ($.isArray( o.disabled ) && $.inArray( index, o.disabled ) == -1 ) ) {
if ( index != o.selected ) {
this.lis.eq( index ).addClass( "ui-state-disabled" ); this.lis.eq( index ).addClass( "ui-state-disabled" );
o.disabled.push( index ); o.disabled = this.lis.map( function( i ) {
o.disabled.sort(); return $(this).is( ".ui-state-disabled" ) ? i : null;
}).get();
if ( o.disabled.length === this.anchors.length ) {
o.disabled = true;
}
this._trigger( "disable", null, this._ui( this.anchors[ index ], this.panels[ index ] ) ); this._trigger( "disable", null, this._ui( this.anchors[ index ], this.panels[ index ] ) );
} }