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/)
Mark Otto's avatar
Mark Otto committed
3
  * Copyright 2011-2019 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 && Data.hasOwnProperty('default') ? Data['default'] : Data;
  EventHandler = EventHandler && EventHandler.hasOwnProperty('default') ? EventHandler['default'] : EventHandler;
  SelectorEngine = SelectorEngine && SelectorEngine.hasOwnProperty('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
82
83
    var evt = document.createEvent('HTMLEvents');
    evt.initEvent(TRANSITION_END, true, true);
    element.dispatchEvent(evt);
XhmikosR's avatar
XhmikosR committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  };

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

  var NAME = 'alert';
XhmikosR's avatar
XhmikosR committed
122
  var VERSION = '4.3.1';
XhmikosR's avatar
Dist    
XhmikosR committed
123
124
125
126
127
128
  var DATA_KEY = 'bs.alert';
  var EVENT_KEY = "." + DATA_KEY;
  var DATA_API_KEY = '.data-api';
  var Selector = {
    DISMISS: '[data-dismiss="alert"]'
  };
XhmikosR's avatar
XhmikosR committed
129
  var Event = {
XhmikosR's avatar
Dist    
XhmikosR committed
130
131
132
133
134
135
136
137
138
    CLOSE: "close" + EVENT_KEY,
    CLOSED: "closed" + EVENT_KEY,
    CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
  };
  var ClassName = {
    ALERT: 'alert',
    FADE: 'fade',
    SHOW: 'show'
  };
XhmikosR's avatar
XhmikosR committed
139
140
141
142
143
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
144

XhmikosR's avatar
Dist    
XhmikosR committed
145
146
147
148
149
  var Alert =
  /*#__PURE__*/
  function () {
    function Alert(element) {
      this._element = element;
XhmikosR's avatar
XhmikosR committed
150
151
152
153

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

fat's avatar
fat committed
156

XhmikosR's avatar
Dist    
XhmikosR committed
157
    var _proto = Alert.prototype;
Jacob Thornton's avatar
Jacob Thornton committed
158

XhmikosR's avatar
Dist    
XhmikosR committed
159
160
161
    // Public
    _proto.close = function close(element) {
      var rootElement = this._element;
fat's avatar
fat committed
162

XhmikosR's avatar
Dist    
XhmikosR committed
163
164
165
      if (element) {
        rootElement = this._getRootElement(element);
      }
fat's avatar
fat committed
166

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

XhmikosR's avatar
XhmikosR committed
169
      if (customEvent === null || customEvent.defaultPrevented) {
XhmikosR's avatar
Dist    
XhmikosR committed
170
171
        return;
      }
Mark Otto's avatar
grunt    
Mark Otto committed
172

XhmikosR's avatar
Dist    
XhmikosR committed
173
174
      this._removeElement(rootElement);
    };
Mark Otto's avatar
grunt    
Mark Otto committed
175

XhmikosR's avatar
Dist    
XhmikosR committed
176
    _proto.dispose = function dispose() {
XhmikosR's avatar
XhmikosR committed
177
      Data.removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
178
      this._element = null;
Mark Otto's avatar
Mark Otto committed
179
180
    } // Private
    ;
Mark Otto's avatar
dist    
Mark Otto committed
181

XhmikosR's avatar
Dist    
XhmikosR committed
182
    _proto._getRootElement = function _getRootElement(element) {
XhmikosR's avatar
XhmikosR committed
183
      var parent = getElementFromSelector(element);
Mark Otto's avatar
dist    
Mark Otto committed
184

XhmikosR's avatar
Dist    
XhmikosR committed
185
      if (!parent) {
XhmikosR's avatar
XhmikosR committed
186
        parent = SelectorEngine.closest(element, "." + ClassName.ALERT);
XhmikosR's avatar
Dist    
XhmikosR committed
187
      }
fat's avatar
fat committed
188

XhmikosR's avatar
Dist    
XhmikosR committed
189
190
      return parent;
    };
fat's avatar
fat committed
191

XhmikosR's avatar
Dist    
XhmikosR committed
192
    _proto._triggerCloseEvent = function _triggerCloseEvent(element) {
XhmikosR's avatar
XhmikosR committed
193
      return EventHandler.trigger(element, Event.CLOSE);
XhmikosR's avatar
Dist    
XhmikosR committed
194
    };
fat's avatar
fat committed
195

XhmikosR's avatar
Dist    
XhmikosR committed
196
197
    _proto._removeElement = function _removeElement(element) {
      var _this = this;
Mark Otto's avatar
grunt    
Mark Otto committed
198

XhmikosR's avatar
XhmikosR committed
199
      element.classList.remove(ClassName.SHOW);
fat's avatar
fat committed
200

XhmikosR's avatar
XhmikosR committed
201
      if (!element.classList.contains(ClassName.FADE)) {
XhmikosR's avatar
Dist    
XhmikosR committed
202
        this._destroyElement(element);
Mark Otto's avatar
Mark Otto committed
203

XhmikosR's avatar
Dist    
XhmikosR committed
204
205
        return;
      }
fat's avatar
fat committed
206

XhmikosR's avatar
XhmikosR committed
207
      var transitionDuration = getTransitionDurationFromElement(element);
208
209
      EventHandler.one(element, TRANSITION_END, function () {
        return _this._destroyElement(element);
XhmikosR's avatar
XhmikosR committed
210
211
      });
      emulateTransitionEnd(element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
212
    };
fat's avatar
fat committed
213

XhmikosR's avatar
Dist    
XhmikosR committed
214
    _proto._destroyElement = function _destroyElement(element) {
XhmikosR's avatar
XhmikosR committed
215
216
217
218
      if (element.parentNode) {
        element.parentNode.removeChild(element);
      }

XhmikosR's avatar
XhmikosR committed
219
      EventHandler.trigger(element, Event.CLOSED);
Mark Otto's avatar
Mark Otto committed
220
221
    } // Static
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
222

XhmikosR's avatar
XhmikosR committed
223
    Alert.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
224
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
225
        var data = Data.getData(this, DATA_KEY);
Mark Otto's avatar
dist    
Mark Otto committed
226

XhmikosR's avatar
Dist    
XhmikosR committed
227
228
229
        if (!data) {
          data = new Alert(this);
        }
Mark Otto's avatar
grunt    
Mark Otto committed
230

XhmikosR's avatar
Dist    
XhmikosR committed
231
232
233
234
235
        if (config === 'close') {
          data[config](this);
        }
      });
    };
Mark Otto's avatar
grunt    
Mark Otto committed
236

XhmikosR's avatar
XhmikosR committed
237
    Alert.handleDismiss = function handleDismiss(alertInstance) {
XhmikosR's avatar
Dist    
XhmikosR committed
238
239
240
241
      return function (event) {
        if (event) {
          event.preventDefault();
        }
Mark Otto's avatar
dist    
Mark Otto committed
242

XhmikosR's avatar
Dist    
XhmikosR committed
243
        alertInstance.close(this);
Mark Otto's avatar
dist    
Mark Otto committed
244
      };
XhmikosR's avatar
Dist    
XhmikosR committed
245
    };
Mark Otto's avatar
dist    
Mark Otto committed
246

XhmikosR's avatar
XhmikosR committed
247
    Alert.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
248
249
250
      return Data.getData(element, DATA_KEY);
    };

XhmikosR's avatar
Dist    
XhmikosR committed
251
252
253
254
255
256
    _createClass(Alert, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }]);
fat's avatar
fat committed
257

XhmikosR's avatar
Dist    
XhmikosR committed
258
259
260
261
262
263
264
    return Alert;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
265
266


XhmikosR's avatar
XhmikosR committed
267
268
  EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert.handleDismiss(new Alert()));
  var $ = getjQuery();
XhmikosR's avatar
Dist    
XhmikosR committed
269
270
271
272
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
273
   * add .alert to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
274
   */
Mark Otto's avatar
dist    
Mark Otto committed
275

276
277
  /* istanbul ignore if */

XhmikosR's avatar
XhmikosR committed
278
279
280
281
  if ($) {
    var JQUERY_NO_CONFLICT = $.fn[NAME];
    $.fn[NAME] = Alert.jQueryInterface;
    $.fn[NAME].Constructor = Alert;
Mark Otto's avatar
dist    
Mark Otto committed
282

XhmikosR's avatar
XhmikosR committed
283
284
285
    $.fn[NAME].noConflict = function () {
      $.fn[NAME] = JQUERY_NO_CONFLICT;
      return Alert.jQueryInterface;
XhmikosR's avatar
XhmikosR committed
286
287
    };
  }
fat's avatar
fat committed
288

289
  return Alert;
Mark Otto's avatar
dist    
Mark Otto committed
290

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