mirror of
https://github.com/jquery/jquery-ui.git
synced 2026-04-20 03:02:41 -04:00
Tabs: Fixed cookie implementation. Fixes #7144 - Tabs: Deprecate cookie option.
This commit is contained in:
@@ -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');
|
||||
element.tabs( "destroy" );
|
||||
element.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( "option", "active", 2 );
|
||||
equals( cookie(), 2, "cookie value updated after activating" );
|
||||
|
||||
el.tabs('destroy');
|
||||
$.cookie(cookieName, 1);
|
||||
el.tabs({ cookie: cookieObj });
|
||||
equals(cookie(), 1, 'initial cookie value, from existing cookie');
|
||||
element.tabs( "destroy" );
|
||||
$.cookie( cookieName, 1 );
|
||||
element.tabs({ cookie: cookieObj });
|
||||
equals( cookie(), 1, "initial cookie value, from existing cookie" );
|
||||
|
||||
el.tabs('destroy');
|
||||
el.tabs({ cookie: cookieObj, collapsible: true });
|
||||
el.tabs('option', 'active', false);
|
||||
equals(cookie(), -1, 'cookie value for all tabs unselected');
|
||||
|
||||
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() {
|
||||
|
||||
71
ui/jquery.ui.tabs.js
vendored
71
ui/jquery.ui.tabs.js
vendored
@@ -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 );
|
||||
|
||||
Reference in New Issue
Block a user