1 | /**
|
---|
2 | * --------------------------------------------------------------------------
|
---|
3 | * Bootstrap base-component.js
|
---|
4 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
5 | * --------------------------------------------------------------------------
|
---|
6 | */
|
---|
7 |
|
---|
8 | import Data from './dom/data.js'
|
---|
9 | import EventHandler from './dom/event-handler.js'
|
---|
10 | import Config from './util/config.js'
|
---|
11 | import { executeAfterTransition, getElement } from './util/index.js'
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Constants
|
---|
15 | */
|
---|
16 |
|
---|
17 | const VERSION = '5.3.3'
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Class definition
|
---|
21 | */
|
---|
22 |
|
---|
23 | class BaseComponent extends Config {
|
---|
24 | constructor(element, config) {
|
---|
25 | super()
|
---|
26 |
|
---|
27 | element = getElement(element)
|
---|
28 | if (!element) {
|
---|
29 | return
|
---|
30 | }
|
---|
31 |
|
---|
32 | this._element = element
|
---|
33 | this._config = this._getConfig(config)
|
---|
34 |
|
---|
35 | Data.set(this._element, this.constructor.DATA_KEY, this)
|
---|
36 | }
|
---|
37 |
|
---|
38 | // Public
|
---|
39 | dispose() {
|
---|
40 | Data.remove(this._element, this.constructor.DATA_KEY)
|
---|
41 | EventHandler.off(this._element, this.constructor.EVENT_KEY)
|
---|
42 |
|
---|
43 | for (const propertyName of Object.getOwnPropertyNames(this)) {
|
---|
44 | this[propertyName] = null
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | _queueCallback(callback, element, isAnimated = true) {
|
---|
49 | executeAfterTransition(callback, element, isAnimated)
|
---|
50 | }
|
---|
51 |
|
---|
52 | _getConfig(config) {
|
---|
53 | config = this._mergeConfigObj(config, this._element)
|
---|
54 | config = this._configAfterMerge(config)
|
---|
55 | this._typeCheckConfig(config)
|
---|
56 | return config
|
---|
57 | }
|
---|
58 |
|
---|
59 | // Static
|
---|
60 | static getInstance(element) {
|
---|
61 | return Data.get(getElement(element), this.DATA_KEY)
|
---|
62 | }
|
---|
63 |
|
---|
64 | static getOrCreateInstance(element, config = {}) {
|
---|
65 | return this.getInstance(element) || new this(element, typeof config === 'object' ? config : null)
|
---|
66 | }
|
---|
67 |
|
---|
68 | static get VERSION() {
|
---|
69 | return VERSION
|
---|
70 | }
|
---|
71 |
|
---|
72 | static get DATA_KEY() {
|
---|
73 | return `bs.${this.NAME}`
|
---|
74 | }
|
---|
75 |
|
---|
76 | static get EVENT_KEY() {
|
---|
77 | return `.${this.DATA_KEY}`
|
---|
78 | }
|
---|
79 |
|
---|
80 | static eventName(name) {
|
---|
81 | return `${name}${this.EVENT_KEY}`
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | export default BaseComponent
|
---|