alert.js 8.76 KB
Newer Older
XhmikosR's avatar
Dist    
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap alert.js v5.0.0-alpha3 (https://getbootstrap.com/)
XhmikosR's avatar
XhmikosR committed
3
  * Copyright 2011-2020 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
XhmikosR's avatar
XhmikosR committed
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
XhmikosR's avatar
Dist    
XhmikosR committed
5
  */
Mark Otto's avatar
dist    
Mark Otto committed
6
(function (global, factory) {
XhmikosR's avatar
XhmikosR committed
7
8
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js')) :
  typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/event-handler.js'], factory) :
XhmikosR's avatar
XhmikosR committed
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Alert = factory(global.Data, global.EventHandler));
XhmikosR's avatar
XhmikosR committed
10
}(this, (function (Data, EventHandler) { 'use strict';
Mark Otto's avatar
dist    
Mark Otto committed
11

XhmikosR's avatar
XhmikosR committed
12
13
14
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

  var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
  var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
Mark Otto's avatar
dist    
Mark Otto committed
16

XhmikosR's avatar
XhmikosR committed
17
18
  /**
   * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
19
   * Bootstrap (v5.0.0-alpha3): util/index.js
XhmikosR's avatar
XhmikosR committed
20
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
XhmikosR's avatar
XhmikosR committed
21
22
23
   * --------------------------------------------------------------------------
   */
  var MILLISECONDS_MULTIPLIER = 1000;
XhmikosR's avatar
XhmikosR committed
24
  var TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
XhmikosR's avatar
XhmikosR committed
25

XhmikosR's avatar
XhmikosR committed
26
  var getSelector = function getSelector(element) {
XhmikosR's avatar
XhmikosR committed
27
    var selector = element.getAttribute('data-bs-target');
XhmikosR's avatar
XhmikosR committed
28
29
30

    if (!selector || selector === '#') {
      var hrefAttr = element.getAttribute('href');
XhmikosR's avatar
XhmikosR committed
31
      selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
XhmikosR's avatar
XhmikosR committed
32
33
    }

XhmikosR's avatar
XhmikosR committed
34
35
36
37
38
39
    return selector;
  };

  var getElementFromSelector = function getElementFromSelector(element) {
    var selector = getSelector(element);
    return selector ? document.querySelector(selector) : null;
XhmikosR's avatar
XhmikosR committed
40
41
42
43
44
45
46
47
48
49
50
51
  };

  var getTransitionDurationFromElement = function getTransitionDurationFromElement(element) {
    if (!element) {
      return 0;
    } // Get transition-duration of the element


    var _window$getComputedSt = window.getComputedStyle(element),
        transitionDuration = _window$getComputedSt.transitionDuration,
        transitionDelay = _window$getComputedSt.transitionDelay;

XhmikosR's avatar
XhmikosR committed
52
53
    var floatTransitionDuration = Number.parseFloat(transitionDuration);
    var floatTransitionDelay = Number.parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
XhmikosR's avatar
XhmikosR committed
54
55
56
57
58
59
60
61

    if (!floatTransitionDuration && !floatTransitionDelay) {
      return 0;
    } // If multiple durations are defined, take the first


    transitionDuration = transitionDuration.split(',')[0];
    transitionDelay = transitionDelay.split(',')[0];
XhmikosR's avatar
XhmikosR committed
62
    return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
XhmikosR's avatar
XhmikosR committed
63
64
65
  };

  var triggerTransitionEnd = function triggerTransitionEnd(element) {
XhmikosR's avatar
XhmikosR committed
66
    element.dispatchEvent(new Event(TRANSITION_END));
XhmikosR's avatar
XhmikosR committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  };

  var emulateTransitionEnd = function emulateTransitionEnd(element, duration) {
    var called = false;
    var durationPadding = 5;
    var emulatedDuration = duration + durationPadding;

    function listener() {
      called = true;
      element.removeEventListener(TRANSITION_END, listener);
    }

    element.addEventListener(TRANSITION_END, listener);
    setTimeout(function () {
      if (!called) {
        triggerTransitionEnd(element);
      }
    }, emulatedDuration);
  };

XhmikosR's avatar
XhmikosR committed
87
88
89
90
  var getjQuery = function getjQuery() {
    var _window = window,
        jQuery = _window.jQuery;

XhmikosR's avatar
XhmikosR committed
91
    if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
XhmikosR's avatar
XhmikosR committed
92
93
94
95
96
97
      return jQuery;
    }

    return null;
  };

XhmikosR's avatar
XhmikosR committed
98
99
100
101
102
103
104
105
  var onDOMContentLoaded = function onDOMContentLoaded(callback) {
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', callback);
    } else {
      callback();
    }
  };

Mark Otto's avatar
Mark Otto committed
106
107
108
  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); } }

  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
XhmikosR's avatar
Dist    
XhmikosR committed
109
110
111
112
113
114
115
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  var NAME = 'alert';
XhmikosR's avatar
XhmikosR committed
116
  var VERSION = '5.0.0-alpha3';
XhmikosR's avatar
Dist    
XhmikosR committed
117
118
119
  var DATA_KEY = 'bs.alert';
  var EVENT_KEY = "." + DATA_KEY;
  var DATA_API_KEY = '.data-api';
XhmikosR's avatar
XhmikosR committed
120
  var SELECTOR_DISMISS = '[data-bs-dismiss="alert"]';
XhmikosR's avatar
XhmikosR committed
121
122
123
124
125
126
  var EVENT_CLOSE = "close" + EVENT_KEY;
  var EVENT_CLOSED = "closed" + EVENT_KEY;
  var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
  var CLASSNAME_ALERT = 'alert';
  var CLASSNAME_FADE = 'fade';
  var CLASSNAME_SHOW = 'show';
XhmikosR's avatar
XhmikosR committed
127
128
129
130
131
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
132

XhmikosR's avatar
XhmikosR committed
133
  var Alert = /*#__PURE__*/function () {
XhmikosR's avatar
Dist    
XhmikosR committed
134
135
    function Alert(element) {
      this._element = element;
XhmikosR's avatar
XhmikosR committed
136
137

      if (this._element) {
XhmikosR's avatar
XhmikosR committed
138
        Data__default['default'].setData(element, DATA_KEY, this);
XhmikosR's avatar
XhmikosR committed
139
      }
XhmikosR's avatar
Dist    
XhmikosR committed
140
    } // Getters
Mark Otto's avatar
dist    
Mark Otto committed
141

fat's avatar
fat committed
142

XhmikosR's avatar
Dist    
XhmikosR committed
143
    var _proto = Alert.prototype;
Jacob Thornton's avatar
Jacob Thornton committed
144

XhmikosR's avatar
Dist    
XhmikosR committed
145
146
    // Public
    _proto.close = function close(element) {
XhmikosR's avatar
XhmikosR committed
147
      var rootElement = element ? this._getRootElement(element) : this._element;
fat's avatar
fat committed
148

XhmikosR's avatar
Dist    
XhmikosR committed
149
      var customEvent = this._triggerCloseEvent(rootElement);
fat's avatar
fat committed
150

XhmikosR's avatar
XhmikosR committed
151
      if (customEvent === null || customEvent.defaultPrevented) {
XhmikosR's avatar
Dist    
XhmikosR committed
152
153
        return;
      }
Mark Otto's avatar
grunt    
Mark Otto committed
154

XhmikosR's avatar
Dist    
XhmikosR committed
155
156
      this._removeElement(rootElement);
    };
Mark Otto's avatar
grunt    
Mark Otto committed
157

XhmikosR's avatar
Dist    
XhmikosR committed
158
    _proto.dispose = function dispose() {
XhmikosR's avatar
XhmikosR committed
159
      Data__default['default'].removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
160
      this._element = null;
Mark Otto's avatar
Mark Otto committed
161
162
    } // Private
    ;
Mark Otto's avatar
dist    
Mark Otto committed
163

XhmikosR's avatar
Dist    
XhmikosR committed
164
    _proto._getRootElement = function _getRootElement(element) {
XhmikosR's avatar
XhmikosR committed
165
      return getElementFromSelector(element) || element.closest("." + CLASSNAME_ALERT);
XhmikosR's avatar
Dist    
XhmikosR committed
166
    };
fat's avatar
fat committed
167

XhmikosR's avatar
Dist    
XhmikosR committed
168
    _proto._triggerCloseEvent = function _triggerCloseEvent(element) {
XhmikosR's avatar
XhmikosR committed
169
      return EventHandler__default['default'].trigger(element, EVENT_CLOSE);
XhmikosR's avatar
Dist    
XhmikosR committed
170
    };
fat's avatar
fat committed
171

XhmikosR's avatar
Dist    
XhmikosR committed
172
173
    _proto._removeElement = function _removeElement(element) {
      var _this = this;
Mark Otto's avatar
grunt    
Mark Otto committed
174

XhmikosR's avatar
XhmikosR committed
175
      element.classList.remove(CLASSNAME_SHOW);
fat's avatar
fat committed
176

XhmikosR's avatar
XhmikosR committed
177
      if (!element.classList.contains(CLASSNAME_FADE)) {
XhmikosR's avatar
Dist    
XhmikosR committed
178
        this._destroyElement(element);
Mark Otto's avatar
Mark Otto committed
179

XhmikosR's avatar
Dist    
XhmikosR committed
180
181
        return;
      }
fat's avatar
fat committed
182

XhmikosR's avatar
XhmikosR committed
183
      var transitionDuration = getTransitionDurationFromElement(element);
XhmikosR's avatar
XhmikosR committed
184
      EventHandler__default['default'].one(element, TRANSITION_END, function () {
185
        return _this._destroyElement(element);
XhmikosR's avatar
XhmikosR committed
186
187
      });
      emulateTransitionEnd(element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
188
    };
fat's avatar
fat committed
189

XhmikosR's avatar
Dist    
XhmikosR committed
190
    _proto._destroyElement = function _destroyElement(element) {
XhmikosR's avatar
XhmikosR committed
191
192
193
194
      if (element.parentNode) {
        element.parentNode.removeChild(element);
      }

XhmikosR's avatar
XhmikosR committed
195
      EventHandler__default['default'].trigger(element, EVENT_CLOSED);
Mark Otto's avatar
Mark Otto committed
196
197
    } // Static
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
198

XhmikosR's avatar
XhmikosR committed
199
    Alert.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
200
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
201
        var data = Data__default['default'].getData(this, DATA_KEY);
Mark Otto's avatar
dist    
Mark Otto committed
202

XhmikosR's avatar
Dist    
XhmikosR committed
203
204
205
        if (!data) {
          data = new Alert(this);
        }
Mark Otto's avatar
grunt    
Mark Otto committed
206

XhmikosR's avatar
Dist    
XhmikosR committed
207
208
209
210
211
        if (config === 'close') {
          data[config](this);
        }
      });
    };
Mark Otto's avatar
grunt    
Mark Otto committed
212

XhmikosR's avatar
XhmikosR committed
213
    Alert.handleDismiss = function handleDismiss(alertInstance) {
XhmikosR's avatar
Dist    
XhmikosR committed
214
215
216
217
      return function (event) {
        if (event) {
          event.preventDefault();
        }
Mark Otto's avatar
dist    
Mark Otto committed
218

XhmikosR's avatar
Dist    
XhmikosR committed
219
        alertInstance.close(this);
Mark Otto's avatar
dist    
Mark Otto committed
220
      };
XhmikosR's avatar
Dist    
XhmikosR committed
221
    };
Mark Otto's avatar
dist    
Mark Otto committed
222

XhmikosR's avatar
XhmikosR committed
223
    Alert.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
224
      return Data__default['default'].getData(element, DATA_KEY);
XhmikosR's avatar
XhmikosR committed
225
226
    };

XhmikosR's avatar
Dist    
XhmikosR committed
227
228
229
230
231
232
    _createClass(Alert, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }]);
fat's avatar
fat committed
233

XhmikosR's avatar
Dist    
XhmikosR committed
234
235
236
237
238
239
240
    return Alert;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
241
242


XhmikosR's avatar
XhmikosR committed
243
  EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()));
XhmikosR's avatar
Dist    
XhmikosR committed
244
245
246
247
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
248
   * add .Alert to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
249
   */
Mark Otto's avatar
dist    
Mark Otto committed
250

XhmikosR's avatar
XhmikosR committed
251
252
253
  onDOMContentLoaded(function () {
    var $ = getjQuery();
    /* istanbul ignore if */
254

XhmikosR's avatar
XhmikosR committed
255
256
257
258
    if ($) {
      var JQUERY_NO_CONFLICT = $.fn[NAME];
      $.fn[NAME] = Alert.jQueryInterface;
      $.fn[NAME].Constructor = Alert;
Mark Otto's avatar
dist    
Mark Otto committed
259

XhmikosR's avatar
XhmikosR committed
260
261
262
263
264
265
      $.fn[NAME].noConflict = function () {
        $.fn[NAME] = JQUERY_NO_CONFLICT;
        return Alert.jQueryInterface;
      };
    }
  });
fat's avatar
fat committed
266

267
  return Alert;
Mark Otto's avatar
dist    
Mark Otto committed
268

XhmikosR's avatar
XhmikosR committed
269
})));
Mark Otto's avatar
dist    
Mark Otto committed
270
//# sourceMappingURL=alert.js.map