3.0.0 -> 3.0.1
[bootswatch] / bower_components / bootstrap / js / tab.js
1 /* ========================================================================
2  * Bootstrap: tab.js v3.0.0
3  * http://getbootstrap.com/javascript/#tabs
4  * ========================================================================
5  * Copyright 2013 Twitter, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ======================================================================== */
19
20
21 +function ($) { "use strict";
22
23   // TAB CLASS DEFINITION
24   // ====================
25
26   var Tab = function (element) {
27     this.element = $(element)
28   }
29
30   Tab.prototype.show = function () {
31     var $this    = this.element
32     var $ul      = $this.closest('ul:not(.dropdown-menu)')
33     var selector = $this.data('target')
34
35     if (!selector) {
36       selector = $this.attr('href')
37       selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
38     }
39
40     if ($this.parent('li').hasClass('active')) return
41
42     var previous = $ul.find('.active:last a')[0]
43     var e        = $.Event('show.bs.tab', {
44       relatedTarget: previous
45     })
46
47     $this.trigger(e)
48
49     if (e.isDefaultPrevented()) return
50
51     var $target = $(selector)
52
53     this.activate($this.parent('li'), $ul)
54     this.activate($target, $target.parent(), function () {
55       $this.trigger({
56         type: 'shown.bs.tab'
57       , relatedTarget: previous
58       })
59     })
60   }
61
62   Tab.prototype.activate = function (element, container, callback) {
63     var $active    = container.find('> .active')
64     var transition = callback
65       && $.support.transition
66       && $active.hasClass('fade')
67
68     function next() {
69       $active
70         .removeClass('active')
71         .find('> .dropdown-menu > .active')
72         .removeClass('active')
73
74       element.addClass('active')
75
76       if (transition) {
77         element[0].offsetWidth // reflow for transition
78         element.addClass('in')
79       } else {
80         element.removeClass('fade')
81       }
82
83       if (element.parent('.dropdown-menu')) {
84         element.closest('li.dropdown').addClass('active')
85       }
86
87       callback && callback()
88     }
89
90     transition ?
91       $active
92         .one($.support.transition.end, next)
93         .emulateTransitionEnd(150) :
94       next()
95
96     $active.removeClass('in')
97   }
98
99
100   // TAB PLUGIN DEFINITION
101   // =====================
102
103   var old = $.fn.tab
104
105   $.fn.tab = function ( option ) {
106     return this.each(function () {
107       var $this = $(this)
108       var data  = $this.data('bs.tab')
109
110       if (!data) $this.data('bs.tab', (data = new Tab(this)))
111       if (typeof option == 'string') data[option]()
112     })
113   }
114
115   $.fn.tab.Constructor = Tab
116
117
118   // TAB NO CONFLICT
119   // ===============
120
121   $.fn.tab.noConflict = function () {
122     $.fn.tab = old
123     return this
124   }
125
126
127   // TAB DATA-API
128   // ============
129
130   $(document).on('click.bs.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
131     e.preventDefault()
132     $(this).tab('show')
133   })
134
135 }(window.jQuery);