Passed
Pull Request — master (#57)
by
unknown
11:34 queued 08:49
created

resources/bootstrap/js/toast.js   B

Complexity

Total Complexity 49
Complexity/F 1.88

Size

Lines of Code 260
Function Count 26

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 152
dl 0
loc 260
rs 8.48
c 0
b 0
f 0
wmc 49
mnd 23
bc 23
fnc 26
bpm 0.8846
cpm 1.8846
noi 9

How to fix   Complexity   

Complexity

Complex classes like resources/bootstrap/js/toast.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*!
2
  * Bootstrap toast.js v4.6.1 (https://getbootstrap.com/)
3
  * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
8
  typeof define === 'function' && define.amd ? define(['jquery', './util'], factory) :
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.jQuery, global.Util));
10
})(this, (function ($, Util) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
  var Util__default = /*#__PURE__*/_interopDefaultLegacy(Util);
16
17
  function _defineProperties(target, props) {
18
    for (var i = 0; i < props.length; i++) {
19
      var descriptor = props[i];
20
      descriptor.enumerable = descriptor.enumerable || false;
21
      descriptor.configurable = true;
22
      if ("value" in descriptor) descriptor.writable = true;
23
      Object.defineProperty(target, descriptor.key, descriptor);
24
    }
25
  }
26
27
  function _createClass(Constructor, protoProps, staticProps) {
28
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
29
    if (staticProps) _defineProperties(Constructor, staticProps);
30
    return Constructor;
31
  }
32
33
  function _extends() {
34
    _extends = Object.assign || function (target) {
35
      for (var i = 1; i < arguments.length; i++) {
36
        var source = arguments[i];
37
38
        for (var key in source) {
39
          if (Object.prototype.hasOwnProperty.call(source, key)) {
40
            target[key] = source[key];
41
          }
42
        }
43
      }
44
45
      return target;
46
    };
47
48
    return _extends.apply(this, arguments);
49
  }
50
51
  /**
52
   * Constants
53
   */
54
55
  var NAME = 'toast';
56
  var VERSION = '4.6.1';
57
  var DATA_KEY = 'bs.toast';
58
  var EVENT_KEY = "." + DATA_KEY;
59
  var JQUERY_NO_CONFLICT = $__default["default"].fn[NAME];
60
  var CLASS_NAME_FADE = 'fade';
61
  var CLASS_NAME_HIDE = 'hide';
62
  var CLASS_NAME_SHOW = 'show';
63
  var CLASS_NAME_SHOWING = 'showing';
64
  var EVENT_CLICK_DISMISS = "click.dismiss" + EVENT_KEY;
65
  var EVENT_HIDE = "hide" + EVENT_KEY;
66
  var EVENT_HIDDEN = "hidden" + EVENT_KEY;
67
  var EVENT_SHOW = "show" + EVENT_KEY;
68
  var EVENT_SHOWN = "shown" + EVENT_KEY;
69
  var SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]';
70
  var Default = {
71
    animation: true,
72
    autohide: true,
73
    delay: 500
74
  };
75
  var DefaultType = {
76
    animation: 'boolean',
77
    autohide: 'boolean',
78
    delay: 'number'
79
  };
80
  /**
81
   * Class definition
82
   */
83
84
  var Toast = /*#__PURE__*/function () {
85
    function Toast(element, config) {
86
      this._element = element;
87
      this._config = this._getConfig(config);
88
      this._timeout = null;
89
90
      this._setListeners();
91
    } // Getters
92
93
94
    var _proto = Toast.prototype;
95
96
    // Public
97
    _proto.show = function show() {
98
      var _this = this;
99
100
      var showEvent = $__default["default"].Event(EVENT_SHOW);
101
      $__default["default"](this._element).trigger(showEvent);
102
103
      if (showEvent.isDefaultPrevented()) {
104
        return;
105
      }
106
107
      this._clearTimeout();
108
109
      if (this._config.animation) {
110
        this._element.classList.add(CLASS_NAME_FADE);
111
      }
112
113
      var complete = function complete() {
114
        _this._element.classList.remove(CLASS_NAME_SHOWING);
115
116
        _this._element.classList.add(CLASS_NAME_SHOW);
117
118
        $__default["default"](_this._element).trigger(EVENT_SHOWN);
119
120
        if (_this._config.autohide) {
121
          _this._timeout = setTimeout(function () {
122
            _this.hide();
123
          }, _this._config.delay);
124
        }
125
      };
126
127
      this._element.classList.remove(CLASS_NAME_HIDE);
128
129
      Util__default["default"].reflow(this._element);
130
131
      this._element.classList.add(CLASS_NAME_SHOWING);
132
133
      if (this._config.animation) {
134
        var transitionDuration = Util__default["default"].getTransitionDurationFromElement(this._element);
135
        $__default["default"](this._element).one(Util__default["default"].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
136
      } else {
137
        complete();
138
      }
139
    };
140
141
    _proto.hide = function hide() {
142
      if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
143
        return;
144
      }
145
146
      var hideEvent = $__default["default"].Event(EVENT_HIDE);
147
      $__default["default"](this._element).trigger(hideEvent);
148
149
      if (hideEvent.isDefaultPrevented()) {
150
        return;
151
      }
152
153
      this._close();
154
    };
155
156
    _proto.dispose = function dispose() {
157
      this._clearTimeout();
158
159
      if (this._element.classList.contains(CLASS_NAME_SHOW)) {
160
        this._element.classList.remove(CLASS_NAME_SHOW);
161
      }
162
163
      $__default["default"](this._element).off(EVENT_CLICK_DISMISS);
164
      $__default["default"].removeData(this._element, DATA_KEY);
165
      this._element = null;
166
      this._config = null;
167
    } // Private
168
    ;
169
170
    _proto._getConfig = function _getConfig(config) {
171
      config = _extends({}, Default, $__default["default"](this._element).data(), typeof config === 'object' && config ? config : {});
172
      Util__default["default"].typeCheckConfig(NAME, config, this.constructor.DefaultType);
173
      return config;
174
    };
175
176
    _proto._setListeners = function _setListeners() {
177
      var _this2 = this;
178
179
      $__default["default"](this._element).on(EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, function () {
180
        return _this2.hide();
181
      });
182
    };
183
184
    _proto._close = function _close() {
185
      var _this3 = this;
186
187
      var complete = function complete() {
188
        _this3._element.classList.add(CLASS_NAME_HIDE);
189
190
        $__default["default"](_this3._element).trigger(EVENT_HIDDEN);
191
      };
192
193
      this._element.classList.remove(CLASS_NAME_SHOW);
194
195
      if (this._config.animation) {
196
        var transitionDuration = Util__default["default"].getTransitionDurationFromElement(this._element);
197
        $__default["default"](this._element).one(Util__default["default"].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
198
      } else {
199
        complete();
200
      }
201
    };
202
203
    _proto._clearTimeout = function _clearTimeout() {
204
      clearTimeout(this._timeout);
205
      this._timeout = null;
206
    } // Static
207
    ;
208
209
    Toast._jQueryInterface = function _jQueryInterface(config) {
210
      return this.each(function () {
211
        var $element = $__default["default"](this);
212
        var data = $element.data(DATA_KEY);
213
214
        var _config = typeof config === 'object' && config;
215
216
        if (!data) {
217
          data = new Toast(this, _config);
218
          $element.data(DATA_KEY, data);
219
        }
220
221
        if (typeof config === 'string') {
222
          if (typeof data[config] === 'undefined') {
223
            throw new TypeError("No method named \"" + config + "\"");
224
          }
225
226
          data[config](this);
227
        }
228
      });
229
    };
230
231
    _createClass(Toast, null, [{
232
      key: "VERSION",
233
      get: function get() {
234
        return VERSION;
235
      }
236
    }, {
237
      key: "DefaultType",
238
      get: function get() {
239
        return DefaultType;
240
      }
241
    }, {
242
      key: "Default",
243
      get: function get() {
244
        return Default;
245
      }
246
    }]);
247
248
    return Toast;
249
  }();
250
  /**
251
   * jQuery
252
   */
253
254
255
  $__default["default"].fn[NAME] = Toast._jQueryInterface;
256
  $__default["default"].fn[NAME].Constructor = Toast;
257
258
  $__default["default"].fn[NAME].noConflict = function () {
259
    $__default["default"].fn[NAME] = JQUERY_NO_CONFLICT;
260
    return Toast._jQueryInterface;
261
  };
262
263
  return Toast;
264
265
}));
266
//# sourceMappingURL=toast.js.map
267