tab.js 3.3 KB

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