toast.js 11.1 KB
Newer Older
XhmikosR's avatar
Dist  
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap toast.js v5.0.0-alpha2 (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-alpha2): 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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;

    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
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
104
105
106
107
108
109
110
  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
XhmikosR committed
111
  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
112
113
114
115

  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
116
  /**
XhmikosR's avatar
Dist    
XhmikosR committed
117
118
119
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
XhmikosR's avatar
Dist  
XhmikosR committed
120
121
   */

XhmikosR's avatar
Dist    
XhmikosR committed
122
  var NAME = 'toast';
XhmikosR's avatar
XhmikosR committed
123
  var VERSION = '5.0.0-alpha2';
XhmikosR's avatar
Dist    
XhmikosR committed
124
125
  var DATA_KEY = 'bs.toast';
  var EVENT_KEY = "." + DATA_KEY;
XhmikosR's avatar
XhmikosR committed
126
127
128
129
130
131
132
133
134
  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
135
136
137
138
139
140
141
142
  var DefaultType = {
    animation: 'boolean',
    autohide: 'boolean',
    delay: 'number'
  };
  var Default = {
    animation: true,
    autohide: true,
XhmikosR's avatar
XhmikosR committed
143
    delay: 5000
XhmikosR's avatar
Dist    
XhmikosR committed
144
  };
XhmikosR's avatar
XhmikosR committed
145
  var SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]';
XhmikosR's avatar
XhmikosR committed
146
147
148
149
150
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
XhmikosR's avatar
Dist  
XhmikosR committed
151

XhmikosR's avatar
XhmikosR committed
152
  var Toast = /*#__PURE__*/function () {
XhmikosR's avatar
Dist    
XhmikosR committed
153
154
155
156
    function Toast(element, config) {
      this._element = element;
      this._config = this._getConfig(config);
      this._timeout = null;
XhmikosR's avatar
Dist  
XhmikosR committed
157

XhmikosR's avatar
Dist    
XhmikosR committed
158
      this._setListeners();
XhmikosR's avatar
XhmikosR committed
159

XhmikosR's avatar
XhmikosR committed
160
      Data__default['default'].setData(element, DATA_KEY, this);
XhmikosR's avatar
Dist    
XhmikosR committed
161
    } // Getters
XhmikosR's avatar
Dist  
XhmikosR committed
162
163


XhmikosR's avatar
Dist    
XhmikosR committed
164
    var _proto = Toast.prototype;
XhmikosR's avatar
Dist  
XhmikosR committed
165

XhmikosR's avatar
Dist    
XhmikosR committed
166
167
168
    // Public
    _proto.show = function show() {
      var _this = this;
XhmikosR's avatar
Dist  
XhmikosR committed
169

XhmikosR's avatar
XhmikosR committed
170
      var showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW);
Mark Otto's avatar
dist v5    
Mark Otto committed
171
172
173
174

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

XhmikosR's avatar
XhmikosR committed
176
177
      this._clearTimeout();

XhmikosR's avatar
Dist    
XhmikosR committed
178
      if (this._config.animation) {
XhmikosR's avatar
XhmikosR committed
179
        this._element.classList.add(CLASS_NAME_FADE);
XhmikosR's avatar
Dist    
XhmikosR committed
180
      }
XhmikosR's avatar
Dist  
XhmikosR committed
181

XhmikosR's avatar
Dist    
XhmikosR committed
182
      var complete = function complete() {
XhmikosR's avatar
XhmikosR committed
183
        _this._element.classList.remove(CLASS_NAME_SHOWING);
Mark Otto's avatar
dist    
Mark Otto committed
184

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

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

XhmikosR's avatar
Dist    
XhmikosR committed
189
        if (_this._config.autohide) {
XhmikosR's avatar
XhmikosR committed
190
191
192
          _this._timeout = setTimeout(function () {
            _this.hide();
          }, _this._config.delay);
XhmikosR's avatar
Dist  
XhmikosR committed
193
194
195
        }
      };

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

198
199
      reflow(this._element);

XhmikosR's avatar
XhmikosR committed
200
      this._element.classList.add(CLASS_NAME_SHOWING);
XhmikosR's avatar
Dist  
XhmikosR committed
201

XhmikosR's avatar
Dist    
XhmikosR committed
202
      if (this._config.animation) {
XhmikosR's avatar
XhmikosR committed
203
        var transitionDuration = getTransitionDurationFromElement(this._element);
XhmikosR's avatar
XhmikosR committed
204
        EventHandler__default['default'].one(this._element, TRANSITION_END, complete);
XhmikosR's avatar
XhmikosR committed
205
        emulateTransitionEnd(this._element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
206
207
208
209
      } else {
        complete();
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
210

XhmikosR's avatar
XhmikosR committed
211
    _proto.hide = function hide() {
XhmikosR's avatar
Dist    
XhmikosR committed
212
      var _this2 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
213

XhmikosR's avatar
XhmikosR committed
214
      if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
XhmikosR's avatar
Dist    
XhmikosR committed
215
216
        return;
      }
XhmikosR's avatar
Dist  
XhmikosR committed
217

XhmikosR's avatar
XhmikosR committed
218
      var hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE);
Mark Otto's avatar
dist v5    
Mark Otto committed
219
220
221
222

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

XhmikosR's avatar
XhmikosR committed
224
      var complete = function complete() {
XhmikosR's avatar
XhmikosR committed
225
        _this2._element.classList.add(CLASS_NAME_HIDE);
XhmikosR's avatar
XhmikosR committed
226

XhmikosR's avatar
XhmikosR committed
227
        EventHandler__default['default'].trigger(_this2._element, EVENT_HIDDEN);
XhmikosR's avatar
XhmikosR committed
228
229
      };

XhmikosR's avatar
XhmikosR committed
230
      this._element.classList.remove(CLASS_NAME_SHOW);
XhmikosR's avatar
XhmikosR committed
231
232
233

      if (this._config.animation) {
        var transitionDuration = getTransitionDurationFromElement(this._element);
XhmikosR's avatar
XhmikosR committed
234
        EventHandler__default['default'].one(this._element, TRANSITION_END, complete);
XhmikosR's avatar
XhmikosR committed
235
        emulateTransitionEnd(this._element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
236
      } else {
XhmikosR's avatar
XhmikosR committed
237
        complete();
XhmikosR's avatar
Dist    
XhmikosR committed
238
239
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
240

XhmikosR's avatar
Dist    
XhmikosR committed
241
    _proto.dispose = function dispose() {
XhmikosR's avatar
XhmikosR committed
242
      this._clearTimeout();
XhmikosR's avatar
Dist  
XhmikosR committed
243

XhmikosR's avatar
XhmikosR committed
244
245
      if (this._element.classList.contains(CLASS_NAME_SHOW)) {
        this._element.classList.remove(CLASS_NAME_SHOW);
XhmikosR's avatar
Dist    
XhmikosR committed
246
      }
XhmikosR's avatar
Dist  
XhmikosR committed
247

XhmikosR's avatar
XhmikosR committed
248
249
      EventHandler__default['default'].off(this._element, EVENT_CLICK_DISMISS);
      Data__default['default'].removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
250
251
      this._element = null;
      this._config = null;
Mark Otto's avatar
Mark Otto committed
252
253
    } // Private
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
254

XhmikosR's avatar
Dist    
XhmikosR committed
255
    _proto._getConfig = function _getConfig(config) {
XhmikosR's avatar
XhmikosR committed
256
      config = _extends({}, Default, Manipulator__default['default'].getDataAttributes(this._element), typeof config === 'object' && config ? config : {});
XhmikosR's avatar
XhmikosR committed
257
      typeCheckConfig(NAME, config, this.constructor.DefaultType);
XhmikosR's avatar
Dist    
XhmikosR committed
258
259
      return config;
    };
XhmikosR's avatar
Dist  
XhmikosR committed
260

XhmikosR's avatar
Dist    
XhmikosR committed
261
262
    _proto._setListeners = function _setListeners() {
      var _this3 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
263

XhmikosR's avatar
XhmikosR committed
264
      EventHandler__default['default'].on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, function () {
XhmikosR's avatar
XhmikosR committed
265
        return _this3.hide();
XhmikosR's avatar
Dist    
XhmikosR committed
266
      });
XhmikosR's avatar
XhmikosR committed
267
268
269
270
271
    };

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

XhmikosR's avatar
XhmikosR committed
275
    Toast.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
276
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
277
        var data = Data__default['default'].getData(this, DATA_KEY);
XhmikosR's avatar
Dist  
XhmikosR committed
278

XhmikosR's avatar
Dist    
XhmikosR committed
279
        var _config = typeof config === 'object' && config;
XhmikosR's avatar
Dist  
XhmikosR committed
280

XhmikosR's avatar
Dist    
XhmikosR committed
281
282
283
        if (!data) {
          data = new Toast(this, _config);
        }
XhmikosR's avatar
Dist  
XhmikosR committed
284

XhmikosR's avatar
Dist    
XhmikosR committed
285
286
287
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
XhmikosR's avatar
Dist  
XhmikosR committed
288
289
          }

XhmikosR's avatar
Dist    
XhmikosR committed
290
          data[config](this);
XhmikosR's avatar
Dist  
XhmikosR committed
291
        }
XhmikosR's avatar
Dist    
XhmikosR committed
292
293
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
294

XhmikosR's avatar
XhmikosR committed
295
    Toast.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
296
      return Data__default['default'].getData(element, DATA_KEY);
XhmikosR's avatar
XhmikosR committed
297
298
    };

XhmikosR's avatar
Dist    
XhmikosR committed
299
300
301
302
303
304
305
306
307
308
    _createClass(Toast, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }, {
      key: "DefaultType",
      get: function get() {
        return DefaultType;
      }
Mark Otto's avatar
Mark Otto committed
309
310
311
312
313
    }, {
      key: "Default",
      get: function get() {
        return Default;
      }
XhmikosR's avatar
Dist    
XhmikosR committed
314
    }]);
XhmikosR's avatar
Dist  
XhmikosR committed
315

XhmikosR's avatar
Dist    
XhmikosR committed
316
317
    return Toast;
  }();
XhmikosR's avatar
XhmikosR committed
318
319

  var $ = getjQuery();
XhmikosR's avatar
Dist    
XhmikosR committed
320
321
322
323
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
324
   *  add .toast to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
325
   */
XhmikosR's avatar
Dist  
XhmikosR committed
326

327
328
  /* istanbul ignore if */

XhmikosR's avatar
XhmikosR committed
329
330
331
332
  if ($) {
    var JQUERY_NO_CONFLICT = $.fn[NAME];
    $.fn[NAME] = Toast.jQueryInterface;
    $.fn[NAME].Constructor = Toast;
XhmikosR's avatar
Dist  
XhmikosR committed
333

XhmikosR's avatar
XhmikosR committed
334
335
336
    $.fn[NAME].noConflict = function () {
      $.fn[NAME] = JQUERY_NO_CONFLICT;
      return Toast.jQueryInterface;
XhmikosR's avatar
XhmikosR committed
337
338
    };
  }
XhmikosR's avatar
Dist  
XhmikosR committed
339
340
341

  return Toast;

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