toast.js 7.6 KB

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