Skip to content
Permalink
Browse files

Tabs: Fixed cookie implementation. Fixes #7144 - Tabs: Deprecate cook…

…ie option.
  • Loading branch information...
scottgonzalez committed May 10, 2011
1 parent a205c2c commit 36fccdda14c15f974ff6aed2315259df6da87be5
Showing with 59 additions and 66 deletions.
  1. +27 −27 tests/unit/tabs/tabs_deprecated.js
  2. +32 −39 ui/jquery.ui.tabs.js
@@ -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() {
@@ -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 );

0 comments on commit 36fccdd

Please sign in to comment.
You can’t perform that action at this time.