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.

199 lines
6.1 KiB

5 months ago
  1. /*!
  2. * Bootstrap toast.js v5.3.3 (https://getbootstrap.com/)
  3. * Copyright 2011-2024 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('./base-component.js'), require('./dom/event-handler.js'), require('./util/component-functions.js'), require('./util/index.js')) :
  8. typeof define === 'function' && define.amd ? define(['./base-component', './dom/event-handler', './util/component-functions', './util/index'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.BaseComponent, global.EventHandler, global.ComponentFunctions, global.Index));
  10. })(this, (function (BaseComponent, EventHandler, componentFunctions_js, index_js) { 'use strict';
  11. /**
  12. * --------------------------------------------------------------------------
  13. * Bootstrap toast.js
  14. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  15. * --------------------------------------------------------------------------
  16. */
  17. /**
  18. * Constants
  19. */
  20. const NAME = 'toast';
  21. const DATA_KEY = 'bs.toast';
  22. const EVENT_KEY = `.${DATA_KEY}`;
  23. const EVENT_MOUSEOVER = `mouseover${EVENT_KEY}`;
  24. const EVENT_MOUSEOUT = `mouseout${EVENT_KEY}`;
  25. const EVENT_FOCUSIN = `focusin${EVENT_KEY}`;
  26. const EVENT_FOCUSOUT = `focusout${EVENT_KEY}`;
  27. const EVENT_HIDE = `hide${EVENT_KEY}`;
  28. const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
  29. const EVENT_SHOW = `show${EVENT_KEY}`;
  30. const EVENT_SHOWN = `shown${EVENT_KEY}`;
  31. const CLASS_NAME_FADE = 'fade';
  32. const CLASS_NAME_HIDE = 'hide'; // @deprecated - kept here only for backwards compatibility
  33. const CLASS_NAME_SHOW = 'show';
  34. const CLASS_NAME_SHOWING = 'showing';
  35. const DefaultType = {
  36. animation: 'boolean',
  37. autohide: 'boolean',
  38. delay: 'number'
  39. };
  40. const Default = {
  41. animation: true,
  42. autohide: true,
  43. delay: 5000
  44. };
  45. /**
  46. * Class definition
  47. */
  48. class Toast extends BaseComponent {
  49. constructor(element, config) {
  50. super(element, config);
  51. this._timeout = null;
  52. this._hasMouseInteraction = false;
  53. this._hasKeyboardInteraction = false;
  54. this._setListeners();
  55. }
  56. // Getters
  57. static get Default() {
  58. return Default;
  59. }
  60. static get DefaultType() {
  61. return DefaultType;
  62. }
  63. static get NAME() {
  64. return NAME;
  65. }
  66. // Public
  67. show() {
  68. const showEvent = EventHandler.trigger(this._element, EVENT_SHOW);
  69. if (showEvent.defaultPrevented) {
  70. return;
  71. }
  72. this._clearTimeout();
  73. if (this._config.animation) {
  74. this._element.classList.add(CLASS_NAME_FADE);
  75. }
  76. const complete = () => {
  77. this._element.classList.remove(CLASS_NAME_SHOWING);
  78. EventHandler.trigger(this._element, EVENT_SHOWN);
  79. this._maybeScheduleHide();
  80. };
  81. this._element.classList.remove(CLASS_NAME_HIDE); // @deprecated
  82. index_js.reflow(this._element);
  83. this._element.classList.add(CLASS_NAME_SHOW, CLASS_NAME_SHOWING);
  84. this._queueCallback(complete, this._element, this._config.animation);
  85. }
  86. hide() {
  87. if (!this.isShown()) {
  88. return;
  89. }
  90. const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE);
  91. if (hideEvent.defaultPrevented) {
  92. return;
  93. }
  94. const complete = () => {
  95. this._element.classList.add(CLASS_NAME_HIDE); // @deprecated
  96. this._element.classList.remove(CLASS_NAME_SHOWING, CLASS_NAME_SHOW);
  97. EventHandler.trigger(this._element, EVENT_HIDDEN);
  98. };
  99. this._element.classList.add(CLASS_NAME_SHOWING);
  100. this._queueCallback(complete, this._element, this._config.animation);
  101. }
  102. dispose() {
  103. this._clearTimeout();
  104. if (this.isShown()) {
  105. this._element.classList.remove(CLASS_NAME_SHOW);
  106. }
  107. super.dispose();
  108. }
  109. isShown() {
  110. return this._element.classList.contains(CLASS_NAME_SHOW);
  111. }
  112. // Private
  113. _maybeScheduleHide() {
  114. if (!this._config.autohide) {
  115. return;
  116. }
  117. if (this._hasMouseInteraction || this._hasKeyboardInteraction) {
  118. return;
  119. }
  120. this._timeout = setTimeout(() => {
  121. this.hide();
  122. }, this._config.delay);
  123. }
  124. _onInteraction(event, isInteracting) {
  125. switch (event.type) {
  126. case 'mouseover':
  127. case 'mouseout':
  128. {
  129. this._hasMouseInteraction = isInteracting;
  130. break;
  131. }
  132. case 'focusin':
  133. case 'focusout':
  134. {
  135. this._hasKeyboardInteraction = isInteracting;
  136. break;
  137. }
  138. }
  139. if (isInteracting) {
  140. this._clearTimeout();
  141. return;
  142. }
  143. const nextElement = event.relatedTarget;
  144. if (this._element === nextElement || this._element.contains(nextElement)) {
  145. return;
  146. }
  147. this._maybeScheduleHide();
  148. }
  149. _setListeners() {
  150. EventHandler.on(this._element, EVENT_MOUSEOVER, event => this._onInteraction(event, true));
  151. EventHandler.on(this._element, EVENT_MOUSEOUT, event => this._onInteraction(event, false));
  152. EventHandler.on(this._element, EVENT_FOCUSIN, event => this._onInteraction(event, true));
  153. EventHandler.on(this._element, EVENT_FOCUSOUT, event => this._onInteraction(event, false));
  154. }
  155. _clearTimeout() {
  156. clearTimeout(this._timeout);
  157. this._timeout = null;
  158. }
  159. // Static
  160. static jQueryInterface(config) {
  161. return this.each(function () {
  162. const data = Toast.getOrCreateInstance(this, config);
  163. if (typeof config === 'string') {
  164. if (typeof data[config] === 'undefined') {
  165. throw new TypeError(`No method named "${config}"`);
  166. }
  167. data[config](this);
  168. }
  169. });
  170. }
  171. }
  172. /**
  173. * Data API implementation
  174. */
  175. componentFunctions_js.enableDismissTrigger(Toast);
  176. /**
  177. * jQuery
  178. */
  179. index_js.defineJQueryPlugin(Toast);
  180. return Toast;
  181. }));
  182. //# sourceMappingURL=toast.js.map