Skip to content

Commit cb0588f

Browse files
committed
Tabs: Deprecate cookie option. Fixes #7144 Tabs: Deprecate cookie option
1 parent c6a6ef5 commit cb0588f

File tree

4 files changed

+90
-69
lines changed

4 files changed

+90
-69
lines changed

tests/unit/tabs/tabs_defaults.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var tabs_defaults = {
77
beforeload: null,
88
beforeActivate: null,
99
collapsible: false,
10-
cookie: null,
1110
disabled: false,
1211
event: "click",
1312
fx: null,

tests/unit/tabs/tabs_deprecated.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,42 @@ test('panelTemplate', function() {
3636
ok(false, "missing test - untested code is broken code.");
3737
});
3838

39+
test('cookie', function() {
40+
expect(6);
41+
42+
el = $('#tabs1');
43+
var cookieName = 'tabs_test', cookieObj = { name: cookieName };
44+
$.cookie(cookieName, null); // blank state
45+
var cookie = function() {
46+
return parseInt($.cookie(cookieName), 10);
47+
};
48+
49+
el.tabs({ cookie: cookieObj });
50+
equals(cookie(), 0, 'initial cookie value');
51+
52+
el.tabs('destroy');
53+
el.tabs({ active: 1, cookie: cookieObj });
54+
equals(cookie(), 1, 'initial cookie value, from active property');
55+
56+
el.tabs('option', 'active', 2);
57+
equals(cookie(), 2, 'cookie value updated after activating');
58+
59+
el.tabs('destroy');
60+
$.cookie(cookieName, 1);
61+
el.tabs({ cookie: cookieObj });
62+
equals(cookie(), 1, 'initial cookie value, from existing cookie');
63+
64+
el.tabs('destroy');
65+
el.tabs({ cookie: cookieObj, collapsible: true });
66+
el.tabs('option', 'active', false);
67+
equals(cookie(), -1, 'cookie value for all tabs unselected');
68+
69+
el.tabs('destroy');
70+
ok($.cookie(cookieName) === null, 'erase cookie after destroy');
71+
72+
});
73+
74+
3975
test('spinner', function() {
4076
expect(4);
4177
stop();

tests/unit/tabs/tabs_options.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,6 @@ test('collapsible', function() {
2222

2323
});
2424

25-
test('cookie', function() {
26-
expect(6);
27-
28-
el = $('#tabs1');
29-
var cookieName = 'tabs_test', cookieObj = { name: cookieName };
30-
$.cookie(cookieName, null); // blank state
31-
var cookie = function() {
32-
return parseInt($.cookie(cookieName), 10);
33-
};
34-
35-
el.tabs({ cookie: cookieObj });
36-
equals(cookie(), 0, 'initial cookie value');
37-
38-
el.tabs('destroy');
39-
el.tabs({ active: 1, cookie: cookieObj });
40-
equals(cookie(), 1, 'initial cookie value, from active property');
41-
42-
el.tabs('option', 'active', 2);
43-
equals(cookie(), 2, 'cookie value updated after activating');
44-
45-
el.tabs('destroy');
46-
$.cookie(cookieName, 1);
47-
el.tabs({ cookie: cookieObj });
48-
equals(cookie(), 1, 'initial cookie value, from existing cookie');
49-
50-
el.tabs('destroy');
51-
el.tabs({ cookie: cookieObj, collapsible: true });
52-
el.tabs('option', 'active', false);
53-
equals(cookie(), -1, 'cookie value for all tabs unselected');
54-
55-
el.tabs('destroy');
56-
ok($.cookie(cookieName) === null, 'erase cookie after destroy');
57-
58-
});
59-
6025
test('disabled', function() {
6126
expect(4);
6227

ui/jquery.ui.tabs.js

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ $.widget( "ui.tabs", {
2929
activate: null,
3030
beforeload: null,
3131
beforeActivate: null,
32-
cookie: null, // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true }
3332
collapsible: false,
3433
disabled: false,
3534
event: "click",
@@ -50,8 +49,7 @@ $.widget( "ui.tabs", {
5049
// Selected tab
5150
// use "selected" option or try to retrieve:
5251
// 1. from fragment identifier in url
53-
// 2. from cookie
54-
// 3. from selected class attribute on <li>
52+
// 2. from selected class attribute on <li>
5553
if ( o.active === undefined ) {
5654
if ( location.hash ) {
5755
this.anchors.each(function( i, a ) {
@@ -61,9 +59,6 @@ $.widget( "ui.tabs", {
6159
}
6260
});
6361
}
64-
if ( typeof o.active !== "number" && o.cookie ) {
65-
o.active = parseInt( self._cookie(), 10 );
66-
}
6762
if ( typeof o.active !== "number" && this.lis.filter( ".ui-tabs-selected" ).length ) {
6863
o.active = this.lis.index( this.lis.filter( ".ui-tabs-selected" ) );
6964
}
@@ -139,12 +134,6 @@ $.widget( "ui.tabs", {
139134
return hash.replace( /:/g, "\\:" );
140135
},
141136

142-
_cookie: function() {
143-
var cookie = this.cookie ||
144-
( this.cookie = this.options.cookie.name || "ui-tabs-" + getNextListId() );
145-
return $.cookie.apply( null, [ cookie ].concat( $.makeArray( arguments ) ) );
146-
},
147-
148137
_ui: function( tab, panel ) {
149138
return {
150139
tab: tab,
@@ -189,11 +178,6 @@ $.widget( "ui.tabs", {
189178
o.disabled = false;
190179
}
191180

192-
// set or update cookie after init and add/remove respectively
193-
if ( o.cookie ) {
194-
this._cookie( o.active, o.cookie );
195-
}
196-
197181
// disable tabs
198182
for ( var i = 0, li; ( li = this.lis[ i ] ); i++ ) {
199183
$( li ).toggleClass( "ui-state-disabled", $.inArray( i, o.disabled ) != -1 );
@@ -382,21 +366,13 @@ $.widget( "ui.tabs", {
382366
o.active = -1;
383367
self.active = null;
384368

385-
if ( o.cookie ) {
386-
self._cookie( o.active, o.cookie );
387-
}
388-
389369
self.element.queue( "tabs", function() {
390370
self._hideTab( clicked, $hide );
391371
}).dequeue( "tabs" );
392372

393373
clicked[ 0 ].blur();
394374
return;
395375
} else if ( !$hide.length ) {
396-
if ( o.cookie ) {
397-
self._cookie( o.active, o.cookie );
398-
}
399-
400376
self.element.queue( "tabs", function() {
401377
self._showTab( clicked, $show, event );
402378
});
@@ -409,10 +385,6 @@ $.widget( "ui.tabs", {
409385
}
410386
}
411387

412-
if ( o.cookie ) {
413-
self._cookie( o.active, o.cookie );
414-
}
415-
416388
// show new tab
417389
if ( $show.length ) {
418390
if ( $hide.length ) {
@@ -507,10 +479,6 @@ $.widget( "ui.tabs", {
507479
}
508480
});
509481

510-
if ( o.cookie ) {
511-
this._cookie( null, o.cookie );
512-
}
513-
514482
return this;
515483
},
516484

@@ -974,6 +942,59 @@ if ( $.uiBackCompat !== false ) {
974942
this.anchors.eq( index ).trigger( this.options.event + ".tabs" );
975943
};
976944
}( jQuery, jQuery.ui.tabs.prototype ) );
945+
946+
// cookie option
947+
(function( $, prototype ) {
948+
$.extend( prototype.options, {
949+
cookie: null // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true }
950+
});
951+
952+
var _create = prototype._create,
953+
_refresh = prototype._refresh,
954+
_eventHandler = prototype._eventHandler,
955+
_destroy = prototype._destroy;
956+
957+
prototype._create = function() {
958+
var o = this.options;
959+
if ( o.active === undefined ) {
960+
if ( typeof o.active !== "number" && o.cookie ) {
961+
o.active = parseInt( this._cookie(), 10 );
962+
}
963+
}
964+
_create.call( this );
965+
};
966+
967+
prototype._cookie = function() {
968+
var cookie = this.cookie ||
969+
( this.cookie = this.options.cookie.name || "ui-tabs-" + getNextListId() );
970+
return $.cookie.apply( null, [ cookie ].concat( $.makeArray( arguments ) ) );
971+
};
972+
973+
prototype._refresh = function() {
974+
_refresh.call( this );
975+
976+
// set or update cookie after init and add/remove respectively
977+
if ( this.options.cookie ) {
978+
this._cookie( this.options.active, this.options.cookie );
979+
}
980+
};
981+
982+
prototype._eventHandler = function( event ) {
983+
_eventHandler.apply( this, arguments );
984+
985+
if ( this.options.cookie ) {
986+
this._cookie( this.options.active, this.options.cookie );
987+
}
988+
};
989+
990+
prototype._destroy = function() {
991+
_destroy.call( this );
992+
993+
if ( this.options.cookie ) {
994+
this._cookie( null, this.options.cookie );
995+
}
996+
};
997+
}( jQuery, jQuery.ui.tabs.prototype ) );
977998
}
978999

9791000
})( jQuery );

0 commit comments

Comments
 (0)