1 /* =============================================================
2 * bootstrap-collapse.js v2.0.4
3 * http://twitter.github.com/bootstrap/javascript.html#collapse
4 * =============================================================
5 * Copyright 2012 Twitter, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============================================================ */
23 "use strict"; // jshint ;_;
26 /* COLLAPSE PUBLIC CLASS DEFINITION
27 * ================================ */
29 var Collapse = function (element, options) {
30 this.$element = $(element)
31 this.options = $.extend({}, $.fn.collapse.defaults, options)
33 if (this.options.parent) {
34 this.$parent = $(this.options.parent)
37 this.options.toggle && this.toggle()
40 Collapse.prototype = {
44 , dimension: function () {
45 var hasWidth = this.$element.hasClass('width')
46 return hasWidth ? 'width' : 'height'
55 if (this.transitioning) return
57 dimension = this.dimension()
58 scroll = $.camelCase(['scroll', dimension].join('-'))
59 actives = this.$parent && this.$parent.find('> .accordion-group > .in')
61 if (actives && actives.length) {
62 hasData = actives.data('collapse')
63 if (hasData && hasData.transitioning) return
64 actives.collapse('hide')
65 hasData || actives.data('collapse', null)
68 this.$element[dimension](0)
69 this.transition('addClass', $.Event('show'), 'shown')
70 this.$element[dimension](this.$element[0][scroll])
75 if (this.transitioning) return
76 dimension = this.dimension()
77 this.reset(this.$element[dimension]())
78 this.transition('removeClass', $.Event('hide'), 'hidden')
79 this.$element[dimension](0)
82 , reset: function (size) {
83 var dimension = this.dimension()
86 .removeClass('collapse')
87 [dimension](size || 'auto')
90 this.$element[size !== null ? 'addClass' : 'removeClass']('collapse')
95 , transition: function (method, startEvent, completeEvent) {
97 , complete = function () {
98 if (startEvent.type == 'show') that.reset()
99 that.transitioning = 0
100 that.$element.trigger(completeEvent)
103 this.$element.trigger(startEvent)
105 if (startEvent.isDefaultPrevented()) return
107 this.transitioning = 1
109 this.$element[method]('in')
111 $.support.transition && this.$element.hasClass('collapse') ?
112 this.$element.one($.support.transition.end, complete) :
116 , toggle: function () {
117 this[this.$element.hasClass('in') ? 'hide' : 'show']()
123 /* COLLAPSIBLE PLUGIN DEFINITION
124 * ============================== */
126 $.fn.collapse = function (option) {
127 return this.each(function () {
129 , data = $this.data('collapse')
130 , options = typeof option == 'object' && option
131 if (!data) $this.data('collapse', (data = new Collapse(this, options)))
132 if (typeof option == 'string') data[option]()
136 $.fn.collapse.defaults = {
140 $.fn.collapse.Constructor = Collapse
143 /* COLLAPSIBLE DATA-API
144 * ==================== */
147 $('body').on('click.collapse.data-api', '[data-toggle=collapse]', function ( e ) {
148 var $this = $(this), href
149 , target = $this.attr('data-target')
150 || e.preventDefault()
151 || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
152 , option = $(target).data('collapse') ? 'toggle' : $this.data()
153 $(target).collapse(option)