alert.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*!
  2. * Bootstrap alert.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.Alert = 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. /**
  28. * ------------------------------------------------------------------------
  29. * Constants
  30. * ------------------------------------------------------------------------
  31. */
  32. var NAME = 'alert';
  33. var VERSION = '4.3.1';
  34. var DATA_KEY = 'bs.alert';
  35. var EVENT_KEY = "." + DATA_KEY;
  36. var DATA_API_KEY = '.data-api';
  37. var JQUERY_NO_CONFLICT = $.fn[NAME];
  38. var Selector = {
  39. DISMISS: '[data-dismiss="alert"]'
  40. };
  41. var Event = {
  42. CLOSE: "close" + EVENT_KEY,
  43. CLOSED: "closed" + EVENT_KEY,
  44. CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
  45. };
  46. var ClassName = {
  47. ALERT: 'alert',
  48. FADE: 'fade',
  49. SHOW: 'show'
  50. /**
  51. * ------------------------------------------------------------------------
  52. * Class Definition
  53. * ------------------------------------------------------------------------
  54. */
  55. };
  56. var Alert =
  57. /*#__PURE__*/
  58. function () {
  59. function Alert(element) {
  60. this._element = element;
  61. } // Getters
  62. var _proto = Alert.prototype;
  63. // Public
  64. _proto.close = function close(element) {
  65. var rootElement = this._element;
  66. if (element) {
  67. rootElement = this._getRootElement(element);
  68. }
  69. var customEvent = this._triggerCloseEvent(rootElement);
  70. if (customEvent.isDefaultPrevented()) {
  71. return;
  72. }
  73. this._removeElement(rootElement);
  74. };
  75. _proto.dispose = function dispose() {
  76. $.removeData(this._element, DATA_KEY);
  77. this._element = null;
  78. } // Private
  79. ;
  80. _proto._getRootElement = function _getRootElement(element) {
  81. var selector = Util.getSelectorFromElement(element);
  82. var parent = false;
  83. if (selector) {
  84. parent = document.querySelector(selector);
  85. }
  86. if (!parent) {
  87. parent = $(element).closest("." + ClassName.ALERT)[0];
  88. }
  89. return parent;
  90. };
  91. _proto._triggerCloseEvent = function _triggerCloseEvent(element) {
  92. var closeEvent = $.Event(Event.CLOSE);
  93. $(element).trigger(closeEvent);
  94. return closeEvent;
  95. };
  96. _proto._removeElement = function _removeElement(element) {
  97. var _this = this;
  98. $(element).removeClass(ClassName.SHOW);
  99. if (!$(element).hasClass(ClassName.FADE)) {
  100. this._destroyElement(element);
  101. return;
  102. }
  103. var transitionDuration = Util.getTransitionDurationFromElement(element);
  104. $(element).one(Util.TRANSITION_END, function (event) {
  105. return _this._destroyElement(element, event);
  106. }).emulateTransitionEnd(transitionDuration);
  107. };
  108. _proto._destroyElement = function _destroyElement(element) {
  109. $(element).detach().trigger(Event.CLOSED).remove();
  110. } // Static
  111. ;
  112. Alert._jQueryInterface = function _jQueryInterface(config) {
  113. return this.each(function () {
  114. var $element = $(this);
  115. var data = $element.data(DATA_KEY);
  116. if (!data) {
  117. data = new Alert(this);
  118. $element.data(DATA_KEY, data);
  119. }
  120. if (config === 'close') {
  121. data[config](this);
  122. }
  123. });
  124. };
  125. Alert._handleDismiss = function _handleDismiss(alertInstance) {
  126. return function (event) {
  127. if (event) {
  128. event.preventDefault();
  129. }
  130. alertInstance.close(this);
  131. };
  132. };
  133. _createClass(Alert, null, [{
  134. key: "VERSION",
  135. get: function get() {
  136. return VERSION;
  137. }
  138. }]);
  139. return Alert;
  140. }();
  141. /**
  142. * ------------------------------------------------------------------------
  143. * Data Api implementation
  144. * ------------------------------------------------------------------------
  145. */
  146. $(document).on(Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()));
  147. /**
  148. * ------------------------------------------------------------------------
  149. * jQuery
  150. * ------------------------------------------------------------------------
  151. */
  152. $.fn[NAME] = Alert._jQueryInterface;
  153. $.fn[NAME].Constructor = Alert;
  154. $.fn[NAME].noConflict = function () {
  155. $.fn[NAME] = JQUERY_NO_CONFLICT;
  156. return Alert._jQueryInterface;
  157. };
  158. return Alert;
  159. }));
  160. //# sourceMappingURL=alert.js.map