collapse.js 4.8 KB

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