alert.js 4.91 KB
Newer Older
fat's avatar
fat committed
1
2
3
4
5
6
7
8
'use strict';

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; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

/**
 * --------------------------------------------------------------------------
9
10
 * Bootstrap (v4.0.0): alert.js
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
fat's avatar
fat committed
11
12
13
 * --------------------------------------------------------------------------
 */

fat's avatar
fat committed
14
var Alert = (function ($) {
fat's avatar
fat committed
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  var NAME = 'alert';
  var VERSION = '4.0.0';
  var DATA_KEY = 'bs.alert';
  var JQUERY_NO_CONFLICT = $.fn[NAME];
  var TRANSITION_DURATION = 150;

  var Selector = {
    DISMISS: '[data-dismiss="alert"]'
  };

  var Event = {
    CLOSE: 'close.bs.alert',
    CLOSED: 'closed.bs.alert',
    CLICK: 'click.bs.alert.data-api'
  };

  var ClassName = {
    ALERT: 'alert',
    FADE: 'fade',
    IN: 'in'
  };

  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */

  var Alert = (function () {
    function Alert(element) {
      _classCallCheck(this, Alert);

      if (element) {
        this.element = element;
      }
fat's avatar
fat committed
57
58
    }

59
60
61
62
    _createClass(Alert, [{
      key: 'close',

      // public
fat's avatar
fat committed
63

64
      value: function close(element) {
fat's avatar
fat committed
65
66
        element = element || this.element;

67
68
        var rootElement = this._getRootElement(element);
        var customEvent = this._triggerCloseEvent(rootElement);
fat's avatar
fat committed
69

70
71
72
        if (customEvent.isDefaultPrevented()) {
          return;
        }
fat's avatar
fat committed
73

74
        this._removeElement(rootElement);
fat's avatar
fat committed
75
      }
76
77
    }, {
      key: '_getRootElement',
fat's avatar
fat committed
78

79
      // private
fat's avatar
fat committed
80

81
82
83
      value: function _getRootElement(element) {
        var parent = false;
        var selector = Util.getSelectorFromElement(element);
fat's avatar
fat committed
84

85
86
87
        if (selector) {
          parent = $(selector)[0];
        }
fat's avatar
fat committed
88

89
90
91
        if (!parent) {
          parent = $(element).closest('.' + ClassName.ALERT)[0];
        }
fat's avatar
fat committed
92

93
94
95
96
97
98
99
100
        return parent;
      }
    }, {
      key: '_triggerCloseEvent',
      value: function _triggerCloseEvent(element) {
        var closeEvent = $.Event(Event.CLOSE);
        $(element).trigger(closeEvent);
        return closeEvent;
fat's avatar
fat committed
101
      }
102
103
104
105
106
107
108
109
110
    }, {
      key: '_removeElement',
      value: function _removeElement(element) {
        $(element).removeClass(ClassName.IN);

        if (!Util.supportsTransitionEnd() || !$(element).hasClass(ClassName.FADE)) {
          this._destroyElement(element);
          return;
        }
fat's avatar
fat committed
111

112
113
114
115
116
117
        $(element).one(Util.TRANSITION_END, this._destroyElement.bind(this, element)).emulateTransitionEnd(TRANSITION_DURATION);
      }
    }, {
      key: '_destroyElement',
      value: function _destroyElement(element) {
        $(element).detach().trigger(Event.CLOSED).remove();
fat's avatar
fat committed
118
      }
119
120
    }], [{
      key: '_jQueryInterface',
fat's avatar
fat committed
121

122
      // static
fat's avatar
fat committed
123

124
125
126
127
      value: function _jQueryInterface(config) {
        return this.each(function () {
          var $element = $(this);
          var data = $element.data(DATA_KEY);
fat's avatar
fat committed
128

129
130
131
132
          if (!data) {
            data = new Alert(this);
            $element.data(DATA_KEY, data);
          }
fat's avatar
fat committed
133

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
          if (config === 'close') {
            data[config](this);
          }
        });
      }
    }, {
      key: '_handleDismiss',
      value: function _handleDismiss(alertInstance) {
        return function (event) {
          if (event) {
            event.preventDefault();
          }

          alertInstance.close(this);
        };
      }
    }]);
fat's avatar
fat committed
151

152
153
    return Alert;
  })();
fat's avatar
fat committed
154

155
156
157
158
159
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
160

161
  $(document).on(Event.CLICK, Selector.DISMISS, Alert._handleDismiss(new Alert()));
fat's avatar
fat committed
162

163
164
165
166
167
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
168

169
170
171
  $.fn[NAME] = Alert._jQueryInterface;
  $.fn[NAME].Constructor = Alert;
  $.fn[NAME].noConflict = function () {
fat's avatar
fat committed
172
    $.fn[NAME] = JQUERY_NO_CONFLICT;
173
174
    return Alert._jQueryInterface;
  };
fat's avatar
fat committed
175

176
  return Alert;
fat's avatar
fat committed
177
})(jQuery);
fat's avatar
fat committed
178
//# sourceMappingURL=alert.js.map