tab.js 3.36 KB
Newer Older
1
/* ========================================================================
fat's avatar
fat committed
2
 * Bootstrap: tab.js v3.0.0
3
 * http://twitter.github.com/bootstrap/javascript.html#tabs
4
 * ========================================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 Twitter, Inc.
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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.
18
 * ======================================================================== */
19
20


21
+function ($) { "use strict";
22

fat's avatar
fat committed
23
24
  // TAB CLASS DEFINITION
  // ====================
25

Jacob Thornton's avatar
Jacob Thornton committed
26
  var Tab = function (element) {
27
28
29
    this.element = $(element)
  }

fat's avatar
fat committed
30
31
32
33
  Tab.prototype.show = function () {
    var $this    = this.element
    var $ul      = $this.closest('ul:not(.dropdown-menu)')
    var selector = $this.attr('data-target')
34

fat's avatar
fat committed
35
36
37
38
    if (!selector) {
      selector = $this.attr('href')
      selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
    }
39

fat's avatar
fat committed
40
    if ($this.parent('li').hasClass('active')) return
41

fat's avatar
fat committed
42
    var previous = $ul.find('.active:last a')[0]
43
    var e        = $.Event('show.bs.tab', {
fat's avatar
fat committed
44
45
      relatedTarget: previous
    })
46

fat's avatar
fat committed
47
    $this.trigger(e)
48

fat's avatar
fat committed
49
    if (e.isDefaultPrevented()) return
50

fat's avatar
fat committed
51
    var $target = $(selector)
52

fat's avatar
fat committed
53
54
55
    this.activate($this.parent('li'), $ul)
    this.activate($target, $target.parent(), function () {
      $this.trigger({
56
        type: 'shown.bs.tab'
fat's avatar
fat committed
57
      , relatedTarget: previous
58
      })
fat's avatar
fat committed
59
60
    })
  }
61

fat's avatar
fat committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  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')
81
      }
82

fat's avatar
fat committed
83
84
85
      if (element.parent('.dropdown-menu')) {
        element.closest('li.dropdown').addClass('active')
      }
86

fat's avatar
fat committed
87
      callback && callback()
88
    }
fat's avatar
fat committed
89
90

    transition ?
91
92
93
      $active
        .one($.support.transition.end, next)
        .emulateTransitionEnd(150) :
fat's avatar
fat committed
94
95
96
      next()

    $active.removeClass('in')
97
98
99
  }


fat's avatar
fat committed
100
101
  // TAB PLUGIN DEFINITION
  // =====================
102

103
104
  var old = $.fn.tab

105
  $.fn.tab = function ( option ) {
106
107
    return this.each(function () {
      var $this = $(this)
108
      var data  = $this.data('bs.tab')
fat's avatar
fat committed
109

110
      if (!data) $this.data('bs.tab', (data = new Tab(this)))
111
112
113
114
      if (typeof option == 'string') data[option]()
    })
  }

115
  $.fn.tab.Constructor = Tab
116
117


fat's avatar
fat committed
118
119
  // TAB NO CONFLICT
  // ===============
120
121
122
123
124
125
126

  $.fn.tab.noConflict = function () {
    $.fn.tab = old
    return this
  }


fat's avatar
fat committed
127
128
  // TAB DATA-API
  // ============
129

130
  $(document).on('click.bs.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
131
132
    e.preventDefault()
    $(this).tab('show')
133
134
  })

135
}(window.jQuery);