Skip to content

Instantly share code, notes, and snippets.

@ravishi
Created August 3, 2010 12:16
Show Gist options
  • Save ravishi/506264 to your computer and use it in GitHub Desktop.
Save ravishi/506264 to your computer and use it in GitHub Desktop.
/**
* jQuery UI Tabs Closable
* http://anohan.wordpress.com/jquery-ui-tabs-closable
*
* Copyright (c) 2010, Dirley Rodrigues
*
* Licensed under the New BSD License
* See: http://www.opensource.org/licenses/bsd-license.php
*
* Requires jQuery UI 1.4.2
*
*
* This plugin extends the original jQuery UI Tabs plugin, adding a
* close button to each tab.
*
* Example of use:
*
* $(document).ready(function() {
* $('#tabs').tabs({
* closeTemplate: '<span><img src="/images/icons/close.png" /></span>',
* close: function(ui) {
* return confirm('Are you sure you want to remove ' + ui.tab.text + '?');
* },
* });
* });
*
* Avaiable options:
*
* * closeTemplate: the close "button" template. default is <span>(x)</span>.
* * close: true or a callback that will be called when the close button
* is pressed. The callback function takes a ui argument and if it return
* false, the tab will not be closed.
*/
(function($) {
var _tabify_orig = $.ui.tabs.prototype._tabify;
$.extend($.ui.tabs.prototype.options, {
closeTemplate: '<span></span>',
close: null,
});
$.extend($.ui.tabs.prototype, {
close: function(index) {
var o = this.options;
if (!o.close || false != o.close.call(null, this._ui(this.lis.eq(index).find('a')[0], this.panels.eq(index)))) {
this.remove(index);
}
},
_tabify: function(init) {
_tabify_orig.apply(this, arguments);
var self = this, o = this.options;
if (o.close) {
this.lis.not(':has(.ui-tabs-close)').each( function() {
$(o.closeTemplate).appendTo(this)
.addClass('ui-tabs-close')
.css('cursor', 'pointer')
.click(function() { self.close(self.lis.index(this.parentNode)); })
});
}
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment