/*
* Tab control.
*
* This plugin is a wrapper for the Twitter Bootstrap Tab component. It provides the following features:
* - Adding tabs
* - Optional close icons with 2 states (modified / unmodified). The icon state can be changed by
* triggering the modified.oc.tab/unmodified.oc.tab events on any element within tab, or on the tab itself.
* - Removing tabs with the Close icon, or with triggering an event from inside a tab pane or tab.
* The removing can be canceled if the confirm.oc.tab event handler returns false.
* - Scrolling tabs if they do not fit the screen
* - Collapsible tabs
*
* Data attributes:
* - data-control="tab" - creates the tab control from an element
* - data-closable - enables the Close Tab feature
* - data-pane-classes - a list of CSS classes to apply new pane elements
*
* Example with data attributes (data-control="tab"):
*
*
').html(content).addClass('tab-pane');
$link.attr('title', title)
$li.append($link)
this.$tabsContainer.append($li)
this.$pagesContainer.append($pane)
if (tabClass !== undefined)
$link.addClass(tabClass)
if (identifier !== undefined)
$li.attr('data-tab-id', identifier)
if (this.options.paneClasses !== undefined)
$pane.addClass(this.options.paneClasses)
this.initTab($li)
$link.tab('show')
$(window).trigger('resize')
this.$tabsContainer.dragScroll('goToElement', $li)
var defaultFocus = $('[default-focus]', $pane)
if (defaultFocus.is(":visible"))
defaultFocus.focus()
this.updateClasses()
}
Tab.prototype.updateTab = function(tab, title, content) {
var tabIndex = this.findTabIndex(tab)
if (tabIndex == -1)
return
var
processedTitle = this.generateTitleText(title, -1),
$tab = $('> li', this.$tabsContainer).eq(tabIndex),
$pane = $('> div', this.$pagesContainer).eq(tabIndex),
$link = $('a', $tab)
$link.text(processedTitle).attr('title', title)
$pane.html(content)
this.initTab($tab)
this.updateClasses()
}
Tab.prototype.generateTitleText = function(title, tabIndex)
{
var newTitle = title
if (this.options.titleAsFileNames)
newTitle = title.replace(/^.*[\\\/]/, '')
if (this.options.maxTitleSymbols && newTitle.length > this.options.maxTitleSymbols)
newTitle = '...'+newTitle.substring(newTitle.length - this.options.maxTitleSymbols)
return newTitle
}
Tab.prototype.closeTab = function(tab, force) {
var tabIndex = this.findTabIndex(tab)
if (tabIndex == -1)
return
var
$tab = $('> li', this.$tabsContainer).eq(tabIndex),
$pane = $('> div', this.$pagesContainer).eq(tabIndex),
isActive = $tab.hasClass('active'),
isModified = $tab.attr('data-modified') !== undefined;
if (isModified && this.options.closeConfirmation !== undefined && force !== true) {
if (!confirm(this.options.closeConfirmation))
return
}
var e = $.Event('beforeClose.oc.tab', { relatedTarget: $pane })
this.$el.trigger(e)
if (e.isDefaultPrevented())
return
$pane.remove()
$tab.remove()
if (isActive)
$('> li > a', this.$tabsContainer).eq(tabIndex-1).tab('show')
if ($('> li > a', this.$tabsContainer).length == 0)
this.$el.trigger('afterAllClosed.oc.tab')
this.$el.trigger('closed.oc.tab')
$(window).trigger('resize')
this.updateClasses()
}
Tab.prototype.updateClasses = function() {
if (this.$tabsContainer.children().length > 0)
this.$el.addClass('has-tabs')
else
this.$el.removeClass('has-tabs')
}
Tab.prototype.modifyTab = function(tab) {
var tabIndex = this.findTabIndex(tab)
if (tabIndex == -1)
return
$('> li', this.$tabsContainer).eq(tabIndex).attr('data-modified', '')
$('> div', this.$pagesContainer).eq(tabIndex).attr('data-modified', '')
}
Tab.prototype.unmodifyTab = function(tab) {
var tabIndex = this.findTabIndex(tab)
if (tabIndex == -1)
return
$('> li', this.$tabsContainer).eq(tabIndex).removeAttr('data-modified')
$('> div', this.$pagesContainer).eq(tabIndex).removeAttr('data-modified')
}
Tab.prototype.findTabIndex = function(tab) {
var tabToFind = tab
if (tab === undefined)
tabToFind = $('li.active', this.$tabsContainer)
var tabParent = this.$pagesContainer
if ($(tabToFind).parent().hasClass('nav-tabs'))
tabParent = this.$tabsContainer
return tabParent.children().index($(tabToFind))
}
Tab.prototype.findTabFromPane = function(pane) {
var id = '#' + $(pane).attr('id'),
tab = $('[data-target="' + id + '"]', this.$tabsContainer)
return tab
}
Tab.prototype.goTo = function(identifier) {
var $tab = $('[data-tab-id="'+identifier+'" ]', this.$tabsContainer)
if ($tab.length == 0)
return false
var tabIndex = this.findTabIndex($tab)
if (tabIndex == -1)
return false
this.goToIndex(tabIndex)
this.$tabsContainer.dragScroll('goToElement', $tab)
return true
}
Tab.prototype.goToPane = function(pane) {
var $pane = $(pane),
$tab = this.findTabFromPane($pane)
if ($pane.length == 0)
return
$pane.removeClass('collapsed')
var tabIndex = this.findTabIndex($pane)
if (tabIndex == -1)
return false
this.goToIndex(tabIndex)
if ($tab.length > 0)
this.$tabsContainer.dragScroll('goToElement', $tab)
return true
}
Tab.prototype.goToElement = function(element) {
return this.goToPane(element.closest('.tab-pane'))
}
Tab.prototype.findByIdentifier = function(identifier) {
return $('[data-tab-id="'+identifier+'" ]', this.$tabsContainer);
}
Tab.prototype.updateIdentifier = function(tab, identifier) {
var index = this.findTabIndex(tab)
if (index == -1)
return
$('> li', this.$tabsContainer).eq(index).attr('data-tab-id', identifier)
}
Tab.prototype.updateTitle = function(tab, title) {
var index = this.findTabIndex(tab)
if (index == -1)
return
var processedTitle = this.generateTitleText(title, index),
$link = $('> li > a', this.$tabsContainer).eq(index)
$link.attr('title', title)
$link.text(processedTitle)
}
Tab.prototype.goToIndex = function(index) {
$('> li > a', this.$tabsContainer).eq(index).tab('show')
}
Tab.prototype.prev = function() {
var tabIndex = this.findTabIndex()
if (tabIndex <= 0)
return
this.goToIndex(tabIndex-1)
}
Tab.prototype.next = function() {
var tabIndex = this.findTabIndex()
if (tabIndex == -1)
return
this.goToIndex(tabIndex+1)
}
Tab.DEFAULTS = {
}
// TAB PLUGIN DEFINITION
// ============================
var old = $.fn.ocTab
$.fn.ocTab = function (option) {
var args = arguments;
return this.each(function () {
var $this = $(this)
var data = $this.data('oc.tab')
var options = $.extend({}, Tab.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('oc.tab', (data = new Tab(this, options)))
if (typeof option == 'string') {
var methodArgs = [];
for (var i=1; i