toast.js 7.5 KB

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