bootstrap-collapse.js 4.6 KB

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