collapse.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ========================================================================
  2. * Bootstrap: collapse.js v3.1.1
  3. * http://getbootstrap.com/javascript/#collapse
  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. // COLLAPSE PUBLIC CLASS DEFINITION
  11. // ================================
  12. var Collapse = function (element, options) {
  13. this.$element = $(element)
  14. this.options = $.extend({}, Collapse.DEFAULTS, options)
  15. this.transitioning = null
  16. if (this.options.parent) this.$parent = $(this.options.parent)
  17. if (this.options.toggle) this.toggle()
  18. }
  19. Collapse.DEFAULTS = {
  20. toggle: true
  21. }
  22. Collapse.prototype.dimension = function () {
  23. var hasWidth = this.$element.hasClass('width')
  24. return hasWidth ? 'width' : 'height'
  25. }
  26. Collapse.prototype.show = function () {
  27. if (this.transitioning || this.$element.hasClass('in')) return
  28. var startEvent = $.Event('show.bs.collapse')
  29. this.$element.trigger(startEvent)
  30. if (startEvent.isDefaultPrevented()) return
  31. var actives = this.$parent && this.$parent.find('> .panel > .in')
  32. if (actives && actives.length) {
  33. var hasData = actives.data('bs.collapse')
  34. if (hasData && hasData.transitioning) return
  35. actives.collapse('hide')
  36. hasData || actives.data('bs.collapse', null)
  37. }
  38. var dimension = this.dimension()
  39. this.$element
  40. .removeClass('collapse')
  41. .addClass('collapsing')
  42. [dimension](0)
  43. this.transitioning = 1
  44. var complete = function () {
  45. this.$element
  46. .removeClass('collapsing')
  47. .addClass('collapse in')
  48. [dimension]('auto')
  49. this.transitioning = 0
  50. this.$element.trigger('shown.bs.collapse')
  51. }
  52. if (!$.support.transition) return complete.call(this)
  53. var scrollSize = $.camelCase(['scroll', dimension].join('-'))
  54. this.$element
  55. .one($.support.transition.end, $.proxy(complete, this))
  56. .emulateTransitionEnd(350)
  57. [dimension](this.$element[0][scrollSize])
  58. }
  59. Collapse.prototype.hide = function () {
  60. if (this.transitioning || !this.$element.hasClass('in')) return
  61. var startEvent = $.Event('hide.bs.collapse')
  62. this.$element.trigger(startEvent)
  63. if (startEvent.isDefaultPrevented()) return
  64. var dimension = this.dimension()
  65. this.$element
  66. [dimension](this.$element[dimension]())
  67. [0].offsetHeight
  68. this.$element
  69. .addClass('collapsing')
  70. .removeClass('collapse')
  71. .removeClass('in')
  72. this.transitioning = 1
  73. var complete = function () {
  74. this.transitioning = 0
  75. this.$element
  76. .trigger('hidden.bs.collapse')
  77. .removeClass('collapsing')
  78. .addClass('collapse')
  79. }
  80. if (!$.support.transition) return complete.call(this)
  81. this.$element
  82. [dimension](0)
  83. .one($.support.transition.end, $.proxy(complete, this))
  84. .emulateTransitionEnd(350)
  85. }
  86. Collapse.prototype.toggle = function () {
  87. this[this.$element.hasClass('in') ? 'hide' : 'show']()
  88. }
  89. // COLLAPSE PLUGIN DEFINITION
  90. // ==========================
  91. var old = $.fn.collapse
  92. $.fn.collapse = function (option) {
  93. return this.each(function () {
  94. var $this = $(this)
  95. var data = $this.data('bs.collapse')
  96. var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option)
  97. if (!data && options.toggle && option == 'show') option = !option
  98. if (!data) $this.data('bs.collapse', (data = new Collapse(this, options)))
  99. if (typeof option == 'string') data[option]()
  100. })
  101. }
  102. $.fn.collapse.Constructor = Collapse
  103. // COLLAPSE NO CONFLICT
  104. // ====================
  105. $.fn.collapse.noConflict = function () {
  106. $.fn.collapse = old
  107. return this
  108. }
  109. // COLLAPSE DATA-API
  110. // =================
  111. $(document).on('click.bs.collapse.data-api', '[data-toggle=collapse]', function (e) {
  112. var $this = $(this), href
  113. var target = $this.attr('data-target')
  114. || e.preventDefault()
  115. || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
  116. var $target = $(target)
  117. var data = $target.data('bs.collapse')
  118. var option = data ? 'toggle' : $this.data()
  119. var parent = $this.attr('data-parent')
  120. var $parent = parent && $(parent)
  121. if (!data || !data.transitioning) {
  122. if ($parent) $parent.find('[data-toggle=collapse][data-parent="' + parent + '"]').not($this).addClass('collapsed')
  123. $this[$target.hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
  124. }
  125. $target.collapse(option)
  126. })
  127. }(jQuery);