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

78 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-09-04 17:58:29 +03:00
/**
* --------------------------------------------------------------------------
2021-10-09 09:33:12 +03:00
* Bootstrap (v5.1.3): 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 {
executeAfterTransition,
getElement
} from './util/index'
import EventHandler from './dom/event-handler'
2019-09-04 17:58:29 +03:00
/**
* Constants
*/
2021-10-09 09:33:12 +03:00
const VERSION = '5.1.3'
/**
* Class definition
*/
2019-09-04 17:58:29 +03:00
class BaseComponent {
constructor(element) {
element = getElement(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
}
// Public
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)
for (const propertyName of Object.getOwnPropertyNames(this)) {
this[propertyName] = null
}
2020-11-20 11:13:11 +01:00
}
_queueCallback(callback, element, isAnimated = true) {
executeAfterTransition(callback, element, isAnimated)
}
// Static
2019-09-04 17:58:29 +03:00
static getInstance(element) {
return Data.get(getElement(element), this.DATA_KEY)
2019-09-04 17:58:29 +03:00
}
static getOrCreateInstance(element, config = {}) {
return this.getInstance(element) || new this(element, typeof config === 'object' ? config : null)
}
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