tab.js 3.8 KB

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