0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-12 00:08:59 +01:00
Bootstrap/js/tab.js

136 lines
3.4 KiB
JavaScript
Raw Normal View History

2013-05-22 04:30:33 +02:00
/* ========================================================================
2013-05-16 20:06:30 +02:00
* Bootstrap: tab.js v3.0.0
2013-07-25 21:24:13 +02:00
* http://twbs.github.com/bootstrap/javascript.html#tabs
2013-05-22 04:30:33 +02:00
* ========================================================================
2013-10-22 18:41:33 +02:00
* Copyright 2013 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2013-05-22 04:30:33 +02:00
* ======================================================================== */
2013-05-22 04:30:33 +02:00
+function ($) { "use strict";
2013-05-17 02:18:15 +02:00
// TAB CLASS DEFINITION
// ====================
2012-07-23 03:28:39 +02:00
var Tab = function (element) {
this.element = $(element)
}
2013-05-17 02:18:15 +02:00
Tab.prototype.show = function () {
var $this = this.element
var $ul = $this.closest('ul:not(.dropdown-menu)')
var selector = $this.data('target')
2013-05-17 02:18:15 +02:00
if (!selector) {
selector = $this.attr('href')
selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
}
2013-05-17 02:18:15 +02:00
if ($this.parent('li').hasClass('active')) return
2013-05-17 02:18:15 +02:00
var previous = $ul.find('.active:last a')[0]
var e = $.Event('show.bs.tab', {
2013-05-17 02:18:15 +02:00
relatedTarget: previous
})
2013-05-17 02:18:15 +02:00
$this.trigger(e)
2013-05-17 02:18:15 +02:00
if (e.isDefaultPrevented()) return
2013-05-17 02:18:15 +02:00
var $target = $(selector)
2013-05-17 02:18:15 +02:00
this.activate($this.parent('li'), $ul)
this.activate($target, $target.parent(), function () {
$this.trigger({
type: 'shown.bs.tab'
2013-05-17 02:18:15 +02:00
, relatedTarget: previous
})
2013-05-17 02:18:15 +02:00
})
}
2013-05-17 02:18:15 +02:00
Tab.prototype.activate = function (element, container, callback) {
var $active = container.find('> .active')
var transition = callback
&& $.support.transition
&& $active.hasClass('fade')
function next() {
$active
.removeClass('active')
.find('> .dropdown-menu > .active')
.removeClass('active')
element.addClass('active')
if (transition) {
element[0].offsetWidth // reflow for transition
element.addClass('in')
} else {
element.removeClass('fade')
}
2013-05-17 02:18:15 +02:00
if (element.parent('.dropdown-menu')) {
element.closest('li.dropdown').addClass('active')
}
2013-05-17 02:18:15 +02:00
callback && callback()
}
2013-05-17 02:18:15 +02:00
transition ?
$active
.one($.support.transition.end, next)
.emulateTransitionEnd(150) :
2013-05-17 02:18:15 +02:00
next()
$active.removeClass('in')
}
2013-05-17 02:18:15 +02:00
// TAB PLUGIN DEFINITION
// =====================
var old = $.fn.tab
$.fn.tab = function ( option ) {
return this.each(function () {
var $this = $(this)
var data = $this.data('bs.tab')
2013-05-17 02:18:15 +02:00
if (!data) $this.data('bs.tab', (data = new Tab(this)))
if (typeof option == 'string') data[option]()
})
}
$.fn.tab.Constructor = Tab
2013-05-17 02:18:15 +02:00
// TAB NO CONFLICT
// ===============
$.fn.tab.noConflict = function () {
$.fn.tab = old
return this
}
2013-05-17 02:18:15 +02:00
// TAB DATA-API
// ============
$(document).on('click.bs.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
e.preventDefault()
$(this).tab('show')
})
}(window.jQuery);