tab.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* ========================================================================
  2. * Bootstrap: tab.js v3.2.0
  3. * http://getbootstrap.com/javascript/#tabs
  4. * ========================================================================
  5. * Copyright 2011-2014 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.2.0'
  16. Tab.prototype.show = function () {
  17. var $this = this.element
  18. var $ul = $this.closest('ul:not(.dropdown-menu)')
  19. var selector = $this.data('target')
  20. if (!selector) {
  21. selector = $this.attr('href')
  22. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
  23. }
  24. if ($this.parent('li').hasClass('active')) return
  25. var previous = $ul.find('.active:last a')[0]
  26. var e = $.Event('show.bs.tab', {
  27. relatedTarget: previous
  28. })
  29. $this.trigger(e)
  30. if (e.isDefaultPrevented()) return
  31. var $target = $(selector)
  32. this.activate($this.closest('li'), $ul)
  33. this.activate($target, $target.parent(), function () {
  34. $this.trigger({
  35. type: 'shown.bs.tab',
  36. relatedTarget: previous
  37. })
  38. })
  39. }
  40. Tab.prototype.activate = function (element, container, callback) {
  41. var $active = container.find('> .active')
  42. var transition = callback
  43. && $.support.transition
  44. && $active.hasClass('fade')
  45. function next() {
  46. $active
  47. .removeClass('active')
  48. .find('> .dropdown-menu > .active')
  49. .removeClass('active')
  50. element.addClass('active')
  51. if (transition) {
  52. element[0].offsetWidth // reflow for transition
  53. element.addClass('in')
  54. } else {
  55. element.removeClass('fade')
  56. }
  57. if (element.parent('.dropdown-menu')) {
  58. element.closest('li.dropdown').addClass('active')
  59. }
  60. callback && callback()
  61. }
  62. transition ?
  63. $active
  64. .one('bsTransitionEnd', next)
  65. .emulateTransitionEnd(150) :
  66. next()
  67. $active.removeClass('in')
  68. }
  69. // TAB PLUGIN DEFINITION
  70. // =====================
  71. function Plugin(option) {
  72. return this.each(function () {
  73. var $this = $(this)
  74. var data = $this.data('bs.tab')
  75. if (!data) $this.data('bs.tab', (data = new Tab(this)))
  76. if (typeof option == 'string') data[option]()
  77. })
  78. }
  79. var old = $.fn.tab
  80. $.fn.tab = Plugin
  81. $.fn.tab.Constructor = Tab
  82. // TAB NO CONFLICT
  83. // ===============
  84. $.fn.tab.noConflict = function () {
  85. $.fn.tab = old
  86. return this
  87. }
  88. // TAB DATA-API
  89. // ============
  90. $(document).on('click.bs.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
  91. e.preventDefault()
  92. Plugin.call($(this), 'show')
  93. })
  94. }(jQuery);