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