bootstrap-tab.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* ========================================================
  2. * bootstrap-tab.js v2.1.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. !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 a').last()[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. $.fn.tab = function ( option ) {
  86. return this.each(function () {
  87. var $this = $(this)
  88. , data = $this.data('tab')
  89. if (!data) $this.data('tab', (data = new Tab(this)))
  90. if (typeof option == 'string') data[option]()
  91. })
  92. }
  93. $.fn.tab.Constructor = Tab
  94. /* TAB DATA-API
  95. * ============ */
  96. $(function () {
  97. $('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
  98. e.preventDefault()
  99. $(this).tab('show')
  100. })
  101. })
  102. }(window.jQuery);