bootstrap-collapse.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* =============================================================
  2. * bootstrap-collapse.js v2.0.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"
  21. var Collapse = function ( element, options ) {
  22. this.$element = $(element)
  23. this.options = $.extend({}, $.fn.collapse.defaults, options)
  24. if (this.options["parent"]) {
  25. this.$parent = $(this.options["parent"])
  26. }
  27. this.options.toggle && this.toggle()
  28. }
  29. Collapse.prototype = {
  30. constructor: Collapse
  31. , dimension: function () {
  32. var hasWidth = this.$element.hasClass('width')
  33. return hasWidth ? 'width' : 'height'
  34. }
  35. , show: function () {
  36. var dimension = this.dimension()
  37. , scroll = $.camelCase(['scroll', dimension].join('-'))
  38. , actives = this.$parent && this.$parent.find('.in')
  39. , hasData
  40. if (actives && actives.length) {
  41. hasData = actives.data('collapse')
  42. actives.collapse('hide')
  43. hasData || actives.data('collapse', null)
  44. }
  45. this.$element[dimension](0)
  46. this.transition('addClass', 'show', 'shown')
  47. this.$element[dimension](this.$element[0][scroll])
  48. }
  49. , hide: function () {
  50. var dimension = this.dimension()
  51. this.reset(this.$element[dimension]())
  52. this.transition('removeClass', 'hide', 'hidden')
  53. this.$element[dimension](0)
  54. }
  55. , reset: function ( size ) {
  56. var dimension = this.dimension()
  57. this.$element
  58. .removeClass('collapse')
  59. [dimension](size || 'auto')
  60. [0].offsetWidth
  61. this.$element[size ? 'addClass' : 'removeClass']('collapse')
  62. return this
  63. }
  64. , transition: function ( method, startEvent, completeEvent ) {
  65. var that = this
  66. , complete = function () {
  67. if (startEvent == 'show') that.reset()
  68. that.$element.trigger(completeEvent)
  69. }
  70. this.$element
  71. .trigger(startEvent)
  72. [method]('in')
  73. $.support.transition && this.$element.hasClass('collapse') ?
  74. this.$element.one($.support.transition.end, complete) :
  75. complete()
  76. }
  77. , toggle: function () {
  78. this[this.$element.hasClass('in') ? 'hide' : 'show']()
  79. }
  80. }
  81. /* COLLAPSIBLE PLUGIN DEFINITION
  82. * ============================== */
  83. $.fn.collapse = function ( option ) {
  84. return this.each(function () {
  85. var $this = $(this)
  86. , data = $this.data('collapse')
  87. , options = typeof option == 'object' && option
  88. if (!data) $this.data('collapse', (data = new Collapse(this, options)))
  89. if (typeof option == 'string') data[option]()
  90. })
  91. }
  92. $.fn.collapse.defaults = {
  93. toggle: true
  94. }
  95. $.fn.collapse.Constructor = Collapse
  96. /* COLLAPSIBLE DATA-API
  97. * ==================== */
  98. $(function () {
  99. $('body').on('click.collapse.data-api', '[data-toggle=collapse]', function ( e ) {
  100. var $this = $(this), href
  101. , target = $this.attr('data-target')
  102. || e.preventDefault()
  103. || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
  104. , option = $(target).data('collapse') ? 'toggle' : $this.data()
  105. $(target).collapse(option)
  106. })
  107. })
  108. }( window.jQuery );