bootstrap-tab.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* ========================================================
  2. * bootstrap-tab.js v2.3.2
  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. !function ($) {
  20. "use strict"; // jshint ;_;
  21. /* TAB CLASS DEFINITION
  22. * ==================== */
  23. var Tab = function (element) {
  24. this.element = $(element)
  25. }
  26. Tab.prototype = {
  27. constructor: Tab
  28. , show: function () {
  29. var $this = this.element
  30. , $ul = $this.closest('ul:not(.dropdown-menu)')
  31. , selector = $this.attr('data-target')
  32. , previous
  33. , $target
  34. , e
  35. if (!selector) {
  36. selector = $this.attr('href')
  37. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
  38. }
  39. if ( $this.parent('li').hasClass('active') ) return
  40. previous = $ul.find('.active:last a')[0]
  41. e = $.Event('show', {
  42. relatedTarget: previous
  43. })
  44. $this.trigger(e)
  45. if (e.isDefaultPrevented()) return
  46. $target = $(selector)
  47. this.activate($this.parent('li'), $ul)
  48. this.activate($target, $target.parent(), function () {
  49. $this.trigger({
  50. type: 'shown'
  51. , relatedTarget: previous
  52. })
  53. })
  54. }
  55. , activate: function ( element, container, callback) {
  56. var $active = container.find('> .active')
  57. , transition = callback
  58. && $.support.transition
  59. && $active.hasClass('fade')
  60. function next() {
  61. $active
  62. .removeClass('active')
  63. .find('> .dropdown-menu > .active')
  64. .removeClass('active')
  65. element.addClass('active')
  66. if (transition) {
  67. element[0].offsetWidth // reflow for transition
  68. element.addClass('in')
  69. } else {
  70. element.removeClass('fade')
  71. }
  72. if ( element.parent('.dropdown-menu') ) {
  73. element.closest('li.dropdown').addClass('active')
  74. }
  75. callback && callback()
  76. }
  77. transition ?
  78. $active.one($.support.transition.end, next) :
  79. next()
  80. $active.removeClass('in')
  81. }
  82. }
  83. /* TAB PLUGIN DEFINITION
  84. * ===================== */
  85. var old = $.fn.tab
  86. $.fn.tab = function ( option ) {
  87. return this.each(function () {
  88. var $this = $(this)
  89. , data = $this.data('tab')
  90. if (!data) $this.data('tab', (data = new Tab(this)))
  91. if (typeof option == 'string') data[option]()
  92. })
  93. }
  94. $.fn.tab.Constructor = Tab
  95. /* TAB NO CONFLICT
  96. * =============== */
  97. $.fn.tab.noConflict = function () {
  98. $.fn.tab = old
  99. return this
  100. }
  101. /* TAB DATA-API
  102. * ============ */
  103. $(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
  104. e.preventDefault()
  105. $(this).tab('show')
  106. })
  107. }(window.jQuery);