Passed
Pull Request — master (#45)
by
unknown
02:56
created

toast.js ➔ _defineProperties   A

Complexity

Conditions 3

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 1
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
/*!
2
  * Bootstrap toast.js v4.5.3 (https://getbootstrap.com/)
3
  * Copyright 2011-2020 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.js'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.jQuery, global.Util));
0 ignored issues
show
Bug introduced by
The variable globalThis seems to be never declared. If this is a global, consider adding a /** global: globalThis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. _extends is already defined in line 17 as a function. While this will work, it can be very confusing.
Loading history...
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
18
19
  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); } }
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20
21
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
  /**
23
   * ------------------------------------------------------------------------
24
   * Constants
25
   * ------------------------------------------------------------------------
26
   */
27
28
  var NAME = 'toast';
29
  var VERSION = '4.5.3';
30
  var DATA_KEY = 'bs.toast';
31
  var EVENT_KEY = "." + DATA_KEY;
32
  var JQUERY_NO_CONFLICT = $__default['default'].fn[NAME];
33
  var EVENT_CLICK_DISMISS = "click.dismiss" + EVENT_KEY;
34
  var EVENT_HIDE = "hide" + EVENT_KEY;
35
  var EVENT_HIDDEN = "hidden" + EVENT_KEY;
36
  var EVENT_SHOW = "show" + EVENT_KEY;
37
  var EVENT_SHOWN = "shown" + EVENT_KEY;
38
  var CLASS_NAME_FADE = 'fade';
39
  var CLASS_NAME_HIDE = 'hide';
40
  var CLASS_NAME_SHOW = 'show';
41
  var CLASS_NAME_SHOWING = 'showing';
42
  var DefaultType = {
43
    animation: 'boolean',
44
    autohide: 'boolean',
45
    delay: 'number'
46
  };
47
  var Default = {
48
    animation: true,
49
    autohide: true,
50
    delay: 500
51
  };
52
  var SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]';
53
  /**
54
   * ------------------------------------------------------------------------
55
   * Class Definition
56
   * ------------------------------------------------------------------------
57
   */
58
59
  var Toast = /*#__PURE__*/function () {
60
    function Toast(element, config) {
61
      this._element = element;
62
      this._config = this._getConfig(config);
63
      this._timeout = null;
64
65
      this._setListeners();
66
    } // Getters
67
68
69
    var _proto = Toast.prototype;
70
71
    // Public
72
    _proto.show = function show() {
73
      var _this = this;
74
75
      var showEvent = $__default['default'].Event(EVENT_SHOW);
76
      $__default['default'](this._element).trigger(showEvent);
77
78
      if (showEvent.isDefaultPrevented()) {
79
        return;
80
      }
81
82
      this._clearTimeout();
83
84
      if (this._config.animation) {
85
        this._element.classList.add(CLASS_NAME_FADE);
86
      }
87
88
      var complete = function complete() {
89
        _this._element.classList.remove(CLASS_NAME_SHOWING);
90
91
        _this._element.classList.add(CLASS_NAME_SHOW);
92
93
        $__default['default'](_this._element).trigger(EVENT_SHOWN);
94
95
        if (_this._config.autohide) {
96
          _this._timeout = setTimeout(function () {
97
            _this.hide();
98
          }, _this._config.delay);
99
        }
100
      };
101
102
      this._element.classList.remove(CLASS_NAME_HIDE);
103
104
      Util__default['default'].reflow(this._element);
105
106
      this._element.classList.add(CLASS_NAME_SHOWING);
107
108
      if (this._config.animation) {
109
        var transitionDuration = Util__default['default'].getTransitionDurationFromElement(this._element);
110
        $__default['default'](this._element).one(Util__default['default'].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
111
      } else {
112
        complete();
113
      }
114
    };
115
116
    _proto.hide = function hide() {
117
      if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
118
        return;
119
      }
120
121
      var hideEvent = $__default['default'].Event(EVENT_HIDE);
122
      $__default['default'](this._element).trigger(hideEvent);
123
124
      if (hideEvent.isDefaultPrevented()) {
125
        return;
126
      }
127
128
      this._close();
129
    };
130
131
    _proto.dispose = function dispose() {
132
      this._clearTimeout();
133
134
      if (this._element.classList.contains(CLASS_NAME_SHOW)) {
135
        this._element.classList.remove(CLASS_NAME_SHOW);
136
      }
137
138
      $__default['default'](this._element).off(EVENT_CLICK_DISMISS);
139
      $__default['default'].removeData(this._element, DATA_KEY);
140
      this._element = null;
141
      this._config = null;
142
    } // Private
143
    ;
144
145
    _proto._getConfig = function _getConfig(config) {
146
      config = _extends({}, Default, $__default['default'](this._element).data(), typeof config === 'object' && config ? config : {});
147
      Util__default['default'].typeCheckConfig(NAME, config, this.constructor.DefaultType);
148
      return config;
149
    };
150
151
    _proto._setListeners = function _setListeners() {
152
      var _this2 = this;
153
154
      $__default['default'](this._element).on(EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, function () {
155
        return _this2.hide();
156
      });
157
    };
158
159
    _proto._close = function _close() {
160
      var _this3 = this;
161
162
      var complete = function complete() {
163
        _this3._element.classList.add(CLASS_NAME_HIDE);
164
165
        $__default['default'](_this3._element).trigger(EVENT_HIDDEN);
166
      };
167
168
      this._element.classList.remove(CLASS_NAME_SHOW);
169
170
      if (this._config.animation) {
171
        var transitionDuration = Util__default['default'].getTransitionDurationFromElement(this._element);
172
        $__default['default'](this._element).one(Util__default['default'].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
173
      } else {
174
        complete();
175
      }
176
    };
177
178
    _proto._clearTimeout = function _clearTimeout() {
179
      clearTimeout(this._timeout);
180
      this._timeout = null;
181
    } // Static
182
    ;
183
184
    Toast._jQueryInterface = function _jQueryInterface(config) {
185
      return this.each(function () {
186
        var $element = $__default['default'](this);
187
        var data = $element.data(DATA_KEY);
188
189
        var _config = typeof config === 'object' && config;
190
191
        if (!data) {
192
          data = new Toast(this, _config);
193
          $element.data(DATA_KEY, data);
194
        }
195
196
        if (typeof config === 'string') {
197
          if (typeof data[config] === 'undefined') {
198
            throw new TypeError("No method named \"" + config + "\"");
199
          }
200
201
          data[config](this);
202
        }
203
      });
204
    };
205
206
    _createClass(Toast, null, [{
207
      key: "VERSION",
208
      get: function get() {
209
        return VERSION;
210
      }
211
    }, {
212
      key: "DefaultType",
213
      get: function get() {
214
        return DefaultType;
215
      }
216
    }, {
217
      key: "Default",
218
      get: function get() {
219
        return Default;
220
      }
221
    }]);
222
223
    return Toast;
224
  }();
225
  /**
226
   * ------------------------------------------------------------------------
227
   * jQuery
228
   * ------------------------------------------------------------------------
229
   */
230
231
232
  $__default['default'].fn[NAME] = Toast._jQueryInterface;
233
  $__default['default'].fn[NAME].Constructor = Toast;
234
235
  $__default['default'].fn[NAME].noConflict = function () {
236
    $__default['default'].fn[NAME] = JQUERY_NO_CONFLICT;
237
    return Toast._jQueryInterface;
238
  };
239
240
  return Toast;
241
242
})));
243
//# sourceMappingURL=toast.js.map
244