alert.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * --------------------------------------------------------------------------
  5. * Bootstrap (v4.0.0-alpha.4): alert.js
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. * --------------------------------------------------------------------------
  8. */
  9. var Alert = function ($) {
  10. /**
  11. * ------------------------------------------------------------------------
  12. * Constants
  13. * ------------------------------------------------------------------------
  14. */
  15. var NAME = 'alert';
  16. var VERSION = '4.0.0-alpha.4';
  17. var DATA_KEY = 'bs.alert';
  18. var EVENT_KEY = '.' + DATA_KEY;
  19. var DATA_API_KEY = '.data-api';
  20. var JQUERY_NO_CONFLICT = $.fn[NAME];
  21. var TRANSITION_DURATION = 150;
  22. var Selector = {
  23. DISMISS: '[data-dismiss="alert"]'
  24. };
  25. var Event = {
  26. CLOSE: 'close' + EVENT_KEY,
  27. CLOSED: 'closed' + EVENT_KEY,
  28. CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY
  29. };
  30. var ClassName = {
  31. ALERT: 'alert',
  32. FADE: 'fade',
  33. IN: 'in'
  34. };
  35. /**
  36. * ------------------------------------------------------------------------
  37. * Class Definition
  38. * ------------------------------------------------------------------------
  39. */
  40. var Alert = function () {
  41. function Alert(element) {
  42. _classCallCheck(this, Alert);
  43. this._element = element;
  44. }
  45. // getters
  46. // public
  47. Alert.prototype.close = function close(element) {
  48. element = element || this._element;
  49. var rootElement = this._getRootElement(element);
  50. var customEvent = this._triggerCloseEvent(rootElement);
  51. if (customEvent.isDefaultPrevented()) {
  52. return;
  53. }
  54. this._removeElement(rootElement);
  55. };
  56. Alert.prototype.dispose = function dispose() {
  57. $.removeData(this._element, DATA_KEY);
  58. this._element = null;
  59. };
  60. // private
  61. Alert.prototype._getRootElement = function _getRootElement(element) {
  62. var selector = Util.getSelectorFromElement(element);
  63. var parent = false;
  64. if (selector) {
  65. parent = $(selector)[0];
  66. }
  67. if (!parent) {
  68. parent = $(element).closest('.' + ClassName.ALERT)[0];
  69. }
  70. return parent;
  71. };
  72. Alert.prototype._triggerCloseEvent = function _triggerCloseEvent(element) {
  73. var closeEvent = $.Event(Event.CLOSE);
  74. $(element).trigger(closeEvent);
  75. return closeEvent;
  76. };
  77. Alert.prototype._removeElement = function _removeElement(element) {
  78. $(element).removeClass(ClassName.IN);
  79. if (!Util.supportsTransitionEnd() || !$(element).hasClass(ClassName.FADE)) {
  80. this._destroyElement(element);
  81. return;
  82. }
  83. $(element).one(Util.TRANSITION_END, $.proxy(this._destroyElement, this, element)).emulateTransitionEnd(TRANSITION_DURATION);
  84. };
  85. Alert.prototype._destroyElement = function _destroyElement(element) {
  86. $(element).detach().trigger(Event.CLOSED).remove();
  87. };
  88. // static
  89. Alert._jQueryInterface = function _jQueryInterface(config) {
  90. return this.each(function () {
  91. var $element = $(this);
  92. var data = $element.data(DATA_KEY);
  93. if (!data) {
  94. data = new Alert(this);
  95. $element.data(DATA_KEY, data);
  96. }
  97. if (config === 'close') {
  98. data[config](this);
  99. }
  100. });
  101. };
  102. Alert._handleDismiss = function _handleDismiss(alertInstance) {
  103. return function (event) {
  104. if (event) {
  105. event.preventDefault();
  106. }
  107. alertInstance.close(this);
  108. };
  109. };
  110. _createClass(Alert, null, [{
  111. key: 'VERSION',
  112. get: function get() {
  113. return VERSION;
  114. }
  115. }]);
  116. return Alert;
  117. }();
  118. /**
  119. * ------------------------------------------------------------------------
  120. * Data Api implementation
  121. * ------------------------------------------------------------------------
  122. */
  123. $(document).on(Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()));
  124. /**
  125. * ------------------------------------------------------------------------
  126. * jQuery
  127. * ------------------------------------------------------------------------
  128. */
  129. $.fn[NAME] = Alert._jQueryInterface;
  130. $.fn[NAME].Constructor = Alert;
  131. $.fn[NAME].noConflict = function () {
  132. $.fn[NAME] = JQUERY_NO_CONFLICT;
  133. return Alert._jQueryInterface;
  134. };
  135. return Alert;
  136. }(jQuery);
  137. //# sourceMappingURL=alert.js.map