toast.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*!
  2. * Bootstrap toast.js v4.5.2 (https://getbootstrap.com/)
  3. * Copyright 2011-2020 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('jquery'), require('./util.js')) :
  8. typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.jQuery, global.Util));
  10. }(this, (function ($, Util) { 'use strict';
  11. $ = $ && Object.prototype.hasOwnProperty.call($, 'default') ? $['default'] : $;
  12. Util = Util && Object.prototype.hasOwnProperty.call(Util, 'default') ? Util['default'] : Util;
  13. function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  14. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  15. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  16. /**
  17. * ------------------------------------------------------------------------
  18. * Constants
  19. * ------------------------------------------------------------------------
  20. */
  21. var NAME = 'toast';
  22. var VERSION = '4.5.2';
  23. var DATA_KEY = 'bs.toast';
  24. var EVENT_KEY = "." + DATA_KEY;
  25. var JQUERY_NO_CONFLICT = $.fn[NAME];
  26. var EVENT_CLICK_DISMISS = "click.dismiss" + EVENT_KEY;
  27. var EVENT_HIDE = "hide" + EVENT_KEY;
  28. var EVENT_HIDDEN = "hidden" + EVENT_KEY;
  29. var EVENT_SHOW = "show" + EVENT_KEY;
  30. var EVENT_SHOWN = "shown" + EVENT_KEY;
  31. var CLASS_NAME_FADE = 'fade';
  32. var CLASS_NAME_HIDE = 'hide';
  33. var CLASS_NAME_SHOW = 'show';
  34. var CLASS_NAME_SHOWING = 'showing';
  35. var DefaultType = {
  36. animation: 'boolean',
  37. autohide: 'boolean',
  38. delay: 'number'
  39. };
  40. var Default = {
  41. animation: true,
  42. autohide: true,
  43. delay: 500
  44. };
  45. var SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]';
  46. /**
  47. * ------------------------------------------------------------------------
  48. * Class Definition
  49. * ------------------------------------------------------------------------
  50. */
  51. var Toast = /*#__PURE__*/function () {
  52. function Toast(element, config) {
  53. this._element = element;
  54. this._config = this._getConfig(config);
  55. this._timeout = null;
  56. this._setListeners();
  57. } // Getters
  58. var _proto = Toast.prototype;
  59. // Public
  60. _proto.show = function show() {
  61. var _this = this;
  62. var showEvent = $.Event(EVENT_SHOW);
  63. $(this._element).trigger(showEvent);
  64. if (showEvent.isDefaultPrevented()) {
  65. return;
  66. }
  67. this._clearTimeout();
  68. if (this._config.animation) {
  69. this._element.classList.add(CLASS_NAME_FADE);
  70. }
  71. var complete = function complete() {
  72. _this._element.classList.remove(CLASS_NAME_SHOWING);
  73. _this._element.classList.add(CLASS_NAME_SHOW);
  74. $(_this._element).trigger(EVENT_SHOWN);
  75. if (_this._config.autohide) {
  76. _this._timeout = setTimeout(function () {
  77. _this.hide();
  78. }, _this._config.delay);
  79. }
  80. };
  81. this._element.classList.remove(CLASS_NAME_HIDE);
  82. Util.reflow(this._element);
  83. this._element.classList.add(CLASS_NAME_SHOWING);
  84. if (this._config.animation) {
  85. var transitionDuration = Util.getTransitionDurationFromElement(this._element);
  86. $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
  87. } else {
  88. complete();
  89. }
  90. };
  91. _proto.hide = function hide() {
  92. if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
  93. return;
  94. }
  95. var hideEvent = $.Event(EVENT_HIDE);
  96. $(this._element).trigger(hideEvent);
  97. if (hideEvent.isDefaultPrevented()) {
  98. return;
  99. }
  100. this._close();
  101. };
  102. _proto.dispose = function dispose() {
  103. this._clearTimeout();
  104. if (this._element.classList.contains(CLASS_NAME_SHOW)) {
  105. this._element.classList.remove(CLASS_NAME_SHOW);
  106. }
  107. $(this._element).off(EVENT_CLICK_DISMISS);
  108. $.removeData(this._element, DATA_KEY);
  109. this._element = null;
  110. this._config = null;
  111. } // Private
  112. ;
  113. _proto._getConfig = function _getConfig(config) {
  114. config = _extends({}, Default, $(this._element).data(), typeof config === 'object' && config ? config : {});
  115. Util.typeCheckConfig(NAME, config, this.constructor.DefaultType);
  116. return config;
  117. };
  118. _proto._setListeners = function _setListeners() {
  119. var _this2 = this;
  120. $(this._element).on(EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, function () {
  121. return _this2.hide();
  122. });
  123. };
  124. _proto._close = function _close() {
  125. var _this3 = this;
  126. var complete = function complete() {
  127. _this3._element.classList.add(CLASS_NAME_HIDE);
  128. $(_this3._element).trigger(EVENT_HIDDEN);
  129. };
  130. this._element.classList.remove(CLASS_NAME_SHOW);
  131. if (this._config.animation) {
  132. var transitionDuration = Util.getTransitionDurationFromElement(this._element);
  133. $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
  134. } else {
  135. complete();
  136. }
  137. };
  138. _proto._clearTimeout = function _clearTimeout() {
  139. clearTimeout(this._timeout);
  140. this._timeout = null;
  141. } // Static
  142. ;
  143. Toast._jQueryInterface = function _jQueryInterface(config) {
  144. return this.each(function () {
  145. var $element = $(this);
  146. var data = $element.data(DATA_KEY);
  147. var _config = typeof config === 'object' && config;
  148. if (!data) {
  149. data = new Toast(this, _config);
  150. $element.data(DATA_KEY, data);
  151. }
  152. if (typeof config === 'string') {
  153. if (typeof data[config] === 'undefined') {
  154. throw new TypeError("No method named \"" + config + "\"");
  155. }
  156. data[config](this);
  157. }
  158. });
  159. };
  160. _createClass(Toast, null, [{
  161. key: "VERSION",
  162. get: function get() {
  163. return VERSION;
  164. }
  165. }, {
  166. key: "DefaultType",
  167. get: function get() {
  168. return DefaultType;
  169. }
  170. }, {
  171. key: "Default",
  172. get: function get() {
  173. return Default;
  174. }
  175. }]);
  176. return Toast;
  177. }();
  178. /**
  179. * ------------------------------------------------------------------------
  180. * jQuery
  181. * ------------------------------------------------------------------------
  182. */
  183. $.fn[NAME] = Toast._jQueryInterface;
  184. $.fn[NAME].Constructor = Toast;
  185. $.fn[NAME].noConflict = function () {
  186. $.fn[NAME] = JQUERY_NO_CONFLICT;
  187. return Toast._jQueryInterface;
  188. };
  189. return Toast;
  190. })));
  191. //# sourceMappingURL=toast.js.map