toast.js 10.9 KB
Newer Older
XhmikosR's avatar
Dist  
XhmikosR committed
1
/*!
XhmikosR's avatar
XhmikosR committed
2
  * Bootstrap toast.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
6
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  */
(function (global, factory) {
XhmikosR's avatar
XhmikosR committed
7
8
9
10
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/eventHandler.js'), require('./dom/manipulator.js')) :
  typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/eventHandler.js', './dom/manipulator.js'], factory) :
  (global = global || self, global.Toast = factory(global.Data, global.EventHandler, global.Manipulator));
}(this, function (Data, EventHandler, Manipulator) { 'use strict';
XhmikosR's avatar
Dist  
XhmikosR 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;
  Manipulator = Manipulator && Manipulator.hasOwnProperty('default') ? Manipulator['default'] : Manipulator;
XhmikosR's avatar
Dist  
XhmikosR committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
58
59
60
61
62
63
64
65

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

  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 _objectSpread(target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i] != null ? arguments[i] : {};
      var ownKeys = Object.keys(source);

      if (typeof Object.getOwnPropertySymbols === 'function') {
        ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
          return Object.getOwnPropertyDescriptor(source, sym).enumerable;
        }));
      }

      ownKeys.forEach(function (key) {
        _defineProperty(target, key, source[key]);
      });
    }

    return target;
  }

XhmikosR's avatar
XhmikosR committed
66
67
68
69
70
71
72
73
  /**
   * --------------------------------------------------------------------------
   * 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
74
75
  var _window = window,
      jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
XhmikosR's avatar
XhmikosR committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

  var toType = function toType(obj) {
    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) {
    element.dispatchEvent(new Event(TRANSITION_END));
  };

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

XhmikosR's avatar
Dist  
XhmikosR committed
142
  /**
XhmikosR's avatar
Dist    
XhmikosR committed
143
144
145
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
XhmikosR's avatar
Dist  
XhmikosR committed
146
147
   */

XhmikosR's avatar
Dist    
XhmikosR committed
148
  var NAME = 'toast';
XhmikosR's avatar
XhmikosR committed
149
  var VERSION = '4.3.1';
XhmikosR's avatar
Dist    
XhmikosR committed
150
151
  var DATA_KEY = 'bs.toast';
  var EVENT_KEY = "." + DATA_KEY;
XhmikosR's avatar
XhmikosR committed
152
  var Event$1 = {
XhmikosR's avatar
Dist    
XhmikosR committed
153
154
155
156
157
158
159
160
161
    CLICK_DISMISS: "click.dismiss" + EVENT_KEY,
    HIDE: "hide" + EVENT_KEY,
    HIDDEN: "hidden" + EVENT_KEY,
    SHOW: "show" + EVENT_KEY,
    SHOWN: "shown" + EVENT_KEY
  };
  var ClassName = {
    FADE: 'fade',
    HIDE: 'hide',
Mark Otto's avatar
dist    
Mark Otto committed
162
163
    SHOW: 'show',
    SHOWING: 'showing'
XhmikosR's avatar
Dist    
XhmikosR committed
164
165
166
167
168
169
170
171
172
173
174
175
176
  };
  var DefaultType = {
    animation: 'boolean',
    autohide: 'boolean',
    delay: 'number'
  };
  var Default = {
    animation: true,
    autohide: true,
    delay: 500
  };
  var Selector = {
    DATA_DISMISS: '[data-dismiss="toast"]'
XhmikosR's avatar
Dist  
XhmikosR committed
177
178
    /**
     * ------------------------------------------------------------------------
XhmikosR's avatar
Dist    
XhmikosR committed
179
     * Class Definition
XhmikosR's avatar
Dist  
XhmikosR committed
180
181
182
     * ------------------------------------------------------------------------
     */

XhmikosR's avatar
Dist    
XhmikosR committed
183
  };
XhmikosR's avatar
Dist  
XhmikosR committed
184

XhmikosR's avatar
Dist    
XhmikosR committed
185
186
187
188
189
190
191
  var Toast =
  /*#__PURE__*/
  function () {
    function Toast(element, config) {
      this._element = element;
      this._config = this._getConfig(config);
      this._timeout = null;
XhmikosR's avatar
Dist  
XhmikosR committed
192

XhmikosR's avatar
Dist    
XhmikosR committed
193
      this._setListeners();
XhmikosR's avatar
XhmikosR committed
194
195

      Data.setData(element, DATA_KEY, this);
XhmikosR's avatar
Dist    
XhmikosR committed
196
    } // Getters
XhmikosR's avatar
Dist  
XhmikosR committed
197
198


XhmikosR's avatar
Dist    
XhmikosR committed
199
    var _proto = Toast.prototype;
XhmikosR's avatar
Dist  
XhmikosR committed
200

XhmikosR's avatar
Dist    
XhmikosR committed
201
202
203
    // Public
    _proto.show = function show() {
      var _this = this;
XhmikosR's avatar
Dist  
XhmikosR committed
204

XhmikosR's avatar
XhmikosR committed
205
      EventHandler.trigger(this._element, Event$1.SHOW);
XhmikosR's avatar
Dist  
XhmikosR committed
206

XhmikosR's avatar
Dist    
XhmikosR committed
207
208
209
      if (this._config.animation) {
        this._element.classList.add(ClassName.FADE);
      }
XhmikosR's avatar
Dist  
XhmikosR committed
210

XhmikosR's avatar
Dist    
XhmikosR committed
211
      var complete = function complete() {
Mark Otto's avatar
dist    
Mark Otto committed
212
213
214
215
        _this._element.classList.remove(ClassName.SHOWING);

        _this._element.classList.add(ClassName.SHOW);

XhmikosR's avatar
XhmikosR committed
216
        EventHandler.trigger(_this._element, Event$1.SHOWN);
XhmikosR's avatar
Dist  
XhmikosR committed
217

XhmikosR's avatar
Dist    
XhmikosR committed
218
219
        if (_this._config.autohide) {
          _this.hide();
XhmikosR's avatar
Dist  
XhmikosR committed
220
221
222
        }
      };

Mark Otto's avatar
dist    
Mark Otto committed
223
224
225
      this._element.classList.remove(ClassName.HIDE);

      this._element.classList.add(ClassName.SHOWING);
XhmikosR's avatar
Dist  
XhmikosR committed
226

XhmikosR's avatar
Dist    
XhmikosR committed
227
      if (this._config.animation) {
XhmikosR's avatar
XhmikosR committed
228
229
230
        var transitionDuration = getTransitionDurationFromElement(this._element);
        EventHandler.one(this._element, TRANSITION_END, complete);
        emulateTransitionEnd(this._element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
231
232
233
234
      } else {
        complete();
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
235

XhmikosR's avatar
Dist    
XhmikosR committed
236
237
    _proto.hide = function hide(withoutTimeout) {
      var _this2 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
238

XhmikosR's avatar
Dist    
XhmikosR committed
239
240
241
      if (!this._element.classList.contains(ClassName.SHOW)) {
        return;
      }
XhmikosR's avatar
Dist  
XhmikosR committed
242

XhmikosR's avatar
XhmikosR committed
243
      EventHandler.trigger(this._element, Event$1.HIDE);
XhmikosR's avatar
Dist  
XhmikosR committed
244

XhmikosR's avatar
Dist    
XhmikosR committed
245
246
247
248
249
250
251
252
      if (withoutTimeout) {
        this._close();
      } else {
        this._timeout = setTimeout(function () {
          _this2._close();
        }, this._config.delay);
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
253

XhmikosR's avatar
Dist    
XhmikosR committed
254
255
256
    _proto.dispose = function dispose() {
      clearTimeout(this._timeout);
      this._timeout = null;
XhmikosR's avatar
Dist  
XhmikosR committed
257

XhmikosR's avatar
Dist    
XhmikosR committed
258
259
260
      if (this._element.classList.contains(ClassName.SHOW)) {
        this._element.classList.remove(ClassName.SHOW);
      }
XhmikosR's avatar
Dist  
XhmikosR committed
261

XhmikosR's avatar
XhmikosR committed
262
263
      EventHandler.off(this._element, Event$1.CLICK_DISMISS);
      Data.removeData(this._element, DATA_KEY);
XhmikosR's avatar
Dist    
XhmikosR committed
264
265
      this._element = null;
      this._config = null;
Mark Otto's avatar
Mark Otto committed
266
267
    } // Private
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
268

XhmikosR's avatar
Dist    
XhmikosR committed
269
    _proto._getConfig = function _getConfig(config) {
XhmikosR's avatar
XhmikosR committed
270
271
      config = _objectSpread({}, Default, Manipulator.getDataAttributes(this._element), typeof config === 'object' && config ? config : {});
      typeCheckConfig(NAME, config, this.constructor.DefaultType);
XhmikosR's avatar
Dist    
XhmikosR committed
272
273
      return config;
    };
XhmikosR's avatar
Dist  
XhmikosR committed
274

XhmikosR's avatar
Dist    
XhmikosR committed
275
276
    _proto._setListeners = function _setListeners() {
      var _this3 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
277

XhmikosR's avatar
XhmikosR committed
278
      EventHandler.on(this._element, Event$1.CLICK_DISMISS, Selector.DATA_DISMISS, function () {
XhmikosR's avatar
Dist    
XhmikosR committed
279
280
281
        return _this3.hide(true);
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
282

XhmikosR's avatar
Dist    
XhmikosR committed
283
284
    _proto._close = function _close() {
      var _this4 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
285

XhmikosR's avatar
Dist    
XhmikosR committed
286
      var complete = function complete() {
Mark Otto's avatar
dist    
Mark Otto committed
287
288
        _this4._element.classList.add(ClassName.HIDE);

XhmikosR's avatar
XhmikosR committed
289
        EventHandler.trigger(_this4._element, Event$1.HIDDEN);
XhmikosR's avatar
Dist    
XhmikosR committed
290
291
292
      };

      this._element.classList.remove(ClassName.SHOW);
XhmikosR's avatar
Dist  
XhmikosR committed
293

XhmikosR's avatar
Dist    
XhmikosR committed
294
      if (this._config.animation) {
XhmikosR's avatar
XhmikosR committed
295
296
297
        var transitionDuration = getTransitionDurationFromElement(this._element);
        EventHandler.one(this._element, TRANSITION_END, complete);
        emulateTransitionEnd(this._element, transitionDuration);
XhmikosR's avatar
Dist    
XhmikosR committed
298
299
300
      } else {
        complete();
      }
Mark Otto's avatar
Mark Otto committed
301
302
    } // Static
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
303

XhmikosR's avatar
Dist    
XhmikosR committed
304
305
    Toast._jQueryInterface = function _jQueryInterface(config) {
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
306
        var data = Data.getData(this, DATA_KEY);
XhmikosR's avatar
Dist  
XhmikosR committed
307

XhmikosR's avatar
Dist    
XhmikosR committed
308
        var _config = typeof config === 'object' && config;
XhmikosR's avatar
Dist  
XhmikosR committed
309

XhmikosR's avatar
Dist    
XhmikosR committed
310
311
312
        if (!data) {
          data = new Toast(this, _config);
        }
XhmikosR's avatar
Dist  
XhmikosR committed
313

XhmikosR's avatar
Dist    
XhmikosR committed
314
315
316
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
XhmikosR's avatar
Dist  
XhmikosR committed
317
318
          }

XhmikosR's avatar
Dist    
XhmikosR committed
319
          data[config](this);
XhmikosR's avatar
Dist  
XhmikosR committed
320
        }
XhmikosR's avatar
Dist    
XhmikosR committed
321
322
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
323

XhmikosR's avatar
XhmikosR committed
324
325
326
327
    Toast._getInstance = function _getInstance(element) {
      return Data.getData(element, DATA_KEY);
    };

XhmikosR's avatar
Dist    
XhmikosR committed
328
329
330
331
332
333
334
335
336
337
    _createClass(Toast, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }, {
      key: "DefaultType",
      get: function get() {
        return DefaultType;
      }
Mark Otto's avatar
Mark Otto committed
338
339
340
341
342
    }, {
      key: "Default",
      get: function get() {
        return Default;
      }
XhmikosR's avatar
Dist    
XhmikosR committed
343
    }]);
XhmikosR's avatar
Dist  
XhmikosR committed
344

XhmikosR's avatar
Dist    
XhmikosR committed
345
346
347
348
349
350
    return Toast;
  }();
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
351
   *  add .toast to jQuery only if jQuery is present
XhmikosR's avatar
Dist    
XhmikosR committed
352
   */
XhmikosR's avatar
Dist  
XhmikosR committed
353
354


XhmikosR's avatar
XhmikosR committed
355
356
357
358
  if (typeof jQuery !== 'undefined') {
    var JQUERY_NO_CONFLICT = jQuery.fn[NAME];
    jQuery.fn[NAME] = Toast._jQueryInterface;
    jQuery.fn[NAME].Constructor = Toast;
XhmikosR's avatar
Dist  
XhmikosR committed
359

XhmikosR's avatar
XhmikosR committed
360
361
362
363
364
    jQuery.fn[NAME].noConflict = function () {
      jQuery.fn[NAME] = JQUERY_NO_CONFLICT;
      return Toast._jQueryInterface;
    };
  }
XhmikosR's avatar
Dist  
XhmikosR committed
365
366
367

  return Toast;

Mark Otto's avatar
Mark Otto committed
368
}));
XhmikosR's avatar
Dist  
XhmikosR committed
369
//# sourceMappingURL=toast.js.map