]> source.dussan.org Git - jquery-ui.git/commitdiff
Tabs: Fixed disable method and cleaned up tests for enable and disable methods.
authorScott González <scott.gonzalez@gmail.com>
Thu, 28 Apr 2011 16:04:08 +0000 (12:04 -0400)
committerScott González <scott.gonzalez@gmail.com>
Thu, 28 Apr 2011 16:04:08 +0000 (12:04 -0400)
tests/unit/tabs/tabs.html
tests/unit/tabs/tabs_deprecated.html
tests/unit/tabs/tabs_methods.js
ui/jquery.ui.tabs.js

index bba0e8a588a06740d577ce4ef48638f01e320418..3e38f5f254d6d465380b4fc5a7420798df59e02a 100644 (file)
                }).get();
                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>
        // disable this stale testsuite for testswarm only
index 1fb50352b26892893be9b1e197899849937bb766..aae11c33d8450b31bce4e325c303873189488678 100644 (file)
                }).get();
                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>
        // disable this stale testsuite for testswarm only
index bd28316c5f4ff13fd5d66058eeba97141fff25d1..c7c04376091424147751002297ef5baf22ea48c1 100644 (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');
 });
 
-test('enable', function() {
-    expect(12);
-
-       el = $('#tabs1').tabs({ disabled: [ 0, 1 ] });
-       el.tabs("enable", 1);
-       ok( $('li:eq(1)', el).is(':not(.ui-state-disabled)'), 'remove class from li');
-       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');
-
-       // 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');
+test( "enable", function() {
+       expect( 8 );
+
+       var element = $( "#tabs1" ).tabs({ disabled: true });
+       tabs_disabled( element, true );
+       element.tabs( "enable" );
+       tabs_disabled( element, false );
+       element.tabs( "destroy" );
+
+       element.tabs({ disabled: [ 0, 1 ] });
+       tabs_disabled( element, [ 0, 1 ] );
+       element.tabs( "enable" );
+       tabs_disabled( element, false );
+});
 
-       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( "enable( index )", function() {
+    expect( 10 );
+
+       var element = $( "#tabs1" ).tabs({ disabled: true });
+       tabs_disabled( element, true );
+       // fully disabled -> partially disabled
+       element.tabs( "enable", 1 );
+       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 );
+});
+
+test( "disable", function() {
+       expect( 8 );
+
+       var element = $( "#tabs1" ).tabs({ disabled: false });
+       tabs_disabled( element, false );
+       element.tabs( "disable" );
+       tabs_disabled( element, true );
+       element.tabs( "destroy" );
+
+       element.tabs({ disabled: [ 0, 1 ] });
+       tabs_disabled( element, [ 0, 1 ] );
+       element.tabs( "disable" );
+       tabs_disabled( element, true );
 });
 
-test('disable', function() {
-    expect(14);
-
-       // normal
-       el = $('#tabs1').tabs();
-       el.tabs('disable', 1);
-       ok( $('li:eq(1)', el).is('.ui-state-disabled'), 'add class to li');
-       same(el.tabs('option', 'disabled'), [ 1 ], 'update disabled property');
-
-       // disable selected
-       el.tabs('disable', 0);
-       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
-       el.tabs('disable');
-       same( $('li.ui-state-disabled', el).length, 3, 'disable all');
-       same(el.tabs('option', 'disabled'), true, 'set to true');
-
-       // all tabs already disabled
-       el.tabs({ disabled: true });
-       el.tabs("disable", 1);
-       ok( $('li.ui-state-disabled', el).length, '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( "disable( index )", function() {
+    expect( 10 );
+
+       var element = $( "#tabs1" ).tabs({ disabled: false });
+       tabs_disabled( element, false );
+       // fully enabled -> partially disabled
+       element.tabs( "disable", 1 );
+       tabs_disabled( element, [ 1 ] );
+       // partially disabled -> partially disabled
+       element.tabs( "disable", 2 );
+       tabs_disabled( element, [ 1, 2 ] );
+       // 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() {
index 5c525b34a99f5f52e72ff2674b4008d13a6f79b1..071fcbc6783ef00e5ad83ef72487a6c434a4f98f 100755 (executable)
@@ -544,6 +544,9 @@ $.widget( "ui.tabs", {
                        disabled = true;
                } else {
                        index = this._getIndex( index );
+                       if ( $.inArray( index, disabled ) !== -1 ) {
+                               return;
+                       }
                        if ( $.isArray( disabled ) ) {
                                disabled = $.merge( [ index ], disabled ).sort();
                        } else {