2015-05-09 23:00:59 -07:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2020-09-29 18:33:00 +03:00
|
|
|
* Bootstrap (v5.0.0-alpha2): 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
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
import {
|
2019-08-02 15:51:05 +02:00
|
|
|
getjQuery,
|
2019-02-23 00:37:55 +02:00
|
|
|
TRANSITION_END,
|
|
|
|
emulateTransitionEnd,
|
|
|
|
getSelectorFromElement,
|
2019-07-23 21:15:00 +02:00
|
|
|
getElementFromSelector,
|
2019-02-23 00:37:55 +02:00
|
|
|
getTransitionDurationFromElement,
|
|
|
|
isElement,
|
|
|
|
reflow,
|
|
|
|
typeCheckConfig
|
2019-10-02 11:43:54 +02:00
|
|
|
} 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-11-14 10:16:56 +01:00
|
|
|
|
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'
|
2020-09-29 18:33:00 +03:00
|
|
|
const VERSION = '5.0.0-alpha2'
|
2019-02-26 13:20:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-07 11:31:42 +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
|
|
|
|
2020-03-07 11:31:42 +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
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
const WIDTH = 'width'
|
|
|
|
const HEIGHT = 'height'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
const SELECTOR_ACTIVES = '.show, .collapsing'
|
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-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
|
|
|
|
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)
|
2020-03-25 15:35:02 +01:00
|
|
|
this._triggerArray = SelectorEngine.find(
|
2020-03-07 11:31:42 +02:00
|
|
|
`${SELECTOR_DATA_TOGGLE}[href="#${element.id}"],` +
|
|
|
|
`${SELECTOR_DATA_TOGGLE}[data-target="#${element.id}"]`
|
2020-03-25 15:35:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
for (let i = 0, len = toggleList.length; i < len; i++) {
|
|
|
|
const elem = toggleList[i]
|
2019-02-23 00:37:55 +02:00
|
|
|
const selector = getSelectorFromElement(elem)
|
2020-03-25 15:35:02 +01:00
|
|
|
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)
|
2017-06-14 13:21:49 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-04-25 03:32:14 -04: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()
|
2015-05-10 19:45:38 -07:00
|
|
|
}
|
2018-06-24 22:02:03 +02:00
|
|
|
|
|
|
|
Data.setData(element, DATA_KEY, this)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-10 19:45:38 -07: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
|
|
|
|
}
|
2015-05-10 19:45:38 -07:00
|
|
|
|
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() {
|
2020-03-07 11:31:42 +02:00
|
|
|
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() {
|
|
|
|
if (this._isTransitioning ||
|
2020-03-07 11:31:42 +02:00
|
|
|
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) {
|
2020-03-25 15:35:02 +01:00
|
|
|
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-parent') === this._config.parent
|
|
|
|
}
|
2018-04-30 16:37:45 +02:00
|
|
|
|
2020-03-07 11:31:42 +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
|
|
|
|
2018-03-11 16:18:56 +01: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-03-11 16:18:56 +01:00
|
|
|
|
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
|
|
|
|
2020-03-07 11:31:42 +02: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 => {
|
2018-03-11 16:18:56 +01:00
|
|
|
if (container !== elemActive) {
|
2019-07-28 15:24:46 +02:00
|
|
|
Collapse.collapseInterface(elemActive, 'hide')
|
2018-03-05 15:40:23 +01:00
|
|
|
}
|
2018-03-11 16:18:56 +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
|
|
|
|
2020-03-07 11:31:42 +02: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 => {
|
2020-03-07 11:31:42 +02:00
|
|
|
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 = () => {
|
2020-03-07 11:31:42 +02:00
|
|
|
this._element.classList.remove(CLASS_NAME_COLLAPSING)
|
2020-04-09 20:56:43 +03:00
|
|
|
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
|
|
|
|
2020-03-07 11:31:42 +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}`
|
2019-02-23 00:37:55 +02:00
|
|
|
const transitionDuration = getTransitionDurationFromElement(this._element)
|
2015-05-09 23:00:59 -07:00
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
EventHandler.one(this._element, TRANSITION_END, complete)
|
2015-05-09 23:00:59 -07:00
|
|
|
|
2019-02-23 00:37:55 +02: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 ||
|
2020-03-07 11:31:42 +02:00
|
|
|
!this._element.classList.contains(CLASS_NAME_SHOW)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
2015-05-09 23:00:59 -07:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:31:42 +02: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
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
reflow(this._element)
|
2015-05-09 23:00:59 -07:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
this._element.classList.add(CLASS_NAME_COLLAPSING)
|
2020-04-09 20:56:43 +03:00
|
|
|
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]
|
2019-07-23 21:15:00 +02:00
|
|
|
const elem = getElementFromSelector(trigger)
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
if (elem && !elem.classList.contains(CLASS_NAME_SHOW)) {
|
|
|
|
trigger.classList.add(CLASS_NAME_COLLAPSED)
|
2019-07-23 21:15:00 +02:00
|
|
|
trigger.setAttribute('aria-expanded', false)
|
2017-06-14 13:21:49 +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
|
|
|
this.setTransitioning(true)
|
2018-03-13 09:59:20 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
const complete = () => {
|
|
|
|
this.setTransitioning(false)
|
2020-03-07 11:31:42 +02:00
|
|
|
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] = ''
|
2019-02-23 00:37:55 +02:00
|
|
|
const transitionDuration = getTransitionDurationFromElement(this._element)
|
2015-05-09 23:00:59 -07:00
|
|
|
|
2019-02-23 00:37:55 +02: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
|
2019-02-23 00:37:55 +02:00
|
|
|
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]
|
2017-09-25 12:41:54 +02:00
|
|
|
}
|
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
|
|
|
}
|
2017-09-25 12:41:54 +02:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
const selector = `${SELECTOR_DATA_TOGGLE}[data-parent="${parent}"]`
|
2015-05-09 23:00:59 -07:00
|
|
|
|
2020-03-25 15:35:02 +01:00
|
|
|
SelectorEngine.find(selector, parent)
|
2019-02-26 13:20:34 +02:00
|
|
|
.forEach(element => {
|
2019-07-23 21:15:00 +02:00
|
|
|
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,
|
2018-06-09 21:11:05 +02:00
|
|
|
...Manipulator.getDataAttributes(element),
|
2020-06-26 18:50:04 +03:00
|
|
|
...(typeof config === 'object' && config ? config : {})
|
2017-09-15 13:13:14 +02:00
|
|
|
}
|
2015-05-09 23:00:59 -07:00
|
|
|
|
2020-04-17 15:09:51 +03: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-06-07 21:43:04 +02:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static getInstance(element) {
|
2018-06-07 21:43:04 +02:00
|
|
|
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
|
|
|
|
2020-03-07 11:31:42 +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)
|
2020-03-25 15:35:02 +01:00
|
|
|
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
|
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
|
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
|