theme/boost/amd/src/bootstrap/base-component.js

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