toast.js 11.4 KB
Newer Older
XhmikosR's avatar
Dist  
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap toast.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
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/manipulator.js')) :
  typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/event-handler.js', './dom/manipulator.js'], factory) :
XhmikosR's avatar
XhmikosR committed
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.Data, global.EventHandler, global.Manipulator));
XhmikosR's avatar
XhmikosR committed
10
}(this, (function (Data, EventHandler, Manipulator) { 'use strict';
XhmikosR's avatar
Dist  
XhmikosR committed
11

XhmikosR's avatar
XhmikosR committed
12
13
14
15
16
  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);
  var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
XhmikosR's avatar
Dist  
XhmikosR committed
17

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

  var toType = function toType(obj) {
XhmikosR's avatar
XhmikosR committed
28
29
30
31
    if (obj === null || obj === undefined) {
      return "" + obj;
    }

XhmikosR's avatar
XhmikosR committed
32
33
34
35
36
37
38
39
40
41
42
43
44
    return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
  };

  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
45
46
    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
47
48
49
50
51
52
53
54

    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
55
    return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
XhmikosR's avatar
XhmikosR committed
56
57
58
  };

  var triggerTransitionEnd = function triggerTransitionEnd(element) {
XhmikosR's avatar
XhmikosR committed
59
    element.dispatchEvent(new Event(TRANSITION_END));
XhmikosR's avatar
XhmikosR committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  };

  var isElement = function isElement(obj) {
    return (obj[0] || obj).nodeType;
  };

  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);
  };

  var typeCheckConfig = function typeCheckConfig(componentName, config, configTypes) {
    Object.keys(configTypes).forEach(function (property) {
      var expectedTypes = configTypes[property];
      var value = config[property];
      var valueType = value && isElement(value) ? 'element' : toType(value);

      if (!new RegExp(expectedTypes).test(valueType)) {
        throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
      }
    });
  };

96
97
98
99
  var reflow = function reflow(element) {
    return element.offsetHeight;
  };

XhmikosR's avatar
XhmikosR committed
100
101
102
103
  var getjQuery = function getjQuery() {
    var _window = window,
        jQuery = _window.jQuery;

XhmikosR's avatar
XhmikosR committed
104
    if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
XhmikosR's avatar
XhmikosR committed
105
106
107
108
109
110
      return jQuery;
    }

    return null;
  };

XhmikosR's avatar
XhmikosR committed
111
112
113
114
115
116
117
118
  var onDOMContentLoaded = function onDOMContentLoaded(callback) {
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', callback);
    } else {
      callback();
    }
  };

XhmikosR's avatar
XhmikosR committed
119
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
Mark Otto's avatar
Mark Otto committed
120
121
122
123

  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
124
  /**
XhmikosR's avatar
Dist    
XhmikosR committed
125
126
127
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
XhmikosR's avatar
Dist  
XhmikosR committed
128
129
   */

XhmikosR's avatar
Dist    
XhmikosR committed
130
  var NAME = 'toast';
XhmikosR's avatar
XhmikosR committed
131
  var VERSION = '5.0.0-alpha3';
XhmikosR's avatar
Dist    
XhmikosR committed
132
133
  var DATA_KEY = 'bs.toast';
  var EVENT_KEY = "." + DATA_KEY;
XhmikosR's avatar
XhmikosR committed
134
135
136
137
138
139
140
141
142
  var EVENT_CLICK_DISMISS = "click.dismiss" + EVENT_KEY;
  var EVENT_HIDE = "hide" + EVENT_KEY;
  var EVENT_HIDDEN = "hidden" + EVENT_KEY;
  var EVENT_SHOW = "show" + EVENT_KEY;
  var EVENT_SHOWN = "shown" + EVENT_KEY;
  var CLASS_NAME_FADE = 'fade';
  var CLASS_NAME_HIDE = 'hide';
  var CLASS_NAME_SHOW = 'show';
  var CLASS_NAME_SHOWING = 'showing';
XhmikosR's avatar
Dist    
XhmikosR committed
143
144
145
146
147
148
149
150
  var DefaultType = {
    animation: 'boolean',
    autohide: 'boolean',
    delay: 'number'
  };
  var Default = {
    animation: true,
    autohide: true,
XhmikosR's avatar
XhmikosR committed
151
    delay: 5000
XhmikosR's avatar
Dist    
XhmikosR committed
152
  };
XhmikosR's avatar
XhmikosR committed
153
  var SELECTOR_DATA_DISMISS = '[data-bs-dismiss="toast"]';
XhmikosR's avatar
XhmikosR committed
154
155
156
157
158
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
XhmikosR's avatar
Dist  
XhmikosR committed
159

XhmikosR's avatar
XhmikosR committed
160
  var Toast = /*#__PURE__*/function () {
XhmikosR's avatar
Dist    
XhmikosR committed
161
162
163
164
    function Toast(element, config) {
      this._element = element;
      this._config = this._getConfig(config);
      this._timeout = null;
XhmikosR's avatar
Dist  
XhmikosR committed
165

XhmikosR's avatar
Dist    
XhmikosR committed
166
      this._setListeners();
XhmikosR's avatar
XhmikosR committed
167

XhmikosR's avatar
XhmikosR committed
168
      Data__default['default'].setData(element, DATA_KEY, this);
XhmikosR's avatar
Dist    
XhmikosR committed
169
    } // Getters
XhmikosR's avatar
Dist  
XhmikosR committed
170
171


XhmikosR's avatar
Dist    
XhmikosR committed
172
    var _proto = Toast.prototype;
XhmikosR's avatar
Dist  
XhmikosR committed
173

XhmikosR's avatar
Dist    
XhmikosR committed
174
175
176
    // Public
    _proto.show = function show() {
      var _this = this;
XhmikosR's avatar
Dist  
XhmikosR committed
177

XhmikosR's avatar
XhmikosR committed
178
      var showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW);
Mark Otto's avatar
dist v5    
Mark Otto committed
179
180
181
182

      if (showEvent.defaultPrevented) {
        return;
      }
XhmikosR's avatar
Dist  
XhmikosR committed
183

XhmikosR's avatar
XhmikosR committed
184
185
      this._clearTimeout();

XhmikosR's avatar
Dist    
XhmikosR committed
186
      if (this._config.animation) {
XhmikosR's avatar
XhmikosR committed
187
        this._element.classList.add(CLASS_NAME_FADE);
XhmikosR's avatar
Dist    
XhmikosR committed
188
      }
XhmikosR's avatar
Dist  
XhmikosR committed
189

XhmikosR's avatar
Dist    
XhmikosR committed
190
      var complete = function complete() {
XhmikosR's avatar
XhmikosR committed
191
        _this._element.classList.remove(CLASS_NAME_SHOWING);
Mark Otto's avatar
dist    
Mark Otto committed
192

XhmikosR's avatar
XhmikosR committed
193
        _this._element.classList.add(CLASS_NAME_SHOW);
Mark Otto's avatar
dist    
Mark Otto committed
194

XhmikosR's avatar
XhmikosR committed
195
        EventHandler__default['default'].trigger(_this._element, EVENT_SHOWN);
XhmikosR's avatar
Dist  
XhmikosR committed
196

XhmikosR's avatar
Dist    
XhmikosR committed
197
        if (_this._config.autohide) {
XhmikosR's avatar
XhmikosR committed
198
199
200
          _this._timeout = setTimeout(function () {
            _this.hide();
          }, _this._config.delay);
XhmikosR's avatar
Dist  
XhmikosR committed
201
202
203
        }
      };

XhmikosR's avatar
XhmikosR committed
204
      this._element.classList.remove(CLASS_NAME_HIDE);
Mark Otto's avatar
dist    
Mark Otto committed
205

206
207
      reflow(this._element);

XhmikosR's avatar
XhmikosR committed
208
      this._element.classList.add(CLASS_NAME_SHOWING);
XhmikosR's avatar
Dist  
XhmikosR committed
209

XhmikosR's avatar
Dist    
XhmikosR committed
210
      if (this._config.animation) {
XhmikosR's avatar
XhmikosR committed
211
        var transitionDuration = getTransitionDurationFromElement(this._element);
XhmikosR's avatar
XhmikosR committed
212
        EventHandler__default['default'].one(this._element, TRANSITION_END, complete);
XhmikosR's avatar
XhmikosR committed
213
        emulateTransitionEnd(this._element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
214
215
216
217
      } else {
        complete();
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
218

XhmikosR's avatar
XhmikosR committed
219
    _proto.hide = function hide() {
XhmikosR's avatar
Dist    
XhmikosR committed
220
      var _this2 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
221

XhmikosR's avatar
XhmikosR committed
222
      if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
XhmikosR's avatar
Dist    
XhmikosR committed
223
224
        return;
      }
XhmikosR's avatar
Dist  
XhmikosR committed
225

XhmikosR's avatar
XhmikosR committed
226
      var hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE);
Mark Otto's avatar
dist v5    
Mark Otto committed
227
228
229
230

      if (hideEvent.defaultPrevented) {
        return;
      }
XhmikosR's avatar
Dist  
XhmikosR committed
231

XhmikosR's avatar
XhmikosR committed
232
      var complete = function complete() {
XhmikosR's avatar
XhmikosR committed
233
        _this2._element.classList.add(CLASS_NAME_HIDE);
XhmikosR's avatar
XhmikosR committed
234

XhmikosR's avatar
XhmikosR committed
235
        EventHandler__default['default'].trigger(_this2._element, EVENT_HIDDEN);
XhmikosR's avatar
XhmikosR committed
236
237
      };

XhmikosR's avatar
XhmikosR committed
238
      this._element.classList.remove(CLASS_NAME_SHOW);
XhmikosR's avatar
XhmikosR committed
239
240
241

      if (this._config.animation) {
        var transitionDuration = getTransitionDurationFromElement(this._element);
XhmikosR's avatar
XhmikosR committed
242
        EventHandler__default['default'].one(this._element, TRANSITION_END, complete);
XhmikosR's avatar
XhmikosR committed
243
        emulateTransitionEnd(this._element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
244
      } else {
XhmikosR's avatar
XhmikosR committed
245
        complete();
XhmikosR's avatar
Dist    
XhmikosR committed
246
247
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
248

XhmikosR's avatar
Dist    
XhmikosR committed
249
    _proto.dispose = function dispose() {
XhmikosR's avatar
XhmikosR committed
250
      this._clearTimeout();
XhmikosR's avatar
Dist  
XhmikosR committed
251

XhmikosR's avatar
XhmikosR committed
252
253
      if (this._element.classList.contains(CLASS_NAME_SHOW)) {
        this._element.classList.remove(CLASS_NAME_SHOW);
XhmikosR's avatar
Dist    
XhmikosR committed
254
      }
XhmikosR's avatar
Dist  
XhmikosR committed
255

XhmikosR's avatar
XhmikosR committed
256
257
      EventHandler__default['default'].off(this._element, EVENT_CLICK_DISMISS);
      Data__default['default'].removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
258
259
      this._element = null;
      this._config = null;
Mark Otto's avatar
Mark Otto committed
260
261
    } // Private
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
262

XhmikosR's avatar
Dist    
XhmikosR committed
263
    _proto._getConfig = function _getConfig(config) {
XhmikosR's avatar
XhmikosR committed
264
      config = _extends({}, Default, Manipulator__default['default'].getDataAttributes(this._element), typeof config === 'object' && config ? config : {});
XhmikosR's avatar
XhmikosR committed
265
      typeCheckConfig(NAME, config, this.constructor.DefaultType);
XhmikosR's avatar
Dist    
XhmikosR committed
266
267
      return config;
    };
XhmikosR's avatar
Dist  
XhmikosR committed
268

XhmikosR's avatar
Dist    
XhmikosR committed
269
270
    _proto._setListeners = function _setListeners() {
      var _this3 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
271

XhmikosR's avatar
XhmikosR committed
272
      EventHandler__default['default'].on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, function () {
XhmikosR's avatar
XhmikosR committed
273
        return _this3.hide();
XhmikosR's avatar
Dist    
XhmikosR committed
274
      });
XhmikosR's avatar
XhmikosR committed
275
276
277
278
279
    };

    _proto._clearTimeout = function _clearTimeout() {
      clearTimeout(this._timeout);
      this._timeout = null;
Mark Otto's avatar
Mark Otto committed
280
281
    } // Static
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
282

XhmikosR's avatar
XhmikosR committed
283
    Toast.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
284
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
285
        var data = Data__default['default'].getData(this, DATA_KEY);
XhmikosR's avatar
Dist  
XhmikosR committed
286

XhmikosR's avatar
Dist    
XhmikosR committed
287
        var _config = typeof config === 'object' && config;
XhmikosR's avatar
Dist  
XhmikosR committed
288

XhmikosR's avatar
Dist    
XhmikosR committed
289
290
291
        if (!data) {
          data = new Toast(this, _config);
        }
XhmikosR's avatar
Dist  
XhmikosR committed
292

XhmikosR's avatar
Dist    
XhmikosR committed
293
294
295
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
XhmikosR's avatar
Dist  
XhmikosR committed
296
297
          }

XhmikosR's avatar
Dist    
XhmikosR committed
298
          data[config](this);
XhmikosR's avatar
Dist  
XhmikosR committed
299
        }
XhmikosR's avatar
Dist    
XhmikosR committed
300
301
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
302

XhmikosR's avatar
XhmikosR committed
303
    Toast.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
304
      return Data__default['default'].getData(element, DATA_KEY);
XhmikosR's avatar
XhmikosR committed
305
306
    };

XhmikosR's avatar
Dist    
XhmikosR committed
307
308
309
310
311
312
313
314
315
316
    _createClass(Toast, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }, {
      key: "DefaultType",
      get: function get() {
        return DefaultType;
      }
Mark Otto's avatar
Mark Otto committed
317
318
319
320
321
    }, {
      key: "Default",
      get: function get() {
        return Default;
      }
XhmikosR's avatar
Dist    
XhmikosR committed
322
    }]);
XhmikosR's avatar
Dist  
XhmikosR committed
323

XhmikosR's avatar
Dist    
XhmikosR committed
324
325
326
327
328
329
    return Toast;
  }();
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
330
   * add .Toast to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
331
   */
XhmikosR's avatar
Dist  
XhmikosR committed
332

333

XhmikosR's avatar
XhmikosR committed
334
335
336
  onDOMContentLoaded(function () {
    var $ = getjQuery();
    /* istanbul ignore if */
XhmikosR's avatar
Dist  
XhmikosR committed
337

XhmikosR's avatar
XhmikosR committed
338
339
340
341
342
343
344
345
346
347
348
    if ($) {
      var JQUERY_NO_CONFLICT = $.fn[NAME];
      $.fn[NAME] = Toast.jQueryInterface;
      $.fn[NAME].Constructor = Toast;

      $.fn[NAME].noConflict = function () {
        $.fn[NAME] = JQUERY_NO_CONFLICT;
        return Toast.jQueryInterface;
      };
    }
  });
XhmikosR's avatar
Dist  
XhmikosR committed
349
350
351

  return Toast;

XhmikosR's avatar
XhmikosR committed
352
})));
XhmikosR's avatar
Dist  
XhmikosR committed
353
//# sourceMappingURL=toast.js.map