Skip to content

Instantly share code, notes, and snippets.

@chicagoworks
Created December 28, 2010 21:54
Show Gist options
  • Save chicagoworks/757773 to your computer and use it in GitHub Desktop.
Save chicagoworks/757773 to your computer and use it in GitHub Desktop.
jQueryui tabs extension. $(el).tabs('activate', tab) is a mix of tabs('add') and tabs('select'). If the tab exists it is selected, if not it iis created. $(el).tabs(close) removes the tab but selects the tab to the left instead of the tab to the r
(function($) {
$.fn.extend($.ui.tabs.prototype, {
//use the same signature as add
activate : function(url, label, index) {
console.log(index)
var self = this,
index = index || self.anchors.length - 1;
//loop thru the tab.anchors collection and look for tab with a matching href
//If tab is local then compare the URL with the anchor or hash (eg #anchor5)
//If the tab is remote then compare the URL with .data('href.tabs')
for (var i = 0; i < self.anchors.length; i++) {
if (self.anchors[i].hash === url || $(self.anchors[i]).data('href.tabs') === url) {
/*if match found, select it and return */
self.select($(self.anchors[i]).attr('href'));
return this;
}
}
self.add(url, label, index).select(index);
return this;
},
//simple a wrapper around .remove() to which first selects the previous|left tab
close : function(index) {
index = this._getIndex(index);
var $li = this.lis.eq(index);
//if target tab was selected then select previous|left tab
//index is zero based, length is 1 base.
if ($li.hasClass("ui-tabs-selected") && this.anchors.length > 1 && index + 1 == this.anchors.length - 1) {
this.select(index - 1);
}
this.remove(index);
}
});
var original = $.ui.tabs.prototype._tabId;
$.ui.tabs.prototype._tabId = function(a) {
return this.options.idPrefix + a.href.substring(a.href.lastIndexOf('/') + 1);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment