tab.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* ========================================================================
  2. * Bootstrap: tab.js v3.3.4
  3. * http://getbootstrap.com/javascript/#tabs
  4. * ========================================================================
  5. * Copyright 2011-2015 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. * ======================================================================== */
  8. +function ($) {
  9. 'use strict';
  10. // TAB CLASS DEFINITION
  11. // ====================
  12. var Tab = function (element) {
  13. this.element = $(element)
  14. }
  15. Tab.VERSION = '3.3.4'
  16. Tab.TRANSITION_DURATION = 150
  17. Tab.prototype.show = function () {
  18. var $this = this.element
  19. var $ul = $this.closest('ul:not(.dropdown-menu)')
  20. var selector = $this.data('target')
  21. if (!selector) {
  22. selector = $this.attr('href')
  23. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
  24. }
  25. if ($this.parent('li').hasClass('active')) return
  26. var $previous = $ul.find('.active:last a')
  27. var hideEvent = $.Event('hide.bs.tab', {
  28. relatedTarget: $this[0]
  29. })
  30. var showEvent = $.Event('show.bs.tab', {
  31. relatedTarget: $previous[0]
  32. })
  33. $previous.trigger(hideEvent)
  34. $this.trigger(showEvent)
  35. if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) return
  36. var $target = $(selector)
  37. this.activate($this.closest('li'), $ul)
  38. this.activate($target, $target.parent(), function () {
  39. $previous.trigger({
  40. type: 'hidden.bs.tab',
  41. relatedTarget: $this[0]
  42. })
  43. $this.trigger({
  44. type: 'shown.bs.tab',
  45. relatedTarget: $previous[0]
  46. })
  47. })
  48. }
  49. Tab.prototype.activate = function (element, container, callback) {
  50. var $active = container.find('> .active')
  51. var transition = callback
  52. && $.support.transition
  53. && (($active.length && $active.hasClass('fade')) || !!container.find('> .fade').length)
  54. function next() {
  55. $active
  56. .removeClass('active')
  57. .find('> .dropdown-menu > .active')
  58. .removeClass('active')
  59. .end()
  60. .find('[data-toggle="tab"]')
  61. .attr('aria-expanded', false)
  62. element
  63. .addClass('active')
  64. .find('[data-toggle="tab"]')
  65. .attr('aria-expanded', true)
  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').length) {
  73. element
  74. .closest('li.dropdown')
  75. .addClass('active')
  76. .end()
  77. .find('[data-toggle="tab"]')
  78. .attr('aria-expanded', true)
  79. }
  80. callback && callback()
  81. }
  82. $active.length && transition ?
  83. $active
  84. .one('bsTransitionEnd', next)
  85. .emulateTransitionEnd(Tab.TRANSITION_DURATION) :
  86. next()
  87. $active.removeClass('in')
  88. }
  89. // TAB PLUGIN DEFINITION
  90. // =====================
  91. function Plugin(option) {
  92. return this.each(function () {
  93. var $this = $(this)
  94. var data = $this.data('bs.tab')
  95. if (!data) $this.data('bs.tab', (data = new Tab(this)))
  96. if (typeof option == 'string') data[option]()
  97. })
  98. }
  99. var old = $.fn.tab
  100. $.fn.tab = Plugin
  101. $.fn.tab.Constructor = Tab
  102. // TAB NO CONFLICT
  103. // ===============
  104. $.fn.tab.noConflict = function () {
  105. $.fn.tab = old
  106. return this
  107. }
  108. // TAB DATA-API
  109. // ============
  110. var clickHandler = function (e) {
  111. e.preventDefault()
  112. Plugin.call($(this), 'show')
  113. }
  114. $(document)
  115. .on('click.bs.tab.data-api', '[data-toggle="tab"]', clickHandler)
  116. .on('click.bs.tab.data-api', '[data-toggle="pill"]', clickHandler)
  117. }(jQuery);