js/bootstrap-tab.js
changeset 0 ba8ab09f730e
equal deleted inserted replaced
-1:000000000000 0:ba8ab09f730e
       
     1 /* ========================================================
       
     2  * bootstrap-tab.js v2.3.1
       
     3  * http://twitter.github.com/bootstrap/javascript.html#tabs
       
     4  * ========================================================
       
     5  * Copyright 2012 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 ($) {
       
    22 
       
    23   "use strict"; // jshint ;_;
       
    24 
       
    25 
       
    26  /* TAB CLASS DEFINITION
       
    27   * ==================== */
       
    28 
       
    29   var Tab = function (element) {
       
    30     this.element = $(element)
       
    31   }
       
    32 
       
    33   Tab.prototype = {
       
    34 
       
    35     constructor: Tab
       
    36 
       
    37   , show: function () {
       
    38       var $this = this.element
       
    39         , $ul = $this.closest('ul:not(.dropdown-menu)')
       
    40         , selector = $this.attr('data-target')
       
    41         , previous
       
    42         , $target
       
    43         , e
       
    44 
       
    45       if (!selector) {
       
    46         selector = $this.attr('href')
       
    47         selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
       
    48       }
       
    49 
       
    50       if ( $this.parent('li').hasClass('active') ) return
       
    51 
       
    52       previous = $ul.find('.active:last a')[0]
       
    53 
       
    54       e = $.Event('show', {
       
    55         relatedTarget: previous
       
    56       })
       
    57 
       
    58       $this.trigger(e)
       
    59 
       
    60       if (e.isDefaultPrevented()) return
       
    61 
       
    62       $target = $(selector)
       
    63 
       
    64       this.activate($this.parent('li'), $ul)
       
    65       this.activate($target, $target.parent(), function () {
       
    66         $this.trigger({
       
    67           type: 'shown'
       
    68         , relatedTarget: previous
       
    69         })
       
    70       })
       
    71     }
       
    72 
       
    73   , activate: function ( element, container, callback) {
       
    74       var $active = container.find('> .active')
       
    75         , transition = callback
       
    76             && $.support.transition
       
    77             && $active.hasClass('fade')
       
    78 
       
    79       function next() {
       
    80         $active
       
    81           .removeClass('active')
       
    82           .find('> .dropdown-menu > .active')
       
    83           .removeClass('active')
       
    84 
       
    85         element.addClass('active')
       
    86 
       
    87         if (transition) {
       
    88           element[0].offsetWidth // reflow for transition
       
    89           element.addClass('in')
       
    90         } else {
       
    91           element.removeClass('fade')
       
    92         }
       
    93 
       
    94         if ( element.parent('.dropdown-menu') ) {
       
    95           element.closest('li.dropdown').addClass('active')
       
    96         }
       
    97 
       
    98         callback && callback()
       
    99       }
       
   100 
       
   101       transition ?
       
   102         $active.one($.support.transition.end, next) :
       
   103         next()
       
   104 
       
   105       $active.removeClass('in')
       
   106     }
       
   107   }
       
   108 
       
   109 
       
   110  /* TAB PLUGIN DEFINITION
       
   111   * ===================== */
       
   112 
       
   113   var old = $.fn.tab
       
   114 
       
   115   $.fn.tab = function ( option ) {
       
   116     return this.each(function () {
       
   117       var $this = $(this)
       
   118         , data = $this.data('tab')
       
   119       if (!data) $this.data('tab', (data = new Tab(this)))
       
   120       if (typeof option == 'string') data[option]()
       
   121     })
       
   122   }
       
   123 
       
   124   $.fn.tab.Constructor = Tab
       
   125 
       
   126 
       
   127  /* TAB NO CONFLICT
       
   128   * =============== */
       
   129 
       
   130   $.fn.tab.noConflict = function () {
       
   131     $.fn.tab = old
       
   132     return this
       
   133   }
       
   134 
       
   135 
       
   136  /* TAB DATA-API
       
   137   * ============ */
       
   138 
       
   139   $(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
       
   140     e.preventDefault()
       
   141     $(this).tab('show')
       
   142   })
       
   143 
       
   144 }(window.jQuery);