Tabs: Fixed disable method and cleaned up tests for enable and disable methods.

This commit is contained in:
Scott González 2011-04-28 12:04:08 -04:00
parent 0e7769c669
commit d819cdd3a9
4 changed files with 131 additions and 66 deletions

View File

@ -46,6 +46,41 @@
}).get(); }).get();
same( actual, expected ); same( actual, expected );
} }
function tabs_disabled( tabs, state ) {
var expected = $.map( new Array( tabs.find ( ".ui-tabs-nav li" ).length ), function( _, index ) {
if ( typeof state === "boolean" ) {
return state ? 1 : 0;
} else {
return $.inArray( index, state ) !== -1 ? 1 : 0;
}
});
var internalState = tabs.tabs( "option", "disabled" );
if ( internalState === false ) {
internalState = [];
}
if ( internalState === true ) {
internalState = $.map( new Array( tabs.find( ".ui-tabs-nav li" ).length ), function( _, index ) {
return index;
});
}
var actual = tabs.find( ".ui-tabs-nav li" ).map(function( index ) {
var tab = $( this ),
tabIsDisabled = tab.hasClass( "ui-state-disabled" );
if ( tabIsDisabled && $.inArray( index, internalState ) !== -1 ) {
return 1;
}
if ( !tabIsDisabled && $.inArray( index, internalState ) === -1 ) {
return 0;
}
return -1; // mixed state - invalid
}).get();
same( tabs.tabs( "option", "disabled" ), state );
same( actual, expected );
}
</script> </script>
<script> <script>
// disable this stale testsuite for testswarm only // disable this stale testsuite for testswarm only

View File

@ -45,6 +45,41 @@
}).get(); }).get();
same( actual, expected ); same( actual, expected );
} }
function tabs_disabled( tabs, state ) {
var expected = $.map( new Array( tabs.find ( ".ui-tabs-nav li" ).length ), function( _, index ) {
if ( typeof state === "boolean" ) {
return state ? 1 : 0;
} else {
return $.inArray( index, state ) !== -1 ? 1 : 0;
}
});
var internalState = tabs.tabs( "option", "disabled" );
if ( internalState === false ) {
internalState = [];
}
if ( internalState === true ) {
internalState = $.map( new Array( tabs.find( ".ui-tabs-nav li" ).length ), function( _, index ) {
return index;
});
}
var actual = tabs.find( ".ui-tabs-nav li" ).map(function( index ) {
var tab = $( this ),
tabIsDisabled = tab.hasClass( "ui-state-disabled" );
if ( tabIsDisabled && $.inArray( index, internalState ) !== -1 ) {
return 1;
}
if ( !tabIsDisabled && $.inArray( index, internalState ) === -1 ) {
return 0;
}
return -1; // mixed state - invalid
}).get();
same( tabs.tabs( "option", "disabled" ), state );
same( actual, expected );
}
</script> </script>
<script> <script>
// disable this stale testsuite for testswarm only // disable this stale testsuite for testswarm only

View File

@ -17,80 +17,72 @@ test('destroy', function() {
ok( $('li:eq(2)', el).is(':not(.ui-state-hover, .ui-state-focus)'), 'remove classes from mouseovered or focused li'); ok( $('li:eq(2)', el).is(':not(.ui-state-hover, .ui-state-focus)'), 'remove classes from mouseovered or focused li');
}); });
test('enable', function() { test( "enable", function() {
expect(12); expect( 8 );
el = $('#tabs1').tabs({ disabled: [ 0, 1 ] }); var element = $( "#tabs1" ).tabs({ disabled: true });
el.tabs("enable", 1); tabs_disabled( element, true );
ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li'); element.tabs( "enable" );
same(el.tabs('option', 'disabled'), [ 0 ], 'update property'); tabs_disabled( element, false );
element.tabs( "destroy" );
// enable all tabs element.tabs({ disabled: [ 0, 1 ] });
el.tabs({ disabled: [ 0, 1 ] }); tabs_disabled( element, [ 0, 1 ] );
el.tabs("enable"); element.tabs( "enable" );
ok( !$('li.ui-state-disabled', el).length, 'enable all'); tabs_disabled( element, false );
same(el.tabs('option', 'disabled'), false, 'update property');
// enable one tab
el.tabs({ disabled: true });
el.tabs("enable", 1);
ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
same(el.tabs('option', 'disabled'), [ 0, 2 ], 'update property');
// all tabs already enabled
el.tabs({ disabled: false });
el.tabs("enable", 1);
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( "enable( index )", function() {
expect(14); expect( 10 );
// normal var element = $( "#tabs1" ).tabs({ disabled: true });
el = $('#tabs1').tabs(); tabs_disabled( element, true );
el.tabs('disable', 1); // fully disabled -> partially disabled
ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li'); element.tabs( "enable", 1 );
same(el.tabs('option', 'disabled'), [ 1 ], 'update disabled property'); tabs_disabled( element, [ 0, 2 ] );
// partially disabled -> partially disabled
element.tabs( "enable", 2 );
tabs_disabled( element, [ 0 ] );
// already enabled tab, no change
element.tabs( "enable", 2 );
tabs_disabled( element, [ 0 ] );
// partially disabled -> fully enabled
element.tabs( "enable", 0 );
tabs_disabled( element, false );
});
// disable selected test( "disable", function() {
el.tabs('disable', 0); expect( 8 );
ok( $('li:eq(0)', el).is('.ui-state-disabled'), 'add class to selected li');
same(el.tabs('option', 'disabled'), [ 0, 1 ], 'update disabled property');
// disable all tabs var element = $( "#tabs1" ).tabs({ disabled: false });
el.tabs('disable'); tabs_disabled( element, false );
same( $('li.ui-state-disabled', el).length, 3, 'disable all'); element.tabs( "disable" );
same(el.tabs('option', 'disabled'), true, 'set to true'); tabs_disabled( element, true );
element.tabs( "destroy" );
// all tabs already disabled element.tabs({ disabled: [ 0, 1 ] });
el.tabs({ disabled: true }); tabs_disabled( element, [ 0, 1 ] );
el.tabs("disable", 1); element.tabs( "disable" );
ok( $('li.ui-state-disabled', el).length, 'disable all'); tabs_disabled( element, true );
same(el.tabs('option', 'disabled'), true, 'set to true'); });
el.tabs("destroy"); test( "disable( index )", function() {
// disable all tabs one by one expect( 10 );
el.tabs();
el.tabs('disable', 0); var element = $( "#tabs1" ).tabs({ disabled: false });
ok( $('li:eq(0)', el).is('.ui-state-disabled'), 'add class to li'); tabs_disabled( element, false );
same(el.tabs('option', 'disabled'), [ 0 ], 'update disabled property'); // fully enabled -> partially disabled
el.tabs('disable', 1); element.tabs( "disable", 1 );
ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li'); tabs_disabled( element, [ 1 ] );
same(el.tabs('option', 'disabled'), [ 0, 1 ], 'update disabled property'); // partially disabled -> partially disabled
el.tabs('disable', 2); element.tabs( "disable", 2 );
ok( $('li:eq(2)', el).is('.ui-state-disabled'), 'add class to li'); tabs_disabled( element, [ 1, 2 ] );
same(el.tabs('option', 'disabled'), true, 'set to true'); // already disabled tab, no change
element.tabs( "disable", 2 );
tabs_disabled( element, [ 1, 2 ] );
// partially disabled -> fully disabled
element.tabs( "disable", 0 );
tabs_disabled( element, true );
}); });
test('refresh', function() { test('refresh', function() {

View File

@ -544,6 +544,9 @@ $.widget( "ui.tabs", {
disabled = true; disabled = true;
} else { } else {
index = this._getIndex( index ); index = this._getIndex( index );
if ( $.inArray( index, disabled ) !== -1 ) {
return;
}
if ( $.isArray( disabled ) ) { if ( $.isArray( disabled ) ) {
disabled = $.merge( [ index ], disabled ).sort(); disabled = $.merge( [ index ], disabled ).sort();
} else { } else {