toast.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*!
  2. * Bootstrap toast.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/event-handler.js'), require('./dom/manipulator.js'), require('./base-component.js')) :
  8. typeof define === 'function' && define.amd ? define(['./dom/event-handler', './dom/manipulator', './base-component'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.EventHandler, global.Manipulator, global.Base));
  10. }(this, (function (EventHandler, Manipulator, BaseComponent) { 'use strict';
  11. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  12. var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
  13. var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
  14. var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
  15. const toType = obj => {
  16. if (obj === null || obj === undefined) {
  17. return `${obj}`;
  18. }
  19. return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
  20. };
  21. const isElement = obj => {
  22. if (!obj || typeof obj !== 'object') {
  23. return false;
  24. }
  25. if (typeof obj.jquery !== 'undefined') {
  26. obj = obj[0];
  27. }
  28. return typeof obj.nodeType !== 'undefined';
  29. };
  30. const typeCheckConfig = (componentName, config, configTypes) => {
  31. Object.keys(configTypes).forEach(property => {
  32. const expectedTypes = configTypes[property];
  33. const value = config[property];
  34. const valueType = value && isElement(value) ? 'element' : toType(value);
  35. if (!new RegExp(expectedTypes).test(valueType)) {
  36. throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`);
  37. }
  38. });
  39. };
  40. const reflow = element => element.offsetHeight;
  41. const getjQuery = () => {
  42. const {
  43. jQuery
  44. } = window;
  45. if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
  46. return jQuery;
  47. }
  48. return null;
  49. };
  50. const DOMContentLoadedCallbacks = [];
  51. const onDOMContentLoaded = callback => {
  52. if (document.readyState === 'loading') {
  53. // add listener on the first call when the document is in loading state
  54. if (!DOMContentLoadedCallbacks.length) {
  55. document.addEventListener('DOMContentLoaded', () => {
  56. DOMContentLoadedCallbacks.forEach(callback => callback());
  57. });
  58. }
  59. DOMContentLoadedCallbacks.push(callback);
  60. } else {
  61. callback();
  62. }
  63. };
  64. const defineJQueryPlugin = plugin => {
  65. onDOMContentLoaded(() => {
  66. const $ = getjQuery();
  67. /* istanbul ignore if */
  68. if ($) {
  69. const name = plugin.NAME;
  70. const JQUERY_NO_CONFLICT = $.fn[name];
  71. $.fn[name] = plugin.jQueryInterface;
  72. $.fn[name].Constructor = plugin;
  73. $.fn[name].noConflict = () => {
  74. $.fn[name] = JQUERY_NO_CONFLICT;
  75. return plugin.jQueryInterface;
  76. };
  77. }
  78. });
  79. };
  80. /**
  81. * --------------------------------------------------------------------------
  82. * Bootstrap (v5.0.2): toast.js
  83. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  84. * --------------------------------------------------------------------------
  85. */
  86. /**
  87. * ------------------------------------------------------------------------
  88. * Constants
  89. * ------------------------------------------------------------------------
  90. */
  91. const NAME = 'toast';
  92. const DATA_KEY = 'bs.toast';
  93. const EVENT_KEY = `.${DATA_KEY}`;
  94. const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`;
  95. const EVENT_MOUSEOVER = `mouseover${EVENT_KEY}`;
  96. const EVENT_MOUSEOUT = `mouseout${EVENT_KEY}`;
  97. const EVENT_FOCUSIN = `focusin${EVENT_KEY}`;
  98. const EVENT_FOCUSOUT = `focusout${EVENT_KEY}`;
  99. const EVENT_HIDE = `hide${EVENT_KEY}`;
  100. const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
  101. const EVENT_SHOW = `show${EVENT_KEY}`;
  102. const EVENT_SHOWN = `shown${EVENT_KEY}`;
  103. const CLASS_NAME_FADE = 'fade';
  104. const CLASS_NAME_HIDE = 'hide';
  105. const CLASS_NAME_SHOW = 'show';
  106. const CLASS_NAME_SHOWING = 'showing';
  107. const DefaultType = {
  108. animation: 'boolean',
  109. autohide: 'boolean',
  110. delay: 'number'
  111. };
  112. const Default = {
  113. animation: true,
  114. autohide: true,
  115. delay: 5000
  116. };
  117. const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="toast"]';
  118. /**
  119. * ------------------------------------------------------------------------
  120. * Class Definition
  121. * ------------------------------------------------------------------------
  122. */
  123. class Toast extends BaseComponent__default['default'] {
  124. constructor(element, config) {
  125. super(element);
  126. this._config = this._getConfig(config);
  127. this._timeout = null;
  128. this._hasMouseInteraction = false;
  129. this._hasKeyboardInteraction = false;
  130. this._setListeners();
  131. } // Getters
  132. static get DefaultType() {
  133. return DefaultType;
  134. }
  135. static get Default() {
  136. return Default;
  137. }
  138. static get NAME() {
  139. return NAME;
  140. } // Public
  141. show() {
  142. const showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW);
  143. if (showEvent.defaultPrevented) {
  144. return;
  145. }
  146. this._clearTimeout();
  147. if (this._config.animation) {
  148. this._element.classList.add(CLASS_NAME_FADE);
  149. }
  150. const complete = () => {
  151. this._element.classList.remove(CLASS_NAME_SHOWING);
  152. this._element.classList.add(CLASS_NAME_SHOW);
  153. EventHandler__default['default'].trigger(this._element, EVENT_SHOWN);
  154. this._maybeScheduleHide();
  155. };
  156. this._element.classList.remove(CLASS_NAME_HIDE);
  157. reflow(this._element);
  158. this._element.classList.add(CLASS_NAME_SHOWING);
  159. this._queueCallback(complete, this._element, this._config.animation);
  160. }
  161. hide() {
  162. if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
  163. return;
  164. }
  165. const hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE);
  166. if (hideEvent.defaultPrevented) {
  167. return;
  168. }
  169. const complete = () => {
  170. this._element.classList.add(CLASS_NAME_HIDE);
  171. EventHandler__default['default'].trigger(this._element, EVENT_HIDDEN);
  172. };
  173. this._element.classList.remove(CLASS_NAME_SHOW);
  174. this._queueCallback(complete, this._element, this._config.animation);
  175. }
  176. dispose() {
  177. this._clearTimeout();
  178. if (this._element.classList.contains(CLASS_NAME_SHOW)) {
  179. this._element.classList.remove(CLASS_NAME_SHOW);
  180. }
  181. super.dispose();
  182. } // Private
  183. _getConfig(config) {
  184. config = { ...Default,
  185. ...Manipulator__default['default'].getDataAttributes(this._element),
  186. ...(typeof config === 'object' && config ? config : {})
  187. };
  188. typeCheckConfig(NAME, config, this.constructor.DefaultType);
  189. return config;
  190. }
  191. _maybeScheduleHide() {
  192. if (!this._config.autohide) {
  193. return;
  194. }
  195. if (this._hasMouseInteraction || this._hasKeyboardInteraction) {
  196. return;
  197. }
  198. this._timeout = setTimeout(() => {
  199. this.hide();
  200. }, this._config.delay);
  201. }
  202. _onInteraction(event, isInteracting) {
  203. switch (event.type) {
  204. case 'mouseover':
  205. case 'mouseout':
  206. this._hasMouseInteraction = isInteracting;
  207. break;
  208. case 'focusin':
  209. case 'focusout':
  210. this._hasKeyboardInteraction = isInteracting;
  211. break;
  212. }
  213. if (isInteracting) {
  214. this._clearTimeout();
  215. return;
  216. }
  217. const nextElement = event.relatedTarget;
  218. if (this._element === nextElement || this._element.contains(nextElement)) {
  219. return;
  220. }
  221. this._maybeScheduleHide();
  222. }
  223. _setListeners() {
  224. EventHandler__default['default'].on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, () => this.hide());
  225. EventHandler__default['default'].on(this._element, EVENT_MOUSEOVER, event => this._onInteraction(event, true));
  226. EventHandler__default['default'].on(this._element, EVENT_MOUSEOUT, event => this._onInteraction(event, false));
  227. EventHandler__default['default'].on(this._element, EVENT_FOCUSIN, event => this._onInteraction(event, true));
  228. EventHandler__default['default'].on(this._element, EVENT_FOCUSOUT, event => this._onInteraction(event, false));
  229. }
  230. _clearTimeout() {
  231. clearTimeout(this._timeout);
  232. this._timeout = null;
  233. } // Static
  234. static jQueryInterface(config) {
  235. return this.each(function () {
  236. const data = Toast.getOrCreateInstance(this, config);
  237. if (typeof config === 'string') {
  238. if (typeof data[config] === 'undefined') {
  239. throw new TypeError(`No method named "${config}"`);
  240. }
  241. data[config](this);
  242. }
  243. });
  244. }
  245. }
  246. /**
  247. * ------------------------------------------------------------------------
  248. * jQuery
  249. * ------------------------------------------------------------------------
  250. * add .Toast to jQuery only if jQuery is present
  251. */
  252. defineJQueryPlugin(Toast);
  253. return Toast;
  254. })));
  255. //# sourceMappingURL=toast.js.map