0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-31 23:52:18 +01:00
Bootstrap/js/src/base-component.js

81 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-09-04 17:58:29 +03:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.0): base-component.js
2019-09-04 17:58:29 +03:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import Data from './dom/data'
import {
emulateTransitionEnd,
execute,
getTransitionDurationFromElement
} from './util/index'
import EventHandler from './dom/event-handler'
2019-09-04 17:58:29 +03:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const VERSION = '5.0.0'
2019-09-04 17:58:29 +03:00
class BaseComponent {
constructor(element) {
element = typeof element === 'string' ? document.querySelector(element) : element
2019-09-04 17:58:29 +03:00
if (!element) {
return
}
this._element = element
Data.set(this._element, this.constructor.DATA_KEY, this)
2019-09-04 17:58:29 +03:00
}
2020-11-20 11:13:11 +01:00
dispose() {
Data.remove(this._element, this.constructor.DATA_KEY)
EventHandler.off(this._element, this.constructor.EVENT_KEY)
Object.getOwnPropertyNames(this).forEach(propertyName => {
this[propertyName] = null
})
2020-11-20 11:13:11 +01:00
}
_queueCallback(callback, element, isAnimated = true) {
if (!isAnimated) {
execute(callback)
return
}
const transitionDuration = getTransitionDurationFromElement(element)
EventHandler.one(element, 'transitionend', () => execute(callback))
emulateTransitionEnd(element, transitionDuration)
}
2019-09-04 17:58:29 +03:00
/** Static */
static getInstance(element) {
return Data.get(element, this.DATA_KEY)
2019-09-04 17:58:29 +03:00
}
static get VERSION() {
return VERSION
}
static get NAME() {
throw new Error('You have to implement the static method "NAME", for each component!')
}
static get DATA_KEY() {
return `bs.${this.NAME}`
}
static get EVENT_KEY() {
return `.${this.DATA_KEY}`
}
2019-09-04 17:58:29 +03:00
}
export default BaseComponent