bootstrap-collapse.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* =============================================================
  2. * bootstrap-collapse.js v2.1.1
  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) 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) 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. /* COLLAPSIBLE PLUGIN DEFINITION
  93. * ============================== */
  94. $.fn.collapse = function (option) {
  95. return this.each(function () {
  96. var $this = $(this)
  97. , data = $this.data('collapse')
  98. , options = typeof option == 'object' && option
  99. if (!data) $this.data('collapse', (data = new Collapse(this, options)))
  100. if (typeof option == 'string') data[option]()
  101. })
  102. }
  103. $.fn.collapse.defaults = {
  104. toggle: true
  105. }
  106. $.fn.collapse.Constructor = Collapse
  107. /* COLLAPSIBLE DATA-API
  108. * ==================== */
  109. $(function () {
  110. $('body').on('click.collapse.data-api', '[data-toggle=collapse]', function (e) {
  111. var $this = $(this), href
  112. , target = $this.attr('data-target')
  113. || e.preventDefault()
  114. || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
  115. , option = $(target).data('collapse') ? 'toggle' : $this.data()
  116. $this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
  117. $(target).collapse(option)
  118. })
  119. })
  120. }(window.jQuery);