collapse.js 4.53 KB
Newer Older
1
/* =============================================================
fat's avatar
fat committed
2
 * Bootstrap: collapse.js v3.0.0
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#collapse
4
 * =============================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 Twitter, Inc.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================ */

20

21
!function ($) { "use strict";
22

23
24
  // COLLAPSE PUBLIC CLASS DEFINITION
  // ================================
25
26

  var Collapse = function (element, options) {
27
28
29
    this.$element      = $(element)
    this.options       = $.extend({}, Collapse.DEFAULTS, options)
    this.transitioning = null
30

31
32
33
    if (this.options.parent) this.$parent = $(this.options.parent)
    if (this.options.toggle) this.toggle()
  }
34

35
36
  Collapse.DEFAULTS = {
    toggle: true
37
38
  }

39
40
41
42
  Collapse.prototype.dimension = function () {
    var hasWidth = this.$element.hasClass('width')
    return hasWidth ? 'width' : 'height'
  }
43

44
45
  Collapse.prototype.show = function () {
    if (this.transitioning || this.$element.hasClass('in')) return
46

47
48
49
    var dimension = this.dimension()
    var scroll    = $.camelCase(['scroll', dimension].join('-'))
    var actives   = this.$parent && this.$parent.find('> .accordion-group > .in')
50

51
52
53
54
55
56
    if (actives && actives.length) {
      var hasData = actives.data('collapse')
      if (hasData && hasData.transitioning) return
      actives.collapse('hide')
      hasData || actives.data('collapse', null)
    }
57

58
    this.$element[dimension](0)
fat's avatar
fat committed
59
    this.transition('addClass', $.Event('bs:collapse:show'), 'shown')
60

61
62
    if ($.support.transition) this.$element[dimension](this.$element[0][scroll])
  }
63

64
65
66
67
  Collapse.prototype.hide = function () {
    if (this.transitioning || !this.$element.hasClass('in')) return
    var dimension = this.dimension()
    this.reset(this.$element[dimension]())
fat's avatar
fat committed
68
    this.transition('removeClass', $.Event('bs:collapse:hide'), 'hidden')
69
70
    this.$element[dimension](0)
  }
71

72
73
  Collapse.prototype.reset = function (size) {
    var dimension = this.dimension()
74

75
76
77
78
    this.$element
      .removeClass('collapse')
      [dimension](size || 'auto')
      [0].offsetWidth
79

80
    this.$element[size !== null ? 'addClass' : 'removeClass']('collapse')
81

82
83
    return this
  }
84

85
86
87
88
89
90
  Collapse.prototype.transition = function (method, startEvent, completeEvent) {
    var that     = this
    var complete = function () {
      if (startEvent.type == 'show') that.reset()
      that.transitioning = 0
      that.$element.trigger(completeEvent)
91
92
    }

93
    this.$element.trigger(startEvent)
94

95
    if (startEvent.isDefaultPrevented()) return
96

97
    this.transitioning = 1
98

99
    this.$element[method]('in')
100

101
102
103
104
    $.support.transition && this.$element.hasClass('collapse') ?
      this.$element.one($.support.transition.end, complete) :
      complete()
  }
105

106
107
  Collapse.prototype.toggle = function () {
    this[this.$element.hasClass('in') ? 'hide' : 'show']()
108
109
  }

110

111
112
  // COLLAPSE PLUGIN DEFINITION
  // ==========================
113
114

  var old = $.fn.collapse
115

116
  $.fn.collapse = function (option) {
117
    return this.each(function () {
118
119
120
121
      var $this   = $(this)
      var data    = $this.data('collapse')
      var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option)

122
123
124
125
126
      if (!data) $this.data('collapse', (data = new Collapse(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }

127
  $.fn.collapse.Constructor = Collapse
128
129


130
131
  // COLLAPSE NO CONFLICT
  // ====================
132

133
134
135
136
137
138
  $.fn.collapse.noConflict = function () {
    $.fn.collapse = old
    return this
  }


139
140
  // COLLAPSE DATA-API
  // =================
141

142
  $(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) {
143
144
    var $this  = $(this), href
    var target = $this.attr('data-target')
145
146
        || e.preventDefault()
        || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
147
148
    var option = $(target).data('collapse') ? 'toggle' : $this.data()

149
150
    $this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
    $(target).collapse(option)
151
152
  })

153
}(window.jQuery);