alert.js 8.31 KB
Newer Older
XhmikosR's avatar
Dist    
XhmikosR committed
1
/*!
2
  * Bootstrap alert.js v5.0.0-alpha1 (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
Dist    
XhmikosR committed
4
5
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  */
Mark Otto's avatar
dist    
Mark Otto committed
6
(function (global, factory) {
XhmikosR's avatar
XhmikosR committed
7
8
9
10
  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) :
  (global = global || self, global.Alert = factory(global.Data, global.EventHandler));
}(this, (function (Data, EventHandler) { 'use strict';
Mark Otto's avatar
dist    
Mark Otto committed
11

XhmikosR's avatar
XhmikosR committed
12
13
  Data = Data && Object.prototype.hasOwnProperty.call(Data, 'default') ? Data['default'] : Data;
  EventHandler = EventHandler && Object.prototype.hasOwnProperty.call(EventHandler, 'default') ? EventHandler['default'] : EventHandler;
Mark Otto's avatar
dist    
Mark Otto committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

  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;
  }
Mark Otto's avatar
dist    
Mark Otto committed
30

XhmikosR's avatar
XhmikosR committed
31
32
  /**
   * --------------------------------------------------------------------------
33
   * Bootstrap (v5.0.0-alpha1): util/index.js
XhmikosR's avatar
XhmikosR committed
34
35
36
37
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
   * --------------------------------------------------------------------------
   */
  var MILLISECONDS_MULTIPLIER = 1000;
XhmikosR's avatar
XhmikosR committed
38
  var TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
XhmikosR's avatar
XhmikosR committed
39

XhmikosR's avatar
XhmikosR committed
40
  var getSelector = function getSelector(element) {
XhmikosR's avatar
XhmikosR committed
41
42
43
44
    var selector = element.getAttribute('data-target');

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

XhmikosR's avatar
XhmikosR committed
48
49
50
51
52
53
    return selector;
  };

  var getElementFromSelector = function getElementFromSelector(element) {
    var selector = getSelector(element);
    return selector ? document.querySelector(selector) : null;
XhmikosR's avatar
XhmikosR committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  };

  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;

    var floatTransitionDuration = parseFloat(transitionDuration);
    var floatTransitionDelay = parseFloat(transitionDelay); // Return 0 if element or transition duration is not found

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


    transitionDuration = transitionDuration.split(',')[0];
    transitionDelay = transitionDelay.split(',')[0];
    return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
  };

  var triggerTransitionEnd = function triggerTransitionEnd(element) {
XhmikosR's avatar
XhmikosR committed
80
    element.dispatchEvent(new Event(TRANSITION_END));
XhmikosR's avatar
XhmikosR committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  };

  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
101
102
103
104
105
106
107
108
109
110
111
  var getjQuery = function getjQuery() {
    var _window = window,
        jQuery = _window.jQuery;

    if (jQuery && !document.body.hasAttribute('data-no-jquery')) {
      return jQuery;
    }

    return null;
  };

XhmikosR's avatar
Dist    
XhmikosR committed
112
113
114
115
116
117
118
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  var NAME = 'alert';
119
  var VERSION = '5.0.0-alpha1';
XhmikosR's avatar
Dist    
XhmikosR committed
120
121
122
  var DATA_KEY = 'bs.alert';
  var EVENT_KEY = "." + DATA_KEY;
  var DATA_API_KEY = '.data-api';
XhmikosR's avatar
XhmikosR committed
123
124
125
126
127
128
129
  var SELECTOR_DISMISS = '[data-dismiss="alert"]';
  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
130
131
132
133
134
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
135

XhmikosR's avatar
XhmikosR committed
136
  var Alert = /*#__PURE__*/function () {
XhmikosR's avatar
Dist    
XhmikosR committed
137
138
    function Alert(element) {
      this._element = element;
XhmikosR's avatar
XhmikosR committed
139
140
141
142

      if (this._element) {
        Data.setData(element, DATA_KEY, this);
      }
XhmikosR's avatar
Dist    
XhmikosR committed
143
    } // Getters
Mark Otto's avatar
dist    
Mark Otto committed
144

fat's avatar
fat committed
145

XhmikosR's avatar
Dist    
XhmikosR committed
146
    var _proto = Alert.prototype;
Jacob Thornton's avatar
Jacob Thornton committed
147

XhmikosR's avatar
Dist    
XhmikosR committed
148
149
150
    // Public
    _proto.close = function close(element) {
      var rootElement = this._element;
fat's avatar
fat committed
151

XhmikosR's avatar
Dist    
XhmikosR committed
152
153
154
      if (element) {
        rootElement = this._getRootElement(element);
      }
fat's avatar
fat committed
155

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

XhmikosR's avatar
XhmikosR committed
158
      if (customEvent === null || customEvent.defaultPrevented) {
XhmikosR's avatar
Dist    
XhmikosR committed
159
160
        return;
      }
Mark Otto's avatar
grunt    
Mark Otto committed
161

XhmikosR's avatar
Dist    
XhmikosR committed
162
163
      this._removeElement(rootElement);
    };
Mark Otto's avatar
grunt    
Mark Otto committed
164

XhmikosR's avatar
Dist    
XhmikosR committed
165
    _proto.dispose = function dispose() {
XhmikosR's avatar
XhmikosR committed
166
      Data.removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
167
      this._element = null;
Mark Otto's avatar
Mark Otto committed
168
169
    } // Private
    ;
Mark Otto's avatar
dist    
Mark Otto committed
170

XhmikosR's avatar
Dist    
XhmikosR committed
171
    _proto._getRootElement = function _getRootElement(element) {
XhmikosR's avatar
XhmikosR committed
172
      return getElementFromSelector(element) || element.closest("." + CLASSNAME_ALERT);
XhmikosR's avatar
Dist    
XhmikosR committed
173
    };
fat's avatar
fat committed
174

XhmikosR's avatar
Dist    
XhmikosR committed
175
    _proto._triggerCloseEvent = function _triggerCloseEvent(element) {
XhmikosR's avatar
XhmikosR committed
176
      return EventHandler.trigger(element, EVENT_CLOSE);
XhmikosR's avatar
Dist    
XhmikosR committed
177
    };
fat's avatar
fat committed
178

XhmikosR's avatar
Dist    
XhmikosR committed
179
180
    _proto._removeElement = function _removeElement(element) {
      var _this = this;
Mark Otto's avatar
grunt    
Mark Otto committed
181

XhmikosR's avatar
XhmikosR committed
182
      element.classList.remove(CLASSNAME_SHOW);
fat's avatar
fat committed
183

XhmikosR's avatar
XhmikosR committed
184
      if (!element.classList.contains(CLASSNAME_FADE)) {
XhmikosR's avatar
Dist    
XhmikosR committed
185
        this._destroyElement(element);
Mark Otto's avatar
Mark Otto committed
186

XhmikosR's avatar
Dist    
XhmikosR committed
187
188
        return;
      }
fat's avatar
fat committed
189

XhmikosR's avatar
XhmikosR committed
190
      var transitionDuration = getTransitionDurationFromElement(element);
191
192
      EventHandler.one(element, TRANSITION_END, function () {
        return _this._destroyElement(element);
XhmikosR's avatar
XhmikosR committed
193
194
      });
      emulateTransitionEnd(element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
195
    };
fat's avatar
fat committed
196

XhmikosR's avatar
Dist    
XhmikosR committed
197
    _proto._destroyElement = function _destroyElement(element) {
XhmikosR's avatar
XhmikosR committed
198
199
200
201
      if (element.parentNode) {
        element.parentNode.removeChild(element);
      }

XhmikosR's avatar
XhmikosR committed
202
      EventHandler.trigger(element, EVENT_CLOSED);
Mark Otto's avatar
Mark Otto committed
203
204
    } // Static
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
205

XhmikosR's avatar
XhmikosR committed
206
    Alert.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
207
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
208
        var data = Data.getData(this, DATA_KEY);
Mark Otto's avatar
dist    
Mark Otto committed
209

XhmikosR's avatar
Dist    
XhmikosR committed
210
211
212
        if (!data) {
          data = new Alert(this);
        }
Mark Otto's avatar
grunt    
Mark Otto committed
213

XhmikosR's avatar
Dist    
XhmikosR committed
214
215
216
217
218
        if (config === 'close') {
          data[config](this);
        }
      });
    };
Mark Otto's avatar
grunt    
Mark Otto committed
219

XhmikosR's avatar
XhmikosR committed
220
    Alert.handleDismiss = function handleDismiss(alertInstance) {
XhmikosR's avatar
Dist    
XhmikosR committed
221
222
223
224
      return function (event) {
        if (event) {
          event.preventDefault();
        }
Mark Otto's avatar
dist    
Mark Otto committed
225

XhmikosR's avatar
Dist    
XhmikosR committed
226
        alertInstance.close(this);
Mark Otto's avatar
dist    
Mark Otto committed
227
      };
XhmikosR's avatar
Dist    
XhmikosR committed
228
    };
Mark Otto's avatar
dist    
Mark Otto committed
229

XhmikosR's avatar
XhmikosR committed
230
    Alert.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
231
232
233
      return Data.getData(element, DATA_KEY);
    };

XhmikosR's avatar
Dist    
XhmikosR committed
234
235
236
237
238
239
    _createClass(Alert, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }]);
fat's avatar
fat committed
240

XhmikosR's avatar
Dist    
XhmikosR committed
241
242
243
244
245
246
247
    return Alert;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
248
249


XhmikosR's avatar
XhmikosR committed
250
  EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()));
XhmikosR's avatar
XhmikosR committed
251
  var $ = getjQuery();
XhmikosR's avatar
Dist    
XhmikosR committed
252
253
254
255
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
256
   * add .alert to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
257
   */
Mark Otto's avatar
dist    
Mark Otto committed
258

259
260
  /* istanbul ignore if */

XhmikosR's avatar
XhmikosR committed
261
262
263
264
  if ($) {
    var JQUERY_NO_CONFLICT = $.fn[NAME];
    $.fn[NAME] = Alert.jQueryInterface;
    $.fn[NAME].Constructor = Alert;
Mark Otto's avatar
dist    
Mark Otto committed
265

XhmikosR's avatar
XhmikosR committed
266
267
268
    $.fn[NAME].noConflict = function () {
      $.fn[NAME] = JQUERY_NO_CONFLICT;
      return Alert.jQueryInterface;
XhmikosR's avatar
XhmikosR committed
269
270
    };
  }
fat's avatar
fat committed
271

272
  return Alert;
Mark Otto's avatar
dist    
Mark Otto committed
273

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