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

442 lines
11 KiB
JavaScript
Raw Normal View History

2015-05-09 23:00:59 -07:00
/**
* --------------------------------------------------------------------------
2019-02-13 18:01:40 +02:00
* Bootstrap (v4.3.1): collapse.js
2015-05-09 23:00:59 -07:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
import {
2019-08-02 15:51:05 +02:00
getjQuery,
TRANSITION_END,
emulateTransitionEnd,
getSelectorFromElement,
getElementFromSelector,
getTransitionDurationFromElement,
isElement,
makeArray,
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'
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 VERSION = '4.3.1'
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 = {
2019-02-26 13:20:34 +02:00
SHOW: `show${EVENT_KEY}`,
SHOWN: `shown${EVENT_KEY}`,
HIDE: `hide${EVENT_KEY}`,
HIDDEN: `hidden${EVENT_KEY}`,
CLICK_DATA_API: `click${EVENT_KEY}${DATA_API_KEY}`
2018-09-26 10:39:01 +02:00
}
const ClassName = {
2019-02-26 13:20:34 +02:00
SHOW: 'show',
COLLAPSE: 'collapse',
COLLAPSING: 'collapsing',
COLLAPSED: 'collapsed'
2018-09-26 10:39:01 +02:00
}
const Dimension = {
2019-02-26 13:20:34 +02:00
WIDTH: 'width',
HEIGHT: 'height'
2018-09-26 10:39:01 +02:00
}
const Selector = {
2019-02-26 13:20:34 +02:00
ACTIVES: '.show, .collapsing',
DATA_TOGGLE: '[data-toggle="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
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
class Collapse {
constructor(element, config) {
this._isTransitioning = false
2019-02-26 13:20:34 +02:00
this._element = element
this._config = this._getConfig(config)
this._triggerArray = makeArray(SelectorEngine.find(
`${Selector.DATA_TOGGLE}[href="#${element.id}"],` +
`${Selector.DATA_TOGGLE}[data-target="#${element.id}"]`
2018-09-26 10:39:01 +02:00
))
const toggleList = makeArray(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 = makeArray(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()
}
Data.setData(element, DATA_KEY, this)
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 VERSION() {
return VERSION
}
2015-05-09 23:00:59 -07:00
2018-09-26 10:39:01 +02:00
static get Default() {
return Default
}
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() {
2017-09-15 13:13:14 +02:00
if (this._element.classList.contains(ClassName.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() {
if (this._isTransitioning ||
2017-09-15 13:13:14 +02:00
this._element.classList.contains(ClassName.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 = makeArray(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-parent') === this._config.parent
}
2018-09-26 10:39:01 +02:00
return elem.classList.contains(ClassName.COLLAPSE)
})
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) {
2019-02-26 13:20:34 +02:00
const tempActiveData = actives.filter(elem => container !== elem)
2018-03-05 15:40:23 +01:00
activesData = tempActiveData[0] ? Data.getData(tempActiveData[0], 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
2017-09-15 13:13:14 +02:00
const startEvent = EventHandler.trigger(this._element, Event.SHOW)
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
2017-09-15 13:13:14 +02:00
this._element.classList.remove(ClassName.COLLAPSE)
this._element.classList.add(ClassName.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 => {
2017-09-15 13:13:14 +02:00
element.classList.remove(ClassName.COLLAPSED)
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 = () => {
2017-09-15 13:13:14 +02:00
this._element.classList.remove(ClassName.COLLAPSING)
this._element.classList.add(ClassName.COLLAPSE)
this._element.classList.add(ClassName.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
2017-09-15 13:13:14 +02: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() {
if (this._isTransitioning ||
2017-09-15 13:13:14 +02:00
!this._element.classList.contains(ClassName.SHOW)) {
2018-09-26 10:39:01 +02:00
return
2015-05-09 23:00:59 -07:00
}
2017-09-15 13:13:14 +02:00
const startEvent = EventHandler.trigger(this._element, Event.HIDE)
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
2017-09-15 13:13:14 +02:00
this._element.classList.add(ClassName.COLLAPSING)
this._element.classList.remove(ClassName.COLLAPSE)
this._element.classList.remove(ClassName.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(ClassName.SHOW)) {
trigger.classList.add(ClassName.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)
2017-09-15 13:13:14 +02:00
this._element.classList.remove(ClassName.COLLAPSING)
this._element.classList.add(ClassName.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() {
2017-09-15 13:13:14 +02:00
Data.removeData(this._element, DATA_KEY)
2015-05-09 23:00:59 -07:00
2019-02-26 13:20:34 +02:00
this._config = null
this._parent = null
this._element = 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() {
2017-09-15 13:13:14 +02:00
const hasWidth = this._element.classList.contains(Dimension.WIDTH)
2018-09-26 10:39:01 +02:00
return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT
}
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-parent="${parent}"]`
2015-05-09 23:00:59 -07:00
makeArray(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) {
2017-09-15 13:13:14 +02:00
if (element) {
const isOpen = element.classList.contains(ClassName.SHOW)
if (triggerArray.length) {
2019-02-26 13:20:34 +02:00
triggerArray.forEach(elem => {
if (isOpen) {
2017-09-15 13:13:14 +02:00
elem.classList.remove(ClassName.COLLAPSED)
2019-02-26 13:20:34 +02:00
} else {
elem.classList.add(ClassName.COLLAPSED)
2017-09-15 13:13:14 +02:00
}
2019-02-26 13:20:34 +02:00
2017-09-15 13:13:14 +02:00
elem.setAttribute('aria-expanded', isOpen)
})
}
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
// 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),
2017-09-15 13:13:14 +02:00
...typeof config === 'object' && config ? config : {}
}
2015-05-09 23:00:59 -07:00
2017-09-15 13:13:14 +02:00
if (!data && _config.toggle && /show|hide/.test(config)) {
_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
}
2019-07-28 15:24:46 +02:00
static getInstance(element) {
return Data.getData(element, DATA_KEY)
}
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
2017-09-15 13:13:14 +02: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 = makeArray(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
2019-08-02 15:51:05 +02:00
const $ = getjQuery()
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2017-09-15 13:13:14 +02:00
* add .collapse to jQuery only if jQuery is present
2018-09-26 10:39:01 +02:00
*/
2019-04-03 20:38:28 +02:00
/* istanbul ignore if */
2019-08-02 15:51:05 +02:00
if ($) {
2019-02-26 13:20:34 +02:00
const JQUERY_NO_CONFLICT = $.fn[NAME]
2019-07-28 15:24:46 +02:00
$.fn[NAME] = Collapse.jQueryInterface
2019-02-26 13:20:34 +02:00
$.fn[NAME].Constructor = Collapse
$.fn[NAME].noConflict = () => {
2017-09-15 13:13:14 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT
2019-07-28 15:24:46 +02:00
return Collapse.jQueryInterface
2017-09-15 13:13:14 +02:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-09 23:00:59 -07:00
export default Collapse