tab.js 2.9 KB

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