base-component.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*!
  2. * Bootstrap base-component.js v5.0.2 (https://getbootstrap.com/)
  3. * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/selector-engine.js'), require('./dom/event-handler.js')) :
  8. typeof define === 'function' && define.amd ? define(['./dom/data', './dom/selector-engine', './dom/event-handler'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Base = factory(global.Data, global.SelectorEngine, global.EventHandler));
  10. }(this, (function (Data, SelectorEngine, EventHandler) { 'use strict';
  11. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  12. var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
  13. var SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
  14. var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
  15. const MILLISECONDS_MULTIPLIER = 1000;
  16. const TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
  17. const getTransitionDurationFromElement = element => {
  18. if (!element) {
  19. return 0;
  20. } // Get transition-duration of the element
  21. let {
  22. transitionDuration,
  23. transitionDelay
  24. } = window.getComputedStyle(element);
  25. const floatTransitionDuration = Number.parseFloat(transitionDuration);
  26. const floatTransitionDelay = Number.parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
  27. if (!floatTransitionDuration && !floatTransitionDelay) {
  28. return 0;
  29. } // If multiple durations are defined, take the first
  30. transitionDuration = transitionDuration.split(',')[0];
  31. transitionDelay = transitionDelay.split(',')[0];
  32. return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
  33. };
  34. const triggerTransitionEnd = element => {
  35. element.dispatchEvent(new Event(TRANSITION_END));
  36. };
  37. const isElement = obj => {
  38. if (!obj || typeof obj !== 'object') {
  39. return false;
  40. }
  41. if (typeof obj.jquery !== 'undefined') {
  42. obj = obj[0];
  43. }
  44. return typeof obj.nodeType !== 'undefined';
  45. };
  46. const getElement = obj => {
  47. if (isElement(obj)) {
  48. // it's a jQuery object or a node element
  49. return obj.jquery ? obj[0] : obj;
  50. }
  51. if (typeof obj === 'string' && obj.length > 0) {
  52. return SelectorEngine__default['default'].findOne(obj);
  53. }
  54. return null;
  55. };
  56. const execute = callback => {
  57. if (typeof callback === 'function') {
  58. callback();
  59. }
  60. };
  61. const executeAfterTransition = (callback, transitionElement, waitForTransition = true) => {
  62. if (!waitForTransition) {
  63. execute(callback);
  64. return;
  65. }
  66. const durationPadding = 5;
  67. const emulatedDuration = getTransitionDurationFromElement(transitionElement) + durationPadding;
  68. let called = false;
  69. const handler = ({
  70. target
  71. }) => {
  72. if (target !== transitionElement) {
  73. return;
  74. }
  75. called = true;
  76. transitionElement.removeEventListener(TRANSITION_END, handler);
  77. execute(callback);
  78. };
  79. transitionElement.addEventListener(TRANSITION_END, handler);
  80. setTimeout(() => {
  81. if (!called) {
  82. triggerTransitionEnd(transitionElement);
  83. }
  84. }, emulatedDuration);
  85. };
  86. /**
  87. * --------------------------------------------------------------------------
  88. * Bootstrap (v5.0.2): base-component.js
  89. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  90. * --------------------------------------------------------------------------
  91. */
  92. /**
  93. * ------------------------------------------------------------------------
  94. * Constants
  95. * ------------------------------------------------------------------------
  96. */
  97. const VERSION = '5.0.2';
  98. class BaseComponent {
  99. constructor(element) {
  100. element = getElement(element);
  101. if (!element) {
  102. return;
  103. }
  104. this._element = element;
  105. Data__default['default'].set(this._element, this.constructor.DATA_KEY, this);
  106. }
  107. dispose() {
  108. Data__default['default'].remove(this._element, this.constructor.DATA_KEY);
  109. EventHandler__default['default'].off(this._element, this.constructor.EVENT_KEY);
  110. Object.getOwnPropertyNames(this).forEach(propertyName => {
  111. this[propertyName] = null;
  112. });
  113. }
  114. _queueCallback(callback, element, isAnimated = true) {
  115. executeAfterTransition(callback, element, isAnimated);
  116. }
  117. /** Static */
  118. static getInstance(element) {
  119. return Data__default['default'].get(element, this.DATA_KEY);
  120. }
  121. static getOrCreateInstance(element, config = {}) {
  122. return this.getInstance(element) || new this(element, typeof config === 'object' ? config : null);
  123. }
  124. static get VERSION() {
  125. return VERSION;
  126. }
  127. static get NAME() {
  128. throw new Error('You have to implement the static method "NAME", for each component!');
  129. }
  130. static get DATA_KEY() {
  131. return `bs.${this.NAME}`;
  132. }
  133. static get EVENT_KEY() {
  134. return `.${this.DATA_KEY}`;
  135. }
  136. }
  137. return BaseComponent;
  138. })));
  139. //# sourceMappingURL=base-component.js.map