]> source.dussan.org Git - jquery-ui.git/commitdiff
Tabs: Fixed cookie implementation. Fixes #7144 - Tabs: Deprecate cookie option.
authorScott González <scott.gonzalez@gmail.com>
Tue, 10 May 2011 13:15:08 +0000 (09:15 -0400)
committerScott González <scott.gonzalez@gmail.com>
Tue, 10 May 2011 13:15:08 +0000 (09:15 -0400)
tests/unit/tabs/tabs_deprecated.js
ui/jquery.ui.tabs.js

index 1448250078b5bdea3034a1c8b7271da9e8251275..49999670ff3786f8efa78e5346e57729db5cde03 100644 (file)
@@ -1,6 +1,6 @@
 (function( $ ) {
 
-module("tabs (deprecated): core");
+module( "tabs (deprecated): core" );
 
 test( "panel ids", function() {
        expect( 2 );
@@ -112,39 +112,39 @@ test( "tabTemplate + panelTemplate", function() {
        ok( element.find( "#new" ).hasClass( "customPanel" ), "panel custom class" );
 });
 
-test('cookie', function() {
-       expect(6);
+test( "cookie", function() {
+       expect( 6 );
 
-       el = $('#tabs1');
-       var cookieName = 'tabs_test', cookieObj = { name: cookieName };
-       $.cookie(cookieName, null); // blank state
-       var cookie = function() {
-               return parseInt($.cookie(cookieName), 10);
-       };
+       var element = $( "#tabs1" ),
+               cookieName = "tabs_test",
+               cookieObj = { name: cookieName };
+       $.cookie( cookieName, null );
+       function cookie() {
+               return parseInt( $.cookie( cookieName ), 10 );
+       }
 
-       el.tabs({ cookie: cookieObj });
-       equals(cookie(), 0, 'initial cookie value');
+       element.tabs({ cookie: cookieObj });
+       equals( cookie(), 0, "initial cookie value" );
 
-       el.tabs('destroy');
-       el.tabs({ active: 1, cookie: cookieObj });
-       equals(cookie(), 1, 'initial cookie value, from active property');
-
-       el.tabs('option', 'active', 2);
-       equals(cookie(), 2, 'cookie value updated after activating');
+       element.tabs( "destroy" );
+       element.tabs({ active: 1, cookie: cookieObj });
+       equals( cookie(), 1, "initial cookie value, from active property" );
 
-       el.tabs('destroy');
-       $.cookie(cookieName, 1);
-       el.tabs({ cookie: cookieObj });
-       equals(cookie(), 1, 'initial cookie value, from existing cookie');
+       element.tabs( "option", "active", 2 );
+       equals( cookie(), 2, "cookie value updated after activating" );
 
-       el.tabs('destroy');
-       el.tabs({ cookie: cookieObj, collapsible: true });
-       el.tabs('option', 'active', false);
-       equals(cookie(), -1, 'cookie value for all tabs unselected');
+       element.tabs( "destroy" );
+       $.cookie( cookieName, 1 );
+       element.tabs({ cookie: cookieObj });
+       equals( cookie(), 1, "initial cookie value, from existing cookie" );
 
-       el.tabs('destroy');
-       ok($.cookie(cookieName) === null, 'erase cookie after destroy');
+       element.tabs( "destroy" );
+       element.tabs({ cookie: cookieObj, collapsible: true });
+       element.tabs( "option", "active", false );
+       equals( cookie(), -1, "cookie value for all tabs unselected" );
 
+       element.tabs( "destroy" );
+       ok( $.cookie( cookieName ) === null, "erase cookie after destroy" );
 });
 
 asyncTest( "spinner", function() {
index e7d4e3513e07863f63fd987e4fcc4fd978f507ca..cd9b04f0ded1703b0b7fc65d2605854943bbc436 100644 (file)
@@ -1003,57 +1003,50 @@ if ( $.uiBackCompat !== false ) {
        }( jQuery, jQuery.ui.tabs.prototype ) );
 
        // cookie option
-       (function( $, prototype ) {
-               $.extend( prototype.options, {
+       $.widget( "ui.tabs", $.ui.tabs, {
+               options: {
                        cookie: null // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true }
-               });
-
-               var _create = prototype._create,
-                       _refresh = prototype._refresh,
-                       _eventHandler = prototype._eventHandler,
-                       _destroy = prototype._destroy;
-
-               prototype._create = function() {
-                       var o = this.options;
-                       if ( o.active === undefined ) {
-                               if ( typeof o.active !== "number" && o.cookie ) {
-                                       o.active = parseInt( this._cookie(), 10 );
+               },
+               _create: function() {
+                       var options = this.options,
+                               active;
+                       if ( options.active == null && options.cookie ) {
+                               active = parseInt( this._cookie(), 10 );
+                               if ( active === -1 ) {
+                                       active = false;
                                }
+                               options.active = active;
                        }
-                       _create.call( this );
-               };
-
-               prototype._cookie = function() {
-                       var cookie = this.cookie ||
-                               ( this.cookie = this.options.cookie.name || "ui-tabs-" + getNextListId() );
-                       return $.cookie.apply( null, [ cookie ].concat( $.makeArray( arguments ) ) );
-               };
-
-               prototype._refresh = function() {
-                       _refresh.call( this );
-
-                       // set or update cookie after init and add/remove respectively
+                       this._super( "_create" );
+               },
+               _cookie: function( active ) {
+                       var cookie = [ this.cookie ||
+                               ( this.cookie = this.options.cookie.name || "ui-tabs-" + getNextListId() ) ];
+                       if ( arguments.length ) {
+                               cookie.push( active === false ? -1 : active );
+                               cookie.push( this.options.cookie );
+                       }
+                       return $.cookie.apply( null, cookie );
+               },
+               _refresh: function() {
+                       this._super( "_refresh" );
                        if ( this.options.cookie ) {
                                this._cookie( this.options.active, this.options.cookie );
                        }
-               };
-
-               prototype._eventHandler = function( event ) {
-                       _eventHandler.apply( this, arguments );
-
+               },
+               _eventHandler: function( event ) {
+                       this._superApply( "_eventHandler", arguments );
                        if ( this.options.cookie ) {
                                this._cookie( this.options.active, this.options.cookie );
                        }
-               };
-
-               prototype._destroy = function() {
-                       _destroy.call( this );
-
+               },
+               _destroy: function() {
+                       this._super( "_destroy" );
                        if ( this.options.cookie ) {
                                this._cookie( null, this.options.cookie );
                        }
-               };
-       }( jQuery, jQuery.ui.tabs.prototype ) );
+               }
+       });
 }
 
 })( jQuery );