0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-15 12:54:23 +01:00
Bootstrap/js/src/collapse.js

425 lines
11 KiB
JavaScript
Raw Normal View History

2015-05-09 23:00:59 -07:00
/**
* --------------------------------------------------------------------------
2020-11-24 16:37:20 +02:00
* Bootstrap (v5.0.0-beta1): collapse.js
2020-06-16 21:41:47 +03:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2015-05-09 23:00:59 -07:00
* --------------------------------------------------------------------------
*/
import {
2019-08-02 15:51:05 +02:00
getjQuery,
onDOMContentLoaded,
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
isElement,
reflow,
typeCheckConfig
} from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
2019-09-04 17:58:29 +03:00
import BaseComponent from './base-component'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2015-05-09 23:00:59 -07:00
2019-02-26 13:20:34 +02:00
const NAME = 'collapse'
const DATA_KEY = 'bs.collapse'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
2018-09-26 10:39:01 +02:00
const Default = {
2019-02-26 13:20:34 +02:00
toggle: true,
parent: ''
2018-09-26 10:39:01 +02:00
}
const DefaultType = {
2019-02-26 13:20:34 +02:00
toggle: 'boolean',
parent: '(string|element)'
2018-09-26 10:39:01 +02:00
}
const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const EVENT_HIDE = `hide${EVENT_KEY}`
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
2018-09-26 10:39:01 +02:00
const CLASS_NAME_SHOW = 'show'
const CLASS_NAME_COLLAPSE = 'collapse'
const CLASS_NAME_COLLAPSING = 'collapsing'
const CLASS_NAME_COLLAPSED = 'collapsed'
2018-09-26 10:39:01 +02:00
const WIDTH = 'width'
const HEIGHT = 'height'
2018-09-26 10:39:01 +02:00
const SELECTOR_ACTIVES = '.show, .collapsing'
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]'
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-09 23:00:59 -07:00
2019-09-04 17:58:29 +03:00
class Collapse extends BaseComponent {
2018-09-26 10:39:01 +02:00
constructor(element, config) {
2019-09-04 17:58:29 +03:00
super(element)
2018-09-26 10:39:01 +02:00
this._isTransitioning = false
2019-02-26 13:20:34 +02:00
this._config = this._getConfig(config)
this._triggerArray = SelectorEngine.find(
`${SELECTOR_DATA_TOGGLE}[href="#${element.id}"],` +
`${SELECTOR_DATA_TOGGLE}[data-bs-target="#${element.id}"]`
)
const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
2018-09-26 10:39:01 +02:00
for (let i = 0, len = toggleList.length; i < len; i++) {
const elem = toggleList[i]
const selector = getSelectorFromElement(elem)
const filterElement = SelectorEngine.find(selector)
2019-02-26 13:20:34 +02:00
.filter(foundElem => foundElem === element)
2018-09-26 10:39:01 +02:00
2017-09-15 13:13:14 +02:00
if (selector !== null && filterElement.length) {
2018-09-26 10:39:01 +02:00
this._selector = selector
this._triggerArray.push(elem)
}
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
this._parent = this._config.parent ? this._getParent() : null
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
if (!this._config.parent) {
this._addAriaAndCollapsedClass(this._element, this._triggerArray)
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
if (this._config.toggle) {
this.toggle()
}
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
// Getters
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
static get Default() {
return Default
}
2019-09-04 17:58:29 +03:00
static get DATA_KEY() {
return DATA_KEY
}
2018-09-26 10:39:01 +02:00
// Public
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
toggle() {
if (this._element.classList.contains(CLASS_NAME_SHOW)) {
2018-09-26 10:39:01 +02:00
this.hide()
} else {
this.show()
2015-05-09 23:00:59 -07:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
show() {
2020-12-02 06:45:15 +02:00
if (this._isTransitioning || this._element.classList.contains(CLASS_NAME_SHOW)) {
2018-09-26 10:39:01 +02:00
return
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
let actives
let activesData
2018-09-10 11:01:14 +02:00
2018-09-26 10:39:01 +02:00
if (this._parent) {
actives = SelectorEngine.find(SELECTOR_ACTIVES, this._parent)
2019-02-26 13:20:34 +02:00
.filter(elem => {
2018-09-26 10:39:01 +02:00
if (typeof this._config.parent === 'string') {
return elem.getAttribute('data-bs-parent') === this._config.parent
2018-09-26 10:39:01 +02:00
}
return elem.classList.contains(CLASS_NAME_COLLAPSE)
2018-09-26 10:39:01 +02:00
})
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
if (actives.length === 0) {
actives = null
2015-05-09 23:00:59 -07:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
const container = SelectorEngine.findOne(this._selector)
2018-09-26 10:39:01 +02:00
if (actives) {
const tempActiveData = actives.find(elem => container !== elem)
activesData = tempActiveData ? Data.getData(tempActiveData, DATA_KEY) : null
2018-09-26 10:39:01 +02:00
if (activesData && activesData._isTransitioning) {
2015-05-09 23:00:59 -07:00
return
}
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
const startEvent = EventHandler.trigger(this._element, EVENT_SHOW)
2017-09-15 13:13:14 +02:00
if (startEvent.defaultPrevented) {
2018-09-26 10:39:01 +02:00
return
}
if (actives) {
2019-02-26 13:20:34 +02:00
actives.forEach(elemActive => {
if (container !== elemActive) {
2019-07-28 15:24:46 +02:00
Collapse.collapseInterface(elemActive, 'hide')
2018-03-05 15:40:23 +01:00
}
if (!activesData) {
Data.setData(elemActive, DATA_KEY, null)
}
2018-03-05 15:40:23 +01:00
})
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
const dimension = this._getDimension()
2015-05-09 23:00:59 -07:00
this._element.classList.remove(CLASS_NAME_COLLAPSE)
this._element.classList.add(CLASS_NAME_COLLAPSING)
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
this._element.style[dimension] = 0
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
if (this._triggerArray.length) {
2019-02-26 13:20:34 +02:00
this._triggerArray.forEach(element => {
element.classList.remove(CLASS_NAME_COLLAPSED)
2017-09-15 13:13:14 +02:00
element.setAttribute('aria-expanded', true)
})
2018-09-26 10:39:01 +02:00
}
this.setTransitioning(true)
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
const complete = () => {
this._element.classList.remove(CLASS_NAME_COLLAPSING)
this._element.classList.add(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW)
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
this._element.style[dimension] = ''
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
this.setTransitioning(false)
2015-05-09 23:00:59 -07:00
EventHandler.trigger(this._element, EVENT_SHOWN)
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1)
const scrollSize = `scroll${capitalizedDimension}`
const transitionDuration = getTransitionDurationFromElement(this._element)
2015-05-09 23:00:59 -07:00
EventHandler.one(this._element, TRANSITION_END, complete)
2015-05-09 23:00:59 -07:00
emulateTransitionEnd(this._element, transitionDuration)
2018-09-26 10:39:01 +02:00
this._element.style[dimension] = `${this._element[scrollSize]}px`
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
hide() {
2020-12-02 06:45:15 +02:00
if (this._isTransitioning || !this._element.classList.contains(CLASS_NAME_SHOW)) {
2018-09-26 10:39:01 +02:00
return
2015-05-09 23:00:59 -07:00
}
const startEvent = EventHandler.trigger(this._element, EVENT_HIDE)
2017-09-15 13:13:14 +02:00
if (startEvent.defaultPrevented) {
2018-09-26 10:39:01 +02:00
return
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
const dimension = this._getDimension()
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px`
2015-05-09 23:00:59 -07:00
reflow(this._element)
2015-05-09 23:00:59 -07:00
this._element.classList.add(CLASS_NAME_COLLAPSING)
this._element.classList.remove(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW)
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
const triggerArrayLength = this._triggerArray.length
if (triggerArrayLength > 0) {
for (let i = 0; i < triggerArrayLength; i++) {
const trigger = this._triggerArray[i]
const elem = getElementFromSelector(trigger)
if (elem && !elem.classList.contains(CLASS_NAME_SHOW)) {
trigger.classList.add(CLASS_NAME_COLLAPSED)
trigger.setAttribute('aria-expanded', false)
}
2015-05-09 23:00:59 -07:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
this.setTransitioning(true)
2018-03-13 09:59:20 +01:00
2018-09-26 10:39:01 +02:00
const complete = () => {
this.setTransitioning(false)
this._element.classList.remove(CLASS_NAME_COLLAPSING)
this._element.classList.add(CLASS_NAME_COLLAPSE)
EventHandler.trigger(this._element, EVENT_HIDDEN)
2015-05-09 23:00:59 -07:00
}
2018-09-26 10:39:01 +02:00
this._element.style[dimension] = ''
const transitionDuration = getTransitionDurationFromElement(this._element)
2015-05-09 23:00:59 -07:00
EventHandler.one(this._element, TRANSITION_END, complete)
emulateTransitionEnd(this._element, transitionDuration)
2018-09-26 10:39:01 +02:00
}
2015-05-13 12:48:34 -07:00
2018-09-26 10:39:01 +02:00
setTransitioning(isTransitioning) {
this._isTransitioning = isTransitioning
}
2015-05-13 12:48:34 -07:00
2018-09-26 10:39:01 +02:00
dispose() {
2020-11-20 11:13:11 +01:00
super.dispose()
2019-02-26 13:20:34 +02:00
this._config = null
this._parent = null
this._triggerArray = null
2018-09-26 10:39:01 +02:00
this._isTransitioning = null
}
// Private
2015-05-13 14:46:50 -07:00
2018-09-26 10:39:01 +02:00
_getConfig(config) {
config = {
...Default,
...config
2015-05-09 23:00:59 -07:00
}
2018-09-26 10:39:01 +02:00
config.toggle = Boolean(config.toggle) // Coerce string values
typeCheckConfig(NAME, config, DefaultType)
2018-09-26 10:39:01 +02:00
return config
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
_getDimension() {
2020-06-10 18:41:32 +03:00
return this._element.classList.contains(WIDTH) ? WIDTH : HEIGHT
2018-09-26 10:39:01 +02:00
}
2018-09-10 11:01:14 +02:00
2018-09-26 10:39:01 +02:00
_getParent() {
2019-02-28 17:22:41 +02:00
let { parent } = this._config
2018-09-26 10:39:01 +02:00
2019-02-28 17:22:41 +02:00
if (isElement(parent)) {
2017-10-02 14:14:07 +02:00
// it's a jQuery object
2019-02-28 17:22:41 +02:00
if (typeof parent.jquery !== 'undefined' || typeof parent[0] !== 'undefined') {
parent = parent[0]
}
2018-09-26 10:39:01 +02:00
} else {
2019-02-28 17:22:41 +02:00
parent = SelectorEngine.findOne(parent)
2018-09-26 10:39:01 +02:00
}
const selector = `${SELECTOR_DATA_TOGGLE}[data-bs-parent="${parent}"]`
2015-05-09 23:00:59 -07:00
SelectorEngine.find(selector, parent)
2019-02-26 13:20:34 +02:00
.forEach(element => {
const selected = getElementFromSelector(element)
2019-04-03 20:38:28 +02:00
2018-09-14 14:27:30 +02:00
this._addAriaAndCollapsedClass(
2019-04-03 20:38:28 +02:00
selected,
2018-09-14 14:27:30 +02:00
[element]
)
})
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
return parent
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
_addAriaAndCollapsedClass(element, triggerArray) {
2020-06-10 18:42:21 +03:00
if (!element || !triggerArray.length) {
return
}
2019-02-26 13:20:34 +02:00
2020-06-10 18:42:21 +03:00
const isOpen = element.classList.contains(CLASS_NAME_SHOW)
triggerArray.forEach(elem => {
if (isOpen) {
elem.classList.remove(CLASS_NAME_COLLAPSED)
} else {
elem.classList.add(CLASS_NAME_COLLAPSED)
2017-09-15 13:13:14 +02:00
}
2020-06-10 18:42:21 +03:00
elem.setAttribute('aria-expanded', isOpen)
})
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
// Static
2015-05-09 23:00:59 -07:00
2019-07-28 15:24:46 +02:00
static collapseInterface(element, config) {
2019-02-26 13:20:34 +02:00
let data = Data.getData(element, DATA_KEY)
2017-09-15 13:13:14 +02:00
const _config = {
...Default,
...Manipulator.getDataAttributes(element),
...(typeof config === 'object' && config ? config : {})
2017-09-15 13:13:14 +02:00
}
2015-05-09 23:00:59 -07:00
if (!data && _config.toggle && typeof config === 'string' && /show|hide/.test(config)) {
2017-09-15 13:13:14 +02:00
_config.toggle = false
}
2015-05-09 23:00:59 -07:00
2017-09-15 13:13:14 +02:00
if (!data) {
data = new Collapse(element, _config)
}
2015-05-09 23:00:59 -07:00
2017-09-15 13:13:14 +02:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
2019-02-26 13:20:34 +02:00
throw new TypeError(`No method named "${config}"`)
2018-09-26 10:39:01 +02:00
}
2019-02-26 13:20:34 +02:00
2017-09-15 13:13:14 +02:00
data[config]()
}
}
2019-07-28 15:24:46 +02:00
static jQueryInterface(config) {
2017-09-15 13:13:14 +02:00
return this.each(function () {
2019-07-28 15:24:46 +02:00
Collapse.collapseInterface(this, config)
2018-09-26 10:39:01 +02:00
})
2015-05-09 23:00:59 -07:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-09 23:00:59 -07:00
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
2018-09-26 10:39:01 +02:00
// preventDefault only for <a> elements (which change the URL) not inside the collapsible element
2017-09-15 13:13:14 +02:00
if (event.target.tagName === 'A') {
2018-09-26 10:39:01 +02:00
event.preventDefault()
}
2015-05-09 23:00:59 -07:00
2019-02-26 13:20:34 +02:00
const triggerData = Manipulator.getDataAttributes(this)
const selector = getSelectorFromElement(this)
const selectorElements = SelectorEngine.find(selector)
2017-09-15 13:13:14 +02:00
2019-02-26 13:20:34 +02:00
selectorElements.forEach(element => {
2017-09-15 13:13:14 +02:00
const data = Data.getData(element, DATA_KEY)
let config
if (data) {
// update parent attribute
if (data._parent === null && typeof triggerData.parent === 'string') {
data._config.parent = triggerData.parent
data._parent = data._getParent()
}
2019-02-26 13:20:34 +02:00
2017-09-15 13:13:14 +02:00
config = 'toggle'
} else {
config = triggerData
}
2019-07-28 15:24:46 +02:00
Collapse.collapseInterface(element, config)
2015-05-09 23:00:59 -07:00
})
2018-09-26 10:39:01 +02:00
})
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-01 15:49:51 +02:00
* add .Collapse to jQuery only if jQuery is present
2018-09-26 10:39:01 +02:00
*/
onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Collapse.jQueryInterface
$.fn[NAME].Constructor = Collapse
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Collapse.jQueryInterface
}
2017-09-15 13:13:14 +02:00
}
})
2015-05-09 23:00:59 -07:00
export default Collapse