Tabs: introduced "collapsible" option while deprecating "deselectable", fixes #4029

This commit is contained in:
Klaus Hartl 2009-02-02 20:15:06 +00:00
parent 43ab3a04e5
commit 24c51e5a95
2 changed files with 42 additions and 26 deletions

View File

@ -10,7 +10,7 @@ var defaults = {
ajaxOptions: null, ajaxOptions: null,
cache: false, cache: false,
cookie: null, cookie: null,
deselectable: false, collapsible: false,
disabled: [], disabled: [],
event: 'click', event: 'click',
fx: null, fx: null,
@ -172,24 +172,35 @@ module('tabs: Options');
}); });
test('deselectable: true', function() { test('collapsible', function() {
expect(7); expect(4);
el = $('#tabs1'); el = $('#tabs1');
el.tabs({ collapsible: true });
equals(el.data('collapsible.tabs'), true, 'option set');
ok(el.is('.ui-tabs-collapsible'), 'extra class "ui-tabs-collapsible" attached');
el.tabs('select', 0);
equals($('div.ui-tabs-hide', '#tabs1').length, 3, 'all panels should be hidden');
el.data('collapsible.tabs', false);
ok(el.is(':not(.ui-tabs-collapsible)'), 'extra class "ui-tabs-collapsible" not attached');
});
// deprecated... shadows collapsible
test('deselectable (deprecated)', function() {
expect(4);
el = $('#tabs1');
el.tabs({ deselectable: true }); el.tabs({ deselectable: true });
equals( el.data('deselectable.tabs'), true, 'option set' ); equals(el.data('collapsible.tabs'), true, 'option set');
equals( $('li.ui-tabs-deselectable', el).length, 1, 'class "ui-tabs-deselectable" attached once'); ok(el.is('.ui-tabs-collapsible'), 'extra class "ui-tabs-collapsible" attached');
equals( $('li', el).index( $('li.ui-tabs-deselectable', el) ), 0, 'class "ui-tabs-deselectable" attached to first tab'); el.tabs('select', 0);
equals($('div.ui-tabs-hide', '#tabs1').length, 3, 'all panels should be hidden');
el.tabs('select', 1); el.data('deselectable.tabs', false);
equals( $('li.ui-tabs-deselectable', el).length, 1, 'class "ui-tabs-deselectable" attached once'); ok(el.is(':not(.ui-tabs-collapsible)'), 'extra class "ui-tabs-collapsible" not attached');
equals( $('li', el).index( $('li.ui-tabs-deselectable', el) ), 1, 'class "ui-tabs-deselectable" attached to second tab');
el.tabs('select', 1);
equals( $('li.ui-tabs-deselectable', el).length, 0, 'class "ui-tabs-deselectable" not attached');
equals( $('div.ui-tabs-hide', '#tabs1').length, 3, 'all panels should be hidden' );
}); });
test('cookie', function() { test('cookie', function() {
@ -218,7 +229,7 @@ module('tabs: Options');
equals(cookie(), 1, 'initial cookie value, from existing cookie'); equals(cookie(), 1, 'initial cookie value, from existing cookie');
el.tabs('destroy'); el.tabs('destroy');
el.tabs({ cookie: cookieObj, deselectable: true }); el.tabs({ cookie: cookieObj, collapsible: true });
el.tabs('select', 0); el.tabs('select', 0);
equals(cookie(), -1, 'cookie value for all tabs unselected'); equals(cookie(), -1, 'cookie value for all tabs unselected');

View File

@ -15,7 +15,8 @@
$.widget("ui.tabs", { $.widget("ui.tabs", {
_init: function() { _init: function() {
// create tabs if (this.options.deselectable !== undefined)
this.options.collapsible = this.options.deselectable;
this._tabify(true); this._tabify(true);
}, },
@ -24,6 +25,9 @@ $.widget("ui.tabs", {
this.select(value); this.select(value);
else { else {
this.options[key] = value; this.options[key] = value;
if (key == 'deselectable')
this.options.collapsible = value;
this._tabify(); this._tabify();
} }
}, },
@ -152,7 +156,6 @@ $.widget("ui.tabs", {
if (o.selected >= 0 && this.$tabs.length) { // check for length avoids error when initializing empty list if (o.selected >= 0 && this.$tabs.length) { // check for length avoids error when initializing empty list
this.$panels.eq(o.selected).removeClass('ui-tabs-hide'); this.$panels.eq(o.selected).removeClass('ui-tabs-hide');
var classes = ['ui-tabs-selected ui-state-active']; var classes = ['ui-tabs-selected ui-state-active'];
if (o.deselectable) classes.push('ui-tabs-deselectable');
this.$lis.eq(o.selected).addClass(classes.join(' ')); this.$lis.eq(o.selected).addClass(classes.join(' '));
// seems to be expected behavior that the show callback is fired // seems to be expected behavior that the show callback is fired
@ -193,6 +196,9 @@ $.widget("ui.tabs", {
else else
o.selected = this.$lis.index(this.$lis.filter('.ui-tabs-selected')); o.selected = this.$lis.index(this.$lis.filter('.ui-tabs-selected'));
// update collapsible
this.element[o.collapsible ? 'addClass' : 'removeClass']('ui-tabs-collapsible');
// set or update cookie after init and add/remove respectively // set or update cookie after init and add/remove respectively
if (o.cookie) this._cookie(o.selected, o.cookie); if (o.cookie) this._cookie(o.selected, o.cookie);
@ -251,7 +257,6 @@ $.widget("ui.tabs", {
// Switch a tab... // Switch a tab...
function switchTab(clicked, $li, $hide, $show) { function switchTab(clicked, $li, $hide, $show) {
var classes = ['ui-tabs-selected ui-state-active']; var classes = ['ui-tabs-selected ui-state-active'];
if (o.deselectable) classes.push('ui-tabs-deselectable');
$li.removeClass('ui-state-default').addClass(classes.join(' ')) $li.removeClass('ui-state-default').addClass(classes.join(' '))
.siblings().removeClass(classes.join(' ')).addClass('ui-state-default'); .siblings().removeClass(classes.join(' ')).addClass('ui-state-default');
hideTab(clicked, $hide, $show); hideTab(clicked, $hide, $show);
@ -263,11 +268,11 @@ $.widget("ui.tabs", {
$hide = self.$panels.filter(':visible'), $hide = self.$panels.filter(':visible'),
$show = $(self._sanitizeSelector(this.hash)); $show = $(self._sanitizeSelector(this.hash));
// If tab is already selected and not deselectable or tab disabled or // If tab is already selected and not collapsible or tab disabled or
// or is already loading or click callback returns false stop here. // or is already loading or click callback returns false stop here.
// Check if click handler returns false last so that it is not executed // Check if click handler returns false last so that it is not executed
// for a disabled or loading tab! // for a disabled or loading tab!
if (($li.hasClass('ui-state-active') && !o.deselectable) if (($li.hasClass('ui-state-active') && !o.collapsible)
|| $li.hasClass('ui-state-disabled') || $li.hasClass('ui-state-disabled')
|| $(this).hasClass('ui-tabs-loading') || $(this).hasClass('ui-tabs-loading')
|| self._trigger('select', null, self._ui(this, $show[0])) === false || self._trigger('select', null, self._ui(this, $show[0])) === false
@ -279,11 +284,11 @@ $.widget("ui.tabs", {
o.selected = self.$tabs.index(this); o.selected = self.$tabs.index(this);
// if tab may be closed TODO avoid redundant code in this block // if tab may be closed TODO avoid redundant code in this block
if (o.deselectable) { if (o.collapsible) {
if ($li.hasClass('ui-state-active')) { if ($li.hasClass('ui-state-active')) {
o.selected = -1; o.selected = -1;
if (o.cookie) self._cookie(o.selected, o.cookie); if (o.cookie) self._cookie(o.selected, o.cookie);
$li.removeClass('ui-tabs-selected ui-state-active ui-tabs-deselectable') $li.removeClass('ui-tabs-selected ui-state-active')
.addClass('ui-state-default'); .addClass('ui-state-default');
self.$panels.stop(); self.$panels.stop();
hideTab(this, $hide); hideTab(this, $hide);
@ -294,7 +299,7 @@ $.widget("ui.tabs", {
self.$panels.stop(); self.$panels.stop();
var a = this; var a = this;
self.load(self.$tabs.index(this), function() { self.load(self.$tabs.index(this), function() {
$li.addClass('ui-tabs-selected ui-state-active ui-tabs-deselectable') $li.addClass('ui-tabs-selected ui-state-active')
.removeClass('ui-state-default'); .removeClass('ui-state-default');
showTab(a, $show); showTab(a, $show);
}); });
@ -367,7 +372,7 @@ $.widget("ui.tabs", {
'ui-tabs-selected ' + 'ui-tabs-selected ' +
'ui-state-active ' + 'ui-state-active ' +
'ui-state-hover ' + 'ui-state-hover ' +
'ui-tabs-deselectable ' + 'ui-tabs-collapsible ' +
'ui-state-disabled ' + 'ui-state-disabled ' +
'ui-tabs-panel ' + 'ui-tabs-panel ' +
'ui-widget-content ' + 'ui-widget-content ' +
@ -556,7 +561,7 @@ $.extend($.ui.tabs, {
ajaxOptions: null, ajaxOptions: null,
cache: false, cache: false,
cookie: null, // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true } cookie: null, // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true }
deselectable: false, collapsible: false,
disabled: [], disabled: [],
event: 'click', event: 'click',
fx: null, // e.g. { height: 'toggle', opacity: 'toggle', duration: 200 } fx: null, // e.g. { height: 'toggle', opacity: 'toggle', duration: 200 }