alert.js 4.99 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
  /**
   * ------------------------------------------------------------------------
   * 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);

fat's avatar
fat committed
54
      this._element = element;
fat's avatar
fat committed
55
56
    }

57
58
59
60
    _createClass(Alert, [{
      key: 'close',

      // public
fat's avatar
fat committed
61

62
      value: function close(element) {
fat's avatar
fat committed
63
        element = element || this._element;
fat's avatar
fat committed
64

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

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

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

77
      // private
fat's avatar
fat committed
78

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

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

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

91
92
93
94
95
96
97
98
        return parent;
      }
    }, {
      key: '_triggerCloseEvent',
      value: function _triggerCloseEvent(element) {
        var closeEvent = $.Event(Event.CLOSE);
        $(element).trigger(closeEvent);
        return closeEvent;
fat's avatar
fat committed
99
      }
100
101
102
103
104
105
106
107
108
    }, {
      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
109

110
111
112
113
114
115
        $(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
116
      }
117
    }], [{
118
119
120
121
122
123
124
125
      key: 'VERSION',

      // getters

      get: function () {
        return VERSION;
      }
    }, {
126
      key: '_jQueryInterface',
fat's avatar
fat committed
127

128
      // static
fat's avatar
fat committed
129

130
131
132
133
      value: function _jQueryInterface(config) {
        return this.each(function () {
          var $element = $(this);
          var data = $element.data(DATA_KEY);
fat's avatar
fat committed
134

135
136
137
138
          if (!data) {
            data = new Alert(this);
            $element.data(DATA_KEY, data);
          }
fat's avatar
fat committed
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
          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
157

158
159
    return Alert;
  })();
fat's avatar
fat committed
160

161
162
163
164
165
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
166

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

169
170
171
172
173
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
174

175
176
177
  $.fn[NAME] = Alert._jQueryInterface;
  $.fn[NAME].Constructor = Alert;
  $.fn[NAME].noConflict = function () {
fat's avatar
fat committed
178
    $.fn[NAME] = JQUERY_NO_CONFLICT;
179
180
    return Alert._jQueryInterface;
  };
fat's avatar
fat committed
181

182
  return Alert;
fat's avatar
fat committed
183
})(jQuery);
fat's avatar
fat committed
184
//# sourceMappingURL=alert.js.map