You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.4 KiB

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