toast.js 7.41 KB
Newer Older
XhmikosR's avatar
Dist  
XhmikosR committed
1
/*!
Mark Otto's avatar
Mark Otto committed
2
  * Bootstrap toast.js v4.2.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
7
8
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  */
(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
  typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
Mark Otto's avatar
Mark Otto committed
9
10
  (global = global || self, global.Toast = factory(global.jQuery, global.Util));
}(this, function ($, Util) { 'use strict';
XhmikosR's avatar
Dist  
XhmikosR committed
11
12
13
14
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

  $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util;

  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
Dist    
XhmikosR committed
66
67
68
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
XhmikosR's avatar
Dist  
XhmikosR committed
69
70
   */

XhmikosR's avatar
Dist    
XhmikosR committed
71
  var NAME = 'toast';
Mark Otto's avatar
Mark Otto committed
72
  var VERSION = '4.2.1';
XhmikosR's avatar
Dist    
XhmikosR committed
73
74
75
76
77
78
79
80
81
82
83
84
85
  var DATA_KEY = 'bs.toast';
  var EVENT_KEY = "." + DATA_KEY;
  var JQUERY_NO_CONFLICT = $.fn[NAME];
  var Event = {
    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
86
87
    SHOW: 'show',
    SHOWING: 'showing'
XhmikosR's avatar
Dist    
XhmikosR committed
88
89
90
91
92
93
94
95
96
97
98
99
100
  };
  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
101
102
    /**
     * ------------------------------------------------------------------------
XhmikosR's avatar
Dist    
XhmikosR committed
103
     * Class Definition
XhmikosR's avatar
Dist  
XhmikosR committed
104
105
106
     * ------------------------------------------------------------------------
     */

XhmikosR's avatar
Dist    
XhmikosR committed
107
  };
XhmikosR's avatar
Dist  
XhmikosR committed
108

XhmikosR's avatar
Dist    
XhmikosR committed
109
110
111
112
113
114
115
  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
116

XhmikosR's avatar
Dist    
XhmikosR committed
117
118
      this._setListeners();
    } // Getters
XhmikosR's avatar
Dist  
XhmikosR committed
119
120


XhmikosR's avatar
Dist    
XhmikosR committed
121
    var _proto = Toast.prototype;
XhmikosR's avatar
Dist  
XhmikosR committed
122

XhmikosR's avatar
Dist    
XhmikosR committed
123
124
125
    // Public
    _proto.show = function show() {
      var _this = this;
XhmikosR's avatar
Dist  
XhmikosR committed
126

XhmikosR's avatar
Dist    
XhmikosR committed
127
      $(this._element).trigger(Event.SHOW);
XhmikosR's avatar
Dist  
XhmikosR committed
128

XhmikosR's avatar
Dist    
XhmikosR committed
129
130
131
      if (this._config.animation) {
        this._element.classList.add(ClassName.FADE);
      }
XhmikosR's avatar
Dist  
XhmikosR committed
132

XhmikosR's avatar
Dist    
XhmikosR committed
133
      var complete = function complete() {
Mark Otto's avatar
dist    
Mark Otto committed
134
135
136
137
        _this._element.classList.remove(ClassName.SHOWING);

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

XhmikosR's avatar
Dist    
XhmikosR committed
138
        $(_this._element).trigger(Event.SHOWN);
XhmikosR's avatar
Dist  
XhmikosR committed
139

XhmikosR's avatar
Dist    
XhmikosR committed
140
141
        if (_this._config.autohide) {
          _this.hide();
XhmikosR's avatar
Dist  
XhmikosR committed
142
143
144
        }
      };

Mark Otto's avatar
dist    
Mark Otto committed
145
146
147
      this._element.classList.remove(ClassName.HIDE);

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

XhmikosR's avatar
Dist    
XhmikosR committed
149
150
151
152
153
154
155
      if (this._config.animation) {
        var transitionDuration = Util.getTransitionDurationFromElement(this._element);
        $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
      } else {
        complete();
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
156

XhmikosR's avatar
Dist    
XhmikosR committed
157
158
    _proto.hide = function hide(withoutTimeout) {
      var _this2 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
159

XhmikosR's avatar
Dist    
XhmikosR committed
160
161
162
      if (!this._element.classList.contains(ClassName.SHOW)) {
        return;
      }
XhmikosR's avatar
Dist  
XhmikosR committed
163

XhmikosR's avatar
Dist    
XhmikosR committed
164
      $(this._element).trigger(Event.HIDE);
XhmikosR's avatar
Dist  
XhmikosR committed
165

XhmikosR's avatar
Dist    
XhmikosR committed
166
167
168
169
170
171
172
173
      if (withoutTimeout) {
        this._close();
      } else {
        this._timeout = setTimeout(function () {
          _this2._close();
        }, this._config.delay);
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
174

XhmikosR's avatar
Dist    
XhmikosR committed
175
176
177
    _proto.dispose = function dispose() {
      clearTimeout(this._timeout);
      this._timeout = null;
XhmikosR's avatar
Dist  
XhmikosR committed
178

XhmikosR's avatar
Dist    
XhmikosR committed
179
180
181
      if (this._element.classList.contains(ClassName.SHOW)) {
        this._element.classList.remove(ClassName.SHOW);
      }
XhmikosR's avatar
Dist  
XhmikosR committed
182

XhmikosR's avatar
Dist    
XhmikosR committed
183
184
185
186
      $(this._element).off(Event.CLICK_DISMISS);
      $.removeData(this._element, DATA_KEY);
      this._element = null;
      this._config = null;
Mark Otto's avatar
Mark Otto committed
187
188
    } // Private
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
189

XhmikosR's avatar
Dist    
XhmikosR committed
190
191
192
193
194
    _proto._getConfig = function _getConfig(config) {
      config = _objectSpread({}, Default, $(this._element).data(), typeof config === 'object' && config ? config : {});
      Util.typeCheckConfig(NAME, config, this.constructor.DefaultType);
      return config;
    };
XhmikosR's avatar
Dist  
XhmikosR committed
195

XhmikosR's avatar
Dist    
XhmikosR committed
196
197
    _proto._setListeners = function _setListeners() {
      var _this3 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
198

XhmikosR's avatar
Dist    
XhmikosR committed
199
200
201
202
      $(this._element).on(Event.CLICK_DISMISS, Selector.DATA_DISMISS, function () {
        return _this3.hide(true);
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
203

XhmikosR's avatar
Dist    
XhmikosR committed
204
205
    _proto._close = function _close() {
      var _this4 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
206

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

XhmikosR's avatar
Dist    
XhmikosR committed
210
211
212
213
        $(_this4._element).trigger(Event.HIDDEN);
      };

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

XhmikosR's avatar
Dist    
XhmikosR committed
215
216
217
218
219
220
      if (this._config.animation) {
        var transitionDuration = Util.getTransitionDurationFromElement(this._element);
        $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
      } else {
        complete();
      }
Mark Otto's avatar
Mark Otto committed
221
222
    } // Static
    ;
XhmikosR's avatar
Dist  
XhmikosR committed
223

XhmikosR's avatar
Dist    
XhmikosR committed
224
225
226
227
    Toast._jQueryInterface = function _jQueryInterface(config) {
      return this.each(function () {
        var $element = $(this);
        var data = $element.data(DATA_KEY);
XhmikosR's avatar
Dist  
XhmikosR committed
228

XhmikosR's avatar
Dist    
XhmikosR committed
229
        var _config = typeof config === 'object' && config;
XhmikosR's avatar
Dist  
XhmikosR committed
230

XhmikosR's avatar
Dist    
XhmikosR committed
231
232
233
234
        if (!data) {
          data = new Toast(this, _config);
          $element.data(DATA_KEY, data);
        }
XhmikosR's avatar
Dist  
XhmikosR committed
235

XhmikosR's avatar
Dist    
XhmikosR committed
236
237
238
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
XhmikosR's avatar
Dist  
XhmikosR committed
239
240
          }

XhmikosR's avatar
Dist    
XhmikosR committed
241
          data[config](this);
XhmikosR's avatar
Dist  
XhmikosR committed
242
        }
XhmikosR's avatar
Dist    
XhmikosR committed
243
244
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
245

XhmikosR's avatar
Dist    
XhmikosR committed
246
247
248
249
250
251
252
253
254
255
256
    _createClass(Toast, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }, {
      key: "DefaultType",
      get: function get() {
        return DefaultType;
      }
    }]);
XhmikosR's avatar
Dist  
XhmikosR committed
257

XhmikosR's avatar
Dist    
XhmikosR committed
258
259
260
261
262
263
264
    return Toast;
  }();
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
XhmikosR's avatar
Dist  
XhmikosR committed
265
266


XhmikosR's avatar
Dist    
XhmikosR committed
267
268
  $.fn[NAME] = Toast._jQueryInterface;
  $.fn[NAME].Constructor = Toast;
XhmikosR's avatar
Dist  
XhmikosR committed
269

XhmikosR's avatar
Dist    
XhmikosR committed
270
271
272
273
  $.fn[NAME].noConflict = function () {
    $.fn[NAME] = JQUERY_NO_CONFLICT;
    return Toast._jQueryInterface;
  };
XhmikosR's avatar
Dist  
XhmikosR committed
274
275
276

  return Toast;

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