Skip to content

Commit fde8c64

Browse files
BrazilianJoescottgonzalez
authored andcommitted
Tabs: Added ability to reference tabs by href. Fixes #3171 - have option to remove tab by href content, not just by index.
1 parent b368491 commit fde8c64

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

ui/jquery.ui.tabs.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,20 @@ $.widget("ui.tabs", {
413413

414414
},
415415

416+
_getIndex: function(index) {
417+
// meta-function to give users option to provide a href string instead of a numerical index.
418+
// also sanitizes numerical indexes to valid values.
419+
if (typeof(index) == 'string') {
420+
index = this.anchors.index(this.anchors.filter('[href$=' + index + ']'));
421+
index = (index ==-1?NaN:index);
422+
}else if (typeof(index) != 'number') {
423+
index = NaN;
424+
}else if (index > this.anchors.length) {
425+
index = this.anchors.length;
426+
}
427+
return index;
428+
},
429+
416430
destroy: function() {
417431
var o = this.options;
418432

@@ -512,6 +526,7 @@ $.widget("ui.tabs", {
512526
},
513527

514528
remove: function(index) {
529+
index = this._getIndex(index);
515530
var o = this.options, $li = this.lis.eq(index).remove(),
516531
$panel = this.panels.eq(index).remove();
517532

@@ -532,6 +547,7 @@ $.widget("ui.tabs", {
532547
},
533548

534549
enable: function(index) {
550+
index = this._getIndex(index);
535551
var o = this.options;
536552
if ($.inArray(index, o.disabled) == -1) {
537553
return;
@@ -546,6 +562,7 @@ $.widget("ui.tabs", {
546562
},
547563

548564
disable: function(index) {
565+
index = this._getIndex(index);
549566
var self = this, o = this.options;
550567
if (index != o.selected) { // cannot disable already selected tab
551568
this.lis.eq(index).addClass('ui-state-disabled');
@@ -561,9 +578,7 @@ $.widget("ui.tabs", {
561578
},
562579

563580
select: function(index) {
564-
if (typeof index == 'string') {
565-
index = this.anchors.index(this.anchors.filter('[href$=' + index + ']'));
566-
}
581+
index = this._getIndex(index);
567582
else if (index === null) { // usage of null is deprecated, TODO remove in next release
568583
index = -1;
569584
}
@@ -576,6 +591,7 @@ $.widget("ui.tabs", {
576591
},
577592

578593
load: function(index) {
594+
index = this._getIndex(index);
579595
var self = this, o = this.options, a = this.anchors.eq(index)[0], url = $.data(a, 'load.tabs');
580596

581597
this.abort();

0 commit comments

Comments
 (0)