alert.js 8.63 KB
Newer Older
XhmikosR's avatar
Dist    
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap alert.js v4.3.1 (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
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/selector-engine.js')) :
  typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/event-handler.js', './dom/selector-engine.js'], factory) :
XhmikosR's avatar
XhmikosR committed
9
  (global = global || self, global.Alert = factory(global.Data, global.EventHandler, global.SelectorEngine));
XhmikosR's avatar
XhmikosR committed
10
}(this, (function (Data, EventHandler, SelectorEngine) { 'use strict';
Mark Otto's avatar
dist    
Mark Otto committed
11

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

  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
31

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

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

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

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

  var getElementFromSelector = function getElementFromSelector(element) {
    var selector = getSelector(element);
    return selector ? document.querySelector(selector) : null;
XhmikosR's avatar
XhmikosR committed
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
80
  };

  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
81
    element.dispatchEvent(new Event(TRANSITION_END));
XhmikosR's avatar
XhmikosR committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  };

  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
102
103
104
105
106
107
108
109
110
111
112
  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
113
114
115
116
117
118
119
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

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

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

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

fat's avatar
fat committed
146

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

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

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

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

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

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

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

XhmikosR's avatar
Dist    
XhmikosR committed
172
    _proto._getRootElement = function _getRootElement(element) {
XhmikosR's avatar
XhmikosR committed
173
      var parent = getElementFromSelector(element);
Mark Otto's avatar
dist    
Mark Otto committed
174

XhmikosR's avatar
Dist    
XhmikosR committed
175
      if (!parent) {
XhmikosR's avatar
XhmikosR committed
176
        parent = SelectorEngine.closest(element, "." + CLASSNAME_ALERT);
XhmikosR's avatar
Dist    
XhmikosR committed
177
      }
fat's avatar
fat committed
178

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

XhmikosR's avatar
Dist    
XhmikosR committed
182
    _proto._triggerCloseEvent = function _triggerCloseEvent(element) {
XhmikosR's avatar
XhmikosR committed
183
      return EventHandler.trigger(element, EVENT_CLOSE);
XhmikosR's avatar
Dist    
XhmikosR committed
184
    };
fat's avatar
fat committed
185

XhmikosR's avatar
Dist    
XhmikosR committed
186
187
    _proto._removeElement = function _removeElement(element) {
      var _this = this;
Mark Otto's avatar
grunt    
Mark Otto committed
188

XhmikosR's avatar
XhmikosR committed
189
      element.classList.remove(CLASSNAME_SHOW);
fat's avatar
fat committed
190

XhmikosR's avatar
XhmikosR committed
191
      if (!element.classList.contains(CLASSNAME_FADE)) {
XhmikosR's avatar
Dist    
XhmikosR committed
192
        this._destroyElement(element);
Mark Otto's avatar
Mark Otto committed
193

XhmikosR's avatar
Dist    
XhmikosR committed
194
195
        return;
      }
fat's avatar
fat committed
196

XhmikosR's avatar
XhmikosR committed
197
      var transitionDuration = getTransitionDurationFromElement(element);
198
199
      EventHandler.one(element, TRANSITION_END, function () {
        return _this._destroyElement(element);
XhmikosR's avatar
XhmikosR committed
200
201
      });
      emulateTransitionEnd(element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
202
    };
fat's avatar
fat committed
203

XhmikosR's avatar
Dist    
XhmikosR committed
204
    _proto._destroyElement = function _destroyElement(element) {
XhmikosR's avatar
XhmikosR committed
205
206
207
208
      if (element.parentNode) {
        element.parentNode.removeChild(element);
      }

XhmikosR's avatar
XhmikosR committed
209
      EventHandler.trigger(element, EVENT_CLOSED);
Mark Otto's avatar
Mark Otto committed
210
211
    } // Static
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
212

XhmikosR's avatar
XhmikosR committed
213
    Alert.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
214
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
215
        var data = Data.getData(this, DATA_KEY);
Mark Otto's avatar
dist    
Mark Otto committed
216

XhmikosR's avatar
Dist    
XhmikosR committed
217
218
219
        if (!data) {
          data = new Alert(this);
        }
Mark Otto's avatar
grunt    
Mark Otto committed
220

XhmikosR's avatar
Dist    
XhmikosR committed
221
222
223
224
225
        if (config === 'close') {
          data[config](this);
        }
      });
    };
Mark Otto's avatar
grunt    
Mark Otto committed
226

XhmikosR's avatar
XhmikosR committed
227
    Alert.handleDismiss = function handleDismiss(alertInstance) {
XhmikosR's avatar
Dist    
XhmikosR committed
228
229
230
231
      return function (event) {
        if (event) {
          event.preventDefault();
        }
Mark Otto's avatar
dist    
Mark Otto committed
232

XhmikosR's avatar
Dist    
XhmikosR committed
233
        alertInstance.close(this);
Mark Otto's avatar
dist    
Mark Otto committed
234
      };
XhmikosR's avatar
Dist    
XhmikosR committed
235
    };
Mark Otto's avatar
dist    
Mark Otto committed
236

XhmikosR's avatar
XhmikosR committed
237
    Alert.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
238
239
240
      return Data.getData(element, DATA_KEY);
    };

XhmikosR's avatar
Dist    
XhmikosR committed
241
242
243
244
245
246
    _createClass(Alert, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }]);
fat's avatar
fat committed
247

XhmikosR's avatar
Dist    
XhmikosR committed
248
249
250
251
252
253
254
    return Alert;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
255
256


XhmikosR's avatar
XhmikosR committed
257
  EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDismiss(new Alert()));
XhmikosR's avatar
XhmikosR committed
258
  var $ = getjQuery();
XhmikosR's avatar
Dist    
XhmikosR committed
259
260
261
262
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
263
   * add .alert to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
264
   */
Mark Otto's avatar
dist    
Mark Otto committed
265

266
267
  /* istanbul ignore if */

XhmikosR's avatar
XhmikosR committed
268
269
270
271
  if ($) {
    var JQUERY_NO_CONFLICT = $.fn[NAME];
    $.fn[NAME] = Alert.jQueryInterface;
    $.fn[NAME].Constructor = Alert;
Mark Otto's avatar
dist    
Mark Otto committed
272

XhmikosR's avatar
XhmikosR committed
273
274
275
    $.fn[NAME].noConflict = function () {
      $.fn[NAME] = JQUERY_NO_CONFLICT;
      return Alert.jQueryInterface;
XhmikosR's avatar
XhmikosR committed
276
277
    };
  }
fat's avatar
fat committed
278

279
  return Alert;
Mark Otto's avatar
dist    
Mark Otto committed
280

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