alert.js 8.54 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) {
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
10
  (global = global || self, global.Alert = factory(global.Data, global.EventHandler, global.SelectorEngine));
}(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
39
  /**
   * --------------------------------------------------------------------------
   * Bootstrap (v4.3.1): util/index.js
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
   * --------------------------------------------------------------------------
   */
  var MILLISECONDS_MULTIPLIER = 1000;
  var TRANSITION_END = 'transitionend';
XhmikosR's avatar
Dist.    
XhmikosR committed
40
41
  var _window = window,
      jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
XhmikosR's avatar
XhmikosR committed
42
43
44
45
46
47
48
49
50
51
52

  var getSelectorFromElement = function getSelectorFromElement(element) {
    var selector = element.getAttribute('data-target');

    if (!selector || selector === '#') {
      var hrefAttr = element.getAttribute('href');
      selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : '';
    }

    try {
      return document.querySelector(selector) ? selector : null;
XhmikosR's avatar
Dist.    
XhmikosR committed
53
    } catch (error) {
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
80
81
      return null;
    }
  };

  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
82
83
84
    var evt = document.createEvent('HTMLEvents');
    evt.initEvent(TRANSITION_END, true, true);
    element.dispatchEvent(evt);
XhmikosR's avatar
XhmikosR committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  };

  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
Dist    
XhmikosR committed
105
106
107
108
109
110
111
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  var NAME = 'alert';
XhmikosR's avatar
XhmikosR committed
112
  var VERSION = '4.3.1';
XhmikosR's avatar
Dist    
XhmikosR committed
113
114
115
116
117
118
  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
119
  var Event = {
XhmikosR's avatar
Dist    
XhmikosR committed
120
121
122
123
124
125
126
127
    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'
Mark Otto's avatar
dist    
Mark Otto committed
128
129
    /**
     * ------------------------------------------------------------------------
XhmikosR's avatar
Dist    
XhmikosR committed
130
     * Class Definition
Mark Otto's avatar
dist    
Mark Otto committed
131
132
     * ------------------------------------------------------------------------
     */
133

XhmikosR's avatar
Dist    
XhmikosR committed
134
  };
135

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

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

fat's avatar
fat committed
147

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

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

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

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

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

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

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

XhmikosR's avatar
Dist    
XhmikosR committed
173
    _proto._getRootElement = function _getRootElement(element) {
XhmikosR's avatar
XhmikosR committed
174
      var selector = getSelectorFromElement(element);
XhmikosR's avatar
Dist    
XhmikosR committed
175
      var parent = false;
Mark Otto's avatar
dist    
Mark Otto committed
176

XhmikosR's avatar
Dist    
XhmikosR committed
177
      if (selector) {
XhmikosR's avatar
XhmikosR committed
178
        parent = SelectorEngine.findOne(selector);
XhmikosR's avatar
Dist    
XhmikosR committed
179
      }
Mark Otto's avatar
dist    
Mark Otto committed
180

XhmikosR's avatar
Dist    
XhmikosR committed
181
      if (!parent) {
XhmikosR's avatar
XhmikosR committed
182
        parent = SelectorEngine.closest(element, "." + ClassName.ALERT);
XhmikosR's avatar
Dist    
XhmikosR committed
183
      }
fat's avatar
fat committed
184

XhmikosR's avatar
Dist    
XhmikosR committed
185
186
      return parent;
    };
fat's avatar
fat committed
187

XhmikosR's avatar
Dist    
XhmikosR committed
188
    _proto._triggerCloseEvent = function _triggerCloseEvent(element) {
XhmikosR's avatar
XhmikosR committed
189
      return EventHandler.trigger(element, Event.CLOSE);
XhmikosR's avatar
Dist    
XhmikosR committed
190
    };
fat's avatar
fat committed
191

XhmikosR's avatar
Dist    
XhmikosR committed
192
193
    _proto._removeElement = function _removeElement(element) {
      var _this = this;
Mark Otto's avatar
grunt    
Mark Otto committed
194

XhmikosR's avatar
XhmikosR committed
195
      element.classList.remove(ClassName.SHOW);
fat's avatar
fat committed
196

XhmikosR's avatar
XhmikosR committed
197
      if (!element.classList.contains(ClassName.FADE)) {
XhmikosR's avatar
Dist    
XhmikosR committed
198
        this._destroyElement(element);
Mark Otto's avatar
Mark Otto committed
199

XhmikosR's avatar
Dist    
XhmikosR committed
200
201
        return;
      }
fat's avatar
fat committed
202

XhmikosR's avatar
XhmikosR committed
203
204
      var transitionDuration = getTransitionDurationFromElement(element);
      EventHandler.one(element, TRANSITION_END, function (event) {
XhmikosR's avatar
Dist    
XhmikosR committed
205
        return _this._destroyElement(element, event);
XhmikosR's avatar
XhmikosR committed
206
207
      });
      emulateTransitionEnd(element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
208
    };
fat's avatar
fat committed
209

XhmikosR's avatar
Dist    
XhmikosR committed
210
    _proto._destroyElement = function _destroyElement(element) {
XhmikosR's avatar
XhmikosR committed
211
212
213
214
      if (element.parentNode) {
        element.parentNode.removeChild(element);
      }

XhmikosR's avatar
XhmikosR committed
215
      EventHandler.trigger(element, Event.CLOSED);
Mark Otto's avatar
Mark Otto committed
216
217
    } // Static
    ;
Mark Otto's avatar
grunt    
Mark Otto committed
218

XhmikosR's avatar
Dist    
XhmikosR committed
219
220
    Alert._jQueryInterface = function _jQueryInterface(config) {
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
221
        var data = Data.getData(this, DATA_KEY);
Mark Otto's avatar
dist    
Mark Otto committed
222

XhmikosR's avatar
Dist    
XhmikosR committed
223
224
225
        if (!data) {
          data = new Alert(this);
        }
Mark Otto's avatar
grunt    
Mark Otto committed
226

XhmikosR's avatar
Dist    
XhmikosR committed
227
228
229
230
231
        if (config === 'close') {
          data[config](this);
        }
      });
    };
Mark Otto's avatar
grunt    
Mark Otto committed
232

XhmikosR's avatar
Dist    
XhmikosR committed
233
234
235
236
237
    Alert._handleDismiss = function _handleDismiss(alertInstance) {
      return function (event) {
        if (event) {
          event.preventDefault();
        }
Mark Otto's avatar
dist    
Mark Otto committed
238

XhmikosR's avatar
Dist    
XhmikosR committed
239
        alertInstance.close(this);
Mark Otto's avatar
dist    
Mark Otto committed
240
      };
XhmikosR's avatar
Dist    
XhmikosR committed
241
    };
Mark Otto's avatar
dist    
Mark Otto committed
242

XhmikosR's avatar
XhmikosR committed
243
244
245
246
    Alert._getInstance = function _getInstance(element) {
      return Data.getData(element, DATA_KEY);
    };

XhmikosR's avatar
Dist    
XhmikosR committed
247
248
249
250
251
252
    _createClass(Alert, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }]);
fat's avatar
fat committed
253

XhmikosR's avatar
Dist    
XhmikosR committed
254
255
256
257
258
259
260
    return Alert;
  }();
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
261
262


XhmikosR's avatar
XhmikosR committed
263
  EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()));
XhmikosR's avatar
Dist    
XhmikosR committed
264
265
266
267
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
268
   * add .alert to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
269
   */
Mark Otto's avatar
dist    
Mark Otto committed
270

XhmikosR's avatar
XhmikosR committed
271
272
273
274
  if (typeof jQuery !== 'undefined') {
    var JQUERY_NO_CONFLICT = jQuery.fn[NAME];
    jQuery.fn[NAME] = Alert._jQueryInterface;
    jQuery.fn[NAME].Constructor = Alert;
Mark Otto's avatar
dist    
Mark Otto committed
275

XhmikosR's avatar
XhmikosR committed
276
277
278
279
280
    jQuery.fn[NAME].noConflict = function () {
      jQuery.fn[NAME] = JQUERY_NO_CONFLICT;
      return Alert._jQueryInterface;
    };
  }
fat's avatar
fat committed
281

282
  return Alert;
Mark Otto's avatar
dist    
Mark Otto committed
283

Mark Otto's avatar
Mark Otto committed
284
}));
Mark Otto's avatar
dist    
Mark Otto committed
285
//# sourceMappingURL=alert.js.map