0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-10 22:24:19 +01:00
Bootstrap/js/collapse.js

456 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-01-03 22:58:44 +01:00
/** =======================================================================
* Bootstrap: collapse.js v4.0.0
* http://getbootstrap.com/javascript/#collapse
2013-05-22 04:30:33 +02:00
* ========================================================================
2015-01-01 01:23:48 +01:00
* Copyright 2011-2015 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2015-01-03 22:58:44 +01:00
* ========================================================================
* @fileoverview - Bootstrap's collapse plugin. Flexible support for
* collapsible components like accordions and navigation.
*
* Public Methods & Properties:
*
* + $.carousel
* + $.carousel.noConflict
* + $.carousel.Constructor
* + $.carousel.Constructor.VERSION
* + $.carousel.Constructor.Defaults
* + $.carousel.Constructor.Defaults.toggle
* + $.carousel.Constructor.Defaults.trigger
* + $.carousel.Constructor.Defaults.parent
* + $.carousel.Constructor.prototype.toggle
* + $.carousel.Constructor.prototype.show
* + $.carousel.Constructor.prototype.hide
*
* ========================================================================
*/
2015-01-03 22:58:44 +01:00
'use strict';
2014-06-10 07:18:05 +02:00
2015-01-03 22:58:44 +01:00
/**
* Our collapse class.
* @param {Element!} element
* @param {Object=} opt_config
* @constructor
*/
var Collapse = function (element, opt_config) {
2015-01-03 22:58:44 +01:00
/** @private {Element} */
this._element = element
2015-01-03 22:58:44 +01:00
/** @private {Object} */
this._config = $.extend({}, Collapse['Defaults'], opt_config)
2015-01-03 22:58:44 +01:00
/** @private {Element} */
this._trigger = typeof this._config['trigger'] == 'string' ?
$(this._config['trigger'])[0] : this._config['trigger']
2015-01-03 22:58:44 +01:00
/** @private {boolean} */
this._isTransitioning = false
2015-01-03 22:58:44 +01:00
/** @private {?Element} */
this._parent = this._config['parent'] ? this._getParent() : null
2015-01-03 22:58:44 +01:00
if (!this._config['parent']) {
this._addAriaAndCollapsedClass(this._element, this._trigger)
}
2015-01-03 22:58:44 +01:00
if (this._config['toggle']) {
this['toggle']()
}
2011-12-01 07:42:22 +01:00
2015-01-03 22:58:44 +01:00
}
/**
* @const
* @type {string}
*/
Collapse['VERSION'] = '4.0.0'
/**
* @const
* @type {Object}
*/
Collapse['Defaults'] = {
'toggle' : true,
'trigger' : '[data-toggle="collapse"]',
'parent' : null
}
/**
* @const
* @type {string}
* @private
*/
Collapse._NAME = 'collapse'
/**
* @const
* @type {string}
* @private
*/
Collapse._DATA_KEY = 'bs.collapse'
/**
* @const
* @type {number}
* @private
*/
Collapse._TRANSITION_DURATION = 600
/**
* @const
* @type {Function}
* @private
*/
Collapse._JQUERY_NO_CONFLICT = $.fn[Collapse._NAME]
/**
* @const
* @enum {string}
* @private
*/
Collapse._Event = {
SHOW : 'show.bs.collapse',
SHOWN : 'shown.bs.collapse',
HIDE : 'hide.bs.collapse',
HIDDEN : 'hidden.bs.collapse'
}
/**
* @const
* @enum {string}
* @private
*/
Collapse._ClassName = {
IN : 'in',
COLLAPSE : 'collapse',
COLLAPSING : 'collapsing',
COLLAPSED : 'collapsed'
}
/**
* @const
* @enum {string}
* @private
*/
Collapse._Dimension = {
WIDTH : 'width',
HEIGHT : 'height'
}
/**
* @const
* @enum {string}
* @private
*/
Collapse._Selector = {
ACTIVES : '.panel > .in, .panel > .collapsing'
}
/**
* Provides the jQuery Interface for the alert component.
* @param {Object|string=} opt_config
* @this {jQuery}
* @return {jQuery}
* @private
*/
Collapse._jQueryInterface = function (opt_config) {
return this.each(function () {
var $this = $(this)
var data = $this.data(Collapse._DATA_KEY)
var config = $.extend({}, Collapse['Defaults'], $this.data(), typeof opt_config == 'object' && opt_config)
2015-01-03 22:58:44 +01:00
if (!data && config['toggle'] && opt_config == 'show') {
config['toggle'] = false
}
2015-01-03 22:58:44 +01:00
if (!data) {
data = new Collapse(this, config)
$this.data(Collapse._DATA_KEY, data)
}
2015-01-03 22:58:44 +01:00
if (typeof opt_config == 'string') {
data[opt_config]()
}
2015-01-03 22:58:44 +01:00
})
}
/**
* Function for getting target element from element
* @return {Element}
* @private
*/
Collapse._getTargetFromElement = function (element) {
var selector = Bootstrap.getSelectorFromElement(element)
return selector ? $(selector)[0] : null
}
2015-01-03 22:58:44 +01:00
/**
* Toggles the collapse element based on the presence of the 'in' class
*/
Collapse.prototype['toggle'] = function () {
if ($(this._element).hasClass(Collapse._ClassName.IN)) {
this['hide']()
} else {
this['show']()
}
}
2015-01-03 22:58:44 +01:00
/**
* Show's the collapsing element
*/
Collapse.prototype['show'] = function () {
if (this._isTransitioning || $(this._element).hasClass(Collapse._ClassName.IN)) {
return
}
2015-01-03 22:58:44 +01:00
var activesData, actives
2015-01-03 22:58:44 +01:00
if (this._parent) {
actives = $.makeArray($(Collapse._Selector.ACTIVES))
if (!actives.length) {
actives = null
}
2015-01-03 22:58:44 +01:00
}
2015-01-03 22:58:44 +01:00
if (actives) {
activesData = $(actives).data(Collapse._DATA_KEY)
if (activesData && activesData._isTransitioning) {
return
}
}
2015-01-03 22:58:44 +01:00
var startEvent = $.Event(Collapse._Event.SHOW)
$(this._element).trigger(startEvent)
if (startEvent.isDefaultPrevented()) {
return
}
2015-01-03 22:58:44 +01:00
if (actives) {
Collapse._jQueryInterface.call($(actives), 'hide')
if (!activesData) {
$(actives).data(Collapse._DATA_KEY, null)
}
}
2015-01-03 22:58:44 +01:00
var dimension = this._getDimension()
2013-07-18 04:28:43 +02:00
2015-01-03 22:58:44 +01:00
$(this._element)
.removeClass(Collapse._ClassName.COLLAPSE)
.addClass(Collapse._ClassName.COLLAPSING)
2013-07-18 04:28:43 +02:00
2015-01-03 22:58:44 +01:00
this._element.style[dimension] = 0
this._element.setAttribute('aria-expanded', true)
2015-01-03 22:58:44 +01:00
if (this._trigger) {
$(this._trigger).removeClass(Collapse._ClassName.COLLAPSED)
this._trigger.setAttribute('aria-expanded', true)
}
2015-01-03 22:58:44 +01:00
this['setTransitioning'](true)
2015-01-03 22:58:44 +01:00
var complete = function () {
$(this._element)
.removeClass(Collapse._ClassName.COLLAPSING)
.addClass(Collapse._ClassName.COLLAPSE)
.addClass(Collapse._ClassName.IN)
2015-01-03 22:58:44 +01:00
this._element.style[dimension] = ''
2015-01-03 22:58:44 +01:00
this['setTransitioning'](false)
2015-01-03 22:58:44 +01:00
$(this._element).trigger(Collapse._Event.SHOWN)
}.bind(this)
2015-01-03 22:58:44 +01:00
if (!Bootstrap.transition) {
complete()
return
}
2015-01-03 22:58:44 +01:00
var scrollSize = 'scroll' + (dimension[0].toUpperCase() + dimension.slice(1))
$(this._element)
.one(Bootstrap.TRANSITION_END, complete)
.emulateTransitionEnd(Collapse._TRANSITION_DURATION)
this._element.style[dimension] = this._element[scrollSize] + 'px'
}
/**
* Hides's the collapsing element
*/
Collapse.prototype['hide'] = function () {
if (this._isTransitioning || !$(this._element).hasClass(Collapse._ClassName.IN)) {
return
}
2015-01-03 22:58:44 +01:00
var startEvent = $.Event(Collapse._Event.HIDE)
$(this._element).trigger(startEvent)
if (startEvent.isDefaultPrevented()) return
var dimension = this._getDimension()
var offsetDimension = dimension === Collapse._Dimension.WIDTH ?
'offsetWidth' : 'offsetHeight'
this._element.style[dimension] = this._element[offsetDimension] + 'px'
Bootstrap.reflow(this._element)
$(this._element)
.addClass(Collapse._ClassName.COLLAPSING)
.removeClass(Collapse._ClassName.COLLAPSE)
.removeClass(Collapse._ClassName.IN)
this._element.setAttribute('aria-expanded', false)
if (this._trigger) {
$(this._trigger).addClass(Collapse._ClassName.COLLAPSED)
this._trigger.setAttribute('aria-expanded', false)
}
2015-01-03 22:58:44 +01:00
this['setTransitioning'](true)
var complete = function () {
this['setTransitioning'](false)
$(this._element)
.removeClass(Collapse._ClassName.COLLAPSING)
.addClass(Collapse._ClassName.COLLAPSE)
.trigger(Collapse._Event.HIDDEN)
}.bind(this)
2015-01-03 22:58:44 +01:00
this._element.style[dimension] = 0
if (!Bootstrap.transition) {
return complete()
}
2015-01-03 22:58:44 +01:00
$(this._element)
.one(Bootstrap.TRANSITION_END, complete)
.emulateTransitionEnd(Collapse._TRANSITION_DURATION)
}
2015-01-03 22:58:44 +01:00
/**
* @param {boolean} isTransitioning
*/
Collapse.prototype['setTransitioning'] = function (isTransitioning) {
this._isTransitioning = isTransitioning
}
/**
* Returns the collapsing dimension
* @return {string}
* @private
*/
Collapse.prototype._getDimension = function () {
var hasWidth = $(this._element).hasClass(Collapse._Dimension.WIDTH)
return hasWidth ? Collapse._Dimension.WIDTH : Collapse._Dimension.HEIGHT
}
/**
* Returns the parent element
* @return {Element}
* @private
*/
Collapse.prototype._getParent = function () {
var selector = '[data-toggle="collapse"][data-parent="' + this._config['parent'] + '"]'
var parent = $(this._config['parent'])[0]
var elements = /** @type {Array.<Element>} */ ($.makeArray($(parent).find(selector)))
for (var i = 0; i < elements.length; i++) {
this._addAriaAndCollapsedClass(Collapse._getTargetFromElement(elements[i]), elements[i])
}
2015-01-03 22:58:44 +01:00
return parent
}
2013-05-16 20:20:18 +02:00
2015-01-03 22:58:44 +01:00
/**
* Returns the parent element
* @param {Element} element
* @param {Element} trigger
* @private
*/
Collapse.prototype._addAriaAndCollapsedClass = function (element, trigger) {
if (element) {
var isOpen = $(element).hasClass(Collapse._ClassName.IN)
element.setAttribute('aria-expanded', isOpen)
2015-01-03 22:58:44 +01:00
if (trigger) {
trigger.setAttribute('aria-expanded', isOpen)
$(trigger).toggleClass(Collapse._ClassName.COLLAPSED, !isOpen)
}
}
2015-01-03 22:58:44 +01:00
}
2015-01-03 22:58:44 +01:00
/**
* ------------------------------------------------------------------------
* jQuery Interface + noConflict implementaiton
* ------------------------------------------------------------------------
*/
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {Function}
*/
$.fn[Collapse._NAME] = Collapse._jQueryInterface
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {Function}
*/
$.fn[Collapse._NAME]['Constructor'] = Collapse
2013-05-16 20:20:18 +02:00
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {Function}
*/
$.fn[Collapse._NAME]['noConflict'] = function () {
$.fn[Collapse._NAME] = Collapse._JQUERY_NO_CONFLICT
return this
}
2015-01-03 22:58:44 +01:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2013-07-18 05:29:41 +02:00
2015-01-03 22:58:44 +01:00
$(document).on('click.bs.collapse.data-api', '[data-toggle="collapse"]', function (event) {
event.preventDefault()
var target = Collapse._getTargetFromElement(this)
var data = $(target).data(Collapse._DATA_KEY)
var config = data ? 'toggle' : $.extend({}, $(this).data(), { trigger: this })
2015-01-03 22:58:44 +01:00
Collapse._jQueryInterface.call($(target), config)
})