collapse.js 5.1 KB

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