toast.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*!
  2. * Bootstrap toast.js v4.4.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 ownKeys(object, enumerableOnly) {
  41. var keys = Object.keys(object);
  42. if (Object.getOwnPropertySymbols) {
  43. var symbols = Object.getOwnPropertySymbols(object);
  44. if (enumerableOnly) symbols = symbols.filter(function (sym) {
  45. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  46. });
  47. keys.push.apply(keys, symbols);
  48. }
  49. return keys;
  50. }
  51. function _objectSpread2(target) {
  52. for (var i = 1; i < arguments.length; i++) {
  53. var source = arguments[i] != null ? arguments[i] : {};
  54. if (i % 2) {
  55. ownKeys(Object(source), true).forEach(function (key) {
  56. _defineProperty(target, key, source[key]);
  57. });
  58. } else if (Object.getOwnPropertyDescriptors) {
  59. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  60. } else {
  61. ownKeys(Object(source)).forEach(function (key) {
  62. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  63. });
  64. }
  65. }
  66. return target;
  67. }
  68. /**
  69. * ------------------------------------------------------------------------
  70. * Constants
  71. * ------------------------------------------------------------------------
  72. */
  73. var NAME = 'toast';
  74. var VERSION = '4.4.1';
  75. var DATA_KEY = 'bs.toast';
  76. var EVENT_KEY = "." + DATA_KEY;
  77. var JQUERY_NO_CONFLICT = $.fn[NAME];
  78. var Event = {
  79. CLICK_DISMISS: "click.dismiss" + EVENT_KEY,
  80. HIDE: "hide" + EVENT_KEY,
  81. HIDDEN: "hidden" + EVENT_KEY,
  82. SHOW: "show" + EVENT_KEY,
  83. SHOWN: "shown" + EVENT_KEY
  84. };
  85. var ClassName = {
  86. FADE: 'fade',
  87. HIDE: 'hide',
  88. SHOW: 'show',
  89. SHOWING: 'showing'
  90. };
  91. var DefaultType = {
  92. animation: 'boolean',
  93. autohide: 'boolean',
  94. delay: 'number'
  95. };
  96. var Default = {
  97. animation: true,
  98. autohide: true,
  99. delay: 500
  100. };
  101. var Selector = {
  102. DATA_DISMISS: '[data-dismiss="toast"]'
  103. };
  104. /**
  105. * ------------------------------------------------------------------------
  106. * Class Definition
  107. * ------------------------------------------------------------------------
  108. */
  109. var Toast =
  110. /*#__PURE__*/
  111. function () {
  112. function Toast(element, config) {
  113. this._element = element;
  114. this._config = this._getConfig(config);
  115. this._timeout = null;
  116. this._setListeners();
  117. } // Getters
  118. var _proto = Toast.prototype;
  119. // Public
  120. _proto.show = function show() {
  121. var _this = this;
  122. var showEvent = $.Event(Event.SHOW);
  123. $(this._element).trigger(showEvent);
  124. if (showEvent.isDefaultPrevented()) {
  125. return;
  126. }
  127. if (this._config.animation) {
  128. this._element.classList.add(ClassName.FADE);
  129. }
  130. var complete = function complete() {
  131. _this._element.classList.remove(ClassName.SHOWING);
  132. _this._element.classList.add(ClassName.SHOW);
  133. $(_this._element).trigger(Event.SHOWN);
  134. if (_this._config.autohide) {
  135. _this._timeout = setTimeout(function () {
  136. _this.hide();
  137. }, _this._config.delay);
  138. }
  139. };
  140. this._element.classList.remove(ClassName.HIDE);
  141. Util.reflow(this._element);
  142. this._element.classList.add(ClassName.SHOWING);
  143. if (this._config.animation) {
  144. var transitionDuration = Util.getTransitionDurationFromElement(this._element);
  145. $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
  146. } else {
  147. complete();
  148. }
  149. };
  150. _proto.hide = function hide() {
  151. if (!this._element.classList.contains(ClassName.SHOW)) {
  152. return;
  153. }
  154. var hideEvent = $.Event(Event.HIDE);
  155. $(this._element).trigger(hideEvent);
  156. if (hideEvent.isDefaultPrevented()) {
  157. return;
  158. }
  159. this._close();
  160. };
  161. _proto.dispose = function dispose() {
  162. clearTimeout(this._timeout);
  163. this._timeout = null;
  164. if (this._element.classList.contains(ClassName.SHOW)) {
  165. this._element.classList.remove(ClassName.SHOW);
  166. }
  167. $(this._element).off(Event.CLICK_DISMISS);
  168. $.removeData(this._element, DATA_KEY);
  169. this._element = null;
  170. this._config = null;
  171. } // Private
  172. ;
  173. _proto._getConfig = function _getConfig(config) {
  174. config = _objectSpread2({}, Default, {}, $(this._element).data(), {}, typeof config === 'object' && config ? config : {});
  175. Util.typeCheckConfig(NAME, config, this.constructor.DefaultType);
  176. return config;
  177. };
  178. _proto._setListeners = function _setListeners() {
  179. var _this2 = this;
  180. $(this._element).on(Event.CLICK_DISMISS, Selector.DATA_DISMISS, function () {
  181. return _this2.hide();
  182. });
  183. };
  184. _proto._close = function _close() {
  185. var _this3 = this;
  186. var complete = function complete() {
  187. _this3._element.classList.add(ClassName.HIDE);
  188. $(_this3._element).trigger(Event.HIDDEN);
  189. };
  190. this._element.classList.remove(ClassName.SHOW);
  191. if (this._config.animation) {
  192. var transitionDuration = Util.getTransitionDurationFromElement(this._element);
  193. $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
  194. } else {
  195. complete();
  196. }
  197. } // Static
  198. ;
  199. Toast._jQueryInterface = function _jQueryInterface(config) {
  200. return this.each(function () {
  201. var $element = $(this);
  202. var data = $element.data(DATA_KEY);
  203. var _config = typeof config === 'object' && config;
  204. if (!data) {
  205. data = new Toast(this, _config);
  206. $element.data(DATA_KEY, data);
  207. }
  208. if (typeof config === 'string') {
  209. if (typeof data[config] === 'undefined') {
  210. throw new TypeError("No method named \"" + config + "\"");
  211. }
  212. data[config](this);
  213. }
  214. });
  215. };
  216. _createClass(Toast, null, [{
  217. key: "VERSION",
  218. get: function get() {
  219. return VERSION;
  220. }
  221. }, {
  222. key: "DefaultType",
  223. get: function get() {
  224. return DefaultType;
  225. }
  226. }, {
  227. key: "Default",
  228. get: function get() {
  229. return Default;
  230. }
  231. }]);
  232. return Toast;
  233. }();
  234. /**
  235. * ------------------------------------------------------------------------
  236. * jQuery
  237. * ------------------------------------------------------------------------
  238. */
  239. $.fn[NAME] = Toast._jQueryInterface;
  240. $.fn[NAME].Constructor = Toast;
  241. $.fn[NAME].noConflict = function () {
  242. $.fn[NAME] = JQUERY_NO_CONFLICT;
  243. return Toast._jQueryInterface;
  244. };
  245. return Toast;
  246. })));
  247. //# sourceMappingURL=toast.js.map