collapse.js 5.13 KB
Newer Older
1
/* ========================================================================
fat's avatar
fat committed
2
 * Bootstrap: collapse.js v3.0.0
Chris Rebert's avatar
Chris Rebert committed
3
 * http://twbs.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
 *
 * 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.
18
 * ======================================================================== */
19

20

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

fat's avatar
fat committed
23
24
  // COLLAPSE PUBLIC CLASS DEFINITION
  // ================================
25
26

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

fat's avatar
fat committed
31
32
33
    if (this.options.parent) this.$parent = $(this.options.parent)
    if (this.options.toggle) this.toggle()
  }
34

fat's avatar
fat committed
35
36
  Collapse.DEFAULTS = {
    toggle: true
37
38
  }

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

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

fat's avatar
fat committed
47
48
49
50
    var startEvent = $.Event('show.bs.collapse')
    this.$element.trigger(startEvent)
    if (startEvent.isDefaultPrevented()) return

51
    var actives   = this.$parent && this.$parent.find('> .accordion-group > .in')
52

fat's avatar
fat committed
53
    if (actives && actives.length) {
fat's avatar
fat committed
54
      var hasData = actives.data('bs.collapse')
fat's avatar
fat committed
55
56
      if (hasData && hasData.transitioning) return
      actives.collapse('hide')
fat's avatar
fat committed
57
      hasData || actives.data('bs.collapse', null)
fat's avatar
fat committed
58
    }
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    var dimension = this.dimension()

    this.$element
      .removeClass('collapse')
      .addClass('collapsing')
      [dimension](0)

    this.transitioning = 1

    var complete = function () {
      this.$element
        .removeClass('collapsing')
        .addClass('in')
        [dimension]('auto')
      this.transitioning = 0
      this.$element.trigger('shown.bs.collapse')
    }

    if (!$.support.transition) return complete.call(this)

    var scrollSize = $.camelCase(['scroll', dimension].join('-'))
81

82
83
84
85
    this.$element
      .one($.support.transition.end, $.proxy(complete, this))
      .emulateTransitionEnd(350)
      [dimension](this.$element[0][scrollSize])
fat's avatar
fat committed
86
  }
87

fat's avatar
fat committed
88
89
  Collapse.prototype.hide = function () {
    if (this.transitioning || !this.$element.hasClass('in')) return
fat's avatar
fat committed
90
91
92
93
94

    var startEvent = $.Event('hide.bs.collapse')
    this.$element.trigger(startEvent)
    if (startEvent.isDefaultPrevented()) return

fat's avatar
fat committed
95
    var dimension = this.dimension()
96

97
98
99
    this.$element
      [dimension](this.$element[dimension]())
      [0].offsetHeight
100

fat's avatar
fat committed
101
    this.$element
102
      .addClass('collapsing')
fat's avatar
fat committed
103
      .removeClass('collapse')
104
      .removeClass('in')
105

106
    this.transitioning = 1
107

fat's avatar
fat committed
108
    var complete = function () {
109
110
111
112
113
      this.transitioning = 0
      this.$element
        .trigger('hidden.bs.collapse')
        .removeClass('collapsing')
        .addClass('collapse')
114
115
    }

116
    if (!$.support.transition) return complete.call(this)
117

118
119
120
121
    this.$element
      [dimension](0)
      .one($.support.transition.end, $.proxy(complete, this))
      .emulateTransitionEnd(350)
fat's avatar
fat committed
122
  }
123

fat's avatar
fat committed
124
125
  Collapse.prototype.toggle = function () {
    this[this.$element.hasClass('in') ? 'hide' : 'show']()
126
127
  }

128

fat's avatar
fat committed
129
130
  // COLLAPSE PLUGIN DEFINITION
  // ==========================
131
132

  var old = $.fn.collapse
133

134
  $.fn.collapse = function (option) {
135
    return this.each(function () {
fat's avatar
fat committed
136
      var $this   = $(this)
fat's avatar
fat committed
137
      var data    = $this.data('bs.collapse')
fat's avatar
fat committed
138
139
      var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option)

fat's avatar
fat committed
140
      if (!data) $this.data('bs.collapse', (data = new Collapse(this, options)))
141
142
143
144
      if (typeof option == 'string') data[option]()
    })
  }

145
  $.fn.collapse.Constructor = Collapse
146
147


fat's avatar
fat committed
148
149
  // COLLAPSE NO CONFLICT
  // ====================
150

151
152
153
154
155
156
  $.fn.collapse.noConflict = function () {
    $.fn.collapse = old
    return this
  }


fat's avatar
fat committed
157
158
  // COLLAPSE DATA-API
  // =================
159

160
  $(document).on('click.bs.collapse.data-api', '[data-toggle=collapse]', function (e) {
fat's avatar
fat committed
161
162
    var $this   = $(this), href
    var target  = $this.attr('data-target')
163
164
        || e.preventDefault()
        || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
fat's avatar
fat committed
165
166
167
168
    var $target = $(target)
    var data    = $target.data('bs.collapse')
    var option  = data ? 'toggle' : $this.data()
    var parent  = $this.attr('data-parent')
169
    var $parent = parent && $(parent)
fat's avatar
fat committed
170

fat's avatar
fat committed
171
172
    if (!data || !data.transitioning) {
      if ($parent) $parent.find('[data-toggle=collapse][data-parent=' + parent + ']').not($this).addClass('collapsed')
fat's avatar
fat committed
173
      $this[$target.hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
fat's avatar
fat committed
174
175
    }

fat's avatar
fat committed
176
    $target.collapse(option)
177
178
  })

179
}(window.jQuery);