toast.js 11.5 KB
Newer Older
XhmikosR's avatar
Dist  
XhmikosR committed
1
/*!
2
  * Bootstrap toast.js v5.0.0-alpha1 (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
6
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  */
(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 = 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
  Data = Data && Object.prototype.hasOwnProperty.call(Data, 'default') ? Data['default'] : Data;
  EventHandler = EventHandler && Object.prototype.hasOwnProperty.call(EventHandler, 'default') ? EventHandler['default'] : EventHandler;
  Manipulator = Manipulator && Object.prototype.hasOwnProperty.call(Manipulator, 'default') ? Manipulator['default'] : Manipulator;
XhmikosR's avatar
Dist  
XhmikosR committed
15

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

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

XhmikosR's avatar
XhmikosR committed
30
31
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
    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
57
    element.dispatchEvent(new Event(TRANSITION_END));
XhmikosR's avatar
XhmikosR committed
58
59
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
  };

  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 + "\"."));
      }
    });
  };

94
95
96
97
  var reflow = function reflow(element) {
    return element.offsetHeight;
  };

XhmikosR's avatar
XhmikosR committed
98
99
100
101
102
103
104
105
106
107
108
  var getjQuery = function getjQuery() {
    var _window = window,
        jQuery = _window.jQuery;

    if (jQuery && !document.body.hasAttribute('data-no-jquery')) {
      return jQuery;
    }

    return null;
  };

Mark Otto's avatar
Mark Otto committed
109
110
111
112
113
114
115
116
117
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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

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

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

XhmikosR's avatar
Dist    
XhmikosR committed
160
      this._setListeners();
XhmikosR's avatar
XhmikosR committed
161
162

      Data.setData(element, DATA_KEY, this);
XhmikosR's avatar
Dist    
XhmikosR committed
163
    } // Getters
XhmikosR's avatar
Dist  
XhmikosR committed
164
165


XhmikosR's avatar
Dist    
XhmikosR committed
166
    var _proto = Toast.prototype;
XhmikosR's avatar
Dist  
XhmikosR committed
167

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

XhmikosR's avatar
XhmikosR committed
172
      var showEvent = EventHandler.trigger(this._element, EVENT_SHOW);
Mark Otto's avatar
dist v5    
Mark Otto committed
173
174
175
176

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

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.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
204
205
        var transitionDuration = getTransitionDurationFromElement(this._element);
        EventHandler.one(this._element, TRANSITION_END, complete);
        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.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.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
234
235

      if (this._config.animation) {
        var transitionDuration = getTransitionDurationFromElement(this._element);
        EventHandler.one(this._element, TRANSITION_END, complete);
        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
242
243
    _proto.dispose = function dispose() {
      clearTimeout(this._timeout);
      this._timeout = null;
XhmikosR's avatar
Dist  
XhmikosR committed
244

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

XhmikosR's avatar
XhmikosR committed
249
      EventHandler.off(this._element, EVENT_CLICK_DISMISS);
XhmikosR's avatar
XhmikosR committed
250
      Data.removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
251
252
      this._element = null;
      this._config = null;
Mark Otto's avatar
Mark Otto committed
253
254
    } // Private
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
255

XhmikosR's avatar
Dist    
XhmikosR committed
256
    _proto._getConfig = function _getConfig(config) {
Mark Otto's avatar
Mark Otto committed
257
      config = _objectSpread(_objectSpread(_objectSpread({}, Default), Manipulator.getDataAttributes(this._element)), typeof config === 'object' && config ? config : {});
XhmikosR's avatar
XhmikosR committed
258
      typeCheckConfig(NAME, config, this.constructor.DefaultType);
XhmikosR's avatar
Dist    
XhmikosR committed
259
260
      return config;
    };
XhmikosR's avatar
Dist  
XhmikosR committed
261

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

XhmikosR's avatar
XhmikosR committed
265
      EventHandler.on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, function () {
XhmikosR's avatar
XhmikosR committed
266
        return _this3.hide();
XhmikosR's avatar
Dist    
XhmikosR committed
267
      });
Mark Otto's avatar
Mark Otto committed
268
269
    } // Static
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
270

XhmikosR's avatar
XhmikosR committed
271
    Toast.jQueryInterface = function jQueryInterface(config) {
XhmikosR's avatar
Dist    
XhmikosR committed
272
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
273
        var data = Data.getData(this, DATA_KEY);
XhmikosR's avatar
Dist  
XhmikosR committed
274

XhmikosR's avatar
Dist    
XhmikosR committed
275
        var _config = typeof config === 'object' && config;
XhmikosR's avatar
Dist  
XhmikosR committed
276

XhmikosR's avatar
Dist    
XhmikosR committed
277
278
279
        if (!data) {
          data = new Toast(this, _config);
        }
XhmikosR's avatar
Dist  
XhmikosR committed
280

XhmikosR's avatar
Dist    
XhmikosR committed
281
282
283
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
XhmikosR's avatar
Dist  
XhmikosR committed
284
285
          }

XhmikosR's avatar
Dist    
XhmikosR committed
286
          data[config](this);
XhmikosR's avatar
Dist  
XhmikosR committed
287
        }
XhmikosR's avatar
Dist    
XhmikosR committed
288
289
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
290

XhmikosR's avatar
XhmikosR committed
291
    Toast.getInstance = function getInstance(element) {
XhmikosR's avatar
XhmikosR committed
292
293
294
      return Data.getData(element, DATA_KEY);
    };

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

XhmikosR's avatar
Dist    
XhmikosR committed
312
313
    return Toast;
  }();
XhmikosR's avatar
XhmikosR committed
314
315

  var $ = getjQuery();
XhmikosR's avatar
Dist    
XhmikosR committed
316
317
318
319
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
320
   *  add .toast to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
321
   */
XhmikosR's avatar
Dist  
XhmikosR committed
322

323
324
  /* istanbul ignore if */

XhmikosR's avatar
XhmikosR committed
325
326
327
328
  if ($) {
    var JQUERY_NO_CONFLICT = $.fn[NAME];
    $.fn[NAME] = Toast.jQueryInterface;
    $.fn[NAME].Constructor = Toast;
XhmikosR's avatar
Dist  
XhmikosR committed
329

XhmikosR's avatar
XhmikosR committed
330
331
332
    $.fn[NAME].noConflict = function () {
      $.fn[NAME] = JQUERY_NO_CONFLICT;
      return Toast.jQueryInterface;
XhmikosR's avatar
XhmikosR committed
333
334
    };
  }
XhmikosR's avatar
Dist  
XhmikosR committed
335
336
337

  return Toast;

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