bootstrap/js/collapse.js

157 lines
4.8 KiB
JavaScript
Raw Normal View History

2013-05-22 02:30:33 +00:00
/* ========================================================================
2013-05-16 18:06:30 +00:00
* Bootstrap: collapse.js v3.0.0
2012-01-24 19:08:03 +00:00
* http://twitter.github.com/bootstrap/javascript.html#collapse
2013-05-22 02:30:33 +00:00
* ========================================================================
2012-01-15 07:28:48 +00:00
* Copyright 2012 Twitter, Inc.
*
* 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.
2013-05-22 02:30:33 +00:00
* ======================================================================== */
2013-05-22 02:30:33 +00:00
+function ($) { "use strict";
2013-05-16 18:20:18 +00:00
// COLLAPSE PUBLIC CLASS DEFINITION
// ================================
var Collapse = function (element, options) {
2013-05-16 18:20:18 +00:00
this.$element = $(element)
this.options = $.extend({}, Collapse.DEFAULTS, options)
this.transitioning = null
2013-05-16 18:20:18 +00:00
if (this.options.parent) this.$parent = $(this.options.parent)
if (this.options.toggle) this.toggle()
}
2013-05-16 18:20:18 +00:00
Collapse.DEFAULTS = {
toggle: true
}
2013-05-16 18:20:18 +00:00
Collapse.prototype.dimension = function () {
var hasWidth = this.$element.hasClass('width')
return hasWidth ? 'width' : 'height'
}
2013-05-16 18:20:18 +00:00
Collapse.prototype.show = function () {
if (this.transitioning || this.$element.hasClass('in')) return
2011-12-01 06:42:22 +00:00
2013-05-16 18:20:18 +00:00
var dimension = this.dimension()
var scroll = $.camelCase(['scroll', dimension].join('-'))
var actives = this.$parent && this.$parent.find('> .accordion-group > .in')
2013-05-16 18:20:18 +00:00
if (actives && actives.length) {
var hasData = actives.data('collapse')
if (hasData && hasData.transitioning) return
actives.collapse('hide')
hasData || actives.data('collapse', null)
}
2013-05-16 18:20:18 +00:00
this.$element[dimension](0)
this.transition('addClass', $.Event('show.bs.collapse'), 'shown.bs.collapse')
2013-05-16 18:20:18 +00:00
if ($.support.transition) this.$element[dimension](this.$element[0][scroll])
}
2013-05-16 18:20:18 +00:00
Collapse.prototype.hide = function () {
if (this.transitioning || !this.$element.hasClass('in')) return
var dimension = this.dimension()
this.reset(this.$element[dimension]())
this.transition('removeClass', $.Event('hide.bs.collapse'), 'hidden')
2013-05-16 18:20:18 +00:00
this.$element[dimension](0)
}
2013-05-16 18:20:18 +00:00
Collapse.prototype.reset = function (size) {
var dimension = this.dimension()
2013-05-16 18:20:18 +00:00
this.$element
.removeClass('collapse')
[dimension](size || 'auto')
[0].offsetWidth
2013-06-28 02:19:22 +00:00
this.$element[size != null ? 'addClass' : 'removeClass']('collapse')
2013-05-16 18:20:18 +00:00
return this
}
2013-05-16 18:20:18 +00:00
Collapse.prototype.transition = function (method, startEvent, completeEvent) {
var that = this
var complete = function () {
if (startEvent.type == 'show') that.reset()
2013-05-16 18:20:18 +00:00
that.transitioning = 0
that.$element.trigger(completeEvent)
}
2013-05-16 18:20:18 +00:00
this.$element.trigger(startEvent)
2013-05-16 18:20:18 +00:00
if (startEvent.isDefaultPrevented()) return
2013-05-16 18:20:18 +00:00
this.transitioning = 1
2013-05-16 18:20:18 +00:00
this.$element[method]('in')
2013-05-16 18:20:18 +00:00
$.support.transition && this.$element.hasClass('collapse') ?
this.$element.one($.support.transition.end, complete) :
complete()
}
2013-05-16 18:20:18 +00:00
Collapse.prototype.toggle = function () {
this[this.$element.hasClass('in') ? 'hide' : 'show']()
}
2013-05-16 18:20:18 +00:00
// COLLAPSE PLUGIN DEFINITION
// ==========================
var old = $.fn.collapse
$.fn.collapse = function (option) {
return this.each(function () {
2013-05-16 18:20:18 +00:00
var $this = $(this)
var data = $this.data('collapse')
var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('collapse', (data = new Collapse(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.collapse.Constructor = Collapse
2013-05-16 18:20:18 +00:00
// COLLAPSE NO CONFLICT
// ====================
$.fn.collapse.noConflict = function () {
$.fn.collapse = old
return this
}
2013-05-16 18:20:18 +00:00
// COLLAPSE DATA-API
// =================
$(document).on('click.bs.collapse.data-api', '[data-toggle=collapse]', function (e) {
2013-05-16 18:20:18 +00:00
var $this = $(this), href
var target = $this.attr('data-target')
|| e.preventDefault()
|| (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
2013-05-16 18:20:18 +00:00
var option = $(target).data('collapse') ? 'toggle' : $this.data()
var parent = $this.attr('data-parent')
var $parent = parent && $(parent)
2013-05-16 18:20:18 +00:00
if ($parent) $parent.find('[data-toggle=collapse][data-parent=' + parent + ']').not($this).addClass('collapsed')
$this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
$(target).collapse(option)
})
}(window.jQuery);