toast.js 7.13 KB
Newer Older
XhmikosR's avatar
Dist  
XhmikosR committed
1
2
3
4
5
6
7
8
9
10
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
/*!
  * Bootstrap toast.js v4.1.3 (https://getbootstrap.com/)
  * Copyright 2011-2018 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  * 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) :
  (global.Toast = factory(global.jQuery,global.Util));
}(this, (function ($,Util) { 'use strict';

  $ = $ && $.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  var NAME = 'toast';
  var VERSION = '4.1.3';
  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',
    SHOW: 'show'
  };
  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
100
101
    /**
     * ------------------------------------------------------------------------
XhmikosR's avatar
Dist    
XhmikosR committed
102
     * Class Definition
XhmikosR's avatar
Dist  
XhmikosR committed
103
104
105
     * ------------------------------------------------------------------------
     */

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

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

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


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

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

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

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

XhmikosR's avatar
Dist    
XhmikosR committed
132
133
      var complete = function complete() {
        $(_this._element).trigger(Event.SHOWN);
XhmikosR's avatar
Dist  
XhmikosR committed
134

XhmikosR's avatar
Dist    
XhmikosR committed
135
136
        if (_this._config.autohide) {
          _this.hide();
XhmikosR's avatar
Dist  
XhmikosR committed
137
138
139
        }
      };

XhmikosR's avatar
Dist    
XhmikosR committed
140
      this._element.classList.add(ClassName.SHOW);
XhmikosR's avatar
Dist  
XhmikosR committed
141

XhmikosR's avatar
Dist    
XhmikosR committed
142
143
144
145
146
147
148
      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
149

XhmikosR's avatar
Dist    
XhmikosR committed
150
151
    _proto.hide = function hide(withoutTimeout) {
      var _this2 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
152

XhmikosR's avatar
Dist    
XhmikosR committed
153
154
155
      if (!this._element.classList.contains(ClassName.SHOW)) {
        return;
      }
XhmikosR's avatar
Dist  
XhmikosR committed
156

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

XhmikosR's avatar
Dist    
XhmikosR committed
159
160
161
162
163
164
165
166
      if (withoutTimeout) {
        this._close();
      } else {
        this._timeout = setTimeout(function () {
          _this2._close();
        }, this._config.delay);
      }
    };
XhmikosR's avatar
Dist  
XhmikosR committed
167

XhmikosR's avatar
Dist    
XhmikosR committed
168
169
170
    _proto.dispose = function dispose() {
      clearTimeout(this._timeout);
      this._timeout = null;
XhmikosR's avatar
Dist  
XhmikosR committed
171

XhmikosR's avatar
Dist    
XhmikosR committed
172
173
174
      if (this._element.classList.contains(ClassName.SHOW)) {
        this._element.classList.remove(ClassName.SHOW);
      }
XhmikosR's avatar
Dist  
XhmikosR committed
175

XhmikosR's avatar
Dist    
XhmikosR committed
176
177
178
179
180
      $(this._element).off(Event.CLICK_DISMISS);
      $.removeData(this._element, DATA_KEY);
      this._element = null;
      this._config = null;
    }; // Private
XhmikosR's avatar
Dist  
XhmikosR committed
181
182


XhmikosR's avatar
Dist    
XhmikosR committed
183
184
185
186
187
    _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
188

XhmikosR's avatar
Dist    
XhmikosR committed
189
190
    _proto._setListeners = function _setListeners() {
      var _this3 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
191

XhmikosR's avatar
Dist    
XhmikosR committed
192
193
194
195
      $(this._element).on(Event.CLICK_DISMISS, Selector.DATA_DISMISS, function () {
        return _this3.hide(true);
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
196

XhmikosR's avatar
Dist    
XhmikosR committed
197
198
    _proto._close = function _close() {
      var _this4 = this;
XhmikosR's avatar
Dist  
XhmikosR committed
199

XhmikosR's avatar
Dist    
XhmikosR committed
200
201
202
203
204
      var complete = function complete() {
        $(_this4._element).trigger(Event.HIDDEN);
      };

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

XhmikosR's avatar
Dist    
XhmikosR committed
206
207
208
209
210
211
212
      if (this._config.animation) {
        var transitionDuration = Util.getTransitionDurationFromElement(this._element);
        $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
      } else {
        complete();
      }
    }; // Static
XhmikosR's avatar
Dist  
XhmikosR committed
213
214


XhmikosR's avatar
Dist    
XhmikosR committed
215
216
217
218
    Toast._jQueryInterface = function _jQueryInterface(config) {
      return this.each(function () {
        var $element = $(this);
        var data = $element.data(DATA_KEY);
XhmikosR's avatar
Dist  
XhmikosR committed
219

XhmikosR's avatar
Dist    
XhmikosR committed
220
        var _config = typeof config === 'object' && config;
XhmikosR's avatar
Dist  
XhmikosR committed
221

XhmikosR's avatar
Dist    
XhmikosR committed
222
223
224
225
        if (!data) {
          data = new Toast(this, _config);
          $element.data(DATA_KEY, data);
        }
XhmikosR's avatar
Dist  
XhmikosR committed
226

XhmikosR's avatar
Dist    
XhmikosR committed
227
228
229
        if (typeof config === 'string') {
          if (typeof data[config] === 'undefined') {
            throw new TypeError("No method named \"" + config + "\"");
XhmikosR's avatar
Dist  
XhmikosR committed
230
231
          }

XhmikosR's avatar
Dist    
XhmikosR committed
232
          data[config](this);
XhmikosR's avatar
Dist  
XhmikosR committed
233
        }
XhmikosR's avatar
Dist    
XhmikosR committed
234
235
      });
    };
XhmikosR's avatar
Dist  
XhmikosR committed
236

XhmikosR's avatar
Dist    
XhmikosR committed
237
238
239
240
241
242
243
244
245
246
247
    _createClass(Toast, null, [{
      key: "VERSION",
      get: function get() {
        return VERSION;
      }
    }, {
      key: "DefaultType",
      get: function get() {
        return DefaultType;
      }
    }]);
XhmikosR's avatar
Dist  
XhmikosR committed
248

XhmikosR's avatar
Dist    
XhmikosR committed
249
250
251
252
253
254
255
    return Toast;
  }();
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
XhmikosR's avatar
Dist  
XhmikosR committed
256
257


XhmikosR's avatar
Dist    
XhmikosR committed
258
259
  $.fn[NAME] = Toast._jQueryInterface;
  $.fn[NAME].Constructor = Toast;
XhmikosR's avatar
Dist  
XhmikosR committed
260

XhmikosR's avatar
Dist    
XhmikosR committed
261
262
263
264
  $.fn[NAME].noConflict = function () {
    $.fn[NAME] = JQUERY_NO_CONFLICT;
    return Toast._jQueryInterface;
  };
XhmikosR's avatar
Dist  
XhmikosR committed
265
266
267
268
269

  return Toast;

})));
//# sourceMappingURL=toast.js.map