From fde8c64fd37d6700e174ccf5ea1574e418db2c1e Mon Sep 17 00:00:00 2001 From: Tiago Freire Date: Fri, 16 Jul 2010 13:37:54 -0300 Subject: Tabs: Added ability to reference tabs by href. Fixes #3171 - have option to remove tab by href content, not just by index. --- ui/jquery.ui.tabs.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'ui/jquery.ui.tabs.js') diff --git a/ui/jquery.ui.tabs.js b/ui/jquery.ui.tabs.js index 2d258d1d2..27754e539 100644 --- a/ui/jquery.ui.tabs.js +++ b/ui/jquery.ui.tabs.js @@ -413,6 +413,20 @@ $.widget("ui.tabs", { }, + _getIndex: function(index) { + // meta-function to give users option to provide a href string instead of a numerical index. + // also sanitizes numerical indexes to valid values. + if (typeof(index) == 'string') { + index = this.anchors.index(this.anchors.filter('[href$=' + index + ']')); + index = (index ==-1?NaN:index); + }else if (typeof(index) != 'number') { + index = NaN; + }else if (index > this.anchors.length) { + index = this.anchors.length; + } + return index; + }, + destroy: function() { var o = this.options; @@ -512,6 +526,7 @@ $.widget("ui.tabs", { }, remove: function(index) { + index = this._getIndex(index); var o = this.options, $li = this.lis.eq(index).remove(), $panel = this.panels.eq(index).remove(); @@ -532,6 +547,7 @@ $.widget("ui.tabs", { }, enable: function(index) { + index = this._getIndex(index); var o = this.options; if ($.inArray(index, o.disabled) == -1) { return; @@ -546,6 +562,7 @@ $.widget("ui.tabs", { }, disable: function(index) { + index = this._getIndex(index); var self = this, o = this.options; if (index != o.selected) { // cannot disable already selected tab this.lis.eq(index).addClass('ui-state-disabled'); @@ -561,9 +578,7 @@ $.widget("ui.tabs", { }, select: function(index) { - if (typeof index == 'string') { - index = this.anchors.index(this.anchors.filter('[href$=' + index + ']')); - } + index = this._getIndex(index); else if (index === null) { // usage of null is deprecated, TODO remove in next release index = -1; } @@ -576,6 +591,7 @@ $.widget("ui.tabs", { }, load: function(index) { + index = this._getIndex(index); var self = this, o = this.options, a = this.anchors.eq(index)[0], url = $.data(a, 'load.tabs'); this.abort(); -- cgit v1.2.3 From eaddfedd668c3397ed3c39d733d57d9c78466633 Mon Sep 17 00:00:00 2001 From: Tiago Freire Date: Fri, 16 Jul 2010 18:10:57 -0300 Subject: Tabs: Fixed a broken commit for #3171. --- ui/jquery.ui.tabs.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'ui/jquery.ui.tabs.js') diff --git a/ui/jquery.ui.tabs.js b/ui/jquery.ui.tabs.js index 27754e539..2a12cb540 100644 --- a/ui/jquery.ui.tabs.js +++ b/ui/jquery.ui.tabs.js @@ -579,13 +579,9 @@ $.widget("ui.tabs", { select: function(index) { index = this._getIndex(index); - else if (index === null) { // usage of null is deprecated, TODO remove in next release - index = -1; - } - if (index == -1 && this.options.collapsible) { + if (isNaN(index) && this.options.collapsible) { index = this.options.selected; } - this.anchors.eq(index).trigger(this.options.event + '.tabs'); return this; }, -- cgit v1.2.3 From 7e03d4ea9deee68e928651ef80c6c673c479b88d Mon Sep 17 00:00:00 2001 From: Scott González Date: Fri, 16 Jul 2010 20:20:28 -0400 Subject: Tabs: Updated tests. --- tests/unit/tabs/tabs.html | 3 ++- tests/unit/tabs/tabs_methods.js | 11 +---------- ui/jquery.ui.tabs.js | 10 +++------- 3 files changed, 6 insertions(+), 18 deletions(-) (limited to 'ui/jquery.ui.tabs.js') diff --git a/tests/unit/tabs/tabs.html b/tests/unit/tabs/tabs.html index 269f47ed5..211237569 100644 --- a/tests/unit/tabs/tabs.html +++ b/tests/unit/tabs/tabs.html @@ -15,7 +15,8 @@ - + + diff --git a/tests/unit/tabs/tabs_methods.js b/tests/unit/tabs/tabs_methods.js index 3eb627830..3812c3e91 100644 --- a/tests/unit/tabs/tabs_methods.js +++ b/tests/unit/tabs/tabs_methods.js @@ -97,7 +97,7 @@ test('remove', function() { }); test('select', function() { - expect(9); + expect(6); el = $('#tabs1').tabs(); @@ -114,21 +114,12 @@ test('select', function() { el.tabs('select', -1); equals(el.tabs('option', 'selected'), -1, 'should collapse tab passing in -1'); - el.tabs('destroy'); - el.tabs({ collapsible: true }); - el.tabs('select', null); - equals(el.tabs('option', 'selected'), -1, 'should collapse tab passing in null (deprecated)'); - el.tabs('select', null); - equals(el.tabs('option', 'selected'), -1, 'should not select tab passing in null a second time (deprecated)'); - el.tabs('destroy'); el.tabs(); el.tabs('select', 0); equals(el.tabs('option', 'selected'), 0, 'should not collapse tab if collapsible is not set to true'); el.tabs('select', -1); equals(el.tabs('option', 'selected'), 0, 'should not collapse tab if collapsible is not set to true'); - el.tabs('select', null); - equals(el.tabs('option', 'selected'), 0, 'should not collapse tab if collapsible is not set to true'); el.tabs('select', '#fragment-2'); equals(el.tabs('option', 'selected'), 1, 'should select tab by id'); diff --git a/ui/jquery.ui.tabs.js b/ui/jquery.ui.tabs.js index 2a12cb540..592f1a866 100644 --- a/ui/jquery.ui.tabs.js +++ b/ui/jquery.ui.tabs.js @@ -416,14 +416,10 @@ $.widget("ui.tabs", { _getIndex: function(index) { // meta-function to give users option to provide a href string instead of a numerical index. // also sanitizes numerical indexes to valid values. - if (typeof(index) == 'string') { + if (typeof index == 'string') { index = this.anchors.index(this.anchors.filter('[href$=' + index + ']')); - index = (index ==-1?NaN:index); - }else if (typeof(index) != 'number') { - index = NaN; - }else if (index > this.anchors.length) { - index = this.anchors.length; } + return index; }, @@ -579,7 +575,7 @@ $.widget("ui.tabs", { select: function(index) { index = this._getIndex(index); - if (isNaN(index) && this.options.collapsible) { + if (index == -1 && this.options.collapsible) { index = this.options.selected; } this.anchors.eq(index).trigger(this.options.event + '.tabs'); -- cgit v1.2.3 From 5435c50765e89f5cfb1c164dda6642c2149dc4db Mon Sep 17 00:00:00 2001 From: Scott González Date: Sat, 17 Jul 2010 18:50:47 -0400 Subject: Tabs: Fixed select method handling for index of -1. --- ui/jquery.ui.tabs.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'ui/jquery.ui.tabs.js') diff --git a/ui/jquery.ui.tabs.js b/ui/jquery.ui.tabs.js index 592f1a866..5555b95f5 100644 --- a/ui/jquery.ui.tabs.js +++ b/ui/jquery.ui.tabs.js @@ -575,8 +575,12 @@ $.widget("ui.tabs", { select: function(index) { index = this._getIndex(index); - if (index == -1 && this.options.collapsible) { - index = this.options.selected; + if (index == -1) { + if (this.options.collapsible && this.options.selected != -1) { + index = this.options.selected; + } else { + return this; + } } this.anchors.eq(index).trigger(this.options.event + '.tabs'); return this; -- cgit v1.2.3