collapse.js 5.8 KB

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