| Total Complexity | 47 |
| Complexity/F | 2.61 |
| Lines of Code | 254 |
| Function Count | 18 |
| Duplicated Lines | 5 |
| Ratio | 1.97 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like resources/bootstrap/js/tab.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 | /*! |
||
| 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.Tab = 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 | /** |
||
| 34 | * Constants |
||
| 35 | */ |
||
| 36 | |||
| 37 | var NAME = 'tab'; |
||
| 38 | var VERSION = '4.6.1'; |
||
| 39 | var DATA_KEY = 'bs.tab'; |
||
| 40 | var EVENT_KEY = "." + DATA_KEY; |
||
| 41 | var DATA_API_KEY = '.data-api'; |
||
| 42 | var JQUERY_NO_CONFLICT = $__default["default"].fn[NAME]; |
||
| 43 | var CLASS_NAME_DROPDOWN_MENU = 'dropdown-menu'; |
||
| 44 | var CLASS_NAME_ACTIVE = 'active'; |
||
| 45 | var CLASS_NAME_DISABLED = 'disabled'; |
||
| 46 | var CLASS_NAME_FADE = 'fade'; |
||
| 47 | var CLASS_NAME_SHOW = 'show'; |
||
| 48 | var EVENT_HIDE = "hide" + EVENT_KEY; |
||
| 49 | var EVENT_HIDDEN = "hidden" + EVENT_KEY; |
||
| 50 | var EVENT_SHOW = "show" + EVENT_KEY; |
||
| 51 | var EVENT_SHOWN = "shown" + EVENT_KEY; |
||
| 52 | var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY; |
||
| 53 | var SELECTOR_DROPDOWN = '.dropdown'; |
||
| 54 | var SELECTOR_NAV_LIST_GROUP = '.nav, .list-group'; |
||
| 55 | var SELECTOR_ACTIVE = '.active'; |
||
| 56 | var SELECTOR_ACTIVE_UL = '> li > .active'; |
||
| 57 | var SELECTOR_DATA_TOGGLE = '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]'; |
||
| 58 | var SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle'; |
||
| 59 | var SELECTOR_DROPDOWN_ACTIVE_CHILD = '> .dropdown-menu .active'; |
||
| 60 | /** |
||
| 61 | * Class definition |
||
| 62 | */ |
||
| 63 | |||
| 64 | var Tab = /*#__PURE__*/function () { |
||
| 65 | function Tab(element) { |
||
| 66 | this._element = element; |
||
| 67 | } // Getters |
||
| 68 | |||
| 69 | |||
| 70 | var _proto = Tab.prototype; |
||
| 71 | |||
| 72 | // Public |
||
| 73 | _proto.show = function show() { |
||
| 74 | var _this = this; |
||
| 75 | |||
| 76 | if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && $__default["default"](this._element).hasClass(CLASS_NAME_ACTIVE) || $__default["default"](this._element).hasClass(CLASS_NAME_DISABLED)) { |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | |||
| 80 | var target; |
||
| 81 | var previous; |
||
| 82 | var listElement = $__default["default"](this._element).closest(SELECTOR_NAV_LIST_GROUP)[0]; |
||
| 83 | var selector = Util__default["default"].getSelectorFromElement(this._element); |
||
| 84 | |||
| 85 | if (listElement) { |
||
| 86 | var itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? SELECTOR_ACTIVE_UL : SELECTOR_ACTIVE; |
||
| 87 | previous = $__default["default"].makeArray($__default["default"](listElement).find(itemSelector)); |
||
| 88 | previous = previous[previous.length - 1]; |
||
| 89 | } |
||
| 90 | |||
| 91 | var hideEvent = $__default["default"].Event(EVENT_HIDE, { |
||
| 92 | relatedTarget: this._element |
||
| 93 | }); |
||
| 94 | var showEvent = $__default["default"].Event(EVENT_SHOW, { |
||
| 95 | relatedTarget: previous |
||
| 96 | }); |
||
| 97 | |||
| 98 | if (previous) { |
||
| 99 | $__default["default"](previous).trigger(hideEvent); |
||
| 100 | } |
||
| 101 | |||
| 102 | $__default["default"](this._element).trigger(showEvent); |
||
| 103 | |||
| 104 | if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (selector) { |
||
| 109 | target = document.querySelector(selector); |
||
| 110 | } |
||
| 111 | |||
| 112 | this._activate(this._element, listElement); |
||
| 113 | |||
| 114 | var complete = function complete() { |
||
| 115 | var hiddenEvent = $__default["default"].Event(EVENT_HIDDEN, { |
||
| 116 | relatedTarget: _this._element |
||
| 117 | }); |
||
| 118 | var shownEvent = $__default["default"].Event(EVENT_SHOWN, { |
||
| 119 | relatedTarget: previous |
||
| 120 | }); |
||
| 121 | $__default["default"](previous).trigger(hiddenEvent); |
||
| 122 | $__default["default"](_this._element).trigger(shownEvent); |
||
| 123 | }; |
||
| 124 | |||
| 125 | if (target) { |
||
| 126 | this._activate(target, target.parentNode, complete); |
||
| 127 | } else { |
||
| 128 | complete(); |
||
| 129 | } |
||
| 130 | }; |
||
| 131 | |||
| 132 | _proto.dispose = function dispose() { |
||
| 133 | $__default["default"].removeData(this._element, DATA_KEY); |
||
| 134 | this._element = null; |
||
| 135 | } // Private |
||
| 136 | ; |
||
| 137 | |||
| 138 | _proto._activate = function _activate(element, container, callback) { |
||
| 139 | var _this2 = this; |
||
| 140 | |||
| 141 | var activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? $__default["default"](container).find(SELECTOR_ACTIVE_UL) : $__default["default"](container).children(SELECTOR_ACTIVE); |
||
| 142 | var active = activeElements[0]; |
||
| 143 | var isTransitioning = callback && active && $__default["default"](active).hasClass(CLASS_NAME_FADE); |
||
| 144 | |||
| 145 | var complete = function complete() { |
||
| 146 | return _this2._transitionComplete(element, active, callback); |
||
| 147 | }; |
||
| 148 | |||
| 149 | if (active && isTransitioning) { |
||
| 150 | var transitionDuration = Util__default["default"].getTransitionDurationFromElement(active); |
||
| 151 | $__default["default"](active).removeClass(CLASS_NAME_SHOW).one(Util__default["default"].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration); |
||
| 152 | } else { |
||
| 153 | complete(); |
||
| 154 | } |
||
| 155 | }; |
||
| 156 | |||
| 157 | _proto._transitionComplete = function _transitionComplete(element, active, callback) { |
||
| 158 | if (active) { |
||
| 159 | $__default["default"](active).removeClass(CLASS_NAME_ACTIVE); |
||
| 160 | var dropdownChild = $__default["default"](active.parentNode).find(SELECTOR_DROPDOWN_ACTIVE_CHILD)[0]; |
||
| 161 | |||
| 162 | if (dropdownChild) { |
||
| 163 | $__default["default"](dropdownChild).removeClass(CLASS_NAME_ACTIVE); |
||
| 164 | } |
||
| 165 | |||
| 166 | if (active.getAttribute('role') === 'tab') { |
||
| 167 | active.setAttribute('aria-selected', false); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | $__default["default"](element).addClass(CLASS_NAME_ACTIVE); |
||
| 172 | |||
| 173 | if (element.getAttribute('role') === 'tab') { |
||
| 174 | element.setAttribute('aria-selected', true); |
||
| 175 | } |
||
| 176 | |||
| 177 | Util__default["default"].reflow(element); |
||
| 178 | |||
| 179 | if (element.classList.contains(CLASS_NAME_FADE)) { |
||
| 180 | element.classList.add(CLASS_NAME_SHOW); |
||
| 181 | } |
||
| 182 | |||
| 183 | var parent = element.parentNode; |
||
| 184 | |||
| 185 | if (parent && parent.nodeName === 'LI') { |
||
| 186 | parent = parent.parentNode; |
||
| 187 | } |
||
| 188 | |||
| 189 | if (parent && $__default["default"](parent).hasClass(CLASS_NAME_DROPDOWN_MENU)) { |
||
| 190 | var dropdownElement = $__default["default"](element).closest(SELECTOR_DROPDOWN)[0]; |
||
| 191 | |||
| 192 | if (dropdownElement) { |
||
| 193 | var dropdownToggleList = [].slice.call(dropdownElement.querySelectorAll(SELECTOR_DROPDOWN_TOGGLE)); |
||
| 194 | $__default["default"](dropdownToggleList).addClass(CLASS_NAME_ACTIVE); |
||
| 195 | } |
||
| 196 | |||
| 197 | element.setAttribute('aria-expanded', true); |
||
| 198 | } |
||
| 199 | |||
| 200 | if (callback) { |
||
| 201 | callback(); |
||
| 202 | } |
||
| 203 | } // Static |
||
| 204 | ; |
||
| 205 | |||
| 206 | Tab._jQueryInterface = function _jQueryInterface(config) { |
||
| 207 | return this.each(function () { |
||
| 208 | var $this = $__default["default"](this); |
||
| 209 | var data = $this.data(DATA_KEY); |
||
| 210 | |||
| 211 | if (!data) { |
||
| 212 | data = new Tab(this); |
||
| 213 | $this.data(DATA_KEY, data); |
||
| 214 | } |
||
| 215 | |||
| 216 | if (typeof config === 'string') { |
||
| 217 | if (typeof data[config] === 'undefined') { |
||
| 218 | throw new TypeError("No method named \"" + config + "\""); |
||
| 219 | } |
||
| 220 | |||
| 221 | data[config](); |
||
| 222 | } |
||
| 223 | }); |
||
| 224 | }; |
||
| 225 | |||
| 226 | _createClass(Tab, null, [{ |
||
| 227 | key: "VERSION", |
||
| 228 | get: function get() { |
||
| 229 | return VERSION; |
||
| 230 | } |
||
| 231 | }]); |
||
| 232 | |||
| 233 | return Tab; |
||
| 234 | }(); |
||
| 235 | /** |
||
| 236 | * Data API implementation |
||
| 237 | */ |
||
| 238 | |||
| 239 | |||
| 240 | $__default["default"](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) { |
||
| 241 | event.preventDefault(); |
||
| 242 | |||
| 243 | Tab._jQueryInterface.call($__default["default"](this), 'show'); |
||
| 244 | }); |
||
| 245 | /** |
||
| 246 | * jQuery |
||
| 247 | */ |
||
| 248 | |||
| 249 | $__default["default"].fn[NAME] = Tab._jQueryInterface; |
||
| 250 | $__default["default"].fn[NAME].Constructor = Tab; |
||
| 251 | |||
| 252 | $__default["default"].fn[NAME].noConflict = function () { |
||
| 253 | $__default["default"].fn[NAME] = JQUERY_NO_CONFLICT; |
||
| 254 | return Tab._jQueryInterface; |
||
| 255 | }; |
||
| 256 | |||
| 257 | return Tab; |
||
| 258 | |||
| 259 | })); |
||
| 260 | //# sourceMappingURL=tab.js.map |
||
| 261 |