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

308 lines
7.6 KiB
JavaScript
Raw Normal View History

2015-05-09 23:00:59 -07:00
/**
* --------------------------------------------------------------------------
2021-10-09 09:33:12 +03:00
* Bootstrap (v5.1.3): 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 {
defineJQueryPlugin,
getElement,
getElementFromSelector,
getSelectorFromElement,
reflow
} from './util/index'
import EventHandler from './dom/event-handler'
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 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'
const CLASS_NAME_DEEPER_CHILDREN = `:scope .${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`
2021-06-14 23:25:11 +03:00
const CLASS_NAME_HORIZONTAL = 'collapse-horizontal'
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 = '.collapse.show, .collapse.collapsing'
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]'
2015-05-09 23:00:59 -07:00
const Default = {
toggle: true,
parent: null
}
const DefaultType = {
toggle: 'boolean',
parent: '(null|element)'
}
2018-09-26 10:39:01 +02:00
/**
* Class definition
2018-09-26 10:39:01 +02:00
*/
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) {
super(element, config)
2019-09-04 17:58:29 +03:00
2018-09-26 10:39:01 +02:00
this._isTransitioning = false
2021-06-10 02:41:30 +03:00
this._triggerArray = []
const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
2021-08-10 18:07:39 +03:00
for (const elem of toggleList) {
const selector = getSelectorFromElement(elem)
const filterElement = SelectorEngine.find(selector)
.filter(foundElem => foundElem === this._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._triggerArray.push(elem)
}
2018-09-26 10:39:01 +02:00
}
2021-06-10 02:51:51 +03:00
this._initializeChildren()
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
if (!this._config.parent) {
this._addAriaAndCollapsedClass(this._triggerArray, this._isShown())
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 (this._config.toggle) {
this.toggle()
}
2018-09-26 10:39:01 +02:00
}
2018-09-26 10:39:01 +02:00
// Getters
static get Default() {
return Default
}
static get DefaultType() {
return DefaultType
}
static get NAME() {
return NAME
2019-09-04 17:58:29 +03:00
}
2018-09-26 10:39:01 +02:00
// Public
toggle() {
if (this._isShown()) {
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() {
if (this._isTransitioning || this._isShown()) {
2018-09-26 10:39:01 +02:00
return
}
2015-05-09 23:00:59 -07:00
let activeChildren = []
2018-09-10 11:01:14 +02:00
// find active children
2021-06-10 02:51:51 +03:00
if (this._config.parent) {
activeChildren = this._getFirstLevelChildren(SELECTOR_ACTIVES)
.filter(element => element !== this._element)
.map(element => Collapse.getOrCreateInstance(element, { toggle: false }))
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
if (activeChildren.length && activeChildren[0]._isTransitioning) {
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
}
for (const activeInstance of activeChildren) {
activeInstance.hide()
}
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
this._addAriaAndCollapsedClass(this._triggerArray, true)
this._isTransitioning = true
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
const complete = () => {
this._isTransitioning = false
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
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}`
2015-05-09 23:00:59 -07:00
this._queueCallback(complete, this._element, true)
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() {
if (this._isTransitioning || !this._isShown()) {
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
2021-08-10 18:07:39 +03:00
for (const trigger of this._triggerArray) {
const elem = getElementFromSelector(trigger)
if (elem && !this._isShown(elem)) {
this._addAriaAndCollapsedClass([trigger], 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
this._isTransitioning = true
2018-03-13 09:59:20 +01:00
2018-09-26 10:39:01 +02:00
const complete = () => {
this._isTransitioning = 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] = ''
2015-05-09 23:00:59 -07:00
this._queueCallback(complete, this._element, true)
2018-09-26 10:39:01 +02:00
}
2015-05-13 12:48:34 -07:00
_isShown(element = this._element) {
return element.classList.contains(CLASS_NAME_SHOW)
2018-09-26 10:39:01 +02:00
}
2015-05-13 12:48:34 -07:00
2018-09-26 10:39:01 +02:00
// Private
_configAfterMerge(config) {
2018-09-26 10:39:01 +02:00
config.toggle = Boolean(config.toggle) // Coerce string values
2021-06-10 02:51:51 +03:00
config.parent = getElement(config.parent)
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() {
2021-06-14 23:25:11 +03:00
return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT
2018-09-26 10:39:01 +02:00
}
2018-09-10 11:01:14 +02:00
2021-06-10 02:51:51 +03:00
_initializeChildren() {
if (!this._config.parent) {
return
}
2015-05-09 23:00:59 -07:00
const children = this._getFirstLevelChildren(SELECTOR_DATA_TOGGLE)
2019-04-03 20:38:28 +02:00
for (const element of children) {
const selected = getElementFromSelector(element)
if (selected) {
this._addAriaAndCollapsedClass([element], this._isShown(selected))
}
}
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
_getFirstLevelChildren(selector) {
const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent)
// remove children if greater depth
return SelectorEngine.find(selector, this._config.parent).filter(elem => !children.includes(elem))
}
_addAriaAndCollapsedClass(triggerArray, isOpen) {
if (!triggerArray.length) {
2020-06-10 18:42:21 +03:00
return
}
2019-02-26 13:20:34 +02:00
for (const elem of triggerArray) {
2020-06-10 18:42:21 +03:00
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
static jQueryInterface(config) {
return this.each(function () {
const _config = {}
if (typeof config === 'string' && /show|hide/.test(config)) {
_config.toggle = false
2018-09-26 10:39:01 +02:00
}
2019-02-26 13:20:34 +02:00
const data = Collapse.getOrCreateInstance(this, _config)
2017-09-15 13:13:14 +02:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[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
2018-09-26 10:39:01 +02:00
*/
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
if (event.target.tagName === 'A' || (event.delegateTarget && event.delegateTarget.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 selector = getSelectorFromElement(this)
const selectorElements = SelectorEngine.find(selector)
2017-09-15 13:13:14 +02:00
for (const element of selectorElements) {
Collapse.getOrCreateInstance(element, { toggle: false }).toggle()
}
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
*/
defineJQueryPlugin(Collapse)
2015-05-09 23:00:59 -07:00
export default Collapse