bootstrap-collapse.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* =============================================================
  2. * bootstrap-collapse.js v2.0.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"
  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.addClass('collapse')
  62. }
  63. , transition: function ( method, startEvent, completeEvent ) {
  64. var that = this
  65. , complete = function () {
  66. if (startEvent == 'show') that.reset()
  67. that.$element.trigger(completeEvent)
  68. }
  69. this.$element
  70. .trigger(startEvent)
  71. [method]('in')
  72. $.support.transition && this.$element.hasClass('collapse') ?
  73. this.$element.one($.support.transition.end, complete) :
  74. complete()
  75. }
  76. , toggle: function () {
  77. this[this.$element.hasClass('in') ? 'hide' : 'show']()
  78. }
  79. }
  80. /* COLLAPSIBLE PLUGIN DEFINITION
  81. * ============================== */
  82. $.fn.collapse = function ( option ) {
  83. return this.each(function () {
  84. var $this = $(this)
  85. , data = $this.data('collapse')
  86. , options = typeof option == 'object' && option
  87. if (!data) $this.data('collapse', (data = new Collapse(this, options)))
  88. if (typeof option == 'string') data[option]()
  89. })
  90. }
  91. $.fn.collapse.defaults = {
  92. toggle: true
  93. }
  94. $.fn.collapse.Constructor = Collapse
  95. /* COLLAPSIBLE DATA-API
  96. * ==================== */
  97. $(function () {
  98. $('body').on('click.collapse.data-api', '[data-toggle=collapse]', function ( e ) {
  99. var $this = $(this), href
  100. , target = $this.attr('data-target')
  101. || e.preventDefault()
  102. || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
  103. , option = $(target).data('collapse') ? 'toggle' : $this.data()
  104. $(target).collapse(option)
  105. })
  106. })
  107. }( window.jQuery );